From 4beebfb2e51f17d7efe92adc76b6301592e3e451 Mon Sep 17 00:00:00 2001 From: chvarkov Date: Fri, 30 Oct 2020 19:47:04 +0300 Subject: [PATCH] Added tests, updated readme. --- README.md | 14 +++++++++++--- package.json | 2 +- src/services/google-recaptcha.validator.ts | 5 +++-- test/google-recaptcha-module.spec.ts | 19 ++++++++++++++++--- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 783a50a..0a891a9 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,6 @@ $ npm i @nestlab/google-recaptcha secretKey: process.env.GOOGLE_RECAPTCHA_SECRET_KEY, response: req => req.headers.recaptcha, skipIf: () => process.env.NODE_ENV !== 'production', - // If your server has trouble connecting to https://www.google.com - // You can use https://recaptcha.net instead - // Or use an agent (see proxy-agent NPM module) useRecaptchaNet: false, agent: null }) @@ -37,6 +34,17 @@ export class AppModule { } ``` +**Configuration options** + +| Property | | Type | Description | +|-------------------|---|----------------------------|-------------| +| `secretKey` | ✔ | string | Google recaptcha secret key | +| `response` | ✔ | (request) => string | Function that returns response (recaptcha token) by request | +| `skipIf` | ✖ | () => boolean | Function that returns true if you need skip check for development or testing | +| `useRecaptchaNet` | ✖ | boolean | If your server has trouble connecting to https://www.google.com. You can use https://recaptcha.net instead, just set true | +| `agent` | ✖ | https.Agent | If you need to use an agent | + + If you want import configs from your [ConfigService](https://docs.nestjs.com/techniques/configuration#getting-started) via [custom getter function](https://docs.nestjs.com/techniques/configuration#custom-getter-functions) that will return `GoogleRecaptchaModuleOptions` object. ```typescript diff --git a/package.json b/package.json index 6c6bfcd..4096f3b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nestlab/google-recaptcha", - "version": "1.1.3", + "version": "1.1.4", "description": "Google recaptcha module for NestJS.", "keywords": [ "nest", diff --git a/src/services/google-recaptcha.validator.ts b/src/services/google-recaptcha.validator.ts index 3aecb7f..1358f03 100644 --- a/src/services/google-recaptcha.validator.ts +++ b/src/services/google-recaptcha.validator.ts @@ -18,8 +18,9 @@ export class GoogleRecaptchaValidator { validate(response: string): Promise { const data = qs.stringify({secret: this.options.secretKey, response}); - return this.http.post( - this.options.useRecaptchaNet ? this.apiUrlUseRecaptchaNet : this.apiUrl, data, { + const url = this.options.useRecaptchaNet ? this.apiUrlUseRecaptchaNet : this.apiUrl; + + return this.http.post(url, data, { headers: this.headers, httpsAgent: this.options.agent } diff --git a/test/google-recaptcha-module.spec.ts b/test/google-recaptcha-module.spec.ts index d98e036..eb1b30f 100644 --- a/test/google-recaptcha-module.spec.ts +++ b/test/google-recaptcha-module.spec.ts @@ -3,6 +3,9 @@ import { INestApplication } from '@nestjs/common'; import { GoogleRecaptchaValidator } from '../src/services/google-recaptcha.validator'; import { GoogleRecaptchaGuard } from '../src/guards/google-recaptcha.guard'; import { GoogleRecaptchaModule } from '../src/google-recaptcha.module'; +import { Agent } from 'https'; +import { RECAPTCHA_OPTIONS } from '../src/provider.declarations'; +import { GoogleRecaptchaModuleOptions } from '../src'; describe('Google recaptcha module', () => { let app: INestApplication; @@ -14,9 +17,9 @@ describe('Google recaptcha module', () => { secretKey: process.env.GOOGLE_RECAPTCHA_SECRET_KEY, response: req => req.headers.authorization, skipIf: () => process.env.NODE_ENV !== 'production', - useRecaptchaNet: false, - agent: null - }) + useRecaptchaNet: true, + agent: new Agent({maxFreeSockets: 10}), + }), ], }).compile(); @@ -34,4 +37,14 @@ describe('Google recaptcha module', () => { expect(guard).toBeInstanceOf(GoogleRecaptchaGuard); }); + + test('Test use recaptcha net options', async () => { + const options: GoogleRecaptchaModuleOptions = app.get(RECAPTCHA_OPTIONS); + + expect(options).toBeDefined(); + expect(options.useRecaptchaNet).toBeTruthy(); + expect(options.agent).toBeDefined(); + expect(options.agent).toBeInstanceOf(Agent); + expect(options.agent.maxFreeSockets).toBe(10); + }); });