Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
refactor(recaptcha): inject config using DI (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneRifle authored and LinHuiqing committed Sep 7, 2021
1 parent d6b2037 commit 973cdcf
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
3 changes: 2 additions & 1 deletion server/src/bootstrap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { authConfig } from './config/auth'
import { fullStoryConfig } from './config/fullstory'
import { mailConfig } from './config/mail'
import { fileConfig } from './config/file'
import { recaptchaConfig } from './config/recaptcha'
import { requestLoggingMiddleware } from './logging'

import { helmetOptions } from './helmet-options'
Expand Down Expand Up @@ -86,7 +87,7 @@ const mailService = new MailService({
})
const postService = new PostService({ Answer, Post, PostTag, Tag, User })
const enquiryService = new EnquiryService({ Agency, mailService })
const recaptchaService = new RecaptchaService({ axios })
const recaptchaService = new RecaptchaService({ axios, ...recaptchaConfig })

const apiOptions = {
agency: new AgencyController({ agencyService }),
Expand Down
9 changes: 8 additions & 1 deletion server/src/modules/enquiry/__tests__/enquiry.routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ describe('/enquiries', () => {
let mockAgency1: AgencyModel

const axios = { get: jest.fn() }
const recaptchaService = new RecaptchaService({ axios })
const googleRecaptchaURL = 'https://recaptcha.net'
const recaptchaSecretKey = ''

const recaptchaService = new RecaptchaService({
axios,
googleRecaptchaURL,
recaptchaSecretKey,
})

const path = '/enquiries'
const app = express()
Expand Down
59 changes: 27 additions & 32 deletions server/src/services/recaptcha/__tests__/recaptcha.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { recaptchaConfig } from '../../../bootstrap/config/recaptcha'

import {
CaptchaConnectionError,
MissingCaptchaError,
VerifyCaptchaError,
} from '../recaptcha.errors'
import { RecaptchaService } from '../recaptcha.service'

const MOCK_PRIVATE_KEY = ''
const MOCK_RESPONSE = 'captchaResponse'
const MOCK_REMOTE_IP = 'remoteIp'

describe('captcha.service', () => {
describe('recaptcha.service', () => {
const axios = { get: jest.fn() }
const recaptchaService = new RecaptchaService({ axios })
const googleRecaptchaURL = 'https://recaptcha.net'
const recaptchaSecretKey = ''

const recaptchaService = new RecaptchaService({
axios,
googleRecaptchaURL,
recaptchaSecretKey,
})

describe('verifyCaptchaResponse', () => {
beforeEach(() => jest.clearAllMocks())
Expand All @@ -40,16 +44,13 @@ describe('captcha.service', () => {
)

// Assert
expect(axios.get).toHaveBeenCalledWith(
recaptchaConfig.googleRecaptchaURL,
{
params: {
secret: MOCK_PRIVATE_KEY,
response: MOCK_RESPONSE,
remoteip: MOCK_REMOTE_IP,
},
expect(axios.get).toHaveBeenCalledWith(googleRecaptchaURL, {
params: {
secret: recaptchaSecretKey,
response: MOCK_RESPONSE,
remoteip: MOCK_REMOTE_IP,
},
)
})
expect(result._unsafeUnwrapErr()).toEqual(new VerifyCaptchaError())
})

Expand All @@ -64,16 +65,13 @@ describe('captcha.service', () => {
)

// Assert
expect(axios.get).toHaveBeenCalledWith(
recaptchaConfig.googleRecaptchaURL,
{
params: {
secret: MOCK_PRIVATE_KEY,
response: MOCK_RESPONSE,
remoteip: MOCK_REMOTE_IP,
},
expect(axios.get).toHaveBeenCalledWith(googleRecaptchaURL, {
params: {
secret: recaptchaSecretKey,
response: MOCK_RESPONSE,
remoteip: MOCK_REMOTE_IP,
},
)
})
expect(result._unsafeUnwrap()).toEqual(true)
})

Expand All @@ -88,16 +86,13 @@ describe('captcha.service', () => {
)

// Assert
expect(axios.get).toHaveBeenCalledWith(
recaptchaConfig.googleRecaptchaURL,
{
params: {
secret: MOCK_PRIVATE_KEY,
response: MOCK_RESPONSE,
remoteip: MOCK_REMOTE_IP,
},
expect(axios.get).toHaveBeenCalledWith(googleRecaptchaURL, {
params: {
secret: recaptchaSecretKey,
response: MOCK_RESPONSE,
remoteip: MOCK_REMOTE_IP,
},
)
})
expect(result._unsafeUnwrapErr()).toEqual(new CaptchaConnectionError())
})
})
Expand Down
19 changes: 15 additions & 4 deletions server/src/services/recaptcha/recaptcha.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AxiosStatic } from 'axios'
import { createLogger } from '../../bootstrap/logging'
import { errAsync, okAsync, ResultAsync } from 'neverthrow'
import { recaptchaConfig } from '../../bootstrap/config/recaptcha'

import {
CaptchaConnectionError,
Expand All @@ -12,9 +11,21 @@ import {
const logger = createLogger(module)
export class RecaptchaService {
private axios: Pick<AxiosStatic, 'get'>
private googleRecaptchaURL: string
private recaptchaSecretKey: string

constructor({ axios }: { axios: Pick<AxiosStatic, 'get'> }) {
constructor({
axios,
googleRecaptchaURL,
recaptchaSecretKey,
}: {
axios: Pick<AxiosStatic, 'get'>
googleRecaptchaURL: string
recaptchaSecretKey: string
}) {
this.axios = axios
this.googleRecaptchaURL = googleRecaptchaURL
this.recaptchaSecretKey = recaptchaSecretKey
}

public verifyCaptchaResponse = (
Expand All @@ -28,10 +39,10 @@ export class RecaptchaService {
return errAsync(new MissingCaptchaError())
}
const verifyCaptchaPromise = this.axios.get<{ success: boolean }>(
recaptchaConfig.googleRecaptchaURL,
this.googleRecaptchaURL,
{
params: {
secret: recaptchaConfig.recaptchaSecretKey,
secret: this.recaptchaSecretKey,
response,
remoteip,
},
Expand Down

0 comments on commit 973cdcf

Please sign in to comment.