Skip to content

Commit

Permalink
refactor(user): inject user message via inversify
Browse files Browse the repository at this point in the history
Inject the user message into UserController rather than getting it via
the config module. This simplifies the corresponding test and will
establish a consistent pattern when testing a future dependency, ie
the user modal message

- Declare userMessage as a dependency id
- Inject userMessage via inversify into UserController as ctor param
- Simplify the UserController test suite, dropping need for jest.mock
  • Loading branch information
LoneRifle committed Oct 5, 2020
1 parent 13f2213 commit ceebf8d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/server/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const DependencyIds = {
safeBrowsingRepository: Symbol.for('safeBrowsingRepository'),
urlThreatScanService: Symbol.for('urlThreatScanService'),
urlCheckController: Symbol.for('urlCheckController'),
userMessage: Symbol.for('userMessage'),
}

export const ERROR_404_PATH = '404.error.ejs'
11 changes: 9 additions & 2 deletions src/server/controllers/UserController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Express from 'express'
import { inject, injectable } from 'inversify'

import {
OwnershipTransferRequest,
UrlCreationRequest,
Expand All @@ -15,18 +16,24 @@ import {
NotFoundError,
} from '../util/error'
import { MessageType } from '../../shared/util/messages'
import { logger, userMessage } from '../config'
import { StorableUrlState } from '../repositories/enums'

import { logger } from '../config'

@injectable()
export class UserController implements UserControllerInterface {
private urlManagementService: UrlManagementServiceInterface

private userMessage: string

public constructor(
@inject(DependencyIds.urlManagementService)
urlManagementService: UrlManagementServiceInterface,
@inject(DependencyIds.userMessage)
userMessage: string,
) {
this.urlManagementService = urlManagementService
this.userMessage = userMessage
}

public createUrl: (
Expand Down Expand Up @@ -208,7 +215,7 @@ export class UserController implements UserControllerInterface {
req: Express.Request,
res: Express.Response,
) => Promise<void> = async (_, res) => {
res.send(userMessage)
res.send(this.userMessage)
return
}
}
Expand Down
21 changes: 14 additions & 7 deletions src/server/inversify.config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import AWS from 'aws-sdk'

import { ApiClient, ScanApi } from 'cloudmersive-virus-api-client'
import { container } from './util/inversify'
import GaLoggerService from './services/GaLoggerService'
import { DependencyIds } from './constants'
import { CookieArrayReducerService } from './services/CookieArrayReducerService'
import { OtpRepository } from './repositories/OtpRepository'
import { MailerNode } from './services/email'
import { CryptographyBcrypt } from './services/cryptography'

import {
DEV_ENV,
accessEndpoint,
bucketEndpoint,
cloudmersiveKey,
logger,
s3Bucket,
userMessage,
} from './config'

import { container } from './util/inversify'
import GaLoggerService from './services/GaLoggerService'
import { DependencyIds } from './constants'
import { CookieArrayReducerService } from './services/CookieArrayReducerService'
import { OtpRepository } from './repositories/OtpRepository'
import { MailerNode } from './services/email'
import { CryptographyBcrypt } from './services/cryptography'

import { MailerNoOp } from './services/emaildev'
import { S3ServerSide } from './services/aws'
import { UrlRepository } from './repositories/UrlRepository'
Expand Down Expand Up @@ -65,6 +70,8 @@ function bindIfUnbound<T>(
}

export default () => {
container.bind(DependencyIds.userMessage).toConstantValue(userMessage)

bindIfUnbound(DependencyIds.urlRepository, UrlRepository)
bindIfUnbound(DependencyIds.urlMapper, UrlMapper)
bindIfUnbound(DependencyIds.userMapper, UserMapper)
Expand Down
20 changes: 5 additions & 15 deletions test/server/controllers/UserController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,19 @@ import httpMocks from 'node-mocks-http'
import { StorableUrlState } from '../../../src/server/repositories/enums'
import { createRequestWithUser } from '../api/util'

import { UserController } from '../../../src/server/controllers/UserController'

const urlManagementService = {
createUrl: jest.fn(),
updateUrl: jest.fn(),
changeOwnership: jest.fn(),
getUrlsWithConditions: jest.fn(),
}

describe('UserController', () => {
const userMessage = 'The quick brown fox jumps over a lazy dog'

jest.resetModules()
jest.mock('../../../src/server/config', () => ({
logger: console,
userMessage,
}))
const userMessage = 'The quick brown fox jumps over a lazy dog'

const {
UserController,
} = require('../../../src/server/controllers/UserController')

afterAll(jest.resetModules)

const controller = new UserController(urlManagementService)
describe('UserController', () => {
const controller = new UserController(urlManagementService, userMessage)

describe('createUrl', () => {
it('rejects multiple file uploads', async () => {
Expand Down

0 comments on commit ceebf8d

Please sign in to comment.