|
| 1 | +import 'reflect-metadata' |
| 2 | + |
| 3 | +import * as express from 'express' |
| 4 | + |
| 5 | +import { FeaturesController } from './FeaturesController' |
| 6 | +import { results } from 'inversify-express-utils' |
| 7 | +import { User } from '../Domain/User/User' |
| 8 | +import { GetUserFeatures } from '../Domain/UseCase/GetUserFeatures/GetUserFeatures' |
| 9 | + |
| 10 | +describe('FeaturesController', () => { |
| 11 | + let getUserFeatures: GetUserFeatures |
| 12 | + |
| 13 | + let request: express.Request |
| 14 | + let response: express.Response |
| 15 | + let user: User |
| 16 | + |
| 17 | + const createController = () => new FeaturesController( |
| 18 | + getUserFeatures, |
| 19 | + ) |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + user = {} as jest.Mocked<User> |
| 23 | + user.uuid = '123' |
| 24 | + |
| 25 | + getUserFeatures = {} as jest.Mocked<GetUserFeatures> |
| 26 | + getUserFeatures.execute = jest.fn() |
| 27 | + |
| 28 | + request = { |
| 29 | + headers: {}, |
| 30 | + body: {}, |
| 31 | + params: {}, |
| 32 | + } as jest.Mocked<express.Request> |
| 33 | + |
| 34 | + response = { |
| 35 | + locals: {}, |
| 36 | + } as jest.Mocked<express.Response> |
| 37 | + }) |
| 38 | + |
| 39 | + it('should get authenticated user features', async () => { |
| 40 | + request.params.userUuid = '1-2-3' |
| 41 | + response.locals.user = { |
| 42 | + uuid: '1-2-3', |
| 43 | + } |
| 44 | + |
| 45 | + getUserFeatures.execute = jest.fn().mockReturnValue({ success: true }) |
| 46 | + |
| 47 | + const httpResponse = <results.JsonResult> await createController().getFeatures(request, response) |
| 48 | + const result = await httpResponse.executeAsync() |
| 49 | + |
| 50 | + expect(getUserFeatures.execute).toHaveBeenCalledWith({ |
| 51 | + userUuid: '1-2-3', |
| 52 | + }) |
| 53 | + |
| 54 | + expect(result.statusCode).toEqual(200) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should not get user features if the user with provided uuid does not exist', async () => { |
| 58 | + request.params.userUuid = '1-2-3' |
| 59 | + response.locals.user = { |
| 60 | + uuid: '1-2-3', |
| 61 | + } |
| 62 | + |
| 63 | + getUserFeatures.execute = jest.fn().mockReturnValue({ success: false }) |
| 64 | + |
| 65 | + const httpResponse = <results.JsonResult> await createController().getFeatures(request, response) |
| 66 | + const result = await httpResponse.executeAsync() |
| 67 | + |
| 68 | + expect(getUserFeatures.execute).toHaveBeenCalledWith({ userUuid: '1-2-3' }) |
| 69 | + |
| 70 | + expect(result.statusCode).toEqual(400) |
| 71 | + |
| 72 | + }) |
| 73 | + |
| 74 | + it('should not get user features if not allowed', async () => { |
| 75 | + request.params.userUuid = '1-2-3' |
| 76 | + response.locals.user = { |
| 77 | + uuid: '2-3-4', |
| 78 | + } |
| 79 | + |
| 80 | + getUserFeatures.execute = jest.fn() |
| 81 | + |
| 82 | + const httpResponse = <results.JsonResult> await createController().getFeatures(request, response) |
| 83 | + const result = await httpResponse.executeAsync() |
| 84 | + |
| 85 | + expect(getUserFeatures.execute).not.toHaveBeenCalled() |
| 86 | + |
| 87 | + expect(result.statusCode).toEqual(401) |
| 88 | + }) |
| 89 | +}) |
0 commit comments