@@ -59,6 +59,9 @@ const TEST_REFRESH_TOKEN = 'refresh-token';
59
59
const TEST_TOKEN_EXPIRY_TIME = '1234' ;
60
60
61
61
const TEST_LOCAL_ID = 'local-id' ;
62
+ const TEST_SERVER_USER : APIUserInfo = {
63
+ localId : TEST_LOCAL_ID
64
+ } ;
62
65
63
66
const TEST_EMAIL = '[email protected] ' ;
64
67
const TEST_PASSWORD = 'some-password' ;
@@ -543,9 +546,7 @@ describe('core/strategies/verifyPasswordResetCode', () => {
543
546
544
547
describe ( 'core/strategies/email_and_password/createUserWithEmailAndPassword' , ( ) => {
545
548
let auth : TestAuth ;
546
- const serverUser : APIUserInfo = {
547
- localId : TEST_LOCAL_ID
548
- } ;
549
+ const serverUser : APIUserInfo = TEST_SERVER_USER ;
549
550
550
551
beforeEach ( async ( ) => {
551
552
auth = await testAuth ( ) ;
@@ -758,9 +759,7 @@ describe('core/strategies/email_and_password/createUserWithEmailAndPassword', ()
758
759
759
760
describe ( 'core/strategies/email_and_password/signInWithEmailAndPassword' , ( ) => {
760
761
let auth : TestAuth ;
761
- const serverUser : APIUserInfo = {
762
- localId : TEST_LOCAL_ID
763
- } ;
762
+ const serverUser : APIUserInfo = TEST_SERVER_USER ;
764
763
765
764
beforeEach ( async ( ) => {
766
765
auth = await testAuth ( ) ;
@@ -842,6 +841,17 @@ describe('password policy cache is updated in auth flows upon error', () => {
842
841
let policyEndpointMockWithTenant : mockFetch . Route ;
843
842
let policyEndpointMockWithOtherTenant : mockFetch . Route ;
844
843
844
+ /**
845
+ * Wait for 50ms to allow the password policy to be fetched and recached.
846
+ */
847
+ async function waitForRecachePasswordPolicy ( ) : Promise < void > {
848
+ await new Promise < void > ( resolve => {
849
+ setTimeout ( ( ) => {
850
+ resolve ( ) ;
851
+ } , 50 ) ;
852
+ } ) ;
853
+ }
854
+
845
855
beforeEach ( async ( ) => {
846
856
auth = await testAuth ( ) ;
847
857
mockFetch . setUp ( ) ;
@@ -868,10 +878,6 @@ describe('password policy cache is updated in auth flows upon error', () => {
868
878
afterEach ( mockFetch . tearDown ) ;
869
879
870
880
context ( '#createUserWithEmailAndPassword' , ( ) => {
871
- const TEST_SERVER_USER : APIUserInfo = {
872
- localId : TEST_LOCAL_ID
873
- } ;
874
-
875
881
beforeEach ( ( ) => {
876
882
mockEndpoint ( Endpoint . SIGN_UP , {
877
883
idToken : TEST_ID_TOKEN ,
@@ -889,6 +895,9 @@ describe('password policy cache is updated in auth flows upon error', () => {
889
895
createUserWithEmailAndPassword ( auth , TEST_EMAIL , TEST_PASSWORD )
890
896
) . to . be . fulfilled ;
891
897
898
+ // Wait to ensure the password policy is not fetched and recached.
899
+ await waitForRecachePasswordPolicy ( ) ;
900
+
892
901
expect ( policyEndpointMock . calls . length ) . to . eq ( 0 ) ;
893
902
expect ( auth . _getPasswordPolicyInternal ( ) ) . to . be . null ;
894
903
} ) ;
@@ -900,6 +909,9 @@ describe('password policy cache is updated in auth flows upon error', () => {
900
909
createUserWithEmailAndPassword ( auth , TEST_EMAIL , TEST_PASSWORD )
901
910
) . to . be . fulfilled ;
902
911
912
+ // Wait to ensure the password policy is not fetched and recached.
913
+ await waitForRecachePasswordPolicy ( ) ;
914
+
903
915
expect ( policyEndpointMock . calls . length ) . to . eq ( 1 ) ;
904
916
expect ( auth . _getPasswordPolicyInternal ( ) ) . to . eql ( CACHED_PASSWORD_POLICY ) ;
905
917
} ) ;
@@ -931,6 +943,9 @@ describe('password policy cache is updated in auth flows upon error', () => {
931
943
createUserWithEmailAndPassword ( auth , TEST_EMAIL , TEST_PASSWORD )
932
944
) . to . be . rejectedWith ( FirebaseError , PASSWORD_ERROR_MSG ) ;
933
945
946
+ // Wait for the password policy to be fetched and recached.
947
+ await waitForRecachePasswordPolicy ( ) ;
948
+
934
949
expect ( policyEndpointMock . calls . length ) . to . eq ( 2 ) ;
935
950
expect ( auth . _getPasswordPolicyInternal ( ) ) . to . eql (
936
951
CACHED_PASSWORD_POLICY_REQUIRE_NUMERIC
@@ -952,6 +967,9 @@ describe('password policy cache is updated in auth flows upon error', () => {
952
967
createUserWithEmailAndPassword ( auth , TEST_EMAIL , TEST_PASSWORD )
953
968
) . to . be . rejectedWith ( FirebaseError , PASSWORD_ERROR_MSG ) ;
954
969
970
+ // Wait for the password policy to be fetched and recached.
971
+ await waitForRecachePasswordPolicy ( ) ;
972
+
955
973
expect ( policyEndpointMockWithTenant . calls . length ) . to . eq ( 2 ) ;
956
974
expect ( auth . _getPasswordPolicyInternal ( ) ) . to . eql (
957
975
CACHED_PASSWORD_POLICY_REQUIRE_NUMERIC
@@ -965,6 +983,9 @@ describe('password policy cache is updated in auth flows upon error', () => {
965
983
createUserWithEmailAndPassword ( auth , TEST_EMAIL , TEST_PASSWORD )
966
984
) . to . be . rejectedWith ( FirebaseError , PASSWORD_ERROR_MSG ) ;
967
985
986
+ // Wait for the password policy to be fetched and recached.
987
+ await waitForRecachePasswordPolicy ( ) ;
988
+
968
989
expect ( policyEndpointMock . calls . length ) . to . eq ( 0 ) ;
969
990
expect ( auth . _getPasswordPolicyInternal ( ) ) . to . be . null ;
970
991
} ) ;
@@ -981,6 +1002,10 @@ describe('password policy cache is updated in auth flows upon error', () => {
981
1002
await expect (
982
1003
createUserWithEmailAndPassword ( auth , TEST_EMAIL , TEST_PASSWORD )
983
1004
) . to . be . rejectedWith ( FirebaseError , PASSWORD_ERROR_MSG ) ;
1005
+
1006
+ // Wait to ensure the password policy is not fetched and recached.
1007
+ await waitForRecachePasswordPolicy ( ) ;
1008
+
984
1009
expect ( policyEndpointMockWithOtherTenant . calls . length ) . to . eq ( 0 ) ;
985
1010
expect ( auth . _getPasswordPolicyInternal ( ) ) . to . be . undefined ;
986
1011
} ) ;
@@ -1000,6 +1025,9 @@ describe('password policy cache is updated in auth flows upon error', () => {
1000
1025
await expect ( confirmPasswordReset ( auth , TEST_OOB_CODE , TEST_PASSWORD ) ) . to
1001
1026
. be . fulfilled ;
1002
1027
1028
+ // Wait to ensure the password policy is not fetched and recached.
1029
+ await waitForRecachePasswordPolicy ( ) ;
1030
+
1003
1031
expect ( policyEndpointMock . calls . length ) . to . eq ( 0 ) ;
1004
1032
expect ( auth . _getPasswordPolicyInternal ( ) ) . to . be . null ;
1005
1033
} ) ;
@@ -1010,6 +1038,9 @@ describe('password policy cache is updated in auth flows upon error', () => {
1010
1038
await expect ( confirmPasswordReset ( auth , TEST_OOB_CODE , TEST_PASSWORD ) ) . to
1011
1039
. be . fulfilled ;
1012
1040
1041
+ // Wait to ensure the password policy is not fetched and recached.
1042
+ await waitForRecachePasswordPolicy ( ) ;
1043
+
1013
1044
expect ( policyEndpointMock . calls . length ) . to . eq ( 1 ) ;
1014
1045
expect ( auth . _getPasswordPolicyInternal ( ) ) . to . eql ( CACHED_PASSWORD_POLICY ) ;
1015
1046
} ) ;
@@ -1041,6 +1072,9 @@ describe('password policy cache is updated in auth flows upon error', () => {
1041
1072
confirmPasswordReset ( auth , TEST_OOB_CODE , TEST_PASSWORD )
1042
1073
) . to . be . rejectedWith ( FirebaseError , PASSWORD_ERROR_MSG ) ;
1043
1074
1075
+ // Wait for the password policy to be fetched and recached.
1076
+ await waitForRecachePasswordPolicy ( ) ;
1077
+
1044
1078
expect ( policyEndpointMock . calls . length ) . to . eq ( 2 ) ;
1045
1079
expect ( auth . _getPasswordPolicyInternal ( ) ) . to . eql (
1046
1080
CACHED_PASSWORD_POLICY_REQUIRE_NUMERIC
@@ -1062,6 +1096,9 @@ describe('password policy cache is updated in auth flows upon error', () => {
1062
1096
confirmPasswordReset ( auth , TEST_OOB_CODE , TEST_PASSWORD )
1063
1097
) . to . be . rejectedWith ( FirebaseError , PASSWORD_ERROR_MSG ) ;
1064
1098
1099
+ // Wait for the password policy to be fetched and recached.
1100
+ await waitForRecachePasswordPolicy ( ) ;
1101
+
1065
1102
expect ( policyEndpointMockWithTenant . calls . length ) . to . eq ( 2 ) ;
1066
1103
expect ( auth . _getPasswordPolicyInternal ( ) ) . to . eql (
1067
1104
CACHED_PASSWORD_POLICY_REQUIRE_NUMERIC
@@ -1075,6 +1112,9 @@ describe('password policy cache is updated in auth flows upon error', () => {
1075
1112
confirmPasswordReset ( auth , TEST_OOB_CODE , TEST_PASSWORD )
1076
1113
) . to . be . rejectedWith ( FirebaseError , PASSWORD_ERROR_MSG ) ;
1077
1114
1115
+ // Wait to ensure the password policy is not fetched and recached.
1116
+ await waitForRecachePasswordPolicy ( ) ;
1117
+
1078
1118
expect ( policyEndpointMock . calls . length ) . to . eq ( 0 ) ;
1079
1119
expect ( auth . _getPasswordPolicyInternal ( ) ) . to . be . null ;
1080
1120
} ) ;
@@ -1091,6 +1131,143 @@ describe('password policy cache is updated in auth flows upon error', () => {
1091
1131
await expect (
1092
1132
confirmPasswordReset ( auth , TEST_OOB_CODE , TEST_PASSWORD )
1093
1133
) . to . be . rejectedWith ( FirebaseError , PASSWORD_ERROR_MSG ) ;
1134
+
1135
+ // Wait to ensure the password policy is not fetched and recached.
1136
+ await waitForRecachePasswordPolicy ( ) ;
1137
+
1138
+ expect ( policyEndpointMockWithOtherTenant . calls . length ) . to . eq ( 0 ) ;
1139
+ expect ( auth . _getPasswordPolicyInternal ( ) ) . to . be . undefined ;
1140
+ } ) ;
1141
+ } ) ;
1142
+ } ) ;
1143
+
1144
+ context ( '#signInWithEmailAndPassword' , ( ) => {
1145
+ beforeEach ( ( ) => {
1146
+ mockEndpoint ( Endpoint . SIGN_IN_WITH_PASSWORD , {
1147
+ idToken : TEST_ID_TOKEN ,
1148
+ refreshToken : TEST_REFRESH_TOKEN ,
1149
+ expiresIn : TEST_TOKEN_EXPIRY_TIME ,
1150
+ localId : TEST_SERVER_USER . localId !
1151
+ } ) ;
1152
+ mockEndpoint ( Endpoint . GET_ACCOUNT_INFO , {
1153
+ users : [ TEST_SERVER_USER ]
1154
+ } ) ;
1155
+ } ) ;
1156
+
1157
+ it ( 'does not update the cached password policy upon successful sign-in when there is no existing policy cache' , async ( ) => {
1158
+ await expect ( signInWithEmailAndPassword ( auth , TEST_EMAIL , TEST_PASSWORD ) )
1159
+ . to . be . fulfilled ;
1160
+
1161
+ // Wait to ensure the password policy is not fetched and recached.
1162
+ await waitForRecachePasswordPolicy ( ) ;
1163
+
1164
+ expect ( policyEndpointMock . calls . length ) . to . eq ( 0 ) ;
1165
+ expect ( auth . _getPasswordPolicyInternal ( ) ) . to . be . null ;
1166
+ } ) ;
1167
+
1168
+ it ( 'does not update the cached password policy upon successful sign-in when there is an existing policy cache' , async ( ) => {
1169
+ await auth . _updatePasswordPolicy ( ) ;
1170
+
1171
+ await expect ( signInWithEmailAndPassword ( auth , TEST_EMAIL , TEST_PASSWORD ) )
1172
+ . to . be . fulfilled ;
1173
+
1174
+ // Wait to ensure the password policy is not fetched and recached.
1175
+ await waitForRecachePasswordPolicy ( ) ;
1176
+
1177
+ expect ( policyEndpointMock . calls . length ) . to . eq ( 1 ) ;
1178
+ expect ( auth . _getPasswordPolicyInternal ( ) ) . to . eql ( CACHED_PASSWORD_POLICY ) ;
1179
+ } ) ;
1180
+
1181
+ context ( 'handles password validation errors' , ( ) => {
1182
+ beforeEach ( ( ) => {
1183
+ mockEndpoint (
1184
+ Endpoint . SIGN_IN_WITH_PASSWORD ,
1185
+ {
1186
+ error : {
1187
+ code : 400 ,
1188
+ message : ServerError . PASSWORD_DOES_NOT_MEET_REQUIREMENTS
1189
+ }
1190
+ } ,
1191
+ 400
1192
+ ) ;
1193
+ } ) ;
1194
+
1195
+ it ( 'updates the cached password policy when password does not meet backend requirements for the project' , async ( ) => {
1196
+ await auth . _updatePasswordPolicy ( ) ;
1197
+ expect ( policyEndpointMock . calls . length ) . to . eq ( 1 ) ;
1198
+ expect ( auth . _getPasswordPolicyInternal ( ) ) . to . eql (
1199
+ CACHED_PASSWORD_POLICY
1200
+ ) ;
1201
+
1202
+ // Password policy changed after previous fetch.
1203
+ policyEndpointMock . response = PASSWORD_POLICY_RESPONSE_REQUIRE_NUMERIC ;
1204
+ await expect (
1205
+ signInWithEmailAndPassword ( auth , TEST_EMAIL , TEST_PASSWORD )
1206
+ ) . to . be . rejectedWith ( FirebaseError , PASSWORD_ERROR_MSG ) ;
1207
+
1208
+ // Wait for the password policy to be fetched and recached.
1209
+ await waitForRecachePasswordPolicy ( ) ;
1210
+
1211
+ expect ( policyEndpointMock . calls . length ) . to . eq ( 2 ) ;
1212
+ expect ( auth . _getPasswordPolicyInternal ( ) ) . to . eql (
1213
+ CACHED_PASSWORD_POLICY_REQUIRE_NUMERIC
1214
+ ) ;
1215
+ } ) ;
1216
+
1217
+ it ( 'updates the cached password policy when password does not meet backend requirements for the tenant' , async ( ) => {
1218
+ auth . tenantId = TEST_TENANT_ID ;
1219
+ await auth . _updatePasswordPolicy ( ) ;
1220
+ expect ( policyEndpointMockWithTenant . calls . length ) . to . eq ( 1 ) ;
1221
+ expect ( auth . _getPasswordPolicyInternal ( ) ) . to . eql (
1222
+ CACHED_PASSWORD_POLICY
1223
+ ) ;
1224
+
1225
+ // Password policy changed after previous fetch.
1226
+ policyEndpointMockWithTenant . response =
1227
+ PASSWORD_POLICY_RESPONSE_REQUIRE_NUMERIC ;
1228
+ await expect (
1229
+ signInWithEmailAndPassword ( auth , TEST_EMAIL , TEST_PASSWORD )
1230
+ ) . to . be . rejectedWith ( FirebaseError , PASSWORD_ERROR_MSG ) ;
1231
+
1232
+ // Wait for the password policy to be fetched and recached.
1233
+ await waitForRecachePasswordPolicy ( ) ;
1234
+
1235
+ expect ( policyEndpointMockWithTenant . calls . length ) . to . eq ( 2 ) ;
1236
+ expect ( auth . _getPasswordPolicyInternal ( ) ) . to . eql (
1237
+ CACHED_PASSWORD_POLICY_REQUIRE_NUMERIC
1238
+ ) ;
1239
+ } ) ;
1240
+
1241
+ it ( 'does not update the cached password policy upon error if policy has not previously been fetched' , async ( ) => {
1242
+ expect ( auth . _getPasswordPolicyInternal ( ) ) . to . be . null ;
1243
+
1244
+ await expect (
1245
+ signInWithEmailAndPassword ( auth , TEST_EMAIL , TEST_PASSWORD )
1246
+ ) . to . be . rejectedWith ( FirebaseError , PASSWORD_ERROR_MSG ) ;
1247
+
1248
+ // Wait to ensure the password policy is not fetched and recached.
1249
+ await waitForRecachePasswordPolicy ( ) ;
1250
+
1251
+ expect ( policyEndpointMock . calls . length ) . to . eq ( 0 ) ;
1252
+ expect ( auth . _getPasswordPolicyInternal ( ) ) . to . be . null ;
1253
+ } ) ;
1254
+
1255
+ it ( 'does not update the cached password policy upon error if tenant changes and policy has not previously been fetched' , async ( ) => {
1256
+ auth . tenantId = TEST_TENANT_ID ;
1257
+ await auth . _updatePasswordPolicy ( ) ;
1258
+ expect ( policyEndpointMockWithTenant . calls . length ) . to . eq ( 1 ) ;
1259
+ expect ( auth . _getPasswordPolicyInternal ( ) ) . to . eql (
1260
+ CACHED_PASSWORD_POLICY
1261
+ ) ;
1262
+
1263
+ auth . tenantId = TEST_TENANT_ID_REQUIRE_NUMERIC ;
1264
+ await expect (
1265
+ signInWithEmailAndPassword ( auth , TEST_EMAIL , TEST_PASSWORD )
1266
+ ) . to . be . rejectedWith ( FirebaseError , PASSWORD_ERROR_MSG ) ;
1267
+
1268
+ // Wait to ensure the password policy is not fetched and recached.
1269
+ await waitForRecachePasswordPolicy ( ) ;
1270
+
1094
1271
expect ( policyEndpointMockWithOtherTenant . calls . length ) . to . eq ( 0 ) ;
1095
1272
expect ( auth . _getPasswordPolicyInternal ( ) ) . to . be . undefined ;
1096
1273
} ) ;
0 commit comments