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

User 커스텀 데코레이터 #363

Merged
merged 2 commits into from
Nov 18, 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
9 changes: 9 additions & 0 deletions nestjs-BE/server/src/auth/decorators/user.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';

export const User = createParamDecorator(
(data: string, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
const user = request.user;
return data ? user?.[data] : user;
},
);
21 changes: 10 additions & 11 deletions nestjs-BE/server/src/profiles/profiles.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import { Test, TestingModule } from '@nestjs/testing';
import { ProfilesController } from './profiles.controller';
import { ProfilesService } from './profiles.service';
import { RequestWithUser } from '../utils/interface';

describe('ProfilesController', () => {
let controller: ProfilesController;
Expand All @@ -31,12 +30,12 @@ describe('ProfilesController', () => {
});

describe('findProfile', () => {
const requestMock = { user: { uuid: 'test uuid' } } as RequestWithUser;
const userUuidMock = 'user uuid';

it('found profile', async () => {
const testProfile = {
uuid: 'profile test uuid',
userUuid: requestMock.user.uuid,
userUuid: userUuidMock,
image: 'www.test.com/image',
nickname: 'test nickname',
};
Expand All @@ -45,15 +44,15 @@ describe('ProfilesController', () => {
.spyOn(profilesService, 'findProfileByUserUuid')
.mockResolvedValue(testProfile);

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

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

Expand All @@ -62,15 +61,15 @@ describe('ProfilesController', () => {
.spyOn(profilesService, 'findProfileByUserUuid')
.mockRejectedValue(new NotFoundException());

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

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

describe('update', () => {
const imageMock = {} as Express.Multer.File;
const requestMock = { user: { uuid: 'test uuid' } } as RequestWithUser;
const userUuidMock = 'user uuid';
const bodyMock = {
uuid: 'profile test uuid',
nickname: 'test nickname',
Expand All @@ -79,7 +78,7 @@ describe('ProfilesController', () => {
it('updated profile', async () => {
const testProfile = {
uuid: 'profile test uuid',
userUuid: requestMock.user.uuid,
userUuid: userUuidMock,
image: 'www.test.com/image',
nickname: 'test nickname',
};
Expand All @@ -90,7 +89,7 @@ describe('ProfilesController', () => {

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

Expand All @@ -100,7 +99,7 @@ describe('ProfilesController', () => {
data: testProfile,
});
expect(profilesService.updateProfile).toHaveBeenCalledWith(
requestMock.user.uuid,
userUuidMock,
bodyMock.uuid,
imageMock,
bodyMock,
Expand All @@ -114,7 +113,7 @@ describe('ProfilesController', () => {

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

Expand Down
13 changes: 5 additions & 8 deletions nestjs-BE/server/src/profiles/profiles.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import {
Patch,
UseInterceptors,
UploadedFile,
Request as Req,
ValidationPipe,
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 { RequestWithUser } from '../utils/interface';
import { User } from '../auth/decorators/user.decorator';

@Controller('profiles')
@ApiTags('profiles')
Expand All @@ -30,10 +29,8 @@ export class ProfilesController {
status: HttpStatus.UNAUTHORIZED,
description: 'Unauthorized.',
})
async findProfileByUserUuid(@Req() req: RequestWithUser) {
const profile = await this.profilesService.findProfileByUserUuid(
req.user.uuid,
);
async findProfileByUserUuid(@User('uuid') userUuid: string) {
const profile = await this.profilesService.findProfileByUserUuid(userUuid);
return { statusCode: HttpStatus.OK, message: 'Success', data: profile };
}

Expand All @@ -50,12 +47,12 @@ export class ProfilesController {
})
async updateProfile(
@UploadedFile() image: Express.Multer.File,
@Req() req: RequestWithUser,
@User('uuid') userUuid: string,
@Body(new ValidationPipe({ whitelist: true }))
updateProfileDto: UpdateProfileDto,
) {
const profile = await this.profilesService.updateProfile(
req.user.uuid,
userUuid,
updateProfileDto.uuid,
image,
updateProfileDto,
Expand Down
45 changes: 22 additions & 23 deletions nestjs-BE/server/src/spaces/spaces.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { SpacesController } from './spaces.controller';
import { SpacesService } from './spaces.service';
import { UpdateSpaceRequestDto } from './dto/update-space.dto';
import { CreateSpaceRequestDto } from './dto/create-space.dto';
import { RequestWithUser } from '../utils/interface';

describe('SpacesController', () => {
let controller: SpacesController;
Expand Down Expand Up @@ -35,10 +34,10 @@ describe('SpacesController', () => {

it('create created', async () => {
const iconMock = { filename: 'icon' } as Express.Multer.File;
const requestMock = { user: { uuid: 'user uuid' } } as RequestWithUser;
const userUuidMock = 'user uuid';
const profileMock = {
uuid: 'profile uuid',
userUuid: requestMock.user.uuid,
userUuid: userUuidMock,
} as Profile;
const bodyMock = {
name: 'new space name',
Expand All @@ -48,31 +47,31 @@ describe('SpacesController', () => {

(spacesService.createSpace as jest.Mock).mockResolvedValue(spaceMock);

const response = controller.createSpace(iconMock, bodyMock, requestMock);
const response = controller.createSpace(iconMock, bodyMock, userUuidMock);

await expect(response).resolves.toEqual({
statusCode: HttpStatus.CREATED,
message: 'Created',
data: spaceMock,
});
expect(spacesService.createSpace).toHaveBeenCalledWith(
requestMock.user.uuid,
userUuidMock,
bodyMock.profileUuid,
iconMock,
bodyMock,
);
});

it('create profile uuid needed', async () => {
const requestMock = { user: { uuid: 'user uuid' } } as RequestWithUser;
const userUuidMock = 'user uuid';
const bodyMock = {
name: 'new space name',
} as CreateSpaceRequestDto;

const response = controller.createSpace(
undefined as Express.Multer.File,
bodyMock,
requestMock,
userUuidMock,
);

await expect(response).rejects.toThrow(BadRequestException);
Expand All @@ -82,14 +81,14 @@ describe('SpacesController', () => {
it('findOne found space', async () => {
const profileUuid = 'profile uuid';
const spaceMock = { uuid: 'space uuid' } as Space;
const requestMock = { user: { uuid: 'user uuid' } } as RequestWithUser;
const userUuidMock = 'user uuid';

(spacesService.findSpace as jest.Mock).mockResolvedValue(spaceMock);

const response = controller.findSpace(
spaceMock.uuid,
profileUuid,
requestMock,
userUuidMock,
);

await expect(response).resolves.toEqual({
Expand All @@ -101,12 +100,12 @@ describe('SpacesController', () => {

it('findOne profile_uuid missing', async () => {
const spaceMock = { uuid: 'space uuid' } as Space;
const requestMock = { user: { uuid: 'user uuid' } } as RequestWithUser;
const userUuidMock = 'user uuid';

const response = controller.findSpace(
spaceMock.uuid,
undefined,
requestMock,
userUuidMock,
);

await expect(response).rejects.toThrow(BadRequestException);
Expand All @@ -117,7 +116,7 @@ describe('SpacesController', () => {
const profileUuid = 'profile uuid';
const iconMock = { filename: 'icon' } as Express.Multer.File;
const bodyMock = { name: 'new space name' } as UpdateSpaceRequestDto;
const requestMock = { user: { uuid: 'user uuid' } } as RequestWithUser;
const userUuidMock = 'user uuid';
const spaceMock = { uuid: spaceUuid } as Space;

(spacesService.updateSpace as jest.Mock).mockResolvedValue(spaceMock);
Expand All @@ -127,7 +126,7 @@ describe('SpacesController', () => {
spaceUuid,
profileUuid,
bodyMock,
requestMock,
userUuidMock,
);

await expect(response).resolves.toEqual({
Expand All @@ -141,14 +140,14 @@ describe('SpacesController', () => {
const iconMock = { filename: 'icon' } as Express.Multer.File;
const spaceUuid = 'space uuid';
const bodyMock = { name: 'new space name' } as UpdateSpaceRequestDto;
const requestMock = { user: { uuid: 'user uuid' } } as RequestWithUser;
const userUuidMock = 'user uuid';

const response = controller.updateSpace(
iconMock,
spaceUuid,
undefined,
bodyMock,
requestMock,
userUuidMock,
);

await expect(response).rejects.toThrow(BadRequestException);
Expand All @@ -158,14 +157,14 @@ describe('SpacesController', () => {
it('joinSpace', async () => {
const spaceMock = { uuid: 'space uuid' };
const bodyMock = { profileUuid: 'profile uuid' };
const requestMock = { user: { uuid: 'user uuid' } } as RequestWithUser;
const userUuidMock = 'user uuid';

(spacesService.joinSpace as jest.Mock).mockResolvedValue(spaceMock);

const response = controller.joinSpace(
spaceMock.uuid,
bodyMock,
requestMock,
userUuidMock,
);

await expect(response).resolves.toEqual({
Expand All @@ -178,14 +177,14 @@ describe('SpacesController', () => {
it('leaveSpace', async () => {
const spaceMock = { uuid: 'space uuid' };
const profileMock = { uuid: 'profile uuid' };
const requestMock = { user: { uuid: 'user uuid' } } as RequestWithUser;
const userUuidMock = 'user uuid';

(spacesService.leaveSpace as jest.Mock).mockResolvedValue(undefined);

const response = controller.leaveSpace(
spaceMock.uuid,
profileMock.uuid,
requestMock,
userUuidMock,
);

await expect(response).resolves.toEqual({
Expand All @@ -197,7 +196,7 @@ describe('SpacesController', () => {
it('findProfilesInSpace', async () => {
const spaceMock = { uuid: 'space uuid' };
const profileMock = { uuid: 'profile uuid' };
const requestMock = { user: { uuid: 'user uuid' } } as RequestWithUser;
const userUuidMock = 'user uuid';
const profilesMock = [];

(spacesService.findProfilesInSpace as jest.Mock).mockResolvedValue(
Expand All @@ -207,7 +206,7 @@ describe('SpacesController', () => {
const response = controller.findProfilesInSpace(
spaceMock.uuid,
profileMock.uuid,
requestMock,
userUuidMock,
);

await expect(response).resolves.toEqual({
Expand All @@ -219,14 +218,14 @@ describe('SpacesController', () => {

it('findProfilesInSpace space uuid needed', async () => {
const spaceMock = { uuid: 'space uuid' };
const requestMock = { user: { uuid: 'user uuid' } } as RequestWithUser;
const userUuidMock = 'user uuid';

(spacesService.findProfilesInSpace as jest.Mock).mockResolvedValue([]);

const response = controller.findProfilesInSpace(
spaceMock.uuid,
undefined,
requestMock,
userUuidMock,
);

await expect(response).rejects.toThrow(BadRequestException);
Expand Down
Loading
Loading