-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(modules/users): implement
controller
- Loading branch information
Showing
7 changed files
with
249 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { beforeEach, describe, expect, test, vi } from 'vitest' | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
|
||
import { UsersController } from './users.controller' | ||
import { UsersRepository } from './users.repository' | ||
|
||
import { userMock, usersMock } from '@common/mocks' | ||
|
||
describe('UsersController', () => { | ||
let usersController: UsersController | ||
let usersRepository: UsersRepository | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
UsersController, | ||
{ | ||
provide: UsersRepository, | ||
useValue: { | ||
findUsers: vi.fn(), | ||
findUserById: vi.fn(), | ||
findUserByProfileId: vi.fn(), | ||
findUserByDisplayName: vi.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile() | ||
|
||
usersController = module.get<UsersController>(UsersController) | ||
usersRepository = module.get<UsersRepository>(UsersRepository) | ||
}) | ||
|
||
test('should be defined', () => { | ||
expect(usersController).toBeDefined() | ||
}) | ||
|
||
describe('getAll', () => { | ||
test('should get all users', async () => { | ||
vi.spyOn(usersRepository, 'findUsers').mockResolvedValue(usersMock) | ||
|
||
expect(await usersController.getAll()).toEqual(usersMock) | ||
|
||
expect(usersRepository.findUsers).toHaveBeenCalled() | ||
}) | ||
|
||
test('should get one user by username', async () => { | ||
vi.spyOn(usersRepository, 'findUserByDisplayName').mockResolvedValue( | ||
userMock | ||
) | ||
|
||
const username = 'username' | ||
|
||
expect(await usersController.getAll(username)).toEqual(userMock) | ||
|
||
expect(usersRepository.findUserByDisplayName).toHaveBeenCalledWith( | ||
username | ||
) | ||
}) | ||
|
||
test('should throw an error if no user is found', async () => { | ||
vi.spyOn(usersRepository, 'findUserByDisplayName') | ||
|
||
const username = 'username' | ||
|
||
await expect(usersController.getAll(username)).rejects.toThrowError() | ||
}) | ||
}) | ||
|
||
describe('getOneById', () => { | ||
test('should get one user by id', async () => { | ||
vi.spyOn(usersRepository, 'findUserById').mockResolvedValue(userMock) | ||
|
||
const id = '1' | ||
|
||
expect(await usersController.getOneById(id)).toEqual(userMock) | ||
|
||
expect(usersRepository.findUserById).toHaveBeenCalledWith(id) | ||
}) | ||
|
||
test('should throw an error if no user is found', async () => { | ||
vi.spyOn(usersRepository, 'findUserById') | ||
|
||
const id = '1' | ||
|
||
await expect(usersController.getOneById(id)).rejects.toThrowError() | ||
|
||
expect(usersRepository.findUserById).toHaveBeenCalledWith(id) | ||
}) | ||
}) | ||
|
||
describe('getOneByProfileId', () => { | ||
test('should get one user by profile id', async () => { | ||
vi.spyOn(usersRepository, 'findUserByProfileId').mockResolvedValue( | ||
userMock | ||
) | ||
|
||
const profileId = '1' | ||
|
||
expect(await usersController.getOneByProfileId(profileId)).toEqual( | ||
userMock | ||
) | ||
|
||
expect(usersRepository.findUserByProfileId).toHaveBeenCalledWith( | ||
profileId | ||
) | ||
}) | ||
|
||
test('should throw an error if no user is found', async () => { | ||
vi.spyOn(usersRepository, 'findUserByProfileId') | ||
|
||
const profileId = '1' | ||
|
||
await expect( | ||
usersController.getOneByProfileId(profileId) | ||
).rejects.toThrowError() | ||
|
||
expect(usersRepository.findUserByProfileId).toHaveBeenCalledWith( | ||
profileId | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { | ||
Controller, | ||
Get, | ||
HttpException, | ||
HttpStatus, | ||
NotFoundException, | ||
Param, | ||
ParseUUIDPipe, | ||
Query, | ||
} from '@nestjs/common' | ||
import { | ||
ApiBadRequestResponse, | ||
ApiNoContentResponse, | ||
ApiNotFoundResponse, | ||
ApiOkResponse, | ||
ApiOperation, | ||
ApiParam, | ||
ApiQuery, | ||
ApiTags, | ||
} from '@nestjs/swagger' | ||
|
||
import { UsersRepository } from './users.repository' | ||
|
||
import { | ||
MANY_SUCCESFULLY_FOUND, | ||
NOT_BEEN_FOUND, | ||
ONE_IS_INVALID, | ||
ONE_SUCCESFULLY_FOUND, | ||
} from '@common/constants' | ||
|
||
export const USER = 'user' | ||
export const USERS = 'users' | ||
|
||
@Controller(USERS) | ||
@ApiTags(USERS) | ||
export class UsersController { | ||
constructor(private readonly usersRepository: UsersRepository) {} | ||
|
||
@Get() | ||
@ApiOperation({ | ||
summary: 'Getting all users.', | ||
}) | ||
@ApiQuery({ name: 'username', required: false }) | ||
@ApiOkResponse({ | ||
description: MANY_SUCCESFULLY_FOUND(USERS), | ||
}) | ||
@ApiNoContentResponse({ | ||
description: NOT_BEEN_FOUND(USER), | ||
}) | ||
async getAll(@Query('username') username?: string) { | ||
if (username) { | ||
const foundUser = await this.usersRepository.findUserByDisplayName( | ||
username | ||
) | ||
|
||
if (!foundUser) | ||
throw new HttpException(NOT_BEEN_FOUND(USER), HttpStatus.NO_CONTENT) | ||
|
||
return foundUser | ||
} | ||
|
||
return await this.usersRepository.findUsers() | ||
} | ||
|
||
@Get(':id') | ||
@ApiOperation({ | ||
summary: 'Getting one user by id.', | ||
}) | ||
@ApiParam({ name: 'id' }) | ||
@ApiOkResponse({ | ||
description: ONE_SUCCESFULLY_FOUND(USER), | ||
}) | ||
@ApiNotFoundResponse({ | ||
description: NOT_BEEN_FOUND(USER), | ||
}) | ||
@ApiBadRequestResponse({ | ||
description: ONE_IS_INVALID('uuid'), | ||
}) | ||
async getOneById(@Param('id', ParseUUIDPipe) id: string) { | ||
const foundUser = await this.usersRepository.findUserById(id) | ||
|
||
if (!foundUser) throw new NotFoundException(NOT_BEEN_FOUND(USER)) | ||
|
||
return foundUser | ||
} | ||
|
||
@Get('profile/:id') | ||
@ApiOperation({ | ||
summary: 'Getting one user by profile id.', | ||
}) | ||
@ApiParam({ name: 'id' }) | ||
@ApiOkResponse({ | ||
description: ONE_SUCCESFULLY_FOUND(USER), | ||
}) | ||
@ApiNotFoundResponse({ | ||
description: NOT_BEEN_FOUND(USER), | ||
}) | ||
async getOneByProfileId(@Param('id') id: string) { | ||
const foundUser = await this.usersRepository.findUserByProfileId(id) | ||
|
||
if (!foundUser) throw new NotFoundException(NOT_BEEN_FOUND(USER)) | ||
|
||
return foundUser | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters