Skip to content

Commit

Permalink
[backend] implement to check online status api (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
kotto5 authored Jan 30, 2024
1 parent f2aa2fa commit 606f420
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
7 changes: 7 additions & 0 deletions backend/src/chat/chat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ export class ChatController {
) {
return this.chatService.findConversation(userId, user);
}

@Get(':userId/online')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
isUserOnline(@Param('userId', ParseIntPipe) userId: number) {
return this.chatService.isOnline(userId);
}
}
2 changes: 2 additions & 0 deletions backend/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,6 @@ export class ChatService {
},
});
}

isOnline = (userId: number) => ({ isOnline: this.clients.has(userId) });
}
57 changes: 52 additions & 5 deletions backend/test/chat-gateway.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Socket, io } from 'socket.io-client';
import { AppModule } from 'src/app.module';
import { MessageEntity } from 'src/chat/entities/message.entity';
import { constants } from './constants';
import { TestApp } from './utils/app';
import { TestApp, UserEntityWithAccessToken } from './utils/app';

async function createNestApp(): Promise<INestApplication> {
const moduleFixture: TestingModule = await Test.createTestingModule({
Expand Down Expand Up @@ -32,6 +32,10 @@ describe('ChatGateway and ChatController (e2e)', () => {
bannedUser1,
mutedUser1;
const waitTime = 500;
type UserAndSocket = {
user: UserEntityWithAccessToken;
ws: Socket;
};

beforeAll(async () => {
//app = await initializeApp();
Expand Down Expand Up @@ -1050,10 +1054,6 @@ describe('ChatGateway and ChatController (e2e)', () => {
// TODO
});
describe('invite pong game', () => {
type UserAndSocket = {
user: any;
ws: Socket;
};
let userAndSockets: UserAndSocket[];

beforeAll(() => {
Expand All @@ -1064,6 +1064,7 @@ describe('ChatGateway and ChatController (e2e)', () => {
extraHeaders: { cookie: 'token=' + user.accessToken },
}),
}));
return userAndSockets.map((s) => connect(s.ws));
});
afterAll(() => {
userAndSockets.map((userAndSocket) => {
Expand Down Expand Up @@ -1425,4 +1426,50 @@ describe('ChatGateway and ChatController (e2e)', () => {
});
});
});
describe('online status', () => {
let userAndSockets: UserAndSocket[];
beforeAll(async () => {
ws1.close();
ws2.close();
ws3.close();
ws4.close();
ws5.close();
ws6.close();
ws7.close();
const users = [user1, user2];
userAndSockets = await users.map((user) => {
return {
user,
ws: io('ws://localhost:3000/chat', {
extraHeaders: { cookie: 'token=' + user.accessToken },
}),
};
});
userAndSockets[1].ws.close();
await connect(userAndSockets[0].ws);
return new Promise<void>((resolve) => setTimeout(resolve, waitTime));
});
afterAll(() => {
userAndSockets.map((userAndSocket) => {
userAndSocket.ws.close();
});
});

it('connected user should be online', async () => {
const u = userAndSockets[0];
const res = await app.isOnline(u.user.id, u.user.accessToken).expect(200);
const body = res.body;
expect(body.isOnline).toEqual(true);
});
it('disconnected user should be offline', async () => {
const u = userAndSockets[1];
const res = await app.isOnline(u.user.id, u.user.accessToken).expect(200);
const body = res.body;
expect(body.isOnline).toEqual(false);
});
it('check online status with invalid access token should be unauthorized', async () => {
const u = userAndSockets[0];
await app.isOnline(u.user.id, '').expect(401);
});
});
});
5 changes: 5 additions & 0 deletions backend/test/utils/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ export class TestApp {
user.accessToken = res2.body.accessToken;
return user;
};

isOnline = (userId: number, accessToken: string) =>
request(this.app.getHttpServer())
.get(`/chat/${userId}/online`)
.set('Authorization', `Bearer ${accessToken}`);
}

export type UserEntityWithAccessToken = UserEntity & { accessToken: string };

0 comments on commit 606f420

Please sign in to comment.