Skip to content

Commit 8bfac9c

Browse files
committed
Add and refactor tests
1 parent 1067b56 commit 8bfac9c

File tree

1 file changed

+207
-97
lines changed

1 file changed

+207
-97
lines changed

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

Lines changed: 207 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -745,76 +745,141 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
745745
expect(user.isAnonymous).to.be.false;
746746
});
747747
});
748+
});
748749

749-
context('#passwordPolicy', () => {
750-
const TEST_MIN_PASSWORD_LENGTH = 6;
751-
const TEST_ALLOWED_NON_ALPHANUMERIC_CHARS = ['!', '(', ')'];
752-
const TEST_SCHEMA_VERSION = 1;
750+
describe('core/strategies/email_and_password/signInWithEmailAndPassword', () => {
751+
let auth: TestAuth;
752+
const serverUser: APIUserInfo = {
753+
localId: 'local-id'
754+
};
755+
756+
beforeEach(async () => {
757+
auth = await testAuth();
758+
mockFetch.setUp();
759+
mockEndpoint(Endpoint.SIGN_IN_WITH_PASSWORD, {
760+
idToken: 'id-token',
761+
refreshToken: 'refresh-token',
762+
expiresIn: '1234',
763+
localId: serverUser.localId!
764+
});
765+
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, {
766+
users: [serverUser]
767+
});
768+
});
769+
afterEach(mockFetch.tearDown);
753770

754-
const TEST_TENANT_ID = 'tenant-id';
755-
const TEST_REQUIRE_NUMERIC_TENANT_ID = 'other-tenant-id';
771+
it('should sign in the user', async () => {
772+
const { _tokenResponse, user, operationType } =
773+
(await signInWithEmailAndPassword(
774+
auth,
775+
'some-email',
776+
'some-password'
777+
)) as UserCredentialInternal;
778+
expect(_tokenResponse).to.eql({
779+
idToken: 'id-token',
780+
refreshToken: 'refresh-token',
781+
expiresIn: '1234',
782+
localId: serverUser.localId!
783+
});
784+
expect(operationType).to.eq(OperationType.SIGN_IN);
785+
expect(user.uid).to.eq(serverUser.localId);
786+
expect(user.isAnonymous).to.be.false;
787+
});
788+
});
756789

757-
const PASSWORD_ERROR_MSG =
758-
'Firebase: The password does not meet the requirements. (auth/password-does-not-meet-requirements).';
790+
describe('password policy cache is updated in auth flows upon error', () => {
791+
let auth: TestAuth;
759792

760-
const passwordPolicyResponse = {
761-
customStrengthOptions: {
762-
minPasswordLength: TEST_MIN_PASSWORD_LENGTH
763-
},
764-
allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS,
765-
schemaVersion: TEST_SCHEMA_VERSION
766-
};
767-
const passwordPolicyResponseRequireNumeric = {
768-
customStrengthOptions: {
769-
minPasswordLength: TEST_MIN_PASSWORD_LENGTH,
770-
containsNumericCharacter: true
771-
},
772-
allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS,
773-
schemaVersion: TEST_SCHEMA_VERSION
774-
};
775-
const cachedPasswordPolicy = {
776-
customStrengthOptions: {
777-
minPasswordLength: TEST_MIN_PASSWORD_LENGTH
793+
const TEST_MIN_PASSWORD_LENGTH = 6;
794+
const TEST_ALLOWED_NON_ALPHANUMERIC_CHARS = ['!', '(', ')'];
795+
const TEST_SCHEMA_VERSION = 1;
796+
797+
const TEST_TENANT_ID = 'tenant-id';
798+
const TEST_REQUIRE_NUMERIC_TENANT_ID = 'other-tenant-id';
799+
800+
const PASSWORD_ERROR_MSG =
801+
'Firebase: The password does not meet the requirements. (auth/password-does-not-meet-requirements).';
802+
803+
const passwordPolicyResponse = {
804+
customStrengthOptions: {
805+
minPasswordLength: TEST_MIN_PASSWORD_LENGTH
806+
},
807+
allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS,
808+
schemaVersion: TEST_SCHEMA_VERSION
809+
};
810+
const passwordPolicyResponseRequireNumeric = {
811+
customStrengthOptions: {
812+
minPasswordLength: TEST_MIN_PASSWORD_LENGTH,
813+
containsNumericCharacter: true
814+
},
815+
allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS,
816+
schemaVersion: TEST_SCHEMA_VERSION
817+
};
818+
const cachedPasswordPolicy = {
819+
customStrengthOptions: {
820+
minPasswordLength: TEST_MIN_PASSWORD_LENGTH
821+
},
822+
allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS
823+
};
824+
const cachedPasswordPolicyRequireNumeric = {
825+
customStrengthOptions: {
826+
minPasswordLength: TEST_MIN_PASSWORD_LENGTH,
827+
containsNumericCharacter: true
828+
},
829+
allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS
830+
};
831+
let policyEndpointMock: mockFetch.Route;
832+
let policyEndpointMockWithTenant: mockFetch.Route;
833+
let policyEndpointMockWithOtherTenant: mockFetch.Route;
834+
835+
beforeEach(async () => {
836+
auth = await testAuth();
837+
mockFetch.setUp();
838+
policyEndpointMock = mockEndpointWithParams(
839+
Endpoint.GET_PASSWORD_POLICY,
840+
{},
841+
passwordPolicyResponse
842+
);
843+
policyEndpointMockWithTenant = mockEndpointWithParams(
844+
Endpoint.GET_PASSWORD_POLICY,
845+
{
846+
tenantId: TEST_TENANT_ID
778847
},
779-
allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS
780-
};
781-
const cachedPasswordPolicyRequireNumeric = {
782-
customStrengthOptions: {
783-
minPasswordLength: TEST_MIN_PASSWORD_LENGTH,
784-
containsNumericCharacter: true
848+
passwordPolicyResponse
849+
);
850+
policyEndpointMockWithOtherTenant = mockEndpointWithParams(
851+
Endpoint.GET_PASSWORD_POLICY,
852+
{
853+
tenantId: TEST_REQUIRE_NUMERIC_TENANT_ID
785854
},
786-
allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS
855+
passwordPolicyResponseRequireNumeric
856+
);
857+
});
858+
afterEach(mockFetch.tearDown);
859+
860+
context('#createUserWithEmailAndPassword', () => {
861+
const serverUser: APIUserInfo = {
862+
localId: 'local-id'
787863
};
788-
let policyEndpointMock: mockFetch.Route;
789-
let policyEndpointMockWithTenant: mockFetch.Route;
790-
let policyEndpointMockWithOtherTenant: mockFetch.Route;
864+
865+
const email = 'some-email';
866+
const password = 'some-password';
791867

792868
beforeEach(() => {
793-
policyEndpointMock = mockEndpointWithParams(
794-
Endpoint.GET_PASSWORD_POLICY,
795-
{},
796-
passwordPolicyResponse
797-
);
798-
policyEndpointMockWithTenant = mockEndpointWithParams(
799-
Endpoint.GET_PASSWORD_POLICY,
800-
{
801-
tenantId: TEST_TENANT_ID
802-
},
803-
passwordPolicyResponse
804-
);
805-
policyEndpointMockWithOtherTenant = mockEndpointWithParams(
806-
Endpoint.GET_PASSWORD_POLICY,
807-
{
808-
tenantId: TEST_REQUIRE_NUMERIC_TENANT_ID
809-
},
810-
passwordPolicyResponseRequireNumeric
811-
);
869+
mockEndpoint(Endpoint.SIGN_UP, {
870+
idToken: 'id-token',
871+
refreshToken: 'refresh-token',
872+
expiresIn: '1234',
873+
localId: serverUser.localId!
874+
});
875+
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, {
876+
users: [serverUser]
877+
});
812878
});
813879

814880
it('does not update the cached password policy upon successful sign up when there is no existing policy cache', async () => {
815-
await expect(
816-
createUserWithEmailAndPassword(auth, 'some-email', 'some-password')
817-
).to.be.fulfilled;
881+
await expect(createUserWithEmailAndPassword(auth, email, password)).to.be
882+
.fulfilled;
818883

819884
expect(policyEndpointMock.calls.length).to.eq(0);
820885
expect(auth._getPasswordPolicy()).to.be.null;
@@ -823,9 +888,8 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
823888
it('does not update the cached password policy upon successful sign up when there is an existing policy cache', async () => {
824889
await auth._updatePasswordPolicy();
825890

826-
await expect(
827-
createUserWithEmailAndPassword(auth, 'some-email', 'some-password')
828-
).to.be.fulfilled;
891+
await expect(createUserWithEmailAndPassword(auth, email, password)).to.be
892+
.fulfilled;
829893

830894
expect(policyEndpointMock.calls.length).to.eq(1);
831895
expect(auth._getPasswordPolicy()).to.eql(cachedPasswordPolicy);
@@ -853,7 +917,7 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
853917
// Password policy changed after previous fetch.
854918
policyEndpointMock.response = passwordPolicyResponseRequireNumeric;
855919
await expect(
856-
createUserWithEmailAndPassword(auth, 'some-email', 'some-password')
920+
createUserWithEmailAndPassword(auth, email, password)
857921
).to.be.rejectedWith(FirebaseError, PASSWORD_ERROR_MSG);
858922

859923
expect(policyEndpointMock.calls.length).to.eq(2);
@@ -866,7 +930,7 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
866930
expect(auth._getPasswordPolicy()).to.be.null;
867931

868932
await expect(
869-
createUserWithEmailAndPassword(auth, 'some-email', 'some-password')
933+
createUserWithEmailAndPassword(auth, email, password)
870934
).to.be.rejectedWith(FirebaseError, PASSWORD_ERROR_MSG);
871935

872936
expect(policyEndpointMock.calls.length).to.eq(0);
@@ -881,51 +945,97 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
881945

882946
auth.tenantId = TEST_REQUIRE_NUMERIC_TENANT_ID;
883947
await expect(
884-
createUserWithEmailAndPassword(auth, 'some-email', 'some-password')
948+
createUserWithEmailAndPassword(auth, email, password)
885949
).to.be.rejectedWith(FirebaseError, PASSWORD_ERROR_MSG);
886950
expect(policyEndpointMockWithOtherTenant.calls.length).to.eq(0);
887951
expect(auth._getPasswordPolicy()).to.be.undefined;
888952
});
889953
});
890954
});
891-
});
892955

893-
describe('core/strategies/email_and_password/signInWithEmailAndPassword', () => {
894-
let auth: TestAuth;
895-
const serverUser: APIUserInfo = {
896-
localId: 'local-id'
897-
};
956+
context('#confirmPasswordReset', () => {
957+
const oobCode = 'oob-code';
958+
const newPassword = 'new-password';
898959

899-
beforeEach(async () => {
900-
auth = await testAuth();
901-
mockFetch.setUp();
902-
mockEndpoint(Endpoint.SIGN_IN_WITH_PASSWORD, {
903-
idToken: 'id-token',
904-
refreshToken: 'refresh-token',
905-
expiresIn: '1234',
906-
localId: serverUser.localId!
960+
beforeEach(() => {
961+
mockEndpoint(Endpoint.RESET_PASSWORD, {
962+
963+
});
907964
});
908-
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, {
909-
users: [serverUser]
965+
966+
it('does not update the cached password policy upon successful password reset when there is no existing policy cache', async () => {
967+
await expect(confirmPasswordReset(auth, oobCode, newPassword)).to.be
968+
.fulfilled;
969+
970+
expect(policyEndpointMock.calls.length).to.eq(0);
971+
expect(auth._getPasswordPolicy()).to.be.null;
910972
});
911-
});
912-
afterEach(mockFetch.tearDown);
913973

914-
it('should sign in the user', async () => {
915-
const { _tokenResponse, user, operationType } =
916-
(await signInWithEmailAndPassword(
917-
auth,
918-
'some-email',
919-
'some-password'
920-
)) as UserCredentialInternal;
921-
expect(_tokenResponse).to.eql({
922-
idToken: 'id-token',
923-
refreshToken: 'refresh-token',
924-
expiresIn: '1234',
925-
localId: serverUser.localId!
974+
it('does not update the cached password policy upon successful password reset when there is an existing policy cache', async () => {
975+
await auth._updatePasswordPolicy();
976+
977+
await expect(confirmPasswordReset(auth, oobCode, newPassword)).to.be
978+
.fulfilled;
979+
980+
expect(policyEndpointMock.calls.length).to.eq(1);
981+
expect(auth._getPasswordPolicy()).to.eql(cachedPasswordPolicy);
982+
});
983+
984+
context('handles password validation errors', () => {
985+
beforeEach(() => {
986+
mockEndpoint(
987+
Endpoint.RESET_PASSWORD,
988+
{
989+
error: {
990+
code: 400,
991+
message: ServerError.PASSWORD_DOES_NOT_MEET_REQUIREMENTS
992+
}
993+
},
994+
400
995+
);
996+
});
997+
998+
it('updates the cached password policy when password does not meet backend requirements', async () => {
999+
await auth._updatePasswordPolicy();
1000+
expect(policyEndpointMock.calls.length).to.eq(1);
1001+
expect(auth._getPasswordPolicy()).to.eql(cachedPasswordPolicy);
1002+
1003+
// Password policy changed after previous fetch.
1004+
policyEndpointMock.response = passwordPolicyResponseRequireNumeric;
1005+
await expect(
1006+
confirmPasswordReset(auth, oobCode, newPassword)
1007+
).to.be.rejectedWith(FirebaseError, PASSWORD_ERROR_MSG);
1008+
1009+
expect(policyEndpointMock.calls.length).to.eq(2);
1010+
expect(auth._getPasswordPolicy()).to.eql(
1011+
cachedPasswordPolicyRequireNumeric
1012+
);
1013+
});
1014+
1015+
it('does not update the cached password policy upon error if policy has not previously been fetched', async () => {
1016+
expect(auth._getPasswordPolicy()).to.be.null;
1017+
1018+
await expect(
1019+
confirmPasswordReset(auth, oobCode, newPassword)
1020+
).to.be.rejectedWith(FirebaseError, PASSWORD_ERROR_MSG);
1021+
1022+
expect(policyEndpointMock.calls.length).to.eq(0);
1023+
expect(auth._getPasswordPolicy()).to.be.null;
1024+
});
1025+
1026+
it('does not update the cached password policy upon error if tenant changes and policy has not previously been fetched', async () => {
1027+
auth.tenantId = TEST_TENANT_ID;
1028+
await auth._updatePasswordPolicy();
1029+
expect(policyEndpointMockWithTenant.calls.length).to.eq(1);
1030+
expect(auth._getPasswordPolicy()).to.eql(cachedPasswordPolicy);
1031+
1032+
auth.tenantId = TEST_REQUIRE_NUMERIC_TENANT_ID;
1033+
await expect(
1034+
confirmPasswordReset(auth, oobCode, newPassword)
1035+
).to.be.rejectedWith(FirebaseError, PASSWORD_ERROR_MSG);
1036+
expect(policyEndpointMockWithOtherTenant.calls.length).to.eq(0);
1037+
expect(auth._getPasswordPolicy()).to.be.undefined;
1038+
});
9261039
});
927-
expect(operationType).to.eq(OperationType.SIGN_IN);
928-
expect(user.uid).to.eq(serverUser.localId);
929-
expect(user.isAnonymous).to.be.false;
9301040
});
9311041
});

0 commit comments

Comments
 (0)