Skip to content

Commit

Permalink
test: 리뷰반영 #382
Browse files Browse the repository at this point in the history
  • Loading branch information
HyoJongPark committed Feb 6, 2024
1 parent 4f498ca commit b53e776
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 67 deletions.
86 changes: 77 additions & 9 deletions backend/test/diaries/dairies.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ describe('Dairies Controller (e2e)', () => {
} as User;

beforeAll(async () => {
await redis.flushall();

const module: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
Expand Down Expand Up @@ -74,11 +76,11 @@ describe('Dairies Controller (e2e)', () => {
});

beforeEach(async () => {
await redis.flushall();
await queryRunner.startTransaction();
});

afterEach(async () => {
await redis.flushall();
await queryRunner.rollbackTransaction();
});

Expand All @@ -101,6 +103,47 @@ describe('Dairies Controller (e2e)', () => {
//then
expect(response.status).toEqual(201);
});

it('request에 필요 값이 없다면, 400에러 반환', async () => {
//given
const mockDiary = {};
await usersRepository.save(mockUser);

//when
const response = await request(app.getHttpServer()).post('/diaries').send(mockDiary);

//then
expect(response.status).toEqual(400);
expect(response.body.message).toHaveLength(5);
expect(response.body.message).toContain('title should not be empty');
expect(response.body.message).toContain('content should not be empty');
expect(response.body.message).toContain('emotion should not be empty');
expect(response.body.message).toContain(
'status must be one of the following values: private, public',
);
expect(response.body.message).toContain('status should not be empty');
});

it('유효하지 않은 status 값으로 요청 시, 400에러 반환', async () => {
//given
const mockDiary = {
title: '일기 제목',
content: '일기 내용',
emotion: '🐶',
status: 'wrong status',
};
await usersRepository.save(mockUser);

//when
const response = await request(app.getHttpServer()).post('/diaries').send(mockDiary);

//then
expect(response.status).toEqual(400);
expect(response.body.message).toHaveLength(1);
expect(response.body.message).toContain(
'status must be one of the following values: private, public',
);
});
});

describe('/diaries/friends (GET)', () => {
Expand All @@ -118,18 +161,11 @@ describe('Dairies Controller (e2e)', () => {
} as Friend;

beforeEach(async () => {
await redis.flushall();
await queryRunner.startTransaction();

await usersRepository.save(mockUser);
await usersRepository.save(mockFriend);
await friendsRepository.save(mockFriendRelation);
});

afterEach(async () => {
await queryRunner.rollbackTransaction();
});

it('일기 존재 시 일기 상세 정보 반환', async () => {
//given
const mockDiary = {
Expand Down Expand Up @@ -244,6 +280,39 @@ describe('Dairies Controller (e2e)', () => {
//then
expect(response.status).toEqual(400);
});

it('상대의 private 일기에 접근하면, 403에러 발생', async () => {
//given
const anotherUser = {
id: 2,
email: '[email protected]',
nickname: 'test',
socialId: 'test123',
socialType: SocialType.NAVER,
profileImage: 'testImage',
} as User;
const mockDiary = {
title: '일기 제목',
content: '일기 내용',
emotion: '🐶',
status: DiaryStatus.PRIVATE,
summary: '요약',
mood: MoodDegree.BAD,
author: anotherUser,
} as Diary;

await usersRepository.save(anotherUser);
await diariesRepository.save(mockDiary);

const diaryId = mockDiary.id;

//when
const response = await request(app.getHttpServer()).get(`/diaries/${diaryId}`);

//then
expect(response.status).toEqual(403);
expect(response.body.message).toEqual('권한이 없는 사용자입니다.');
});
});

describe('/diaries/:id (PATCH)', () => {
Expand Down Expand Up @@ -359,7 +428,6 @@ describe('Dairies Controller (e2e)', () => {
await diariesRepository.save(mockDiary);
});

//TODO
it('유효하지 않은 일자 타입으로 요청이 오면 400에러 발생', async () => {
//given
const dto = {
Expand Down
49 changes: 0 additions & 49 deletions backend/test/tags/app.e2e-spec.ts

This file was deleted.

18 changes: 9 additions & 9 deletions backend/test/tags/tags.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ describe('TagsController (e2e)', () => {
await app.close();
});

describe('/search/:keyword (GET)', () => {
beforeEach(async () => {
await redis.flushall();
await queryRunner.startTransaction();
});
beforeEach(async () => {
await queryRunner.startTransaction();
});

afterEach(async () => {
await queryRunner.rollbackTransaction();
});
afterEach(async () => {
await redis.flushall();
await queryRunner.rollbackTransaction();
});

describe('/search/:keyword (GET)', () => {
it('일치하는 키워드가 없으면 빈 문자열 리스트 반환', async () => {
//given
const url = `/tags/search/${encodeURIComponent('안녕')}`;
Expand All @@ -70,7 +70,7 @@ describe('TagsController (e2e)', () => {

//then
expect(response.status).toEqual(200);
expect(body).toHaveLength(0);
expect(body.keywords).toHaveLength(0);
});

it('일치하는 키워드가 있으면 모든 유사 문자열 리스트 반환', async () => {
Expand Down

0 comments on commit b53e776

Please sign in to comment.