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 all 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
1 change: 1 addition & 0 deletions src/auth/user-record.ts
Expand Up @@ -337,6 +337,7 @@ export class UserMetadata {
return {
lastSignInTime: this.lastSignInTime,
creationTime: this.creationTime,
lastRefreshTime: this.lastRefreshTime,
};
}
}
Expand Down
9 changes: 8 additions & 1 deletion test/integration/auth.spec.ts
Expand Up @@ -31,7 +31,7 @@ import { deepExtend, deepCopy } from '../../src/utils/deep-copy';
import {
AuthProviderConfig, CreateTenantRequest, DeleteUsersResult, PhoneMultiFactorInfo,
TenantAwareAuth, UpdatePhoneMultiFactorInfoRequest, UpdateTenantRequest, UserImportOptions,
UserImportRecord, UserRecord, getAuth,
UserImportRecord, UserMetadata, UserRecord, getAuth,
} from '../../lib/auth/index';

const chalk = require('chalk'); // eslint-disable-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -2517,6 +2517,8 @@ describe('admin.auth', () => {
metadata: {
lastSignInTime: now,
creationTime: now,
// TODO(rsgowman): Enable once importing users supports lastRefreshTime
//lastRefreshTime: now,
},
providerData: [
{
Expand Down Expand Up @@ -2548,6 +2550,11 @@ describe('admin.auth', () => {
providerId: 'phone',
phoneNumber: importUserRecord.phoneNumber!,
});
// The lastRefreshTime should be set to null
type Writable<UserMetadata> = {
-readonly [k in keyof UserMetadata]: UserMetadata[k];
};
(importUserRecord.metadata as Writable<UserMetadata>).lastRefreshTime = null;
const actualUserRecord: {[key: string]: any} = userRecord.toJSON();
for (const key of Object.keys(importUserRecord)) {
expect(JSON.stringify(actualUserRecord[key]))
Expand Down
4 changes: 4 additions & 0 deletions test/unit/auth/user-record.spec.ts
Expand Up @@ -83,6 +83,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 @@ -159,6 +160,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 @@ -629,6 +631,7 @@ describe('UserMetadata', () => {
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 @@ -890,6 +893,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