Skip to content

Commit 7aca281

Browse files
committed
Fix issues based on suggestions
1 parent 7c13752 commit 7aca281

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,12 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
751751
const TEST_ALLOWED_NON_ALPHANUMERIC_CHARS = ['!', '(', ')'];
752752
const TEST_SCHEMA_VERSION = 1;
753753

754+
const TEST_TENANT_ID = 'tenant-id';
755+
const TEST_REQUIRE_NUMERIC_TENANT_ID = 'other-tenant-id';
756+
757+
const PASSWORD_ERROR_MSG =
758+
'Firebase: The password does not meet the requirements. (auth/password-does-not-meet-requirements).';
759+
754760
const passwordPolicyResponse = {
755761
customStrengthOptions: {
756762
minPasswordLength: TEST_MIN_PASSWORD_LENGTH
@@ -792,20 +798,20 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
792798
policyEndpointMockWithTenant = mockEndpointWithParams(
793799
Endpoint.GET_PASSWORD_POLICY,
794800
{
795-
tenantId: 'tenant-id'
801+
tenantId: TEST_TENANT_ID
796802
},
797803
passwordPolicyResponse
798804
);
799805
policyEndpointMockWithOtherTenant = mockEndpointWithParams(
800806
Endpoint.GET_PASSWORD_POLICY,
801807
{
802-
tenantId: 'other-tenant-id'
808+
tenantId: TEST_REQUIRE_NUMERIC_TENANT_ID
803809
},
804810
passwordPolicyResponseRequireNumeric
805811
);
806812
});
807813

808-
it('does not update the password policy upon successful sign up when there is no existing policy cache', async () => {
814+
it('does not update the cached password policy upon successful sign up when there is no existing policy cache', async () => {
809815
await expect(
810816
createUserWithEmailAndPassword(auth, 'some-email', 'some-password')
811817
).to.be.fulfilled;
@@ -814,7 +820,7 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
814820
expect(auth._getPasswordPolicy()).to.be.null;
815821
});
816822

817-
it('does not update the password policy upon successful sign up when there is an existing policy cache', async () => {
823+
it('does not update the cached password policy upon successful sign up when there is an existing policy cache', async () => {
818824
await auth._updatePasswordPolicy();
819825

820826
await expect(
@@ -839,50 +845,44 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
839845
);
840846
});
841847

842-
it('updates the password policy when password does not meet backend requirements', async () => {
848+
it('updates the cached password policy when password does not meet backend requirements', async () => {
843849
await auth._updatePasswordPolicy();
844850
expect(policyEndpointMock.calls.length).to.eq(1);
845851
expect(auth._getPasswordPolicy()).to.eql(cachedPasswordPolicy);
846852

853+
// Password policy changed after previous fetch.
847854
policyEndpointMock.response = passwordPolicyResponseRequireNumeric;
848855
await expect(
849856
createUserWithEmailAndPassword(auth, 'some-email', 'some-password')
850-
).to.be.rejectedWith(
851-
FirebaseError,
852-
'Firebase: The password does not meet the requirements. (auth/password-does-not-meet-requirements).'
853-
);
857+
).to.be.rejectedWith(FirebaseError, PASSWORD_ERROR_MSG);
854858

855859
expect(policyEndpointMock.calls.length).to.eq(2);
856860
expect(auth._getPasswordPolicy()).to.eql(
857861
cachedPasswordPolicyRequireNumeric
858862
);
859863
});
860864

861-
it('does not update the password policy upon error if policy has not previously been fetched', async () => {
865+
it('does not update the cached password policy upon error if policy has not previously been fetched', async () => {
866+
expect(auth._getPasswordPolicy()).to.be.null;
867+
862868
await expect(
863869
createUserWithEmailAndPassword(auth, 'some-email', 'some-password')
864-
).to.be.rejectedWith(
865-
FirebaseError,
866-
'Firebase: The password does not meet the requirements. (auth/password-does-not-meet-requirements).'
867-
);
870+
).to.be.rejectedWith(FirebaseError, PASSWORD_ERROR_MSG);
868871

869872
expect(policyEndpointMock.calls.length).to.eq(0);
870873
expect(auth._getPasswordPolicy()).to.be.null;
871874
});
872875

873-
it('does not update the password policy upon error if tenant changes and policy has not previously been fetched', async () => {
874-
auth.tenantId = 'tenant-id';
876+
it('does not update the cached password policy upon error if tenant changes and policy has not previously been fetched', async () => {
877+
auth.tenantId = TEST_TENANT_ID;
875878
await auth._updatePasswordPolicy();
876879
expect(policyEndpointMockWithTenant.calls.length).to.eq(1);
877880
expect(auth._getPasswordPolicy()).to.eql(cachedPasswordPolicy);
878881

879-
auth.tenantId = 'other-tenant-id';
882+
auth.tenantId = TEST_REQUIRE_NUMERIC_TENANT_ID;
880883
await expect(
881884
createUserWithEmailAndPassword(auth, 'some-email', 'some-password')
882-
).to.be.rejectedWith(
883-
FirebaseError,
884-
'Firebase: The password does not meet the requirements. (auth/password-does-not-meet-requirements).'
885-
);
885+
).to.be.rejectedWith(FirebaseError, PASSWORD_ERROR_MSG);
886886
expect(policyEndpointMockWithOtherTenant.calls.length).to.eq(0);
887887
expect(auth._getPasswordPolicy()).to.be.undefined;
888888
});

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ export async function createUserWithEmailAndPassword(
308308
);
309309
return signUp(authInternal, requestWithRecaptcha);
310310
} else {
311-
// Fetch the password policy if the password did not meet policy requirements and there is an existing policy cached.
311+
// Only fetch the password policy if the password did not meet policy requirements and there is an existing policy cached.
312+
// A developer must call validatePassword at least once for the cache to be automatically updated.
312313
if (
313314
error.code ===
314315
`auth/${AuthErrorCode.PASSWORD_DOES_NOT_MEET_REQUIREMENTS}` &&

0 commit comments

Comments
 (0)