Skip to content

Commit

Permalink
review delet
Browse files Browse the repository at this point in the history
  • Loading branch information
eldpswp99 committed Nov 16, 2023
1 parent a6b7b32 commit cc27c21
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
15 changes: 14 additions & 1 deletion backend/src/review/review.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Body,
Controller,
Delete,
Get,
NotFoundException,
Param,
Expand Down Expand Up @@ -109,7 +110,7 @@ export class ReviewController {

@UseGuards(JwtAccessGuard)
@Get('/:reviewId')
async getReviewDetail(
async removeReview(
@Req() { user }: UserRequest,
@Param('reviewId') reviewId: number,
) {
Expand All @@ -119,6 +120,18 @@ export class ReviewController {
return new ReviewDetailDto(review, review.user);
}

@UseGuards(JwtAccessGuard)
@Delete('/:reviewId')
async getReviewDetail(
@Req() { user }: UserRequest,
@Param('reviewId') reviewId: number,
) {
const review = await this.reviewRepository.findOfReviewId(reviewId);

if (!review) throw new NotFoundException('리뷰를 찾을 수 없습니다.');
return review.softRemove();
}

@UseGuards(JwtAccessGuard)
@Get('/users/:userId')
async getReviewOfUser(
Expand Down
86 changes: 86 additions & 0 deletions backend/src/test/review/delete-review.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { NestExpressApplication } from '@nestjs/platform-express';
import { AppModule } from '../../app.module';
import { Test } from '@nestjs/testing';
import { DataSource } from 'typeorm';
import { appSetting } from '../../main';
import * as supertest from 'supertest';
import { UserEntity } from '../../user/models/user.entity';
import { UserFixture } from '../fixture/user.fixture';
import { HttpStatus } from '@nestjs/common';
import { RestaurantEntity } from '../../review/models/restaurant.entity';
import { RestaurantFixture } from '../fixture/restaurant.fixture';
import { ReviewEntity } from '../../review/models/review.entity';
import { ReviewFixture } from '../fixture/review.fixture';
import { ImageFixture } from '../fixture/image.fixture';

describe('Get review detail test', () => {
let testServer: NestExpressApplication;
let dataSource: DataSource;
let user: UserEntity;
let accessToken: string;
let restaurant: RestaurantEntity;
let review: ReviewEntity;

beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [AppModule],
}).compile();

testServer = module.createNestApplication<NestExpressApplication>();
dataSource = testServer.get(DataSource);
await dataSource.synchronize(true);
appSetting(testServer);

await testServer.init();
});

beforeEach(async () => {
await dataSource.synchronize(true);

user = await UserFixture.create({
name: 'hi',
username: 'hello',
password: 'world',
});

const { body } = await supertest(testServer.getHttpServer())
.post('/auth/login')
.send({
username: 'hello',
password: 'world',
})
.expect(HttpStatus.CREATED);

accessToken = body.accessToken;

restaurant = await RestaurantFixture.create({});
const image = await ImageFixture.create({});
review = await ReviewFixture.create({
restaurant,
images: [image],
user,
});
});

it('unauthorized', async () => {
await supertest(testServer.getHttpServer())
.delete(`/reviews/${review.id}`)
.expect(HttpStatus.UNAUTHORIZED);
});

it('OK', async () => {
await supertest(testServer.getHttpServer())
.delete(`/reviews/${review.id}`)
.set('Authorization', `Bearer ${accessToken}`)
.expect(HttpStatus.OK);

expect(await ReviewEntity.findOneBy({ id: review.id })).toBeNull();
});

it('리뷰 없으면 404', async () => {
const { body } = await supertest(testServer.getHttpServer())
.delete(`/reviews/999999`)
.set('Authorization', `Bearer ${accessToken}`)
.expect(HttpStatus.NOT_FOUND);
});
});

0 comments on commit cc27c21

Please sign in to comment.