Skip to content

Commit ebd5677

Browse files
authored
Fixed a flaky auth integration test by retrying the GetUser() API call (#907)
1 parent 5554e4a commit ebd5677

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

test/integration/auth.spec.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,29 @@ describe('admin.auth', () => {
332332

333333
// Login to set the lastRefreshTime.
334334
await firebase.auth!().signInWithEmailAndPassword('[email protected]', 'p4ssword')
335-
.then(() => admin.auth().getUser('lastRefreshTimeUser'))
336-
.then((userRecord) => {
337-
expect(userRecord.metadata.lastRefreshTime).to.exist;
338-
expect(isUTCString(userRecord.metadata.lastRefreshTime!));
339-
const creationTime = new Date(userRecord.metadata.creationTime).getTime();
340-
const lastRefreshTime = new Date(userRecord.metadata.lastRefreshTime!).getTime();
335+
.then(async () => {
336+
// Attempt to retrieve the user 3 times (with a small delay between
337+
// each attempt). Occassionally, this call retrieves the user data
338+
// without the lastLoginTime/lastRefreshTime set; possibly because
339+
// it's hitting a different server than the login request uses.
340+
let userRecord = null;
341+
342+
for (let i = 0; i < 3; i++) {
343+
userRecord = await admin.auth().getUser('lastRefreshTimeUser');
344+
if (userRecord.metadata.lastRefreshTime) {
345+
break;
346+
}
347+
348+
await new Promise((resolve) => {
349+
setTimeout(resolve, 1000 * Math.pow(2, i));
350+
});
351+
}
352+
353+
const metadata = userRecord!.metadata;
354+
expect(metadata.lastRefreshTime).to.exist;
355+
expect(isUTCString(metadata.lastRefreshTime!));
356+
const creationTime = new Date(metadata.creationTime).getTime();
357+
const lastRefreshTime = new Date(metadata.lastRefreshTime!).getTime();
341358
expect(creationTime).lte(lastRefreshTime);
342359
expect(lastRefreshTime).lte(creationTime + 3600 * 1000);
343360
});

0 commit comments

Comments
 (0)