Skip to content

Commit

Permalink
Merge pull request #607 from boostcampwm2023/BE-TestUserService-#606
Browse files Browse the repository at this point in the history
[BE/#606] user service 테스트 코드 작성
  • Loading branch information
namewhat99 authored Jan 14, 2024
2 parents b15592f + 7482eff commit 05e5514
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 6 deletions.
4 changes: 2 additions & 2 deletions BE/src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class UsersController {
@Headers('authorization') token: string,
) {
await this.usersService.checkAuth(id, userId);
await this.usersService.removeUser(id, userId, token);
await this.usersService.removeUser(userId, token);
await this.notificationService.removeRegistrationToken(userId);
}

Expand All @@ -67,7 +67,7 @@ export class UsersController {
? await this.imageService.uploadImage(file)
: null;
const nickname = body ? body.nickname : null;
await this.usersService.updateUserById(id, nickname, imageLocation, userId);
await this.usersService.updateUserById(nickname, imageLocation, userId);
}

@Post('registration-token')
Expand Down
164 changes: 164 additions & 0 deletions BE/src/users/users.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UsersService } from './users.service';
import { UserRepository } from './user.repository';
import { ConfigService } from '@nestjs/config';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { HttpException } from '@nestjs/common';
import { UserEntity } from '../entities/user.entity';
const mockRepository = {
save: jest.fn(),
findOne: jest.fn(),
update: jest.fn(),
};

const mockUserRepository = {
getRepository: jest.fn().mockReturnValue(mockRepository),
softDeleteCascade: jest.fn(),
};

jest.mock('jsonwebtoken', () => ({
decode: jest.fn(() => {
return {
exp: 1703128397,
};
}),
}));

describe('UsersService', function () {
let service: UsersService;
let repository;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
UsersService,
{
provide: UserRepository,
useValue: mockUserRepository,
},
{
provide: ConfigService,
useValue: { get: jest.fn((key: string) => 'default image') },
},
{
provide: CACHE_MANAGER,
useValue: { set: jest.fn((key: string) => 'mocked-value') },
},
],
}).compile();

service = module.get<UsersService>(UsersService);
repository = module.get(UserRepository);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('checkAuth', function () {
it('should return 404', function () {
repository.getRepository().findOne.mockResolvedValue(null);
expect(async () => {
await service.checkAuth('user', 'user');
}).rejects.toThrowError(
new HttpException('유저가 존재하지 않습니다.', 404),
);
});

it('should return 403', function () {
repository.getRepository().findOne.mockResolvedValue(new UserEntity());
expect(async () => {
await service.checkAuth('user', 'user1');
}).rejects.toThrowError(new HttpException('수정 권한이 없습니다.', 403));
});

it('should pass', async function () {
repository.getRepository().findOne.mockResolvedValue(new UserEntity());
await service.checkAuth('user', 'user');
});
});

describe('findUserById', function () {
it('should return null', async function () {
repository.getRepository().findOne.mockResolvedValue(null);
const res = await service.findUserById('user');
expect(res).toEqual(null);
});

it('should return user with profile image', async function () {
const user = new UserEntity();
user.profile_img = 'www.test.com';
user.nickname = 'user';
repository.getRepository().findOne.mockResolvedValue(user);
const res = await service.findUserById('user');
expect(res.nickname).toEqual('user');
expect(res.profile_img).toEqual('www.test.com');
});

it('should return user with default profile image', async function () {
const user = new UserEntity();
user.profile_img = null;
user.nickname = 'user';
repository.getRepository().findOne.mockResolvedValue(user);
const res = await service.findUserById('user');
expect(res.nickname).toEqual('user');
expect(res.profile_img).toEqual('default image');
});
});

describe('updateUserById', function () {
it('should update only nickname', async function () {
const nickname = 'test';
const imageLocation = null;
const userId = 'user';
const updateEntity = new UserEntity();
updateEntity.nickname = nickname;
updateEntity.profile_img = undefined;
await service.updateUserById(nickname, imageLocation, userId);
expect(repository.getRepository().update).toHaveBeenCalledWith(
{
user_hash: userId,
},
updateEntity,
);
});

it('should update only image', async function () {
const nickname = null;
const imageLocation = 'test';
const userId = 'user';
const updateEntity = new UserEntity();
updateEntity.nickname = undefined;
updateEntity.profile_img = imageLocation;
await service.updateUserById(nickname, imageLocation, userId);
expect(repository.getRepository().update).toHaveBeenCalledWith(
{
user_hash: userId,
},
updateEntity,
);
});

it('should update all', async function () {
const nickname = 'test';
const imageLocation = 'test';
const userId = 'user';
const updateEntity = new UserEntity();
updateEntity.nickname = nickname;
updateEntity.profile_img = imageLocation;
await service.updateUserById(nickname, imageLocation, userId);
expect(repository.getRepository().update).toHaveBeenCalledWith(
{
user_hash: userId,
},
updateEntity,
);
});
});

describe('removeUser', function () {
it('should remove', async function () {
jest.spyOn(Math, 'floor').mockReturnValue(1703128360);
await service.removeUser('user', 'token');
});
});
});
7 changes: 3 additions & 4 deletions BE/src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HttpException, Inject, Injectable } from '@nestjs/common';
import { CreateUserDto } from './dto/createUser.dto';
import { UserEntity } from 'src/entities/user.entity';
import { hashMaker } from 'src/common/hashMaker';
import { UserEntity } from '../entities/user.entity';
import { hashMaker } from '../common/hashMaker';
import { ConfigService } from '@nestjs/config';
import * as jwt from 'jsonwebtoken';
import { CACHE_MANAGER, CacheStore } from '@nestjs/cache-manager';
Expand Down Expand Up @@ -40,7 +40,7 @@ export class UsersService {
}
}

async removeUser(id: string, userId: string, accessToken: string) {
async removeUser(userId: string, accessToken: string) {
const decodedToken: any = jwt.decode(accessToken);
if (decodedToken && decodedToken.exp) {
const ttl: number = decodedToken.exp - Math.floor(Date.now() / 1000);
Expand All @@ -64,7 +64,6 @@ export class UsersService {
}

async updateUserById(
id: string,
nickname: string,
imageLocation: string,
userId: string,
Expand Down

0 comments on commit 05e5514

Please sign in to comment.