Skip to content

Commit 70383b1

Browse files
committed
Rename caching helper method and clean up test cases
1 parent 6ba19df commit 70383b1

File tree

2 files changed

+106
-57
lines changed

2 files changed

+106
-57
lines changed

packages/auth/src/core/strategies/email_and_password.test.ts

Lines changed: 103 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,17 @@ import { MockGreCAPTCHATopLevel } from '../../platform_browser/recaptcha/recaptc
5454
use(chaiAsPromised);
5555
use(sinonChai);
5656

57+
const TEST_ID_TOKEN = 'id-token';
58+
const TEST_REFRESH_TOKEN = 'refresh-token';
59+
const TEST_TOKEN_EXPIRY_TIME = '1234';
60+
61+
const TEST_LOCAL_ID = 'local-id';
62+
63+
const TEST_EMAIL = '[email protected]';
64+
const TEST_PASSWORD = 'some-password';
65+
5766
describe('core/strategies/sendPasswordResetEmail', () => {
58-
const email = '[email protected]';
67+
const email = TEST_EMAIL;
5968

6069
let auth: TestAuth;
6170

@@ -323,7 +332,7 @@ describe('core/strategies/confirmPasswordReset', () => {
323332

324333
it('should confirm the password reset and not return the email', async () => {
325334
const mock = mockEndpoint(Endpoint.RESET_PASSWORD, {
326-
335+
email: TEST_EMAIL
327336
});
328337
const response = await confirmPasswordReset(auth, oobCode, newPassword);
329338
expect(response).to.be.undefined;
@@ -395,7 +404,7 @@ describe('core/strategies/applyActionCode', () => {
395404

396405
describe('core/strategies/checkActionCode', () => {
397406
const oobCode = 'oob-code';
398-
const email = '[email protected]';
407+
const email = TEST_EMAIL;
399408
const newEmail = '[email protected]';
400409

401410
let auth: TestAuth;
@@ -410,7 +419,7 @@ describe('core/strategies/checkActionCode', () => {
410419
it('should verify the oob code', async () => {
411420
const mock = mockEndpoint(Endpoint.RESET_PASSWORD, {
412421
requestType: ActionCodeOperation.PASSWORD_RESET,
413-
422+
email: TEST_EMAIL
414423
});
415424
const response = await checkActionCode(auth, oobCode);
416425
expect(response).to.eql({
@@ -478,7 +487,7 @@ describe('core/strategies/checkActionCode', () => {
478487

479488
describe('core/strategies/verifyPasswordResetCode', () => {
480489
const oobCode = 'oob-code';
481-
const email = '[email protected]';
490+
const email = TEST_EMAIL;
482491

483492
let auth: TestAuth;
484493

@@ -492,7 +501,7 @@ describe('core/strategies/verifyPasswordResetCode', () => {
492501
it('should verify the oob code', async () => {
493502
const mock = mockEndpoint(Endpoint.RESET_PASSWORD, {
494503
requestType: ActionCodeOperation.PASSWORD_RESET,
495-
504+
email: TEST_EMAIL,
496505
previousEmail: null
497506
});
498507
const response = await verifyPasswordResetCode(auth, oobCode);
@@ -535,16 +544,16 @@ describe('core/strategies/verifyPasswordResetCode', () => {
535544
describe('core/strategies/email_and_password/createUserWithEmailAndPassword', () => {
536545
let auth: TestAuth;
537546
const serverUser: APIUserInfo = {
538-
localId: 'local-id'
547+
localId: TEST_LOCAL_ID
539548
};
540549

541550
beforeEach(async () => {
542551
auth = await testAuth();
543552
mockFetch.setUp();
544553
mockEndpoint(Endpoint.SIGN_UP, {
545-
idToken: 'id-token',
546-
refreshToken: 'refresh-token',
547-
expiresIn: '1234',
554+
idToken: TEST_ID_TOKEN,
555+
refreshToken: TEST_REFRESH_TOKEN,
556+
expiresIn: TEST_TOKEN_EXPIRY_TIME,
548557
localId: serverUser.localId!
549558
});
550559
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, {
@@ -557,13 +566,13 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
557566
const { _tokenResponse, user, operationType } =
558567
(await createUserWithEmailAndPassword(
559568
auth,
560-
'some-email',
561-
'some-password'
569+
TEST_EMAIL,
570+
TEST_PASSWORD
562571
)) as UserCredentialInternal;
563572
expect(_tokenResponse).to.eql({
564-
idToken: 'id-token',
565-
refreshToken: 'refresh-token',
566-
expiresIn: '1234',
573+
idToken: TEST_ID_TOKEN,
574+
refreshToken: TEST_REFRESH_TOKEN,
575+
expiresIn: TEST_TOKEN_EXPIRY_TIME,
567576
localId: serverUser.localId!
568577
});
569578
expect(operationType).to.eq(OperationType.SIGN_IN);
@@ -622,13 +631,13 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
622631
const { _tokenResponse, user, operationType } =
623632
(await createUserWithEmailAndPassword(
624633
auth,
625-
'some-email',
626-
'some-password'
634+
TEST_EMAIL,
635+
TEST_PASSWORD
627636
)) as UserCredentialInternal;
628637
expect(_tokenResponse).to.eql({
629-
idToken: 'id-token',
630-
refreshToken: 'refresh-token',
631-
expiresIn: '1234',
638+
idToken: TEST_ID_TOKEN,
639+
refreshToken: TEST_REFRESH_TOKEN,
640+
expiresIn: TEST_TOKEN_EXPIRY_TIME,
632641
localId: serverUser.localId!
633642
});
634643
expect(operationType).to.eq(OperationType.SIGN_IN);
@@ -653,13 +662,13 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
653662
const { _tokenResponse, user, operationType } =
654663
(await createUserWithEmailAndPassword(
655664
auth,
656-
'some-email',
657-
'some-password'
665+
TEST_EMAIL,
666+
TEST_PASSWORD
658667
)) as UserCredentialInternal;
659668
expect(_tokenResponse).to.eql({
660-
idToken: 'id-token',
661-
refreshToken: 'refresh-token',
662-
expiresIn: '1234',
669+
idToken: TEST_ID_TOKEN,
670+
refreshToken: TEST_REFRESH_TOKEN,
671+
expiresIn: TEST_TOKEN_EXPIRY_TIME,
663672
localId: serverUser.localId!
664673
});
665674
expect(operationType).to.eq(OperationType.SIGN_IN);
@@ -676,8 +685,8 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
676685
mockEndpointWithParams(
677686
Endpoint.SIGN_UP,
678687
{
679-
email: 'some-email',
680-
password: 'some-password',
688+
email: TEST_EMAIL,
689+
password: TEST_PASSWORD,
681690
clientType: RecaptchaClientType.WEB
682691
},
683692
{
@@ -693,16 +702,16 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
693702
mockEndpointWithParams(
694703
Endpoint.SIGN_UP,
695704
{
696-
email: 'some-email',
697-
password: 'some-password',
705+
email: TEST_EMAIL,
706+
password: TEST_PASSWORD,
698707
captchaResp: 'recaptcha-response',
699708
clientType: RecaptchaClientType.WEB,
700709
recaptchaVersion: RecaptchaVersion.ENTERPRISE
701710
},
702711
{
703-
idToken: 'id-token',
704-
refreshToken: 'refresh-token',
705-
expiresIn: '1234',
712+
idToken: TEST_ID_TOKEN,
713+
refreshToken: TEST_REFRESH_TOKEN,
714+
expiresIn: TEST_TOKEN_EXPIRY_TIME,
706715
localId: serverUser.localId!
707716
}
708717
);
@@ -731,13 +740,13 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
731740
const { _tokenResponse, user, operationType } =
732741
(await createUserWithEmailAndPassword(
733742
auth,
734-
'some-email',
735-
'some-password'
743+
TEST_EMAIL,
744+
TEST_PASSWORD
736745
)) as UserCredentialInternal;
737746
expect(_tokenResponse).to.eql({
738-
idToken: 'id-token',
739-
refreshToken: 'refresh-token',
740-
expiresIn: '1234',
747+
idToken: TEST_ID_TOKEN,
748+
refreshToken: TEST_REFRESH_TOKEN,
749+
expiresIn: TEST_TOKEN_EXPIRY_TIME,
741750
localId: serverUser.localId!
742751
});
743752
expect(operationType).to.eq(OperationType.SIGN_IN);
@@ -750,16 +759,16 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
750759
describe('core/strategies/email_and_password/signInWithEmailAndPassword', () => {
751760
let auth: TestAuth;
752761
const serverUser: APIUserInfo = {
753-
localId: 'local-id'
762+
localId: TEST_LOCAL_ID
754763
};
755764

756765
beforeEach(async () => {
757766
auth = await testAuth();
758767
mockFetch.setUp();
759768
mockEndpoint(Endpoint.SIGN_IN_WITH_PASSWORD, {
760-
idToken: 'id-token',
761-
refreshToken: 'refresh-token',
762-
expiresIn: '1234',
769+
idToken: TEST_ID_TOKEN,
770+
refreshToken: TEST_REFRESH_TOKEN,
771+
expiresIn: TEST_TOKEN_EXPIRY_TIME,
763772
localId: serverUser.localId!
764773
});
765774
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, {
@@ -772,13 +781,13 @@ describe('core/strategies/email_and_password/signInWithEmailAndPassword', () =>
772781
const { _tokenResponse, user, operationType } =
773782
(await signInWithEmailAndPassword(
774783
auth,
775-
'some-email',
776-
'some-password'
784+
TEST_EMAIL,
785+
TEST_PASSWORD
777786
)) as UserCredentialInternal;
778787
expect(_tokenResponse).to.eql({
779-
idToken: 'id-token',
780-
refreshToken: 'refresh-token',
781-
expiresIn: '1234',
788+
idToken: TEST_ID_TOKEN,
789+
refreshToken: TEST_REFRESH_TOKEN,
790+
expiresIn: TEST_TOKEN_EXPIRY_TIME,
782791
localId: serverUser.localId!
783792
});
784793
expect(operationType).to.eq(OperationType.SIGN_IN);
@@ -829,8 +838,6 @@ describe('password policy cache is updated in auth flows upon error', () => {
829838
allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_STRING,
830839
schemaVersion: TEST_SCHEMA_VERSION
831840
};
832-
const TEST_EMAIL = '[email protected]';
833-
const TEST_PASSWORD = 'some-password';
834841
let policyEndpointMock: mockFetch.Route;
835842
let policyEndpointMockWithTenant: mockFetch.Route;
836843
let policyEndpointMockWithOtherTenant: mockFetch.Route;
@@ -862,14 +869,14 @@ describe('password policy cache is updated in auth flows upon error', () => {
862869

863870
context('#createUserWithEmailAndPassword', () => {
864871
const TEST_SERVER_USER: APIUserInfo = {
865-
localId: 'local-id'
872+
localId: TEST_LOCAL_ID
866873
};
867874

868875
beforeEach(() => {
869876
mockEndpoint(Endpoint.SIGN_UP, {
870-
idToken: 'id-token',
871-
refreshToken: 'refresh-token',
872-
expiresIn: '1234',
877+
idToken: TEST_ID_TOKEN,
878+
refreshToken: TEST_REFRESH_TOKEN,
879+
expiresIn: TEST_TOKEN_EXPIRY_TIME,
873880
localId: TEST_SERVER_USER.localId!
874881
});
875882
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, {
@@ -911,7 +918,7 @@ describe('password policy cache is updated in auth flows upon error', () => {
911918
);
912919
});
913920

914-
it('updates the cached password policy when password does not meet backend requirements', async () => {
921+
it('updates the cached password policy when password does not meet backend requirements for the project', async () => {
915922
await auth._updatePasswordPolicy();
916923
expect(policyEndpointMock.calls.length).to.eq(1);
917924
expect(auth._getPasswordPolicyInternal()).to.eql(
@@ -930,6 +937,27 @@ describe('password policy cache is updated in auth flows upon error', () => {
930937
);
931938
});
932939

940+
it('updates the cached password policy when password does not meet backend requirements for the tenant', async () => {
941+
auth.tenantId = TEST_TENANT_ID;
942+
await auth._updatePasswordPolicy();
943+
expect(policyEndpointMockWithTenant.calls.length).to.eq(1);
944+
expect(auth._getPasswordPolicyInternal()).to.eql(
945+
CACHED_PASSWORD_POLICY
946+
);
947+
948+
// Password policy changed after previous fetch.
949+
policyEndpointMockWithTenant.response =
950+
PASSWORD_POLICY_RESPONSE_REQUIRE_NUMERIC;
951+
await expect(
952+
createUserWithEmailAndPassword(auth, TEST_EMAIL, TEST_PASSWORD)
953+
).to.be.rejectedWith(FirebaseError, PASSWORD_ERROR_MSG);
954+
955+
expect(policyEndpointMockWithTenant.calls.length).to.eq(2);
956+
expect(auth._getPasswordPolicyInternal()).to.eql(
957+
CACHED_PASSWORD_POLICY_REQUIRE_NUMERIC
958+
);
959+
});
960+
933961
it('does not update the cached password policy upon error if policy has not previously been fetched', async () => {
934962
expect(auth._getPasswordPolicyInternal()).to.be.null;
935963

@@ -1000,7 +1028,7 @@ describe('password policy cache is updated in auth flows upon error', () => {
10001028
);
10011029
});
10021030

1003-
it('updates the cached password policy when password does not meet backend requirements', async () => {
1031+
it('updates the cached password policy when password does not meet backend requirements for the project', async () => {
10041032
await auth._updatePasswordPolicy();
10051033
expect(policyEndpointMock.calls.length).to.eq(1);
10061034
expect(auth._getPasswordPolicyInternal()).to.eql(
@@ -1019,6 +1047,27 @@ describe('password policy cache is updated in auth flows upon error', () => {
10191047
);
10201048
});
10211049

1050+
it('updates the cached password policy when password does not meet backend requirements for the tenant', async () => {
1051+
auth.tenantId = TEST_TENANT_ID;
1052+
await auth._updatePasswordPolicy();
1053+
expect(policyEndpointMockWithTenant.calls.length).to.eq(1);
1054+
expect(auth._getPasswordPolicyInternal()).to.eql(
1055+
CACHED_PASSWORD_POLICY
1056+
);
1057+
1058+
// Password policy changed after previous fetch.
1059+
policyEndpointMockWithTenant.response =
1060+
PASSWORD_POLICY_RESPONSE_REQUIRE_NUMERIC;
1061+
await expect(
1062+
confirmPasswordReset(auth, TEST_OOB_CODE, TEST_PASSWORD)
1063+
).to.be.rejectedWith(FirebaseError, PASSWORD_ERROR_MSG);
1064+
1065+
expect(policyEndpointMockWithTenant.calls.length).to.eq(2);
1066+
expect(auth._getPasswordPolicyInternal()).to.eql(
1067+
CACHED_PASSWORD_POLICY_REQUIRE_NUMERIC
1068+
);
1069+
});
1070+
10221071
it('does not update the cached password policy upon error if policy has not previously been fetched', async () => {
10231072
expect(auth._getPasswordPolicyInternal()).to.be.null;
10241073

packages/auth/src/core/strategies/email_and_password.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import { RecaptchaActionName, RecaptchaClientType } from '../../api';
5353
*
5454
* @private
5555
*/
56-
async function updatePasswordPolicyIfCached(auth: Auth): Promise<void> {
56+
async function recachePasswordPolicy(auth: Auth): Promise<void> {
5757
const authInternal = _castAuth(auth);
5858
if (authInternal._getPasswordPolicyInternal()) {
5959
await authInternal._updatePasswordPolicy();
@@ -184,7 +184,7 @@ export async function confirmPasswordReset(
184184
error.code ===
185185
`auth/${AuthErrorCode.PASSWORD_DOES_NOT_MEET_REQUIREMENTS}`
186186
) {
187-
await updatePasswordPolicyIfCached(auth);
187+
await recachePasswordPolicy(auth);
188188
}
189189

190190
return Promise.reject(error);
@@ -343,7 +343,7 @@ export async function createUserWithEmailAndPassword(
343343
error.code ===
344344
`auth/${AuthErrorCode.PASSWORD_DOES_NOT_MEET_REQUIREMENTS}`
345345
) {
346-
await updatePasswordPolicyIfCached(auth);
346+
await recachePasswordPolicy(auth);
347347
}
348348

349349
return Promise.reject(error);

0 commit comments

Comments
 (0)