Skip to content

Commit

Permalink
Fix export users (#1690)
Browse files Browse the repository at this point in the history
  • Loading branch information
kossnocorp authored and bkendall committed Oct 8, 2019
1 parent 790209c commit 33da3e4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 40 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* Firestore Emulator now serves WebChannel traffic on the same port as gRPC.
* Fix bug where emulators could not find free ports on Windows Subsystem for Linux.
* Fix bug where emulators could not find free ports on Windows Subsystem for Linux.
* Fixes invalid JSON output in `auth.export` within a scripting environment.
9 changes: 6 additions & 3 deletions src/accountExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ var validateOptions = function(options, fileName) {
return exportOptions;
};

var _writeUsersToFile = (function() {
var _createWriteUsersToFile = function() {
var jsonSep = "";
return function(userList, format, writeStream) {
userList.map(function(user) {
Expand All @@ -136,9 +136,12 @@ var _writeUsersToFile = (function() {
}
});
};
})();
};

var serialExportUsers = function(projectId, options) {
if (!options.writeUsersToFile) {
options.writeUsersToFile = _createWriteUsersToFile();
}
var postBody = {
targetProjectId: projectId,
maxResults: options.batchSize,
Expand All @@ -156,7 +159,7 @@ var serialExportUsers = function(projectId, options) {
.then(function(ret) {
var userList = ret.body.users;
if (userList && userList.length > 0) {
_writeUsersToFile(userList, options.format, options.writeStream);
options.writeUsersToFile(userList, options.format, options.writeStream);
utils.logSuccess("Exported " + userList.length + " account(s) successfully.");
// The identitytoolkit API do not return a nextPageToken value
// consistently when the last page is reached
Expand Down
109 changes: 73 additions & 36 deletions src/test/accountExporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,42 +120,7 @@ describe("accountExporter", function() {
for (var i = 0; i < 20; i++) {
trailingCommas.push(",");
}
nock("https://www.googleapis.com")
.post("/identitytoolkit/v3/relyingparty/downloadAccount", {
maxResults: 3,
targetProjectId: "test-project-id",
})
.reply(200, {
users: userList.slice(0, 3),
nextPageToken: "3",
})
.post("/identitytoolkit/v3/relyingparty/downloadAccount", {
maxResults: 3,
nextPageToken: "3",
targetProjectId: "test-project-id",
})
.reply(200, {
users: userList.slice(3, 6),
nextPageToken: "6",
})
.post("/identitytoolkit/v3/relyingparty/downloadAccount", {
maxResults: 3,
nextPageToken: "6",
targetProjectId: "test-project-id",
})
.reply(200, {
users: userList.slice(6, 7),
nextPageToken: "7",
})
.post("/identitytoolkit/v3/relyingparty/downloadAccount", {
maxResults: 3,
nextPageToken: "7",
targetProjectId: "test-project-id",
})
.reply(200, {
users: [],
nextPageToken: "7",
});
mockAllUsersRequests();

return serialExportUsers("test-project-id", {
format: "csv",
Expand Down Expand Up @@ -225,5 +190,77 @@ describe("accountExporter", function() {
expect(spyWrite.getCall(0).args[0]).to.eq(expectedEntry + "," + os.EOL);
});
});

it("should not emit redundant comma in JSON on consecutive calls", function() {
mockAllUsersRequests();

const correctString =
'{\n "localId": "0",\n "email": "test0@test.org",\n "displayName": "John Tester0"\n}';

const firstWriteSpy = sinon.spy();
return serialExportUsers("test-project-id", {
format: "JSON",
batchSize: 3,
writeStream: { write: firstWriteSpy, end: function() {} },
}).then(function() {
expect(firstWriteSpy.args[0][0]).to.be.eq(
correctString,
"The first call did not emit the correct string"
);

mockAllUsersRequests();

const secondWriteSpy = sinon.spy();
return serialExportUsers("test-project-id", {
format: "JSON",
batchSize: 3,
writeStream: { write: secondWriteSpy, end: function() {} },
}).then(() => {
expect(secondWriteSpy.args[0][0]).to.be.eq(
correctString,
"The second call did not emit the correct string"
);
});
});
});

function mockAllUsersRequests() {
nock("https://www.googleapis.com")
.post("/identitytoolkit/v3/relyingparty/downloadAccount", {
maxResults: 3,
targetProjectId: "test-project-id",
})
.reply(200, {
users: userList.slice(0, 3),
nextPageToken: "3",
})
.post("/identitytoolkit/v3/relyingparty/downloadAccount", {
maxResults: 3,
nextPageToken: "3",
targetProjectId: "test-project-id",
})
.reply(200, {
users: userList.slice(3, 6),
nextPageToken: "6",
})
.post("/identitytoolkit/v3/relyingparty/downloadAccount", {
maxResults: 3,
nextPageToken: "6",
targetProjectId: "test-project-id",
})
.reply(200, {
users: userList.slice(6, 7),
nextPageToken: "7",
})
.post("/identitytoolkit/v3/relyingparty/downloadAccount", {
maxResults: 3,
nextPageToken: "7",
targetProjectId: "test-project-id",
})
.reply(200, {
users: [],
nextPageToken: "7",
});
}
});
});

0 comments on commit 33da3e4

Please sign in to comment.