@@ -27,7 +27,9 @@ import * as mockFetch from '../../../test/helpers/mock_fetch';
27
27
import { ServerError } from '../errors' ;
28
28
import {
29
29
finalizeEnrollPhoneMfa ,
30
+ finalizeEnrollTotpMfa ,
30
31
startEnrollPhoneMfa ,
32
+ startEnrollTotpMfa ,
31
33
withdrawMfa
32
34
} from './mfa' ;
33
35
@@ -89,7 +91,7 @@ describe('api/account_management/startEnrollPhoneMfa', () => {
89
91
90
92
await expect ( startEnrollPhoneMfa ( auth , request ) ) . to . be . rejectedWith (
91
93
FirebaseError ,
92
- "Firebase: This user's credential isn't valid for this project. This can happen if the user's token has been tampered with, or if the user isn't for the project associated with this API key. ( auth/invalid-user-token)."
94
+ ' auth/invalid-user-token'
93
95
) ;
94
96
expect ( mock . calls [ 0 ] . request ) . to . eql ( request ) ;
95
97
} ) ;
@@ -152,6 +154,139 @@ describe('api/account_management/finalizeEnrollPhoneMfa', () => {
152
154
) ;
153
155
154
156
await expect ( finalizeEnrollPhoneMfa ( auth , request ) ) . to . be . rejectedWith (
157
+ FirebaseError ,
158
+ 'auth/invalid-verification-id'
159
+ ) ;
160
+ expect ( mock . calls [ 0 ] . request ) . to . eql ( request ) ;
161
+ } ) ;
162
+ } ) ;
163
+
164
+ describe ( 'api/account_management/startEnrollTotpMfa' , ( ) => {
165
+ const request = {
166
+ idToken : 'id-token' ,
167
+ totpEnrollmentInfo : { }
168
+ } ;
169
+
170
+ let auth : TestAuth ;
171
+
172
+ beforeEach ( async ( ) => {
173
+ auth = await testAuth ( ) ;
174
+ mockFetch . setUp ( ) ;
175
+ } ) ;
176
+
177
+ afterEach ( mockFetch . tearDown ) ;
178
+
179
+ it ( 'should POST to the correct endpoint' , async ( ) => {
180
+ const currentTime = new Date ( ) . toISOString ( ) ;
181
+ const mock = mockEndpoint ( Endpoint . START_MFA_ENROLLMENT , {
182
+ totpSessionInfo : {
183
+ sharedSecretKey : 'key123' ,
184
+ verificationCodeLength : 6 ,
185
+ hashingAlgorithm : 'SHA256' ,
186
+ periodSec : 30 ,
187
+ sessionInfo : 'session-info' ,
188
+ finalizeEnrollmentTime : currentTime
189
+ }
190
+ } ) ;
191
+
192
+ const response = await startEnrollTotpMfa ( auth , request ) ;
193
+ expect ( response . totpSessionInfo . sharedSecretKey ) . to . eq ( 'key123' ) ;
194
+ expect ( response . totpSessionInfo . verificationCodeLength ) . to . eq ( 6 ) ;
195
+ expect ( response . totpSessionInfo . hashingAlgorithm ) . to . eq ( 'SHA256' ) ;
196
+ expect ( response . totpSessionInfo . periodSec ) . to . eq ( 30 ) ;
197
+ expect ( response . totpSessionInfo . sessionInfo ) . to . eq ( 'session-info' ) ;
198
+ expect ( response . totpSessionInfo . finalizeEnrollmentTime ) . to . eq ( currentTime ) ;
199
+ expect ( mock . calls [ 0 ] . request ) . to . eql ( request ) ;
200
+ expect ( mock . calls [ 0 ] . method ) . to . eq ( 'POST' ) ;
201
+ expect ( mock . calls [ 0 ] . headers ! . get ( HttpHeader . CONTENT_TYPE ) ) . to . eq (
202
+ 'application/json'
203
+ ) ;
204
+ expect ( mock . calls [ 0 ] . headers ! . get ( HttpHeader . X_CLIENT_VERSION ) ) . to . eq (
205
+ 'testSDK/0.0.0'
206
+ ) ;
207
+ } ) ;
208
+
209
+ it ( 'should handle errors' , async ( ) => {
210
+ const mock = mockEndpoint (
211
+ Endpoint . START_MFA_ENROLLMENT ,
212
+ {
213
+ error : {
214
+ code : 400 ,
215
+ message : ServerError . INVALID_ID_TOKEN ,
216
+ errors : [
217
+ {
218
+ message : ServerError . INVALID_ID_TOKEN
219
+ }
220
+ ]
221
+ }
222
+ } ,
223
+ 400
224
+ ) ;
225
+
226
+ await expect ( startEnrollTotpMfa ( auth , request ) ) . to . be . rejectedWith (
227
+ FirebaseError ,
228
+ 'auth/invalid-user-token'
229
+ ) ;
230
+ expect ( mock . calls [ 0 ] . request ) . to . eql ( request ) ;
231
+ } ) ;
232
+ } ) ;
233
+
234
+ describe ( 'api/account_management/finalizeEnrollTotpMfa' , ( ) => {
235
+ const request = {
236
+ idToken : 'id-token' ,
237
+ displayName : 'my-otp-app' ,
238
+ totpVerificationInfo : {
239
+ sessionInfo : 'session-info' ,
240
+ verificationCode : 'code'
241
+ }
242
+ } ;
243
+
244
+ let auth : TestAuth ;
245
+
246
+ beforeEach ( async ( ) => {
247
+ auth = await testAuth ( ) ;
248
+ mockFetch . setUp ( ) ;
249
+ } ) ;
250
+
251
+ afterEach ( mockFetch . tearDown ) ;
252
+
253
+ it ( 'should POST to the correct endpoint' , async ( ) => {
254
+ const mock = mockEndpoint ( Endpoint . FINALIZE_MFA_ENROLLMENT , {
255
+ idToken : 'id-token' ,
256
+ refreshToken : 'refresh-token'
257
+ } ) ;
258
+
259
+ const response = await finalizeEnrollTotpMfa ( auth , request ) ;
260
+ expect ( response . idToken ) . to . eq ( 'id-token' ) ;
261
+ expect ( response . refreshToken ) . to . eq ( 'refresh-token' ) ;
262
+ expect ( mock . calls [ 0 ] . request ) . to . eql ( request ) ;
263
+ expect ( mock . calls [ 0 ] . method ) . to . eq ( 'POST' ) ;
264
+ expect ( mock . calls [ 0 ] . headers ! . get ( HttpHeader . CONTENT_TYPE ) ) . to . eq (
265
+ 'application/json'
266
+ ) ;
267
+ expect ( mock . calls [ 0 ] . headers ! . get ( HttpHeader . X_CLIENT_VERSION ) ) . to . eq (
268
+ 'testSDK/0.0.0'
269
+ ) ;
270
+ } ) ;
271
+
272
+ it ( 'should handle errors' , async ( ) => {
273
+ const mock = mockEndpoint (
274
+ Endpoint . FINALIZE_MFA_ENROLLMENT ,
275
+ {
276
+ error : {
277
+ code : 400 ,
278
+ message : ServerError . INVALID_SESSION_INFO ,
279
+ errors : [
280
+ {
281
+ message : ServerError . INVALID_SESSION_INFO
282
+ }
283
+ ]
284
+ }
285
+ } ,
286
+ 400
287
+ ) ;
288
+
289
+ await expect ( finalizeEnrollTotpMfa ( auth , request ) ) . to . be . rejectedWith (
155
290
FirebaseError ,
156
291
'Firebase: The verification ID used to create the phone auth credential is invalid. (auth/invalid-verification-id).'
157
292
) ;
0 commit comments