@@ -21,7 +21,7 @@ import * as chaiAsPromised from 'chai-as-promised';
21
21
22
22
import { deepCopy } from '../../../src/utils/deep-copy' ;
23
23
import {
24
- GetAccountInfoUserResponse , ProviderUserInfoResponse , MultiFactorInfoResponse ,
24
+ GetAccountInfoUserResponse , ProviderUserInfoResponse , MultiFactorInfoResponse , TotpMultiFactorInfo ,
25
25
} from '../../../src/auth/user-record' ;
26
26
import {
27
27
UserInfo , UserMetadata , UserRecord , MultiFactorSettings , MultiFactorInfo , PhoneMultiFactorInfo ,
@@ -379,18 +379,157 @@ describe('PhoneMultiFactorInfo', () => {
379
379
} ) ;
380
380
} ) ;
381
381
382
- describe ( 'MultiFactorInfo ' , ( ) => {
382
+ describe ( 'TotpMultiFactorInfo ' , ( ) => {
383
383
const serverResponse : MultiFactorInfoResponse = {
384
+ mfaEnrollmentId : 'enrollmentId1' ,
385
+ displayName : 'displayName1' ,
386
+ enrolledAt : now . toISOString ( ) ,
387
+ totpInfo : { } ,
388
+ } ;
389
+ const totpMultiFactorInfo = new TotpMultiFactorInfo ( serverResponse ) ;
390
+ const totpMultiFactorInfoMissingFields = new TotpMultiFactorInfo ( {
391
+ mfaEnrollmentId : serverResponse . mfaEnrollmentId ,
392
+ totpInfo : serverResponse . totpInfo ,
393
+ } ) ;
394
+
395
+ describe ( 'constructor' , ( ) => {
396
+ it ( 'should throw when an empty object is provided' , ( ) => {
397
+ expect ( ( ) => {
398
+ return new TotpMultiFactorInfo ( { } as any ) ;
399
+ } ) . to . throw ( 'INTERNAL ASSERT FAILED: Invalid multi-factor info response' ) ;
400
+ } ) ;
401
+
402
+ it ( 'should throw when an undefined response is provided' , ( ) => {
403
+ expect ( ( ) => {
404
+ return new TotpMultiFactorInfo ( undefined as any ) ;
405
+ } ) . to . throw ( 'INTERNAL ASSERT FAILED: Invalid multi-factor info response' ) ;
406
+ } ) ;
407
+
408
+ it ( 'should succeed when mfaEnrollmentId and totpInfo are both provided' , ( ) => {
409
+ expect ( ( ) => {
410
+ return new TotpMultiFactorInfo ( {
411
+ mfaEnrollmentId : 'enrollmentId1' ,
412
+ totpInfo : { } ,
413
+ } ) ;
414
+ } ) . not . to . throw ( Error ) ;
415
+ } ) ;
416
+
417
+ it ( 'should throw when only mfaEnrollmentId is provided' , ( ) => {
418
+ expect ( ( ) => {
419
+ return new TotpMultiFactorInfo ( {
420
+ mfaEnrollmentId : 'enrollmentId1' ,
421
+ } as any ) ;
422
+ } ) . to . throw ( 'INTERNAL ASSERT FAILED: Invalid multi-factor info response' ) ;
423
+ } ) ;
424
+
425
+ it ( 'should throw when only totpInfo is provided' , ( ) => {
426
+ expect ( ( ) => {
427
+ return new TotpMultiFactorInfo ( {
428
+ totpInfo : { } ,
429
+ } as any ) ;
430
+ } ) . to . throw ( 'INTERNAL ASSERT FAILED: Invalid multi-factor info response' ) ;
431
+ } ) ;
432
+ } ) ;
433
+
434
+ describe ( 'getters' , ( ) => {
435
+ it ( 'should set missing optional fields to null' , ( ) => {
436
+ expect ( totpMultiFactorInfoMissingFields . uid ) . to . equal ( serverResponse . mfaEnrollmentId ) ;
437
+ expect ( totpMultiFactorInfoMissingFields . displayName ) . to . be . undefined ;
438
+ expect ( totpMultiFactorInfoMissingFields . totpInfo ) . to . equal ( serverResponse . totpInfo ) ;
439
+ expect ( totpMultiFactorInfoMissingFields . enrollmentTime ) . to . be . null ;
440
+ expect ( totpMultiFactorInfoMissingFields . factorId ) . to . equal ( 'totp' ) ;
441
+ } ) ;
442
+
443
+ it ( 'should return expected factorId' , ( ) => {
444
+ expect ( totpMultiFactorInfo . factorId ) . to . equal ( 'totp' ) ;
445
+ } ) ;
446
+
447
+ it ( 'should throw when modifying readonly factorId property' , ( ) => {
448
+ expect ( ( ) => {
449
+ ( totpMultiFactorInfo as any ) . factorId = 'other' ;
450
+ } ) . to . throw ( Error ) ;
451
+ } ) ;
452
+
453
+ it ( 'should return expected displayName' , ( ) => {
454
+ expect ( totpMultiFactorInfo . displayName ) . to . equal ( serverResponse . displayName ) ;
455
+ } ) ;
456
+
457
+ it ( 'should throw when modifying readonly displayName property' , ( ) => {
458
+ expect ( ( ) => {
459
+ ( totpMultiFactorInfo as any ) . displayName = 'Modified' ;
460
+ } ) . to . throw ( Error ) ;
461
+ } ) ;
462
+
463
+ it ( 'should return expected totpInfo object' , ( ) => {
464
+ expect ( totpMultiFactorInfo . totpInfo ) . to . equal ( serverResponse . totpInfo ) ;
465
+ } ) ;
466
+
467
+ it ( 'should return expected uid' , ( ) => {
468
+ expect ( totpMultiFactorInfo . uid ) . to . equal ( serverResponse . mfaEnrollmentId ) ;
469
+ } ) ;
470
+
471
+ it ( 'should throw when modifying readonly uid property' , ( ) => {
472
+ expect ( ( ) => {
473
+ ( totpMultiFactorInfo as any ) . uid = 'modifiedEnrollmentId' ;
474
+ } ) . to . throw ( Error ) ;
475
+ } ) ;
476
+
477
+ it ( 'should return expected enrollmentTime' , ( ) => {
478
+ expect ( totpMultiFactorInfo . enrollmentTime ) . to . equal ( now . toUTCString ( ) ) ;
479
+ } ) ;
480
+
481
+ it ( 'should throw when modifying readonly uid property' , ( ) => {
482
+ expect ( ( ) => {
483
+ ( totpMultiFactorInfo as any ) . enrollmentTime = new Date ( ) . toISOString ( ) ;
484
+ } ) . to . throw ( Error ) ;
485
+ } ) ;
486
+ } ) ;
487
+
488
+ describe ( 'toJSON' , ( ) => {
489
+ it ( 'should return expected JSON object' , ( ) => {
490
+ expect ( totpMultiFactorInfo . toJSON ( ) ) . to . deep . equal ( {
491
+ uid : 'enrollmentId1' ,
492
+ displayName : 'displayName1' ,
493
+ enrollmentTime : now . toUTCString ( ) ,
494
+ totpInfo : { } ,
495
+ factorId : 'totp' ,
496
+ } ) ;
497
+ } ) ;
498
+
499
+ it ( 'should return expected JSON object with missing fields set to null' , ( ) => {
500
+ expect ( totpMultiFactorInfoMissingFields . toJSON ( ) ) . to . deep . equal ( {
501
+ uid : 'enrollmentId1' ,
502
+ displayName : undefined ,
503
+ enrollmentTime : null ,
504
+ totpInfo : { } ,
505
+ factorId : 'totp' ,
506
+ } ) ;
507
+ } ) ;
508
+ } ) ;
509
+ } ) ;
510
+
511
+ describe ( 'MultiFactorInfo' , ( ) => {
512
+ const phoneServerResponse : MultiFactorInfoResponse = {
384
513
mfaEnrollmentId : 'enrollmentId1' ,
385
514
displayName : 'displayName1' ,
386
515
enrolledAt : now . toISOString ( ) ,
387
516
phoneInfo : '+16505551234' ,
388
517
} ;
389
- const phoneMultiFactorInfo = new PhoneMultiFactorInfo ( serverResponse ) ;
518
+ const phoneMultiFactorInfo = new PhoneMultiFactorInfo ( phoneServerResponse ) ;
519
+ const totpServerResponse : MultiFactorInfoResponse = {
520
+ mfaEnrollmentId : 'enrollmentId1' ,
521
+ displayName : 'displayName1' ,
522
+ enrolledAt : now . toISOString ( ) ,
523
+ totpInfo : { } ,
524
+ } ;
525
+ const totpMultiFactorInfo = new TotpMultiFactorInfo ( totpServerResponse ) ;
390
526
391
527
describe ( 'initMultiFactorInfo' , ( ) => {
392
528
it ( 'should return expected PhoneMultiFactorInfo' , ( ) => {
393
- expect ( MultiFactorInfo . initMultiFactorInfo ( serverResponse ) ) . to . deep . equal ( phoneMultiFactorInfo ) ;
529
+ expect ( MultiFactorInfo . initMultiFactorInfo ( phoneServerResponse ) ) . to . deep . equal ( phoneMultiFactorInfo ) ;
530
+ } ) ;
531
+ it ( 'should return expected TotpMultiFactorInfo' , ( ) => {
532
+ expect ( MultiFactorInfo . initMultiFactorInfo ( totpServerResponse ) ) . to . deep . equal ( totpMultiFactorInfo ) ;
394
533
} ) ;
395
534
396
535
it ( 'should return null for invalid MultiFactorInfo' , ( ) => {
@@ -425,6 +564,12 @@ describe('MultiFactorSettings', () => {
425
564
enrolledAt : now . toISOString ( ) ,
426
565
secretKey : 'SECRET_KEY' ,
427
566
} ,
567
+ {
568
+ mfaEnrollmentId : 'enrollmentId5' ,
569
+ displayName : 'displayName1' ,
570
+ enrolledAt : now . toISOString ( ) ,
571
+ totpInfo : { } ,
572
+ } ,
428
573
] ,
429
574
} ;
430
575
const expectedMultiFactorInfo = [
@@ -439,6 +584,12 @@ describe('MultiFactorSettings', () => {
439
584
enrolledAt : now . toISOString ( ) ,
440
585
phoneInfo : '+16505556789' ,
441
586
} ) ,
587
+ new TotpMultiFactorInfo ( {
588
+ mfaEnrollmentId : 'enrollmentId5' ,
589
+ displayName : 'displayName1' ,
590
+ enrolledAt : now . toISOString ( ) ,
591
+ totpInfo : { } ,
592
+ } )
442
593
] ;
443
594
444
595
describe ( 'constructor' , ( ) => {
@@ -457,9 +608,10 @@ describe('MultiFactorSettings', () => {
457
608
it ( 'should populate expected enrolledFactors' , ( ) => {
458
609
const multiFactor = new MultiFactorSettings ( serverResponse ) ;
459
610
460
- expect ( multiFactor . enrolledFactors . length ) . to . equal ( 2 ) ;
611
+ expect ( multiFactor . enrolledFactors . length ) . to . equal ( 3 ) ;
461
612
expect ( multiFactor . enrolledFactors [ 0 ] ) . to . deep . equal ( expectedMultiFactorInfo [ 0 ] ) ;
462
613
expect ( multiFactor . enrolledFactors [ 1 ] ) . to . deep . equal ( expectedMultiFactorInfo [ 1 ] ) ;
614
+ expect ( multiFactor . enrolledFactors [ 2 ] ) . to . deep . equal ( expectedMultiFactorInfo [ 2 ] ) ;
463
615
} ) ;
464
616
} ) ;
465
617
@@ -504,6 +656,7 @@ describe('MultiFactorSettings', () => {
504
656
enrolledFactors : [
505
657
expectedMultiFactorInfo [ 0 ] . toJSON ( ) ,
506
658
expectedMultiFactorInfo [ 1 ] . toJSON ( ) ,
659
+ expectedMultiFactorInfo [ 2 ] . toJSON ( ) ,
507
660
] ,
508
661
} ) ;
509
662
} ) ;
0 commit comments