@@ -29,7 +29,6 @@ import {
29
29
cleanUpTestInstance ,
30
30
getTestInstance ,
31
31
getTotpCode ,
32
- delay ,
33
32
email ,
34
33
incorrectTotpCode
35
34
} from '../../helpers/integration/helpers' ;
@@ -38,6 +37,7 @@ import {
38
37
TotpMultiFactorGenerator ,
39
38
TotpSecret
40
39
} from '../../../src/mfa/assertions/totp' ;
40
+ import { getEmulatorUrl } from '../../helpers/integration/settings' ;
41
41
42
42
use ( chaiAsPromised ) ;
43
43
use ( sinonChai ) ;
@@ -46,16 +46,26 @@ describe(' Integration tests: Mfa TOTP', () => {
46
46
let auth : Auth ;
47
47
let totpSecret : TotpSecret ;
48
48
let displayName : string ;
49
+ let totpTimestamp : Date ;
50
+ let emulatorUrl : string | null ;
49
51
beforeEach ( async ( ) => {
50
- auth = getTestInstance ( ) ;
51
- displayName = 'totp-integration-test' ;
52
+ emulatorUrl = getEmulatorUrl ( ) ;
53
+ if ( ! emulatorUrl ) {
54
+ auth = getTestInstance ( ) ;
55
+ displayName = 'totp-integration-test' ;
56
+ }
52
57
} ) ;
53
58
54
59
afterEach ( async ( ) => {
55
- await cleanUpTestInstance ( auth ) ;
60
+ if ( ! emulatorUrl ) {
61
+ await cleanUpTestInstance ( auth ) ;
62
+ }
56
63
} ) ;
57
64
58
- it ( 'should not enroll if incorrect totp supplied' , async ( ) => {
65
+ it ( 'should not enroll if incorrect totp supplied' , async function ( ) {
66
+ if ( emulatorUrl ) {
67
+ this . skip ( ) ;
68
+ }
59
69
const cr = await signInWithEmailAndPassword ( auth , email , 'password' ) ;
60
70
const mfaUser = multiFactor ( cr . user ) ;
61
71
const session = await mfaUser . getSession ( ) ;
@@ -71,7 +81,10 @@ describe(' Integration tests: Mfa TOTP', () => {
71
81
) . to . be . rejectedWith ( 'auth/invalid-verification-code' ) ;
72
82
} ) ;
73
83
74
- it ( 'should enroll using correct otp' , async ( ) => {
84
+ it ( 'should enroll using correct otp' , async function ( ) {
85
+ if ( emulatorUrl ) {
86
+ this . skip ( ) ;
87
+ }
75
88
const cr = await signInWithEmailAndPassword ( auth , email , 'password' ) ;
76
89
77
90
const mfaUser = multiFactor ( cr . user ) ;
@@ -80,10 +93,13 @@ describe(' Integration tests: Mfa TOTP', () => {
80
93
81
94
totpSecret = await TotpMultiFactorGenerator . generateSecret ( session ) ;
82
95
96
+ totpTimestamp = new Date ( ) ;
97
+
83
98
const totpVerificationCode = getTotpCode (
84
99
totpSecret . secretKey ,
85
100
totpSecret . codeIntervalSeconds ,
86
- totpSecret . codeLength
101
+ totpSecret . codeLength ,
102
+ totpTimestamp
87
103
) ;
88
104
89
105
const multiFactorAssertion =
@@ -95,9 +111,12 @@ describe(' Integration tests: Mfa TOTP', () => {
95
111
. fulfilled ;
96
112
} ) ;
97
113
98
- it ( 'should not allow sign-in with incorrect totp' , async ( ) => {
99
- let resolver ;
114
+ it ( 'should not allow sign-in with incorrect totp' , async function ( ) {
115
+ let resolver : any ;
100
116
117
+ if ( emulatorUrl ) {
118
+ this . skip ( ) ;
119
+ }
101
120
try {
102
121
await signInWithEmailAndPassword ( auth , email , 'password' ) ;
103
122
@@ -120,13 +139,11 @@ describe(' Integration tests: Mfa TOTP', () => {
120
139
}
121
140
} ) ;
122
141
123
- it ( 'should allow sign-in with for correct totp and unenroll successfully' , async ( ) => {
124
- let resolver ;
125
-
126
- await delay ( 30 * 1000 ) ;
127
- //TODO(bhparijat) generate the otp code for the next time window by passing the appropriate
128
- //timestamp to avoid the 30s delay. The delay is needed because the otp code used for enrollment
129
- //cannot be reused for signing in.
142
+ it ( 'should allow sign-in with for correct totp and unenroll successfully' , async function ( ) {
143
+ let resolver : any ;
144
+ if ( emulatorUrl ) {
145
+ this . skip ( ) ;
146
+ }
130
147
try {
131
148
await signInWithEmailAndPassword ( auth , email , 'password' ) ;
132
149
@@ -138,11 +155,15 @@ describe(' Integration tests: Mfa TOTP', () => {
138
155
resolver = getMultiFactorResolver ( auth , error as any ) ;
139
156
expect ( resolver . hints ) . to . have . length ( 1 ) ;
140
157
158
+ totpTimestamp . setSeconds ( totpTimestamp . getSeconds ( ) + 30 ) ;
159
+
141
160
const totpVerificationCode = getTotpCode (
142
161
totpSecret . secretKey ,
143
162
totpSecret . codeIntervalSeconds ,
144
- totpSecret . codeLength
163
+ totpSecret . codeLength ,
164
+ totpTimestamp
145
165
) ;
166
+
146
167
const assertion = TotpMultiFactorGenerator . assertionForSignIn (
147
168
resolver . hints [ 0 ] . uid ,
148
169
totpVerificationCode
@@ -152,6 +173,8 @@ describe(' Integration tests: Mfa TOTP', () => {
152
173
const mfaUser = multiFactor ( userCredential . user ) ;
153
174
154
175
await expect ( mfaUser . unenroll ( resolver . hints [ 0 ] . uid ) ) . to . be . fulfilled ;
176
+ await expect ( signInWithEmailAndPassword ( auth , email , 'password' ) ) . to . be
177
+ . fulfilled ;
155
178
}
156
- } ) . timeout ( 32000 ) ;
179
+ } ) ;
157
180
} ) ;
0 commit comments