@@ -33,7 +33,8 @@ import {
33
33
addTokenListener ,
34
34
removeTokenListener ,
35
35
formatDummyToken ,
36
- defaultTokenErrorData
36
+ defaultTokenErrorData ,
37
+ getLimitedUseToken
37
38
} from './internal-api' ;
38
39
import * as reCAPTCHA from './recaptcha' ;
39
40
import * as client from './client' ;
@@ -46,11 +47,11 @@ import {
46
47
setInitialState ,
47
48
getDebugState
48
49
} from './state' ;
49
- import { AppCheck , AppCheckTokenListener } from './public-types' ;
50
+ import { AppCheckTokenListener } from './public-types' ;
50
51
import { Deferred } from '@firebase/util' ;
51
52
import { ReCaptchaEnterpriseProvider , ReCaptchaV3Provider } from './providers' ;
52
53
import { AppCheckService } from './factory' ;
53
- import { AppCheckTokenResult , ListenerType } from './types' ;
54
+ import { ListenerType } from './types' ;
54
55
import { AppCheckError , ERROR_FACTORY } from './errors' ;
55
56
56
57
const fakeRecaptchaToken = 'fake-recaptcha-token' ;
@@ -666,24 +667,14 @@ describe('internal api', () => {
666
667
} ) ;
667
668
668
669
describe ( 'getToken() for limited use' , ( ) => {
669
- function getLimitedUseToken (
670
- appCheck : AppCheck
671
- ) : Promise < AppCheckTokenResult > {
672
- return getToken (
673
- appCheck as AppCheckService ,
674
- /*forceRefresh*/ true ,
675
- /* isLimitedUse */ true
676
- ) ;
677
- }
678
-
679
670
it ( 'uses customTokenProvider to get an AppCheck token' , async ( ) => {
680
671
const customTokenProvider = getFakeCustomTokenProvider ( ) ;
681
672
const customProviderSpy = spy ( customTokenProvider , 'getToken' ) ;
682
673
683
674
const appCheck = initializeAppCheck ( app , {
684
675
provider : customTokenProvider
685
676
} ) ;
686
- const token = await getLimitedUseToken ( appCheck ) ;
677
+ const token = await getLimitedUseToken ( appCheck as AppCheckService ) ;
687
678
688
679
expect ( customProviderSpy ) . to . be . called ;
689
680
expect ( token ) . to . deep . equal ( {
@@ -698,7 +689,7 @@ describe('internal api', () => {
698
689
const appCheck = initializeAppCheck ( app , {
699
690
provider : customTokenProvider
700
691
} ) ;
701
- await getLimitedUseToken ( appCheck ) ;
692
+ await getLimitedUseToken ( appCheck as AppCheckService ) ;
702
693
703
694
expect ( getStateReference ( app ) . token ) . to . be . undefined ;
704
695
expect ( getStateReference ( app ) . isTokenAutoRefreshEnabled ) . to . be . false ;
@@ -709,15 +700,13 @@ describe('internal api', () => {
709
700
provider : new ReCaptchaV3Provider ( FAKE_SITE_KEY )
710
701
} ) ;
711
702
712
- const reCAPTCHASpy = stub ( reCAPTCHA , 'getToken' ) . returns (
713
- Promise . resolve ( fakeRecaptchaToken )
714
- ) ;
703
+ const reCAPTCHASpy = stubGetRecaptchaToken ( ) ;
715
704
const exchangeTokenStub : SinonStub = stub (
716
705
client ,
717
706
'exchangeToken'
718
707
) . returns ( Promise . resolve ( fakeRecaptchaAppCheckToken ) ) ;
719
708
720
- const token = await getLimitedUseToken ( appCheck ) ;
709
+ const token = await getLimitedUseToken ( appCheck as AppCheckService ) ;
721
710
722
711
expect ( reCAPTCHASpy ) . to . be . called ;
723
712
@@ -732,15 +721,13 @@ describe('internal api', () => {
732
721
provider : new ReCaptchaEnterpriseProvider ( FAKE_SITE_KEY )
733
722
} ) ;
734
723
735
- const reCAPTCHASpy = stub ( reCAPTCHA , 'getToken' ) . returns (
736
- Promise . resolve ( fakeRecaptchaToken )
737
- ) ;
724
+ const reCAPTCHASpy = stubGetRecaptchaToken ( ) ;
738
725
const exchangeTokenStub : SinonStub = stub (
739
726
client ,
740
727
'exchangeToken'
741
728
) . returns ( Promise . resolve ( fakeRecaptchaAppCheckToken ) ) ;
742
729
743
- const token = await getLimitedUseToken ( appCheck ) ;
730
+ const token = await getLimitedUseToken ( appCheck as AppCheckService ) ;
744
731
745
732
expect ( reCAPTCHASpy ) . to . be . called ;
746
733
@@ -750,32 +737,6 @@ describe('internal api', () => {
750
737
expect ( token ) . to . deep . equal ( { token : fakeRecaptchaAppCheckToken . token } ) ;
751
738
} ) ;
752
739
753
- it ( 'resolves with a dummy token and an error if failed to get a token' , async ( ) => {
754
- const errorStub = stub ( console , 'error' ) ;
755
- const appCheck = initializeAppCheck ( app , {
756
- provider : new ReCaptchaV3Provider ( FAKE_SITE_KEY )
757
- } ) ;
758
-
759
- const reCAPTCHASpy = stub ( reCAPTCHA , 'getToken' ) . returns (
760
- Promise . resolve ( fakeRecaptchaToken )
761
- ) ;
762
-
763
- const error = new Error ( 'oops, something went wrong' ) ;
764
- stub ( client , 'exchangeToken' ) . returns ( Promise . reject ( error ) ) ;
765
-
766
- const token = await getLimitedUseToken ( appCheck ) ;
767
-
768
- expect ( reCAPTCHASpy ) . to . be . called ;
769
- expect ( token ) . to . deep . equal ( {
770
- token : formatDummyToken ( defaultTokenErrorData ) ,
771
- error
772
- } ) ;
773
- expect ( errorStub . args [ 0 ] [ 1 ] . message ) . to . include (
774
- 'oops, something went wrong'
775
- ) ;
776
- errorStub . restore ( ) ;
777
- } ) ;
778
-
779
740
it ( 'exchanges debug token if in debug mode' , async ( ) => {
780
741
const exchangeTokenStub : SinonStub = stub (
781
742
client ,
@@ -789,64 +750,12 @@ describe('internal api', () => {
789
750
provider : new ReCaptchaV3Provider ( FAKE_SITE_KEY )
790
751
} ) ;
791
752
792
- const token = await getLimitedUseToken ( appCheck ) ;
753
+ const token = await getLimitedUseToken ( appCheck as AppCheckService ) ;
793
754
expect ( exchangeTokenStub . args [ 0 ] [ 0 ] . body [ 'debug_token' ] ) . to . equal (
794
755
'my-debug-token'
795
756
) ;
796
757
expect ( token ) . to . deep . equal ( { token : fakeRecaptchaAppCheckToken . token } ) ;
797
758
} ) ;
798
-
799
- it ( 'throttles for a period less than 1d on 503' , async ( ) => {
800
- // More detailed check of exponential backoff in providers.test.ts
801
- const appCheck = initializeAppCheck ( app , {
802
- provider : new ReCaptchaV3Provider ( FAKE_SITE_KEY )
803
- } ) ;
804
- const warnStub = stub ( logger , 'warn' ) ;
805
- stub ( client , 'exchangeToken' ) . returns (
806
- Promise . reject (
807
- ERROR_FACTORY . create ( AppCheckError . FETCH_STATUS_ERROR , {
808
- httpStatus : 503
809
- } )
810
- )
811
- ) ;
812
-
813
- const token = await getLimitedUseToken ( appCheck ) ;
814
-
815
- // ReCaptchaV3Provider's _throttleData is private so checking
816
- // the resulting error message to be sure it has roughly the
817
- // correct throttle time. This also tests the time formatter.
818
- // Check both the error itself and that it makes it through to
819
- // console.warn
820
- expect ( token . error ?. message ) . to . include ( '503' ) ;
821
- expect ( token . error ?. message ) . to . include ( '00m' ) ;
822
- expect ( token . error ?. message ) . to . not . include ( '1d' ) ;
823
- expect ( warnStub . args [ 0 ] [ 0 ] ) . to . include ( '503' ) ;
824
- } ) ;
825
-
826
- it ( 'throttles 1d on 403' , async ( ) => {
827
- const appCheck = initializeAppCheck ( app , {
828
- provider : new ReCaptchaV3Provider ( FAKE_SITE_KEY )
829
- } ) ;
830
- const warnStub = stub ( logger , 'warn' ) ;
831
- stub ( client , 'exchangeToken' ) . returns (
832
- Promise . reject (
833
- ERROR_FACTORY . create ( AppCheckError . FETCH_STATUS_ERROR , {
834
- httpStatus : 403
835
- } )
836
- )
837
- ) ;
838
-
839
- const token = await getLimitedUseToken ( appCheck ) ;
840
-
841
- // ReCaptchaV3Provider's _throttleData is private so checking
842
- // the resulting error message to be sure it has roughly the
843
- // correct throttle time. This also tests the time formatter.
844
- // Check both the error itself and that it makes it through to
845
- // console.warn
846
- expect ( token . error ?. message ) . to . include ( '403' ) ;
847
- expect ( token . error ?. message ) . to . include ( '1d' ) ;
848
- expect ( warnStub . args [ 0 ] [ 0 ] ) . to . include ( '403' ) ;
849
- } ) ;
850
759
} ) ;
851
760
852
761
describe ( 'addTokenListener' , ( ) => {
0 commit comments