Skip to content

Commit

Permalink
Added tests, updated readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
chvarkov committed Oct 30, 2020
1 parent aa099cf commit 4beebfb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestlab/google-recaptcha",
"version": "1.1.3",
"version": "1.1.4",
"description": "Google recaptcha module for NestJS.",
"keywords": [
"nest",
Expand Down
5 changes: 3 additions & 2 deletions src/services/google-recaptcha.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export class GoogleRecaptchaValidator {
validate(response: string): Promise<GoogleRecaptchaValidationResult> {
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
}
Expand Down
19 changes: 16 additions & 3 deletions test/google-recaptcha-module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -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);
});
});

0 comments on commit 4beebfb

Please sign in to comment.