@@ -29,7 +29,7 @@ import {
2929
3030import { isV2 , isEnterprise , RecaptchaConfig } from './recaptcha' ;
3131import { GetRecaptchaConfigResponse } from '../../api/authentication/recaptcha' ;
32- import { EnforcementState } from '../../api/index' ;
32+ import { EnforcementState , RecaptchaProvider } from '../../api/index' ;
3333
3434use ( chaiAsPromised ) ;
3535use ( sinonChai ) ;
@@ -39,17 +39,60 @@ describe('platform_browser/recaptcha/recaptcha', () => {
3939 let recaptchaV2 : MockReCaptcha ;
4040 let recaptchaV3 : MockGreCAPTCHA ;
4141 let recaptchaEnterprise : MockGreCAPTCHATopLevel ;
42- let recaptchaConfig : RecaptchaConfig ;
4342
4443 const TEST_SITE_KEY = 'test-site-key' ;
4544
4645 const GET_RECAPTCHA_CONFIG_RESPONSE : GetRecaptchaConfigResponse = {
4746 recaptchaKey : 'projects/testproj/keys/' + TEST_SITE_KEY ,
4847 recaptchaEnforcementState : [
49- { provider : 'EMAIL_PASSWORD_PROVIDER' , enforcementState : 'ENFORCE' }
48+ {
49+ provider : RecaptchaProvider . EMAIL_PASSWORD_PROVIDER ,
50+ enforcementState : EnforcementState . ENFORCE
51+ } ,
52+ {
53+ provider : RecaptchaProvider . PHONE_PROVIDER ,
54+ enforcementState : EnforcementState . AUDIT
55+ }
5056 ]
5157 } ;
5258
59+ const GET_RECAPTCHA_CONFIG_RESPONSE_OFF : GetRecaptchaConfigResponse = {
60+ recaptchaKey : 'projects/testproj/keys/' + TEST_SITE_KEY ,
61+ recaptchaEnforcementState : [
62+ {
63+ provider : RecaptchaProvider . EMAIL_PASSWORD_PROVIDER ,
64+ enforcementState : EnforcementState . OFF
65+ } ,
66+ {
67+ provider : RecaptchaProvider . PHONE_PROVIDER ,
68+ enforcementState : EnforcementState . OFF
69+ }
70+ ]
71+ } ;
72+
73+ const GET_RECAPTCHA_CONFIG_RESPONSE_ENFORCE_AND_OFF : GetRecaptchaConfigResponse =
74+ {
75+ recaptchaKey : 'projects/testproj/keys/' + TEST_SITE_KEY ,
76+ recaptchaEnforcementState : [
77+ {
78+ provider : RecaptchaProvider . EMAIL_PASSWORD_PROVIDER ,
79+ enforcementState : EnforcementState . ENFORCE
80+ } ,
81+ {
82+ provider : RecaptchaProvider . PHONE_PROVIDER ,
83+ enforcementState : EnforcementState . OFF
84+ }
85+ ]
86+ } ;
87+
88+ const recaptchaConfig = new RecaptchaConfig ( GET_RECAPTCHA_CONFIG_RESPONSE ) ;
89+ const recaptchaConfigOff = new RecaptchaConfig (
90+ GET_RECAPTCHA_CONFIG_RESPONSE_OFF
91+ ) ;
92+ const recaptchaConfigEnforceAndOff = new RecaptchaConfig (
93+ GET_RECAPTCHA_CONFIG_RESPONSE_ENFORCE_AND_OFF
94+ ) ;
95+
5396 context ( '#verify' , ( ) => {
5497 beforeEach ( async ( ) => {
5598 auth = await testAuth ( ) ;
@@ -74,30 +117,63 @@ describe('platform_browser/recaptcha/recaptcha', () => {
74117 } ) ;
75118
76119 context ( '#RecaptchaConfig' , ( ) => {
77- beforeEach ( async ( ) => {
78- recaptchaConfig = new RecaptchaConfig ( GET_RECAPTCHA_CONFIG_RESPONSE ) ;
79- } ) ;
80-
81120 it ( 'should construct the recaptcha config from the backend response' , ( ) => {
82121 expect ( recaptchaConfig . siteKey ) . to . eq ( TEST_SITE_KEY ) ;
83122 expect ( recaptchaConfig . recaptchaEnforcementState [ 0 ] ) . to . eql ( {
84- provider : 'EMAIL_PASSWORD_PROVIDER' ,
85- enforcementState : 'ENFORCE'
123+ provider : RecaptchaProvider . EMAIL_PASSWORD_PROVIDER ,
124+ enforcementState : EnforcementState . ENFORCE
125+ } ) ;
126+ expect ( recaptchaConfig . recaptchaEnforcementState [ 1 ] ) . to . eql ( {
127+ provider : RecaptchaProvider . PHONE_PROVIDER ,
128+ enforcementState : EnforcementState . AUDIT
129+ } ) ;
130+ expect ( recaptchaConfigEnforceAndOff . recaptchaEnforcementState [ 1 ] ) . to . eql ( {
131+ provider : RecaptchaProvider . PHONE_PROVIDER ,
132+ enforcementState : EnforcementState . OFF
86133 } ) ;
87134 } ) ;
88135
89136 it ( '#getProviderEnforcementState should return the correct enforcement state of the provider' , ( ) => {
90137 expect (
91- recaptchaConfig . getProviderEnforcementState ( 'EMAIL_PASSWORD_PROVIDER' )
138+ recaptchaConfig . getProviderEnforcementState (
139+ RecaptchaProvider . EMAIL_PASSWORD_PROVIDER
140+ )
92141 ) . to . eq ( EnforcementState . ENFORCE ) ;
142+ expect (
143+ recaptchaConfig . getProviderEnforcementState (
144+ RecaptchaProvider . PHONE_PROVIDER
145+ )
146+ ) . to . eq ( EnforcementState . AUDIT ) ;
147+ expect (
148+ recaptchaConfigEnforceAndOff . getProviderEnforcementState (
149+ RecaptchaProvider . PHONE_PROVIDER
150+ )
151+ ) . to . eq ( EnforcementState . OFF ) ;
93152 expect ( recaptchaConfig . getProviderEnforcementState ( 'invalid-provider' ) ) . to
94153 . be . null ;
95154 } ) ;
96155
97156 it ( '#isProviderEnabled should return the enablement state of the provider' , ( ) => {
98- expect ( recaptchaConfig . isProviderEnabled ( 'EMAIL_PASSWORD_PROVIDER' ) ) . to . be
99- . true ;
157+ expect (
158+ recaptchaConfig . isProviderEnabled (
159+ RecaptchaProvider . EMAIL_PASSWORD_PROVIDER
160+ )
161+ ) . to . be . true ;
162+ expect (
163+ recaptchaConfig . isProviderEnabled ( RecaptchaProvider . PHONE_PROVIDER )
164+ ) . to . be . true ;
165+ expect (
166+ recaptchaConfigEnforceAndOff . isProviderEnabled (
167+ RecaptchaProvider . PHONE_PROVIDER
168+ )
169+ ) . to . be . false ;
100170 expect ( recaptchaConfig . isProviderEnabled ( 'invalid-provider' ) ) . to . be . false ;
101171 } ) ;
172+
173+ it ( '#isAnyProviderEnabled should return true if at least one provider is enabled' , ( ) => {
174+ expect ( recaptchaConfig . isAnyProviderEnabled ( ) ) . to . be . true ;
175+ expect ( recaptchaConfigEnforceAndOff . isAnyProviderEnabled ( ) ) . to . be . true ;
176+ expect ( recaptchaConfigOff . isAnyProviderEnabled ( ) ) . to . be . false ;
177+ } ) ;
102178 } ) ;
103179} ) ;
0 commit comments