Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

프로필 모듈 리팩토링 #359

Merged
merged 8 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 79 additions & 61 deletions nestjs-BE/server/src/profiles/profiles.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,106 +1,124 @@
import {
ForbiddenException,
HttpStatus,
NotFoundException,
} from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { ProfilesController } from './profiles.controller';
import { ProfilesService } from './profiles.service';
import { UploadService } from '../upload/upload.service';
import { RequestWithUser } from '../utils/interface';
import { NotFoundException } from '@nestjs/common';

describe('ProfilesController', () => {
let controller: ProfilesController;
let profilesService: ProfilesService;
let uploadService: UploadService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ProfilesController],
providers: [
{
provide: ProfilesService,
useValue: { findProfile: jest.fn(), updateProfile: jest.fn() },
useValue: {
findProfileByUserUuid: jest.fn(),
updateProfile: jest.fn(),
},
},
{ provide: UploadService, useValue: { uploadFile: jest.fn() } },
],
}).compile();

controller = module.get<ProfilesController>(ProfilesController);
profilesService = module.get<ProfilesService>(ProfilesService);
uploadService = module.get<UploadService>(UploadService);
});

it('findProfile found profile', async () => {
const requestMock = { user: { uuid: 'user test uuid' } } as RequestWithUser;
const testProfile = {
uuid: 'profile test uuid',
userUuid: requestMock.user.uuid,
image: 'www.test.com/image',
nickname: 'test nickname',
};
jest.spyOn(profilesService, 'findProfile').mockResolvedValue(testProfile);
describe('findProfile', () => {
const requestMock = { user: { uuid: 'test uuid' } } as RequestWithUser;

it('found profile', async () => {
const testProfile = {
uuid: 'profile test uuid',
userUuid: requestMock.user.uuid,
image: 'www.test.com/image',
nickname: 'test nickname',
};

const response = controller.findProfile(requestMock);
jest
.spyOn(profilesService, 'findProfileByUserUuid')
.mockResolvedValue(testProfile);

await expect(response).resolves.toEqual({
statusCode: 200,
message: 'Success',
data: testProfile,
const response = controller.findProfileByUserUuid(requestMock);

await expect(response).resolves.toEqual({
statusCode: HttpStatus.OK,
message: 'Success',
data: testProfile,
});
expect(profilesService.findProfileByUserUuid).toHaveBeenCalledWith(
requestMock.user.uuid,
);
});
expect(profilesService.findProfile).toHaveBeenCalledWith(
requestMock.user.uuid,
);
});

it('findProfile not found profile', async () => {
const requestMock = { user: { uuid: 'test uuid' } } as RequestWithUser;
jest.spyOn(profilesService, 'findProfile').mockResolvedValue(null);
it('not found profile', async () => {
jest
.spyOn(profilesService, 'findProfileByUserUuid')
.mockRejectedValue(new NotFoundException());

const response = controller.findProfile(requestMock);
const response = controller.findProfileByUserUuid(requestMock);

await expect(response).rejects.toThrow(NotFoundException);
await expect(response).rejects.toThrow(NotFoundException);
});
});

it('update updated profile', async () => {
describe('update', () => {
const imageMock = {} as Express.Multer.File;
const requestMock = { user: { uuid: 'test uuid' } } as RequestWithUser;
const bodyMock = {
nickname: 'test nickname',
};
const testImageUrl = 'www.test.com/image';
const testProfile = {
uuid: 'profile test uuid',
userUuid: requestMock.user.uuid,
image: 'www.test.com/image',
nickname: 'test nickname',
};
jest.spyOn(uploadService, 'uploadFile').mockResolvedValue(testImageUrl);
jest.spyOn(profilesService, 'updateProfile').mockResolvedValue(testProfile);

const response = controller.update(imageMock, requestMock, bodyMock);
it('updated profile', async () => {
const testProfile = {
uuid: 'profile test uuid',
userUuid: requestMock.user.uuid,
image: 'www.test.com/image',
nickname: 'test nickname',
};

jest
.spyOn(profilesService, 'updateProfile')
.mockResolvedValue(testProfile);

const response = controller.updateProfile(
imageMock,
requestMock,
bodyMock,
);

await expect(response).resolves.toEqual({
statusCode: 200,
message: 'Success',
data: testProfile,
await expect(response).resolves.toEqual({
statusCode: HttpStatus.OK,
message: 'Success',
data: testProfile,
});
expect(profilesService.updateProfile).toHaveBeenCalledWith(
requestMock.user.uuid,
bodyMock.uuid,
imageMock,
bodyMock,
);
});
expect(uploadService.uploadFile).toHaveBeenCalled();
expect(uploadService.uploadFile).toHaveBeenCalledWith(imageMock);
expect(profilesService.updateProfile).toHaveBeenCalledWith(
requestMock.user.uuid,
{ nickname: bodyMock.nickname, image: testImageUrl },
);
});

it('update not found user', async () => {
const imageMock = {} as Express.Multer.File;
const requestMock = { user: { uuid: 'test uuid' } } as RequestWithUser;
const bodyMock = {
nickname: 'test nickname',
};
const testImageUrl = 'www.test.com/image';
jest.spyOn(uploadService, 'uploadFile').mockResolvedValue(testImageUrl);
jest.spyOn(profilesService, 'updateProfile').mockResolvedValue(null);
it('not found user', async () => {
jest
.spyOn(profilesService, 'updateProfile')
.mockRejectedValue(new ForbiddenException());

const response = controller.update(imageMock, requestMock, bodyMock);
const response = controller.updateProfile(
imageMock,
requestMock,
bodyMock,
);

await expect(response).rejects.toThrow(NotFoundException);
await expect(response).rejects.toThrow(ForbiddenException);
});
});
});
37 changes: 16 additions & 21 deletions nestjs-BE/server/src/profiles/profiles.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,59 @@ import {
UploadedFile,
Request as Req,
ValidationPipe,
NotFoundException,
HttpStatus,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { ApiTags, ApiResponse, ApiOperation } from '@nestjs/swagger';
import { ProfilesService } from './profiles.service';
import { UpdateProfileDto } from './dto/update-profile.dto';
import { ApiTags, ApiResponse, ApiOperation } from '@nestjs/swagger';
import { UploadService } from '../upload/upload.service';
import { RequestWithUser } from '../utils/interface';

@Controller('profiles')
@ApiTags('profiles')
export class ProfilesController {
constructor(
private readonly profilesService: ProfilesService,
private readonly uploadService: UploadService,
) {}
constructor(private readonly profilesService: ProfilesService) {}

@Get()
@ApiOperation({ summary: 'Get profile' })
@ApiResponse({
status: 200,
status: HttpStatus.OK,
description: 'Return the profile data.',
})
@ApiResponse({
status: 401,
status: HttpStatus.UNAUTHORIZED,
description: 'Unauthorized.',
})
async findProfile(@Req() req: RequestWithUser) {
const profile = await this.profilesService.findProfile(req.user.uuid);
if (!profile) throw new NotFoundException();
return { statusCode: 200, message: 'Success', data: profile };
async findProfileByUserUuid(@Req() req: RequestWithUser) {
const profile = await this.profilesService.findProfileByUserUuid(
req.user.uuid,
);
return { statusCode: HttpStatus.OK, message: 'Success', data: profile };
}

@Patch()
@UseInterceptors(FileInterceptor('image'))
@ApiOperation({ summary: 'Update profile' })
@ApiResponse({
status: 200,
status: HttpStatus.OK,
description: 'Profile has been successfully updated.',
})
@ApiResponse({
status: 401,
status: HttpStatus.UNAUTHORIZED,
description: 'Unauthorized.',
})
async update(
async updateProfile(
@UploadedFile() image: Express.Multer.File,
@Req() req: RequestWithUser,
@Body(new ValidationPipe({ whitelist: true }))
updateProfileDto: UpdateProfileDto,
) {
if (image) {
updateProfileDto.image = await this.uploadService.uploadFile(image);
}
const profile = await this.profilesService.updateProfile(
req.user.uuid,
updateProfileDto.uuid,
image,
updateProfileDto,
);
if (!profile) throw new NotFoundException();
return { statusCode: 200, message: 'Success', data: profile };
return { statusCode: HttpStatus.OK, message: 'Success', data: profile };
}
}
Loading
Loading