@@ -20,16 +20,23 @@ import chaiAsPromised from 'chai-as-promised';
20
20
import * as sinon from 'sinon' ;
21
21
import sinonChai from 'sinon-chai' ;
22
22
23
- import { Endpoint , RecaptchaClientType , RecaptchaVersion } from '../../api' ;
23
+ import {
24
+ Endpoint ,
25
+ RecaptchaClientType ,
26
+ RecaptchaVersion ,
27
+ RecaptchaActionName
28
+ } from '../../api' ;
24
29
import { mockEndpointWithParams } from '../../../test/helpers/api/helper' ;
25
30
import { testAuth , TestAuth } from '../../../test/helpers/mock_auth' ;
26
31
import * as mockFetch from '../../../test/helpers/mock_fetch' ;
27
32
import { ServerError } from '../../api/errors' ;
33
+ import { AuthInternal } from '../../model/auth' ;
28
34
29
35
import { MockGreCAPTCHATopLevel } from './recaptcha_mock' ;
30
36
import {
31
37
RecaptchaEnterpriseVerifier ,
32
- FAKE_TOKEN
38
+ FAKE_TOKEN ,
39
+ handleRecaptchaFlow
33
40
} from './recaptcha_enterprise_verifier' ;
34
41
35
42
use ( chaiAsPromised ) ;
@@ -117,4 +124,86 @@ describe('platform_browser/recaptcha/recaptcha_enterprise_verifier', () => {
117
124
expect ( await verifier . verify ( ) ) . to . eq ( FAKE_TOKEN ) ;
118
125
} ) ;
119
126
} ) ;
127
+
128
+ context ( 'handleRecaptchaFlow' , ( ) => {
129
+ let mockAuthInstance : AuthInternal ;
130
+ let mockRequest : any ;
131
+ let mockActionMethod : sinon . SinonStub ;
132
+
133
+ beforeEach ( async ( ) => {
134
+ mockAuthInstance = await testAuth ( ) ;
135
+ mockRequest = { } ;
136
+ mockActionMethod = sinon . stub ( ) ;
137
+ } ) ;
138
+
139
+ afterEach ( ( ) => {
140
+ sinon . restore ( ) ;
141
+ } ) ;
142
+
143
+ it ( 'should handle recaptcha when emailPasswordEnabled is true' , async ( ) => {
144
+ if ( typeof window === 'undefined' ) {
145
+ return ;
146
+ }
147
+ sinon . stub ( mockAuthInstance , '_getRecaptchaConfig' ) . returns ( {
148
+ emailPasswordEnabled : true ,
149
+ siteKey : 'mock_site_key'
150
+ } ) ;
151
+ mockActionMethod . resolves ( 'success' ) ;
152
+
153
+ const result = await handleRecaptchaFlow (
154
+ mockAuthInstance ,
155
+ mockRequest ,
156
+ RecaptchaActionName . GET_OOB_CODE ,
157
+ mockActionMethod
158
+ ) ;
159
+
160
+ expect ( result ) . to . equal ( 'success' ) ;
161
+ expect ( mockActionMethod ) . to . have . been . calledOnce ;
162
+ } ) ;
163
+
164
+ it ( 'should handle action without recaptcha when emailPasswordEnabled is false and no error' , async ( ) => {
165
+ if ( typeof window === 'undefined' ) {
166
+ return ;
167
+ }
168
+ sinon . stub ( mockAuthInstance , '_getRecaptchaConfig' ) . returns ( {
169
+ emailPasswordEnabled : false ,
170
+ siteKey : 'mock_site_key'
171
+ } ) ;
172
+ mockActionMethod . resolves ( 'success' ) ;
173
+
174
+ const result = await handleRecaptchaFlow (
175
+ mockAuthInstance ,
176
+ mockRequest ,
177
+ RecaptchaActionName . GET_OOB_CODE ,
178
+ mockActionMethod
179
+ ) ;
180
+
181
+ expect ( result ) . to . equal ( 'success' ) ;
182
+ expect ( mockActionMethod ) . to . have . been . calledOnce ;
183
+ } ) ;
184
+
185
+ it ( 'should handle MISSING_RECAPTCHA_TOKEN error when emailPasswordEnabled is false' , async ( ) => {
186
+ if ( typeof window === 'undefined' ) {
187
+ return ;
188
+ }
189
+ sinon . stub ( mockAuthInstance , '_getRecaptchaConfig' ) . returns ( {
190
+ emailPasswordEnabled : false ,
191
+ siteKey : 'mock_site_key'
192
+ } ) ;
193
+ mockActionMethod . onFirstCall ( ) . rejects ( {
194
+ code : 'auth/MISSING_RECAPTCHA_TOKEN'
195
+ } ) ;
196
+ mockActionMethod . onSecondCall ( ) . resolves ( 'success-after-recaptcha' ) ;
197
+
198
+ const result = await handleRecaptchaFlow (
199
+ mockAuthInstance ,
200
+ mockRequest ,
201
+ RecaptchaActionName . GET_OOB_CODE ,
202
+ mockActionMethod
203
+ ) ;
204
+
205
+ expect ( result ) . to . equal ( 'success-after-recaptcha' ) ;
206
+ expect ( mockActionMethod ) . to . have . been . calledTwice ;
207
+ } ) ;
208
+ } ) ;
120
209
} ) ;
0 commit comments