Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinav Kumar <[email protected]>
  • Loading branch information
abhinavkrin committed Dec 13, 2024
1 parent 0ed6584 commit df876a0
Showing 1 changed file with 191 additions and 0 deletions.
191 changes: 191 additions & 0 deletions apps/meteor/tests/end-to-end/api/channels.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit df876a0

Please sign in to comment.