Skip to content

Commit

Permalink
Fixed a flaky auth integration test by retrying the GetUser() API call (
Browse files Browse the repository at this point in the history
  • Loading branch information
rsgowman committed Jun 16, 2020
1 parent 5554e4a commit ebd5677
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions test/integration/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,29 @@ describe('admin.auth', () => {

// Login to set the lastRefreshTime.
await firebase.auth!().signInWithEmailAndPassword('lastRefreshTimeUser@example.com', 'p4ssword')
.then(() => admin.auth().getUser('lastRefreshTimeUser'))
.then((userRecord) => {
expect(userRecord.metadata.lastRefreshTime).to.exist;
expect(isUTCString(userRecord.metadata.lastRefreshTime!));
const creationTime = new Date(userRecord.metadata.creationTime).getTime();
const lastRefreshTime = new Date(userRecord.metadata.lastRefreshTime!).getTime();
.then(async () => {
// Attempt to retrieve the user 3 times (with a small delay between
// each attempt). Occassionally, this call retrieves the user data
// without the lastLoginTime/lastRefreshTime set; possibly because
// it's hitting a different server than the login request uses.
let userRecord = null;

for (let i = 0; i < 3; i++) {
userRecord = await admin.auth().getUser('lastRefreshTimeUser');
if (userRecord.metadata.lastRefreshTime) {
break;
}

await new Promise((resolve) => {
setTimeout(resolve, 1000 * Math.pow(2, i));
});
}

const metadata = userRecord!.metadata;
expect(metadata.lastRefreshTime).to.exist;
expect(isUTCString(metadata.lastRefreshTime!));
const creationTime = new Date(metadata.creationTime).getTime();
const lastRefreshTime = new Date(metadata.lastRefreshTime!).getTime();
expect(creationTime).lte(lastRefreshTime);
expect(lastRefreshTime).lte(creationTime + 3600 * 1000);
});
Expand Down

0 comments on commit ebd5677

Please sign in to comment.