Skip to content

Commit

Permalink
Add user controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBartusek committed Apr 14, 2024
1 parent 3c8ca27 commit 1308175
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
54 changes: 50 additions & 4 deletions apps/api/src/models/users/users.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { Request } from 'express';
import { Types } from 'mongoose';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
import { mockUserRequest } from '../../mocks/mock-user-request';
import { UpdateUserDto } from './dto/update-user.dto';
import { BadRequestException } from '@nestjs/common';

describe('UsersController', () => {
let controller: UsersController;
Expand All @@ -20,6 +23,13 @@ describe('UsersController', () => {
},
};
}),
updateProfile: jest.fn((id, dto) => {
return {
_id: id,
profile: dto,
};
}),
isUsernameTaken: jest.fn(),
};

beforeEach(async () => {
Expand All @@ -39,7 +49,9 @@ describe('UsersController', () => {
});

it('should get user by id', () => {
expect(controller.findOne(new Types.ObjectId())).resolves.toEqual({
const user = controller.findOne(new Types.ObjectId());

expect(user).resolves.toEqual({
id: expect.any(Types.ObjectId),
username: expect.any(String),
image: {
Expand All @@ -49,10 +61,12 @@ describe('UsersController', () => {
});
});

it('should get authenticated user', () => {
const mockRequest = { user: { id: new Types.ObjectId().toString() } } as Request;
it('should get authenticated user', async () => {
const request = mockUserRequest;

expect(controller.findAuthenticated(mockRequest)).resolves.toEqual({
const user = await controller.findAuthenticated(request);

expect(user).toEqual({
id: expect.any(Types.ObjectId),
username: expect.any(String),
email: expect.any(String),
Expand All @@ -62,4 +76,36 @@ describe('UsersController', () => {
},
});
});

describe('Update profile', () => {
it('should update profile', async () => {
const request = mockUserRequest;
mockUserService.isUsernameTaken.mockResolvedValue(false);

const dto: UpdateUserDto = {
username: 'updated',
image: { hasImage: false },
};
const user = await controller.updateProfile(request, dto);

expect(user).toStrictEqual(
expect.objectContaining({
username: 'updated',
}),
);
});

it('should error on taken username', async () => {
const request = mockUserRequest;
mockUserService.isUsernameTaken.mockResolvedValue(true);

const dto: UpdateUserDto = {
username: 'updated',
image: { hasImage: false },
};
const user = controller.updateProfile(request, dto);

expect(user).rejects.toThrowError(BadRequestException);
});
});
});
4 changes: 4 additions & 0 deletions apps/api/src/models/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export class UsersController {
throw new ForbiddenException('This action is not available for demo accounts');
}

// Username taken check should ideally be an DTO decorator.
// But since this project is designed to use PUT requests.
// Users won't be able to update their username, since it will
// always be taken, by them
const usernameTaken = await this.usersService.isUsernameTaken(dto.username);
if (usernameTaken) {
throw new BadRequestException('This username is already taken');
Expand Down

0 comments on commit 1308175

Please sign in to comment.