From df876a0cd72d305424261197bbbcd98fd4549ba1 Mon Sep 17 00:00:00 2001 From: Abhinav Kumar Date: Fri, 13 Dec 2024 23:28:16 +0530 Subject: [PATCH] added tests Signed-off-by: Abhinav Kumar --- apps/meteor/tests/end-to-end/api/channels.ts | 191 +++++++++++++++++++ 1 file changed, 191 insertions(+) diff --git a/apps/meteor/tests/end-to-end/api/channels.ts b/apps/meteor/tests/end-to-end/api/channels.ts index 75aff5ad770a..e1ddac8e5fb5 100644 --- a/apps/meteor/tests/end-to-end/api/channels.ts +++ b/apps/meteor/tests/end-to-end/api/channels.ts @@ -1,5 +1,6 @@ import type { Credentials } from '@rocket.chat/api-client'; import type { IIntegration, IMessage, IRoom, ITeam, IUser } from '@rocket.chat/core-typings'; +import { Random } from '@rocket.chat/random'; import { expect, assert } from 'chai'; import { after, before, describe, it } from 'mocha'; @@ -1479,6 +1480,196 @@ describe('[Channels]', () => { }); }); + describe('[/channels.membersOrderedByRole]', () => { + let testChannel: IRoom; + let ownerUser: IUser; + let moderatorUser: IUser; + let memberUser1: IUser; + let memberUser2: IUser; + + let ownerCredentials: { 'X-Auth-Token': string; 'X-User-Id': string }; + + before(async () => { + ownerUser = await createUser({ username: Random.id(), roles: ['admin'] }); + ownerCredentials = await login(ownerUser.username, password); + + moderatorUser = await createUser({ username: Random.id() }); + memberUser1 = await createUser({ username: Random.id() }); + memberUser2 = await createUser({ username: Random.id() }); + + // Create a public channel + const roomCreationResponse = await createRoom({ + type: 'c', + name: `channel.membersOrderedByRole.test.${Date.now()}`, + credentials: ownerCredentials, + }); + testChannel = roomCreationResponse.body.channel; + + await request + .post(api('channels.invite')) + .set(ownerCredentials) + .send({ + roomId: testChannel._id, + userId: moderatorUser._id, + }) + .expect(200); + + await request + .post(api('channels.invite')) + .set(ownerCredentials) + .send({ + roomId: testChannel._id, + userId: memberUser1._id, + }) + .expect(200); + + await request + .post(api('channels.invite')) + .set(ownerCredentials) + .send({ + roomId: testChannel._id, + userId: memberUser2._id, + }) + .expect(200); + + await request + .post(api('channels.addModerator')) + .set(ownerCredentials) + .send({ + roomId: testChannel._id, + userId: moderatorUser._id, + }) + .expect(200); + }); + + after(async () => { + await deleteRoom({ type: 'c', roomId: testChannel._id }); + await deleteUser(ownerUser); + await deleteUser(moderatorUser); + await deleteUser(memberUser1); + await deleteUser(memberUser2); + }); + + it('should return a list of members ordered by owner, moderator, then members by default', async () => { + const response = await request + .get(api('channels.membersOrderedByRole')) + .set(credentials) + .query({ + roomId: testChannel._id, + }) + .expect('Content-Type', 'application/json') + .expect(200); + + expect(response.body).to.have.property('success', true); + expect(response.body.members).to.be.an('array'); + + // We expect Owner first, then Moderator, then Members + // The endpoint defaults to rolesInOrder = ["owner", "moderator"] + const [first, second, ...rest] = response.body.members; + expect(first.username).to.equal(ownerUser.username); + expect(second.username).to.equal(moderatorUser.username); + + const memberUsernames = rest.map((m: any) => m.username); + expect(memberUsernames).to.include(memberUser1.username); + expect(memberUsernames).to.include(memberUser2.username); + + expect(response.body).to.have.property('total'); + expect(response.body.total).to.be.gte(4); + }); + + it('should allow custom role order', async () => { + // Switch role order: moderator, owner + // This should display moderator first, then owner, then members + const response = await request + .get(api('channels.membersOrderedByRole')) + .set(credentials) + .query({ + roomId: testChannel._id, + rolesOrder: ['moderator', 'owner'], + }) + .expect('Content-Type', 'application/json') + .expect(200); + + expect(response.body).to.have.property('success', true); + const [first, second, ...rest] = response.body.members; + expect(first.username).to.equal(moderatorUser.username); // now moderator first + expect(second.username).to.equal(ownerUser.username); // owner second + expect(rest.map((m: any) => m.username)).to.include(memberUser1.username); + expect(rest.map((m: any) => m.username)).to.include(memberUser2.username); + }); + + it('should support pagination', async () => { + const response = await request + .get(api('channels.membersOrderedByRole')) + .set(credentials) + .query({ + roomId: testChannel._id, + count: 2, + offset: 0, + }) + .expect('Content-Type', 'application/json') + .expect(200); + + expect(response.body).to.have.property('success', true); + expect(response.body.members).to.have.lengthOf(2); + expect(response.body.total).to.be.gte(4); + }); + + it('should return matched members when using filter param', async () => { + const response = await request + .get(api(`channels.membersOrderedByRole`)) + .set(credentials) + .query({ + roomId: testChannel._id, + filter: memberUser1.username, + }) + .expect('Content-Type', 'application/json') + .expect(200); + + expect(response.body).to.have.property('success', true); + expect(response.body.members).to.have.lengthOf(1); + expect(response.body.members[0]).have.property('username', memberUser1.username); + }); + + it('should return empty list if no matches (e.g., filter by status that no one has)', async () => { + const response = await request + .get(api(`channels.membersOrderedByRole`)) + .set(credentials) + .query({ + 'roomId': testChannel._id, + 'status[]': 'SomeRandomStatus', + }) + .expect('Content-Type', 'application/json') + .expect(200); + + expect(response.body).to.have.property('success', true); + expect(response.body.members).to.be.an.empty('array'); + }); + + it('should support custom sorting by username descending', async () => { + const response = await request + .get(api('channels.membersOrderedByRole')) + .set(credentials) + .query({ + roomId: testChannel._id, + sort: JSON.stringify({ username: -1 }), + }) + .expect('Content-Type', 'application/json') + .expect(200); + + expect(response.body).to.have.property('success', true); + const usernames = response.body.members.map((m: any) => m.username); + const expected = [ + ownerUser.username, // since owner + moderatorUser.username, // since moderator + ...(memberUser1.username!.localeCompare(memberUser2.username || '') < 0 + ? [memberUser2.username, memberUser1.username] + : [memberUser1.username, memberUser2.username]), + ]; + expect(usernames).to.deep.equal(expected); + }); + }); + describe('/channels.getIntegrations', () => { let integrationCreatedByAnUser: IIntegration; let userCredentials: Credentials;