1
1
import { Controller , INestApplication , LiteralObject , Post } from '@nestjs/common' ;
2
- import { ClassificationReason , GoogleRecaptchaModule , Recaptcha , RecaptchaResult , RecaptchaVerificationResult } from '../../src' ;
2
+ import { ClassificationReason , ErrorCode , GoogleRecaptchaModule , Recaptcha , RecaptchaResult , RecaptchaVerificationResult } from '../../src' ;
3
3
import { Test , TestingModule } from '@nestjs/testing' ;
4
4
import { Request } from 'express' ;
5
5
import { RECAPTCHA_HTTP_SERVICE } from '../../src/provider.declarations' ;
@@ -9,6 +9,7 @@ import { VerifyResponseV2 } from '../../src/interfaces/verify-response';
9
9
import { TestHttp } from '../utils/test-http' ;
10
10
import { VerifyResponseEnterprise } from '../../src/interfaces/verify-response-enterprise' ;
11
11
import { GoogleRecaptchaEnterpriseReason } from '../../src/enums/google-recaptcha-enterprise-reason' ;
12
+ import { TestErrorFilter } from '../assets/test-error-filter' ;
12
13
13
14
@Controller ( 'test' )
14
15
class TestController {
@@ -64,6 +65,8 @@ describe('HTTP Recaptcha Enterprise', () => {
64
65
65
66
app = module . createNestApplication ( ) ;
66
67
68
+ app . useGlobalFilters ( new TestErrorFilter ( ) ) ;
69
+
67
70
await app . init ( ) ;
68
71
69
72
http = new TestHttp ( app . getHttpServer ( ) ) ;
@@ -108,6 +111,42 @@ describe('HTTP Recaptcha Enterprise', () => {
108
111
expect ( res . body . success ) . toBe ( true ) ;
109
112
} ) ;
110
113
114
+ test ( 'Enterprise token malformed' , async ( ) => {
115
+ mockedRecaptchaApi . addResponse < VerifyResponseEnterprise > ( 'test_enterprise_token_malformed' , {
116
+ name : 'name' ,
117
+ event : {
118
+ userIpAddress : '0.0.0.0' ,
119
+ siteKey : 'siteKey' ,
120
+ userAgent : 'UA' ,
121
+ token : '' ,
122
+ hashedAccountId : '' ,
123
+ expectedAction : 'Submit' ,
124
+ } ,
125
+ tokenProperties : {
126
+ valid : false ,
127
+ invalidReason : GoogleRecaptchaEnterpriseReason . Malformed ,
128
+ hostname : '' ,
129
+ action : '' ,
130
+ createTime : '1970-01-01T00:00:00Z' ,
131
+ } ,
132
+ } ) ;
133
+
134
+ const res : request . Response = await http . post (
135
+ '/test/submit' ,
136
+ { } ,
137
+ {
138
+ headers : {
139
+ Recaptcha : 'test_enterprise_token_malformed' ,
140
+ } ,
141
+ }
142
+ ) ;
143
+
144
+ expect ( res . statusCode ) . toBe ( 400 ) ;
145
+ expect ( res . body . errorCodes ) . toBeDefined ( ) ;
146
+ expect ( res . body . errorCodes . length ) . toBe ( 1 ) ;
147
+ expect ( res . body . errorCodes [ 0 ] ) . toBe ( ErrorCode . InvalidInputResponse ) ;
148
+ } ) ;
149
+
111
150
test ( 'Enterprise without token properties' , async ( ) => {
112
151
mockedRecaptchaApi . addResponse < VerifyResponseEnterprise > ( 'test_enterprise_without_token_props' , {
113
152
name : 'name' ,
@@ -132,6 +171,10 @@ describe('HTTP Recaptcha Enterprise', () => {
132
171
) ;
133
172
134
173
expect ( res . statusCode ) . toBe ( 400 ) ;
174
+
175
+ expect ( res . body . errorCodes ) . toBeDefined ( ) ;
176
+ expect ( res . body . errorCodes . length ) . toBe ( 1 ) ;
177
+ expect ( res . body . errorCodes [ 0 ] ) . toBe ( ErrorCode . InvalidInputResponse ) ;
135
178
} ) ;
136
179
137
180
test ( 'Enterprise API error' , async ( ) => {
@@ -150,6 +193,10 @@ describe('HTTP Recaptcha Enterprise', () => {
150
193
) ;
151
194
152
195
expect ( res . statusCode ) . toBe ( 500 ) ;
196
+
197
+ expect ( res . body . errorCodes ) . toBeDefined ( ) ;
198
+ expect ( res . body . errorCodes . length ) . toBe ( 1 ) ;
199
+ expect ( res . body . errorCodes [ 0 ] ) . toBe ( ErrorCode . UnknownError ) ;
153
200
} ) ;
154
201
155
202
test ( 'Enterprise Network error' , async ( ) => {
@@ -168,6 +215,10 @@ describe('HTTP Recaptcha Enterprise', () => {
168
215
) ;
169
216
170
217
expect ( res . statusCode ) . toBe ( 500 ) ;
218
+
219
+ expect ( res . body . errorCodes ) . toBeDefined ( ) ;
220
+ expect ( res . body . errorCodes . length ) . toBe ( 1 ) ;
221
+ expect ( res . body . errorCodes [ 0 ] ) . toBe ( ErrorCode . NetworkError ) ;
171
222
} ) ;
172
223
173
224
test ( 'Enterprise Expired token' , async ( ) => {
@@ -205,6 +256,10 @@ describe('HTTP Recaptcha Enterprise', () => {
205
256
) ;
206
257
207
258
expect ( res . statusCode ) . toBe ( 400 ) ;
259
+ expect ( res . body . errorCodes ) . toBeDefined ( ) ;
260
+ expect ( res . body . errorCodes . length ) . toBe ( 2 ) ;
261
+ expect ( res . body . errorCodes [ 0 ] ) . toBe ( ErrorCode . TimeoutOrDuplicate ) ;
262
+ expect ( res . body . errorCodes [ 1 ] ) . toBe ( ErrorCode . ForbiddenAction ) ;
208
263
} ) ;
209
264
210
265
test ( 'Enterprise Invalid action' , async ( ) => {
@@ -277,5 +332,48 @@ describe('HTTP Recaptcha Enterprise', () => {
277
332
) ;
278
333
279
334
expect ( res . statusCode ) . toBe ( 400 ) ;
335
+ expect ( res . body . errorCodes ) . toBeDefined ( ) ;
336
+ expect ( res . body . errorCodes . length ) . toBe ( 1 ) ;
337
+ expect ( res . body . errorCodes [ 0 ] ) . toBe ( ErrorCode . LowScore ) ;
338
+ } ) ;
339
+
340
+ test ( 'Enterprise Invalid reason unspecified + low score' , async ( ) => {
341
+ mockedRecaptchaApi . addResponse < VerifyResponseEnterprise > ( 'test_enterprise_inv_reason_unspecified_low_score' , {
342
+ name : 'name' ,
343
+ event : {
344
+ token : 'token' ,
345
+ siteKey : 'siteKey' ,
346
+ userAgent : '' ,
347
+ userIpAddress : '' ,
348
+ expectedAction : 'Submit' ,
349
+ hashedAccountId : '' ,
350
+ } ,
351
+ riskAnalysis : {
352
+ score : 0.6 ,
353
+ reasons : [ ] ,
354
+ } ,
355
+ tokenProperties : {
356
+ valid : true ,
357
+ invalidReason : GoogleRecaptchaEnterpriseReason . InvalidReasonUnspecified ,
358
+ hostname : 'localhost' ,
359
+ action : 'Submit' ,
360
+ createTime : '2022-09-07T19:53:55.566Z' ,
361
+ } ,
362
+ } ) ;
363
+
364
+ const res : request . Response = await http . post (
365
+ '/test/submit' ,
366
+ { } ,
367
+ {
368
+ headers : {
369
+ Recaptcha : 'test_enterprise_inv_reason_unspecified_low_score' ,
370
+ } ,
371
+ }
372
+ ) ;
373
+
374
+ expect ( res . statusCode ) . toBe ( 400 ) ;
375
+ expect ( res . body . errorCodes ) . toBeDefined ( ) ;
376
+ expect ( res . body . errorCodes . length ) . toBe ( 1 ) ;
377
+ expect ( res . body . errorCodes [ 0 ] ) . toBe ( ErrorCode . LowScore ) ;
280
378
} ) ;
281
379
} ) ;
0 commit comments