Skip to content

Commit

Permalink
[backend] Add API GET /room/:id/bans (#188)
Browse files Browse the repository at this point in the history
[backend]
* Add test for GET /room/:id/bans
* Add API GET /room/:id/bans

---------

Co-authored-by: Shun Usami <[email protected]>
  • Loading branch information
lim396 and usatie authored Jan 4, 2024
1 parent b3b2643 commit 55875da
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 3 deletions.
2 changes: 2 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import { HistoryModule } from './history/history.module';
import { PrismaModule } from './prisma/prisma.module';
import { RoomModule } from './room/room.module';
import { UserModule } from './user/user.module';
import { BanModule } from './room/ban/ban.module';

@Module({
imports: [
UserModule,
PrismaModule,
AuthModule,
BanModule,
RoomModule,
EventsModule,
ChatModule,
Expand Down
7 changes: 7 additions & 0 deletions backend/src/room/ban/ban.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Controller,
Get,
Delete,
Param,
ParseIntPipe,
Expand All @@ -17,6 +18,12 @@ import { BanService } from './ban.service';
export class BanController {
constructor(private readonly banService: BanService) {}

@Get()
@UseGuards(AdminGuard)
findAll(@Param('roomId', ParseIntPipe) roomId: number) {
return this.banService.findAll(roomId);
}

@Put(':userId')
@UseGuards(AdminGuard)
create(
Expand Down
19 changes: 19 additions & 0 deletions backend/src/room/ban/ban.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ export class BanService {
return { message: 'Ban user successfully' };
}

async findAll(roomId: number) {
const tmp = await this.prisma.banUserOnRoom.findMany({
where: {
roomId: roomId,
},
include: {
user: {
select: {
id: true,
name: true,
avatarURL: true,
},
},
},
});
const users = tmp.map((item) => item.user);
return users;
}

async remove(roomId: number, userId: number) {
await this.prisma.$transaction(async (prisma) => {
const user = await prisma.banUserOnRoom.findUnique({
Expand Down
3 changes: 1 addition & 2 deletions backend/src/room/room.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Module } from '@nestjs/common';
import { PrismaModule } from 'src/prisma/prisma.module';
import { BanModule } from './ban/ban.module';
import { RoomController } from './room.controller';
import { RoomService } from './room.service';

@Module({
controllers: [RoomController],
providers: [RoomService],
imports: [PrismaModule, BanModule],
imports: [PrismaModule],
})
export class RoomModule {}
181 changes: 180 additions & 1 deletion backend/test/room.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import supertest from 'supertest';
import { constants } from './constants';
import { TestApp, UserEntityWithAccessToken } from './utils/app';
import { initializeApp } from './utils/initialize';
import { expectRoom } from './utils/matcher';
import { expectPublicUser, expectRoom } from './utils/matcher';

describe('RoomController (e2e)', () => {
let app: TestApp;
Expand Down Expand Up @@ -1678,6 +1678,185 @@ describe('RoomController (e2e)', () => {
});
});

describe('GET /room/:id/bans (Get banned users)', () => {
describe('Owner', () => {
let _publicRoom: RoomEntity;
let bannedUser: UserEntityWithAccessToken;
beforeAll(async () => {
bannedUser = await app.createAndLoginUser({
name: 'BANNED USER',
email: '[email protected]',
password: '12345678',
});
_publicRoom = await setupRoom(constants.room.publicRoom);
// await app.banUser(_publicRoom.id, bannedUser.id, owner.accessToken);
});
afterAll(async () => {
await app.deleteRoom(_publicRoom.id, owner.accessToken).expect(204);
await app.deleteUser(bannedUser.id, bannedUser.accessToken).expect(204);
});
describe('No banned users in the room', () => {
it('should get empty array (200 OK)', async () => {
const res = await app
.getBannedUsers(_publicRoom.id, owner.accessToken)
.expect(200);
expect(res.body).toBeInstanceOf(Array);
expect(res.body).toHaveLength(0);
});
});
it('Ban user', async () => {
await app
.banUser(_publicRoom.id, bannedUser.id, owner.accessToken)
.expect(200);
});
describe('Banned users in the room', () => {
it('should get banned users (200 OK)', async () => {
const res = await app
.getBannedUsers(_publicRoom.id, owner.accessToken)
.expect(200);
expect(res.body).toBeInstanceOf(Array);
res.body.forEach(expectPublicUser);
const {
/* eslint-disable */
email,
twoFactorEnabled,
accessToken,
/* eslint-enable */
...bannedUserWithoutAccessToken
} = bannedUser;

expect(res.body).toContainEqual(bannedUserWithoutAccessToken);
});
});
});
describe('Admin', () => {
let _publicRoom: RoomEntity;
let bannedUser: UserEntityWithAccessToken;
beforeAll(async () => {
bannedUser = await app.createAndLoginUser({
name: 'BANNED USER',
email: '[email protected]',
password: '12345678',
});
_publicRoom = await setupRoom(constants.room.publicRoom);
// await app.banUser(_publicRoom.id, bannedUser.id, owner.accessToken);
});
afterAll(async () => {
await app.deleteRoom(_publicRoom.id, owner.accessToken).expect(204);
await app.deleteUser(bannedUser.id, bannedUser.accessToken).expect(204);
});
describe('No banned users in the room', () => {
it('should get empty array (200 OK)', async () => {
const res = await app
.getBannedUsers(_publicRoom.id, admin.accessToken)
.expect(200);
expect(res.body).toBeInstanceOf(Array);
expect(res.body).toHaveLength(0);
});
});
it('Ban user', async () => {
await app
.banUser(_publicRoom.id, bannedUser.id, owner.accessToken)
.expect(200);
});
describe('Banned users in the room', () => {
it('should get banned users (200 OK)', async () => {
const res = await app
.getBannedUsers(_publicRoom.id, admin.accessToken)
.expect(200);
expect(res.body).toBeInstanceOf(Array);
res.body.forEach(expectPublicUser);
const {
/* eslint-disable */
email,
twoFactorEnabled,
accessToken,
/* eslint-enable */
...bannedUserWithoutAccessToken
} = bannedUser;

expect(res.body).toContainEqual(bannedUserWithoutAccessToken);
});
});
});
describe('Member', () => {
let _publicRoom: RoomEntity;
let bannedUser: UserEntityWithAccessToken;
beforeAll(async () => {
bannedUser = await app.createAndLoginUser({
name: 'BANNED USER',
email: '[email protected]',
password: '12345678',
});
_publicRoom = await setupRoom(constants.room.publicRoom);
// await app.banUser(_publicRoom.id, bannedUser.id, owner.accessToken);
});
afterAll(async () => {
await app.deleteRoom(_publicRoom.id, owner.accessToken).expect(204);
await app.deleteUser(bannedUser.id, bannedUser.accessToken).expect(204);
});
describe('No banned users in the room', () => {
it('should get empty array (200 OK)', async () => {
const res = await app
.getBannedUsers(_publicRoom.id, admin.accessToken)
.expect(200);
expect(res.body).toBeInstanceOf(Array);
expect(res.body).toHaveLength(0);
});
});
it('Ban user', async () => {
await app
.banUser(_publicRoom.id, bannedUser.id, owner.accessToken)
.expect(200);
});
describe('Banned users in the room', () => {
it('should not get banned users (403 Forbidden)', async () => {
await app
.getBannedUsers(publicRoom.id, member.accessToken)
.expect(403);
});
});
});
describe('notMember', () => {
let _publicRoom: RoomEntity;
let bannedUser: UserEntityWithAccessToken;
beforeAll(async () => {
bannedUser = await app.createAndLoginUser({
name: 'BANNED USER',
email: '[email protected]',
password: '12345678',
});
_publicRoom = await setupRoom(constants.room.publicRoom);
// await app.banUser(_publicRoom.id, bannedUser.id, owner.accessToken);
});
afterAll(async () => {
await app.deleteRoom(_publicRoom.id, owner.accessToken).expect(204);
await app.deleteUser(bannedUser.id, bannedUser.accessToken).expect(204);
});
describe('No banned users in the room', () => {
it('should get empty array (200 OK)', async () => {
const res = await app
.getBannedUsers(_publicRoom.id, admin.accessToken)
.expect(200);
expect(res.body).toBeInstanceOf(Array);
expect(res.body).toHaveLength(0);
});
});
it('Ban user', async () => {
await app
.banUser(_publicRoom.id, bannedUser.id, owner.accessToken)
.expect(200);
});
describe('Banned users in the room', () => {
it('should not get banned users (403 Forbidden)', async () => {
await app
.getBannedUsers(publicRoom.id, notMember.accessToken)
.expect(403);
});
});
});
});

describe('DELETE /room/:id/bans/:userId (Unban user)', () => {
describe('Owner', () => {
let _publicRoom: RoomEntity;
Expand Down
5 changes: 5 additions & 0 deletions backend/test/utils/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ export class TestApp {
.delete(`/room/${roomId}/bans/${userId}`)
.set('Authorization', `Bearer ${accessToken}`);

getBannedUsers = (roomId: number, accessToken: string) =>
request(this.app.getHttpServer())
.get(`/room/${roomId}/bans`)
.set('Authorization', `Bearer ${accessToken}`);

muteUser = (
roomId: number,
userId: number,
Expand Down

0 comments on commit 55875da

Please sign in to comment.