15
15
* limitations under the License.
16
16
*/
17
17
18
- import { expect } from 'chai' ;
18
+ import { expect , use } from 'chai' ;
19
+ import chaiAsPromised from 'chai-as-promised' ;
19
20
import * as sinon from 'sinon' ;
20
21
22
+ import { FirebaseError } from '@firebase/util' ;
23
+
21
24
import {
22
25
mockEndpoint ,
23
26
mockEndpointWithParams
@@ -37,6 +40,8 @@ import { FAKE_TOKEN } from '../recaptcha/recaptcha_enterprise_verifier';
37
40
import { MockGreCAPTCHATopLevel } from '../recaptcha/recaptcha_mock' ;
38
41
import { ApplicationVerifierInternal } from '../../model/application_verifier' ;
39
42
43
+ use ( chaiAsPromised ) ;
44
+
40
45
describe ( 'platform_browser/providers/phone' , ( ) => {
41
46
let auth : TestAuth ;
42
47
let v2Verifier : ApplicationVerifierInternal ;
@@ -104,6 +109,83 @@ describe('platform_browser/providers/phone', () => {
104
109
} ) ;
105
110
} ) ;
106
111
112
+ it ( 'throws an error if verify without appVerifier when recaptcha enterprise is disabled' , async ( ) => {
113
+ const recaptchaConfigResponseOff = {
114
+ recaptchaKey : 'foo/bar/to/site-key' ,
115
+ recaptchaEnforcementState : [
116
+ {
117
+ provider : RecaptchaAuthProvider . PHONE_PROVIDER ,
118
+ enforcementState : EnforcementState . OFF
119
+ }
120
+ ]
121
+ } ;
122
+ const recaptcha = new MockGreCAPTCHATopLevel ( ) ;
123
+ if ( typeof window === 'undefined' ) {
124
+ return ;
125
+ }
126
+ window . grecaptcha = recaptcha ;
127
+ sinon
128
+ . stub ( recaptcha . enterprise , 'execute' )
129
+ . returns ( Promise . resolve ( 'enterprise-token' ) ) ;
130
+
131
+ mockEndpointWithParams (
132
+ Endpoint . GET_RECAPTCHA_CONFIG ,
133
+ {
134
+ clientType : RecaptchaClientType . WEB ,
135
+ version : RecaptchaVersion . ENTERPRISE
136
+ } ,
137
+ recaptchaConfigResponseOff
138
+ ) ;
139
+
140
+ const provider = new PhoneAuthProvider ( auth ) ;
141
+ await expect (
142
+ provider . verifyPhoneNumber ( '+15105550000' )
143
+ ) . to . be . rejectedWith ( FirebaseError , 'auth/argument-error' ) ;
144
+ } ) ;
145
+
146
+ it ( 'calls the server without appVerifier when recaptcha enterprise is enabled' , async ( ) => {
147
+ const recaptchaConfigResponseEnforce = {
148
+ recaptchaKey : 'foo/bar/to/site-key' ,
149
+ recaptchaEnforcementState : [
150
+ {
151
+ provider : RecaptchaAuthProvider . PHONE_PROVIDER ,
152
+ enforcementState : EnforcementState . ENFORCE
153
+ }
154
+ ]
155
+ } ;
156
+ const recaptcha = new MockGreCAPTCHATopLevel ( ) ;
157
+ if ( typeof window === 'undefined' ) {
158
+ return ;
159
+ }
160
+ window . grecaptcha = recaptcha ;
161
+ sinon
162
+ . stub ( recaptcha . enterprise , 'execute' )
163
+ . returns ( Promise . resolve ( 'enterprise-token' ) ) ;
164
+
165
+ mockEndpointWithParams (
166
+ Endpoint . GET_RECAPTCHA_CONFIG ,
167
+ {
168
+ clientType : RecaptchaClientType . WEB ,
169
+ version : RecaptchaVersion . ENTERPRISE
170
+ } ,
171
+ recaptchaConfigResponseEnforce
172
+ ) ;
173
+
174
+ const route = mockEndpoint ( Endpoint . SEND_VERIFICATION_CODE , {
175
+ sessionInfo : 'verification-id'
176
+ } ) ;
177
+
178
+ const provider = new PhoneAuthProvider ( auth ) ;
179
+ const result = await provider . verifyPhoneNumber ( '+15105550000' ) ;
180
+ expect ( result ) . to . eq ( 'verification-id' ) ;
181
+ expect ( route . calls [ 0 ] . request ) . to . eql ( {
182
+ phoneNumber : '+15105550000' ,
183
+ captchaResponse : 'enterprise-token' ,
184
+ clientType : RecaptchaClientType . WEB ,
185
+ recaptchaVersion : RecaptchaVersion . ENTERPRISE
186
+ } ) ;
187
+ } ) ;
188
+
107
189
it ( 'calls the server when recaptcha enterprise is enabled' , async ( ) => {
108
190
const recaptchaConfigResponseEnforce = {
109
191
recaptchaKey : 'foo/bar/to/site-key' ,
0 commit comments