Skip to content

Commit

Permalink
Merge pull request #219 from litespace-org/moehab/delete-message
Browse files Browse the repository at this point in the history
  • Loading branch information
neuodev authored Dec 23, 2024
2 parents 6046384 + 08f0f10 commit 8e42d4f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
4 changes: 4 additions & 0 deletions services/server/fixtures/wss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export class ClientSocket {
this.client.emit(Wss.ClientEvent.SendMessage, { roomId, ref, text });
}

deleteMessage(msgId: number) {
this.client.emit(Wss.ClientEvent.DeleteMessage, { id: msgId });
}

markMessageAsRead(msgId: number) {
this.client.emit(Wss.ClientEvent.MarkMessageAsRead, { id: msgId });
}
Expand Down
2 changes: 1 addition & 1 deletion services/server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ export const platformConfig = {
* Tutor interview duration in minutes
*/
interviewDuration: 30,
};
}
78 changes: 78 additions & 0 deletions services/server/tests/wss/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,84 @@ describe("wss message test suite", () => {
});
});


describe("deleting messages", () => {
it("should return not found if the message doesn't exist.", async () => {
const tutorApi = await Api.forTutor();
const tutor = await tutorApi.findCurrentUser()
const tutorSocket = new ClientSocket(tutor.token);

tutorSocket.deleteMessage(123);
const res = await tutorSocket.wait(Wss.ServerEvent.Revert);

expect(res.code).to.eq(Wss.RevertErrorCode.MessageNotFound);
});

it("should return not found if the message is already deleted.", async () => {
const tutorApi = await Api.forTutor();
const tutor = await tutorApi.findCurrentUser()
const tutorSocket = new ClientSocket(tutor.token);

const roomId = await rooms.create([tutor.user.id]);
tutorSocket.sendMessage(roomId, 1, "Lesson will start soon.");
// wait for message to be saved in db
const msg = await tutorSocket.wait(Wss.ServerEvent.RoomMessage);

// first deletion
tutorSocket.deleteMessage(msg.id);
await tutorSocket.wait(Wss.ServerEvent.RoomMessageDeleted);

// second deletion
tutorSocket.deleteMessage(msg.id);
const res = await tutorSocket.wait(Wss.ServerEvent.Revert);

expect(res.code).to.eq(Wss.RevertErrorCode.MessageNotFound);
});

it("should return forbidden if the user is not the owner.", async () => {
const tutorApi = await Api.forTutor();
const tutor = await tutorApi.findCurrentUser()
const tutorSocket = new ClientSocket(tutor.token);

const roomId = await rooms.create([tutor.user.id]);
tutorSocket.sendMessage(roomId, 1, "Lesson will start soon.");
// wait for message to be saved in db
const msg = await tutorSocket.wait(Wss.ServerEvent.RoomMessage);

const secTutorApi = await Api.forTutor();
const secTutor = await secTutorApi.findCurrentUser()
const secTutorSocket = new ClientSocket(secTutor.token);

secTutorSocket.deleteMessage(msg.id);
const res = await secTutorSocket.wait(Wss.ServerEvent.Revert);

expect(res.code).to.eq(Wss.RevertErrorCode.NotOwner);
});

it("should successfully delete the message (mark as deleted).", async () => {
const tutorApi = await Api.forTutor();
const tutor = await tutorApi.findCurrentUser()
const tutorSocket = new ClientSocket(tutor.token);

const roomId = await rooms.create([tutor.user.id]);
tutorSocket.sendMessage(roomId, 1, "Lesson will start soon.");
// wait for message to be saved in db
const msg = await tutorSocket.wait(Wss.ServerEvent.RoomMessage);

tutorSocket.deleteMessage(msg.id);
// wait for message to be saved in db
const res = await tutorSocket.wait(Wss.ServerEvent.RoomMessageDeleted);

const found = await messages.findById(res.messageId);
expect(found).to.not.eq(null);
expect(found?.id).to.eq(msg.id);
expect(found?.deleted).to.eq(true);

const list = await messages.findRoomMessages({ room: res.roomId })
expect(list.total).to.eq(0);
});
});

describe("reading messages", () => {
it("should throw not found error if the message doesn't exist.", async () => {
const studentApi = await Api.forStudent();
Expand Down

0 comments on commit 8e42d4f

Please sign in to comment.