Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth): Add lastRefreshTime to UserMetadata toJSON method. #889

Merged
merged 5 commits into from Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/auth/user-record.ts
Expand Up @@ -326,14 +326,16 @@ export class UserMetadata {
// These bugs have already been addressed since then.
utils.addReadonlyGetter(this, 'creationTime', parseDate(response.createdAt));
utils.addReadonlyGetter(this, 'lastSignInTime', parseDate(response.lastLoginAt));
utils.addReadonlyGetter(this, 'lastRefreshTime', parseDate(response.lastRefreshAt));
const lastRefreshAt = response.lastRefreshAt ? new Date(response.lastRefreshAt).toUTCString() : null;
utils.addReadonlyGetter(this, 'lastRefreshTime', lastRefreshAt);
}

/** @return The plain object representation of the user's metadata. */
public toJSON(): object {
return {
lastSignInTime: this.lastSignInTime,
creationTime: this.creationTime,
lastRefreshTime: this.lastRefreshTime,
};
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/integration/auth.spec.ts
Expand Up @@ -336,6 +336,12 @@ describe('admin.auth', () => {
.then((userRecord) => {
expect(userRecord.metadata.lastRefreshTime).to.exist;
expect(isUTCString(userRecord.metadata.lastRefreshTime!));
expect(new Date(userRecord.metadata.creationTime).getTime())
.lte(new Date(userRecord.metadata.lastRefreshTime!).getTime());
const creationTimePlus1Hour = new Date(userRecord.metadata.creationTime);
creationTimePlus1Hour.setHours(creationTimePlus1Hour.getHours()+1);
expect(new Date(userRecord.metadata.lastRefreshTime!).getTime())
.lte(creationTimePlus1Hour.getTime());
});
} finally {
admin.auth().deleteUser('lastRefreshTimeUser');
Expand Down Expand Up @@ -1796,6 +1802,7 @@ describe('admin.auth', () => {
metadata: {
lastSignInTime: now,
creationTime: now,
lastRefreshTime: null, // TODO(rsgowman): Switch to 'now' once importing users supports lastRefreshTime
},
providerData: [
{
Expand Down
6 changes: 6 additions & 0 deletions test/unit/auth/user-record.spec.ts
Expand Up @@ -80,6 +80,7 @@ function getValidUserResponse(tenantId?: string): GetAccountInfoUserResponse {
validSince: '1476136676',
lastLoginAt: '1476235905000',
createdAt: '1476136676000',
lastRefreshAt: '2016-10-12T01:31:45.000Z',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR. But are the fields like createdAt actually sent as strings (quoted), not numbers?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah they are all sent as strings. Here is a snippet:

      "lastLoginAt": "1590622314664",
      "createdAt": "1590545248026",
      "lastRefreshAt": "2020-05-28T01:44:39.262Z"

customAttributes: JSON.stringify({
admin: true,
}),
Expand Down Expand Up @@ -156,6 +157,7 @@ function getUserJSON(tenantId?: string): object {
metadata: {
lastSignInTime: new Date(1476235905000).toUTCString(),
creationTime: new Date(1476136676000).toUTCString(),
lastRefreshTime: new Date(1476235905000).toUTCString(),
},
customClaims: {
admin: true,
Expand Down Expand Up @@ -616,14 +618,17 @@ describe('UserInfo', () => {
describe('UserMetadata', () => {
const expectedLastLoginAt = 1476235905000;
const expectedCreatedAt = 1476136676000;
const expectedLastRefreshAt = '2016-10-12T01:31:45.000Z';
const actualMetadata: UserMetadata = new UserMetadata({
localId: 'uid123',
lastLoginAt: expectedLastLoginAt.toString(),
createdAt: expectedCreatedAt.toString(),
lastRefreshAt: expectedLastRefreshAt,
});
const expectedMetadataJSON = {
lastSignInTime: new Date(expectedLastLoginAt).toUTCString(),
creationTime: new Date(expectedCreatedAt).toUTCString(),
lastRefreshTime: new Date(expectedLastRefreshAt).toUTCString(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are lacking test cases for lastRefreshTime. Most existing test cases only check for the other 2 fields. We might want to address that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code now (2y later) I see some test cases for this, but perhaps not as many as there should be. I think that's independent of this PR though.

};

describe('constructor', () => {
Expand Down Expand Up @@ -872,6 +877,7 @@ describe('UserRecord', () => {
const metadata = new UserMetadata({
createdAt: '1476136676000',
lastLoginAt: '1476235905000',
lastRefreshAt: '2016-10-12T01:31:45.000Z',
} as any);
expect(userRecord.metadata).to.deep.equal(metadata);
});
Expand Down