From bff8a1902e0899e308744b61dd2ab5d60fcdbe3e Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Fri, 12 Jan 2024 23:14:23 +0900 Subject: [PATCH 1/8] =?UTF-8?q?[BE]=20Refactor=20:=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EA=B2=BD=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/users/users.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BE/src/users/users.service.ts b/BE/src/users/users.service.ts index 99ce0ca..d247cc9 100644 --- a/BE/src/users/users.service.ts +++ b/BE/src/users/users.service.ts @@ -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'; From b6a410dc276e36d7b23ffc49184cecd4947b640b Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Fri, 12 Jan 2024 23:14:50 +0900 Subject: [PATCH 2/8] =?UTF-8?q?[BE]=20Test=20:=20userService=20test=20?= =?UTF-8?q?=EC=84=B8=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/users/users.service.spec.ts | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 BE/src/users/users.service.spec.ts diff --git a/BE/src/users/users.service.spec.ts b/BE/src/users/users.service.spec.ts new file mode 100644 index 0000000..ccc1376 --- /dev/null +++ b/BE/src/users/users.service.spec.ts @@ -0,0 +1,46 @@ +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'; + +const mockRepository = { + save: jest.fn(), + findOne: jest.fn(), + update: jest.fn(), +}; + +const mockUserRepository = { + getRepository: jest.fn().mockReturnValue(mockRepository), + softDeleteCascade: jest.fn(), +}; +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) => 'mocked-value') }, + }, + { + provide: CACHE_MANAGER, + useValue: { set: jest.fn((key: string) => 'mocked-value') }, + }, + ], + }).compile(); + + service = module.get(UsersService); + repository = module.get(UserRepository); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); From 3c85fdbd990eff44e424351e366caee9ea0bb1da Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Fri, 12 Jan 2024 23:20:25 +0900 Subject: [PATCH 3/8] =?UTF-8?q?[BE]=20Test=20:=20checkAuth=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/users/users.service.spec.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/BE/src/users/users.service.spec.ts b/BE/src/users/users.service.spec.ts index ccc1376..4274858 100644 --- a/BE/src/users/users.service.spec.ts +++ b/BE/src/users/users.service.spec.ts @@ -3,6 +3,8 @@ 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(), @@ -43,4 +45,27 @@ describe('UsersService', function () { 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'); + }); + }); }); From b025a99248b73ec2a694349def7e94b23f24ce5d Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Fri, 12 Jan 2024 23:27:20 +0900 Subject: [PATCH 4/8] =?UTF-8?q?[BE]=20Test=20:=20findUserById=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/users/users.service.spec.ts | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/BE/src/users/users.service.spec.ts b/BE/src/users/users.service.spec.ts index 4274858..3133cda 100644 --- a/BE/src/users/users.service.spec.ts +++ b/BE/src/users/users.service.spec.ts @@ -29,7 +29,7 @@ describe('UsersService', function () { }, { provide: ConfigService, - useValue: { get: jest.fn((key: string) => 'mocked-value') }, + useValue: { get: jest.fn((key: string) => 'default image') }, }, { provide: CACHE_MANAGER, @@ -68,4 +68,32 @@ describe('UsersService', function () { 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'); + }); + }); }); From 4488d805881edc65167b6766aaef2d77bc4199b3 Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Fri, 12 Jan 2024 23:31:11 +0900 Subject: [PATCH 5/8] =?UTF-8?q?[BE]=20Refactor=20:=20=EB=B6=88=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=9C=20=EC=9D=B8=EC=9E=90=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/users/users.controller.ts | 2 +- BE/src/users/users.service.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/BE/src/users/users.controller.ts b/BE/src/users/users.controller.ts index d319093..c0ac6f3 100644 --- a/BE/src/users/users.controller.ts +++ b/BE/src/users/users.controller.ts @@ -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') diff --git a/BE/src/users/users.service.ts b/BE/src/users/users.service.ts index d247cc9..c850716 100644 --- a/BE/src/users/users.service.ts +++ b/BE/src/users/users.service.ts @@ -64,7 +64,6 @@ export class UsersService { } async updateUserById( - id: string, nickname: string, imageLocation: string, userId: string, From f093c7f9170dab7d37c1185bc252bcaed155f384 Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Fri, 12 Jan 2024 23:57:42 +0900 Subject: [PATCH 6/8] =?UTF-8?q?[BE]=20Test=20:=20updateUserById=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/users/users.service.spec.ts | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/BE/src/users/users.service.spec.ts b/BE/src/users/users.service.spec.ts index 3133cda..1d303bc 100644 --- a/BE/src/users/users.service.spec.ts +++ b/BE/src/users/users.service.spec.ts @@ -96,4 +96,56 @@ describe('UsersService', function () { 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('', function () {}); }); From 106ba727e43d0fb3d24774bb71e53773cfb37578 Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Sat, 13 Jan 2024 00:13:20 +0900 Subject: [PATCH 7/8] =?UTF-8?q?[BE]=20Refactor=20:=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EC=9D=B8=EC=9E=90=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/users/users.controller.ts | 2 +- BE/src/users/users.service.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/BE/src/users/users.controller.ts b/BE/src/users/users.controller.ts index c0ac6f3..56bc671 100644 --- a/BE/src/users/users.controller.ts +++ b/BE/src/users/users.controller.ts @@ -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); } diff --git a/BE/src/users/users.service.ts b/BE/src/users/users.service.ts index c850716..ce547e6 100644 --- a/BE/src/users/users.service.ts +++ b/BE/src/users/users.service.ts @@ -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); From 7482effe69f9f0d3e7a16628c1428d4cf871585d Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Sat, 13 Jan 2024 00:13:46 +0900 Subject: [PATCH 8/8] =?UTF-8?q?[BE]=20Test=20:=20removeUser=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/users/users.service.spec.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/BE/src/users/users.service.spec.ts b/BE/src/users/users.service.spec.ts index 1d303bc..a416be9 100644 --- a/BE/src/users/users.service.spec.ts +++ b/BE/src/users/users.service.spec.ts @@ -5,7 +5,6 @@ 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(), @@ -16,6 +15,15 @@ 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; @@ -147,5 +155,10 @@ describe('UsersService', function () { }); }); - describe('', function () {}); + describe('removeUser', function () { + it('should remove', async function () { + jest.spyOn(Math, 'floor').mockReturnValue(1703128360); + await service.removeUser('user', 'token'); + }); + }); });