@@ -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,79 @@ 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
+ sinon . stub ( mockAuthInstance , '_getRecaptchaConfig' ) . returns ( {
145
+ emailPasswordEnabled : true ,
146
+ siteKey : "mock_site_key"
147
+ } ) ;
148
+ mockActionMethod . resolves ( 'success' ) ;
149
+
150
+ const result = await handleRecaptchaFlow (
151
+ mockAuthInstance ,
152
+ mockRequest ,
153
+ RecaptchaActionName . GET_OOB_CODE ,
154
+ mockActionMethod
155
+ ) ;
156
+
157
+ expect ( result ) . to . equal ( 'success' ) ;
158
+ expect ( mockActionMethod ) . to . have . been . calledOnce ;
159
+ // Add more assertions as needed, e.g., checking if injectRecaptchaFields was called
160
+ } ) ;
161
+
162
+ it ( 'should handle action without recaptcha when emailPasswordEnabled is false and no error' , async ( ) => {
163
+ sinon . stub ( mockAuthInstance , '_getRecaptchaConfig' ) . returns ( {
164
+ emailPasswordEnabled : false ,
165
+ siteKey : 'mock_site_key'
166
+ } ) ;
167
+ mockActionMethod . resolves ( 'success' ) ;
168
+
169
+ const result = await handleRecaptchaFlow (
170
+ mockAuthInstance ,
171
+ mockRequest ,
172
+ RecaptchaActionName . GET_OOB_CODE ,
173
+ mockActionMethod
174
+ ) ;
175
+
176
+ expect ( result ) . to . equal ( 'success' ) ;
177
+ expect ( mockActionMethod ) . to . have . been . calledOnce ;
178
+ } ) ;
179
+
180
+ it ( 'should handle MISSING_RECAPTCHA_TOKEN error when emailPasswordEnabled is false' , async ( ) => {
181
+ sinon . stub ( mockAuthInstance , '_getRecaptchaConfig' ) . returns ( {
182
+ emailPasswordEnabled : false ,
183
+ siteKey : 'mock_site_key'
184
+ } ) ;
185
+ mockActionMethod . onFirstCall ( ) . rejects ( {
186
+ code : 'auth/MISSING_RECAPTCHA_TOKEN'
187
+ } ) ;
188
+ mockActionMethod . onSecondCall ( ) . resolves ( 'success-after-recaptcha' ) ;
189
+
190
+ const result = await handleRecaptchaFlow (
191
+ mockAuthInstance ,
192
+ mockRequest ,
193
+ RecaptchaActionName . GET_OOB_CODE ,
194
+ mockActionMethod
195
+ ) ;
196
+
197
+ expect ( result ) . to . equal ( 'success-after-recaptcha' ) ;
198
+ expect ( mockActionMethod ) . to . have . been . calledTwice ;
199
+ // Add more assertions as needed, e.g., checking if injectRecaptchaFields was called
200
+ } ) ;
120
201
} ) ;
202
+ } ) ;
0 commit comments