Skip to content

Commit

Permalink
Merge pull request #161 from chvarkov/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
chvarkov authored Sep 10, 2024
2 parents fbd6c2e + d203fd7 commit 44f12d0
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Test

on:
push:
branches: [ "develop" ]
pull_request:
branches: [ "master", "develop" ]

Expand Down
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"homepage": "https://github.com/chvarkov/google-recaptcha",
"dependencies": {
"axios": "^1.7.4"
"axios": "^1.7.7"
},
"peerDependencies": {
"@nestjs/common": ">=8.0.0 <11.0.0",
Expand Down Expand Up @@ -66,7 +66,7 @@
"ts-jest": "^29.2.5",
"ts-loader": "^9.3.1",
"ts-node": "^10.9.1",
"typescript": "^5.5.4"
"typescript": "^4.9.5"
},
"jest": {
"moduleFileExtensions": [
Expand Down
3 changes: 1 addition & 2 deletions src/helpers/get-error-info.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

import * as axios from 'axios';
import { LiteralObject } from '../interfaces/literal-object';

export function isAxiosError(error: Error | axios.AxiosError): error is axios.AxiosError {
export function isAxiosError<T = LiteralObject>(error: Error | axios.AxiosError<T>): error is axios.AxiosError<T> {
return (<axios.AxiosError>error).isAxiosError;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class GoogleRecaptchaEnterpriseValidator extends AbstractGoogleRecaptchaV

return this.axios.post<VerifyResponseEnterprise>(url, body, config)
.then((res) => res.data)
.then((data): VerifyResponse => {
.then((data: VerifyResponseEnterprise): VerifyResponse => {
if (this.options.valueOf.debug) {
this.logger.debug(data, `${GoogleRecaptchaContext.GoogleRecaptchaEnterprise}.response`);
}
Expand All @@ -110,7 +110,7 @@ export class GoogleRecaptchaEnterpriseValidator extends AbstractGoogleRecaptchaV
this.logger.debug(getErrorInfo(err), `${GoogleRecaptchaContext.GoogleRecaptchaEnterprise}.error`);
}

const networkErrorCode = err.isAxiosError && err.code;
const networkErrorCode = err.isAxiosError && !err.response && err.code;

if (networkErrorCode) {
throw new GoogleRecaptchaNetworkException(networkErrorCode);
Expand Down
4 changes: 2 additions & 2 deletions src/services/validators/google-recaptcha.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { RecaptchaVerificationResult } from '../../models/recaptcha-verification
import { GoogleRecaptchaContext } from '../../enums/google-recaptcha-context';
import { getErrorInfo } from '../../helpers/get-error-info';
import { AxiosInstance } from 'axios';
import { RecaptchaConfigRef } from "../../models/recaptcha-config-ref";
import { RecaptchaConfigRef } from '../../models/recaptcha-config-ref';

@Injectable()
export class GoogleRecaptchaValidator extends AbstractGoogleRecaptchaValidator<VerifyResponseV3> {
Expand Down Expand Up @@ -105,7 +105,7 @@ export class GoogleRecaptchaValidator extends AbstractGoogleRecaptchaValidator<V
this.logger.debug(getErrorInfo(err), `${GoogleRecaptchaContext.GoogleRecaptcha}.error`);
}

const networkErrorCode = err.isAxiosError && err.code;
const networkErrorCode = err.isAxiosError && !err.response && err.code;

if (networkErrorCode) {
throw new GoogleRecaptchaNetworkException(networkErrorCode);
Expand Down
4 changes: 1 addition & 3 deletions test/integrations/http-recaptcha-enterprice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ describe('HTTP Recaptcha Enterprise', () => {
});

test('Enterprise Network error', async () => {
mockedRecaptchaApi.addError<VerifyResponseV2>('test_enterprise_network_err', {
code: 'ECONNRESET',
});
mockedRecaptchaApi.addNetworkError('test_enterprise_network_err', 'ECONNRESET');

const res: request.Response = await http.post(
'/test/submit',
Expand Down
1 change: 1 addition & 0 deletions test/load-module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ describe('loadModule', () => {

test('failed load', () => {
expect(() => loadModule('@unknown/unknown-package', true)).toThrowError('Cannot find module');
expect(() => loadModule('@unknown/unknown-package', false)).toThrowError('Cannot find module');
});
});
24 changes: 24 additions & 0 deletions test/utils/mocked-recaptcha-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import * as qs from 'qs';

export class MockedRecaptchaApi {
private readonly responseMap: Map<string, LiteralObject> = new Map<string, LiteralObject>();
private readonly networkErrorMap: Map<string, string> = new Map<string, string>();
private readonly errorMap: Map<string, LiteralObject> = new Map<
string,
{ statusCode?: number; code?: string; payload?: LiteralObject }
>();

getAxios(): AxiosInstance {
const responseMap = this.responseMap;
const networkErrorMap = this.networkErrorMap;
const errorMap = this.errorMap;
const instance = axios.create({});
return Object.assign(instance, {
Expand All @@ -34,6 +36,22 @@ export class MockedRecaptchaApi {
return Promise.resolve(res);
}

const networkError = networkErrorMap.get(token);

if (networkError) {
const err: AxiosError = {
request: data,
message: 'Request was failed',
isAxiosError: true,
stack: new Error().stack,
name: 'AxiosError',
code: networkError,
toJSON: () => ({}),
};

return Promise.reject(err);
}

const errData = errorMap.get(token);

if (errData) {
Expand Down Expand Up @@ -75,4 +93,10 @@ export class MockedRecaptchaApi {

return this;
}

addNetworkError(token: string, errorCode: string): this {
this.networkErrorMap.set(token, errorCode);

return this;
}
}

0 comments on commit 44f12d0

Please sign in to comment.