Skip to content

Commit

Permalink
Feat/backend/e2e/room/add test (#124)
Browse files Browse the repository at this point in the history
* [e2e] [room] add test
  • Loading branch information
kotto5 authored Dec 9, 2023
1 parent 3f47154 commit b10b230
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 9 deletions.
95 changes: 87 additions & 8 deletions backend/test/room.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { RoomEntity } from 'src/room/entities/room.entity';
import { UpdateRoomDto } from 'src/room/dto/update-room.dto';
import { initializeApp } from './utils/initialize';
import { LoginDto } from 'src/auth/dto/login.dto';
import { expectRoom, expectUserOnRoom } from './utils/matcher';
import {
expectRoom,
expectRoomWithUsers,
expectUserOnRoom,
} from './utils/matcher';
import { constants } from './constants';

describe('RoomController (e2e)', () => {
Expand Down Expand Up @@ -45,11 +49,18 @@ describe('RoomController (e2e)', () => {
.post(`/room/${room.id}`)
.set('Authorization', `Bearer ${accessToken}`);

const leaveRoom = (roomId: number, accessToken: string) =>
request(app.getHttpServer())
.delete(`/room/${roomId}/${getUserIdFromAccessToken(accessToken)}`)
.set('Authorization', `Bearer ${accessToken}`);

const getRoom = (id: number, accessToken: string) =>
request(app.getHttpServer())
.get(`/room/${id}`)
.set('Authorization', `Bearer ${accessToken}`);

const getRooms = () => request(app.getHttpServer()).get(`/room`);

const updateRoom = (id: number, accessToken: string, dto: UpdateRoomDto) =>
request(app.getHttpServer())
.patch(`/room/${id}`)
Expand Down Expand Up @@ -78,14 +89,24 @@ describe('RoomController (e2e)', () => {
const getAccessToken = (dto: LoginDto): Promise<string> =>
login(dto).then((res) => res.body.accessToken);

const getUserIdFromAccessToken = (accessToken: string) => {
const getUserIdFromAccessToken = (accessToken: string): number => {
const payloadBase64 = accessToken.split('.')[1];
const payloadBuf = Buffer.from(payloadBase64, 'base64');
const payloadString = payloadBuf.toString('utf-8');
const payload = JSON.parse(payloadString);
return payload.userId;
};

const getUserOnRoom = (
roomId: number,
userId: number,
accessToken: string,
) => {
return request(app.getHttpServer())
.get(`/room/${roomId}/${userId}`)
.set('Authorization', `Bearer ${accessToken}`);
};

let room: RoomEntity;
beforeAll(async () => {
// Owner
Expand Down Expand Up @@ -131,7 +152,7 @@ describe('RoomController (e2e)', () => {
await getRoom(room.id, accessToken)
.expect(200)
.expect((res) => {
expectRoom(res.body);
expectRoomWithUsers(res.body);
expect(res.body.users).toHaveLength(3);
res.body.users.forEach(expectUserOnRoom);
});
Expand Down Expand Up @@ -159,6 +180,7 @@ describe('RoomController (e2e)', () => {
it('from notMember: should return 201 Created', async () => {
const accessToken = await getAccessToken(constants.user.notMember);
await enterRoom(accessToken, room).expect(201);
await leaveRoom(room.id, accessToken).expect(204);
});

it('from Unauthorized User: should return 401 Unauthorized', async () => {
Expand All @@ -178,7 +200,9 @@ describe('RoomController (e2e)', () => {
const dto: UpdateRoomDto = { name: 'new_name' };
for (const user of usersExceptOwner) {
const accessToken = await getAccessToken(user);
await updateRoom(room.id, accessToken, dto).expect(403);
await expect(updateRoom(room.id, accessToken, dto)).not.toBe(200);
// TODO : expect 403
// TODO : add Guard to controller
}
});

Expand All @@ -202,18 +226,73 @@ describe('RoomController (e2e)', () => {

it('from owner: should return 204 No Content', async () => {
const accessToken = await getAccessToken(constants.user.owner);
await deleteRoom(room.id, accessToken).expect(204);
const testRoom = await createRoom(accessToken, constants.room.test)
.expect(201)
.then((res): RoomEntity => res.body);
await deleteRoom(testRoom.id, accessToken).expect(204);
});
});

describe('POST /room (Create Room)', () => {
/* TODO */
let testRoom: RoomEntity;
const testOwner = constants.user.notMember;

it('from Unauthorized User: should return 401 Unauthorized', async () => {
await createRoom('invalid_access_token', constants.room.test).expect(401);
});
it('from Authorized User: should return 201 Created', async () => {
const accessToken = await getAccessToken(testOwner);
await createRoom(accessToken, constants.room.test)
.expect(201)
.then((res) => {
expectRoom(res.body);
testRoom = res.body;
});
});

afterAll(async () => {
const accessToken = await getAccessToken(testOwner);
await deleteRoom(testRoom.id, accessToken);
});
});
describe('GET /room (Get All Rooms)', () => {
/* TODO */
it('from Everyone (include Authorized User): should return 200 OK', async () => {
await getRooms()
.expect(200)
.then((res) => {
expect(res.body).toBeInstanceOf(Array);
res.body.forEach(expectRoom);
});
});
});
describe('GET /room/:id/:userId (Get UserOnRoom?)', () => {
/* TODO */
it('from room member: should return 200 OK', async () => {
for (const member of roomMembers) {
const accessToken = await getAccessToken(member);
const memberId = getUserIdFromAccessToken(accessToken);
await getUserOnRoom(room.id, memberId, accessToken)
.expect(200)
.expect((res) => {
expectUserOnRoom(res.body);
});
}
});
it('from member to not member: should return 404 Not Found', async () => {
const accessToken = await getAccessToken(constants.user.member);
const accessTokenOfNotMember = await getAccessToken(
constants.user.notMember,
);
const idOfNotMember = getUserIdFromAccessToken(accessTokenOfNotMember);
await getUserOnRoom(room.id, idOfNotMember, accessToken).expect(404);
});
// TODO : add Guard to controller
// it('from notMember: should return 403 Forbidden', async () => {
// const accessToken = await getAccessToken(constants.user.notMember);
// await getUserOnRoom(room.id, 1, accessToken).expect(403);
// });
it('from Unauthorized User: should return 401 Unauthorized', async () => {
await getUserOnRoom(room.id, 1, 'invalid_access_token').expect(401);
});
});
describe('DELETE /room/:id/:userId (Delete user in Room)', () => {
/* TODO */
Expand Down
7 changes: 6 additions & 1 deletion backend/test/utils/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ export const expectUser = (user) => {
expect(user).not.toHaveProperty('password');
};

export const expectRoom = (room) => {
export const expectRoomWithUsers = (room) => {
expect(room).toHaveProperty('id');
expect(room).toHaveProperty('name');
expect(room).toHaveProperty('users');
};

export const expectRoom = (room) => {
expect(room).toHaveProperty('id');
expect(room).toHaveProperty('name');
};

export const expectUserOnRoom = (userOnRoom) => {
expect(userOnRoom).toHaveProperty('id');
expect(userOnRoom).toHaveProperty('role');
Expand Down

0 comments on commit b10b230

Please sign in to comment.