diff --git a/apps/meteor/tests/data/api-data.js b/apps/meteor/tests/data/api-data.js index b311af16e764..cab98c41eb15 100644 --- a/apps/meteor/tests/data/api-data.js +++ b/apps/meteor/tests/data/api-data.js @@ -15,7 +15,6 @@ export function wait(cb, time) { export const apiUsername = `api${username}-${Date.now()}`; export const apiEmail = `api${email}-${Date.now()}`; -export const apiPublicChannelName = `api${publicChannelName}-${Date.now()}`; export const apiPrivateChannelName = `api${privateChannelName}-${Date.now()}`; export const apiRoleNameUsers = `api${roleNameUsers}`; @@ -25,7 +24,6 @@ export const apiRoleScopeSubscriptions = `${roleScopeSubscriptions}`; export const apiRoleDescription = `api${roleDescription}`; export const reservedWords = ['admin', 'administrator', 'system', 'user']; -export const channel = {}; export const group = {}; export const message = {}; export const directMessage = {}; diff --git a/apps/meteor/tests/data/channel.ts b/apps/meteor/tests/data/channel.ts index e3a23691b7ba..1cd02fb09022 100644 --- a/apps/meteor/tests/data/channel.ts +++ b/apps/meteor/tests/data/channel.ts @@ -1,2 +1 @@ -export const publicChannelName = `channel-test-${Date.now()}`; export const privateChannelName = `private-channel-test-${Date.now()}`; diff --git a/apps/meteor/tests/data/uploads.helper.ts b/apps/meteor/tests/data/uploads.helper.ts index 29c7a143484c..8eb1e7931965 100644 --- a/apps/meteor/tests/data/uploads.helper.ts +++ b/apps/meteor/tests/data/uploads.helper.ts @@ -7,18 +7,30 @@ import { password } from './user'; import { createUser, login } from './users.helper'; import { imgURL } from './interactions'; import { updateSetting } from './permissions.helper'; -import { createRoom } from './rooms.helper'; +import { createRoom, deleteRoom } from './rooms.helper'; import { createVisitor } from './livechat/rooms'; -export async function testFileUploads(filesEndpoint: 'channels.files' | 'groups.files' | 'im.files', room: { _id: string; name?: string; t: string;}, invalidRoomError = 'error-room-not-found') { +export async function testFileUploads(filesEndpoint: 'channels.files' | 'groups.files' | 'im.files', roomType: 'c' | 'd' | 'p', invalidRoomError = 'error-room-not-found') { + let testRoom: Record; + const propertyMap = { + 'c': 'channel', + 'p': 'group', + 'd': 'room', + }; + before(async function () { await updateSetting('VoIP_Enabled', true); await updateSetting('Message_KeepHistory', true); + + testRoom = (await createRoom({ type: roomType, ...(roomType === 'd' ? { username: 'rocket.cat' } : { name: `channel-files-${Date.now()}` }) } as any)).body[propertyMap[roomType]]; }); after(async function () { - await updateSetting('VoIP_Enabled', false); - await updateSetting('Message_KeepHistory', false); + await Promise.all([ + deleteRoom({ type: 'c', roomId: testRoom._id }), + updateSetting('VoIP_Enabled', false), + updateSetting('Message_KeepHistory', false), + ]); }); const createVoipRoom = async function () { @@ -34,6 +46,7 @@ export async function testFileUploads(filesEndpoint: 'channels.files' | 'groups. username: null, members: null, }); + return roomResponse.body.room; }; @@ -74,7 +87,7 @@ export async function testFileUploads(filesEndpoint: 'channels.files' | 'groups. .get(api(filesEndpoint)) .set(credentials) .query({ - roomId: room._id, + roomId: testRoom._id, }) .expect('Content-Type', 'application/json') .expect(200) @@ -90,7 +103,7 @@ export async function testFileUploads(filesEndpoint: 'channels.files' | 'groups. .get(api(filesEndpoint)) .set(credentials) .query({ - roomId: room._id, + roomId: testRoom._id, count: 5, offset: 0, }) @@ -104,14 +117,14 @@ export async function testFileUploads(filesEndpoint: 'channels.files' | 'groups. }); it('should succeed when searching by roomName', function (done) { - if (!room.name) { + if (!testRoom.name) { this.skip(); } request .get(api(filesEndpoint)) .set(credentials) .query({ - roomName: room.name, + roomName: testRoom.name, }) .expect('Content-Type', 'application/json') .expect(200) @@ -123,14 +136,14 @@ export async function testFileUploads(filesEndpoint: 'channels.files' | 'groups. }); it('should succeed when searching by roomName even requested with count and offset params', function (done) { - if (!room.name) { + if (!testRoom.name) { this.skip(); } request .get(api(filesEndpoint)) .set(credentials) .query({ - roomName: room.name, + roomName: testRoom.name, count: 5, offset: 0, }) @@ -145,7 +158,7 @@ export async function testFileUploads(filesEndpoint: 'channels.files' | 'groups. it('should not return thumbnails', async function () { await request - .post(api(`rooms.upload/${room._id}`)) + .post(api(`rooms.upload/${testRoom._id}`)) .set(credentials) .attach('file', imgURL) .expect('Content-Type', 'application/json') @@ -158,7 +171,7 @@ export async function testFileUploads(filesEndpoint: 'channels.files' | 'groups. .get(api(filesEndpoint)) .set(credentials) .query({ - roomId: room._id, + roomId: testRoom._id, }) .expect('Content-Type', 'application/json') .expect(200) @@ -179,7 +192,7 @@ export async function testFileUploads(filesEndpoint: 'channels.files' | 'groups. let fileId: string; await request - .post(api(`rooms.upload/${room._id}`)) + .post(api(`rooms.upload/${testRoom._id}`)) .set(credentials) .attach('file', imgURL) .expect('Content-Type', 'application/json') @@ -195,7 +208,7 @@ export async function testFileUploads(filesEndpoint: 'channels.files' | 'groups. .post(api('chat.delete')) .set(credentials) .send({ - roomId: room._id, + roomId: testRoom._id, msgId, }) .expect('Content-Type', 'application/json') @@ -205,7 +218,7 @@ export async function testFileUploads(filesEndpoint: 'channels.files' | 'groups. .get(api(filesEndpoint)) .set(credentials) .query({ - roomId: room._id, + roomId: testRoom._id, }) .expect('Content-Type', 'application/json') .expect(200) diff --git a/apps/meteor/tests/end-to-end/api/02-channels.js b/apps/meteor/tests/end-to-end/api/02-channels.js index f0f61aa5661e..5291b3621b43 100644 --- a/apps/meteor/tests/end-to-end/api/02-channels.js +++ b/apps/meteor/tests/end-to-end/api/02-channels.js @@ -1,11 +1,11 @@ import { expect } from 'chai'; import { after, before, describe, it } from 'mocha'; -import { getCredentials, api, request, credentials, apiPublicChannelName, channel, reservedWords } from '../../data/api-data.js'; +import { getCredentials, api, request, credentials, reservedWords } from '../../data/api-data.js'; import { CI_MAX_ROOMS_PER_GUEST as maxRoomsPerGuest } from '../../data/constants'; import { createIntegration, removeIntegration } from '../../data/integration.helper'; import { updatePermission, updateSetting } from '../../data/permissions.helper'; -import { createRoom } from '../../data/rooms.helper'; +import { createRoom, deleteRoom } from '../../data/rooms.helper'; import { testFileUploads } from '../../data/uploads.helper'; import { adminUsername, password } from '../../data/user'; import { createUser, login, deleteUser } from '../../data/users.helper'; @@ -24,7 +24,11 @@ function getRoomInfo(roomId) { }); } +const channel = {}; + describe('[Channels]', function () { + const apiPublicChannelName = `api-channel-test-${Date.now()}`; + this.retries(0); before((done) => getCredentials(done)); @@ -50,6 +54,498 @@ describe('[Channels]', function () { .end(done); }); + after(async () => { + await deleteRoom({ type: 'c', roomId: channel._id }); + }); + + it('/channels.invite', async () => { + const roomInfo = await getRoomInfo(channel._id); + + return request + .post(api('channels.invite')) + .set(credentials) + .send({ + roomId: channel._id, + userId: 'rocket.cat', + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.nested.property('channel._id'); + expect(res.body).to.have.nested.property('channel.name', apiPublicChannelName); + expect(res.body).to.have.nested.property('channel.t', 'c'); + expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs + 1); + }); + }); + + it('/channels.addModerator', (done) => { + request + .post(api('channels.addModerator')) + .set(credentials) + .send({ + roomId: channel._id, + userId: 'rocket.cat', + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + + it('/channels.addModerator should fail with missing room Id', (done) => { + request + .post(api('channels.addModerator')) + .set(credentials) + .send({ + userId: 'rocket.cat', + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + }) + .end(done); + }); + + it('/channels.addModerator should fail with missing user Id', (done) => { + request + .post(api('channels.addModerator')) + .set(credentials) + .send({ + roomId: channel._id, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + }) + .end(done); + }); + + it('/channels.removeModerator', (done) => { + request + .post(api('channels.removeModerator')) + .set(credentials) + .send({ + roomId: channel._id, + userId: 'rocket.cat', + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + + it('/channels.removeModerator should fail on invalid room id', (done) => { + request + .post(api('channels.removeModerator')) + .set(credentials) + .send({ + userId: 'rocket.cat', + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + }) + .end(done); + }); + + it('/channels.removeModerator should fail on invalid user id', (done) => { + request + .post(api('channels.removeModerator')) + .set(credentials) + .send({ + roomId: channel._id, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + }) + .end(done); + }); + + it('/channels.addOwner', (done) => { + request + .post(api('channels.addOwner')) + .set(credentials) + .send({ + roomId: channel._id, + userId: 'rocket.cat', + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + + it('/channels.removeOwner', (done) => { + request + .post(api('channels.removeOwner')) + .set(credentials) + .send({ + roomId: channel._id, + userId: 'rocket.cat', + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + + it('/channels.kick', async () => { + const roomInfo = await getRoomInfo(channel._id); + + return request + .post(api('channels.kick')) + .set(credentials) + .send({ + roomId: channel._id, + userId: 'rocket.cat', + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.nested.property('channel._id'); + expect(res.body).to.have.nested.property('channel.name', apiPublicChannelName); + expect(res.body).to.have.nested.property('channel.t', 'c'); + expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs + 1); + }); + }); + + it('/channels.invite', async () => { + const roomInfo = await getRoomInfo(channel._id); + + return request + .post(api('channels.invite')) + .set(credentials) + .send({ + roomId: channel._id, + userId: 'rocket.cat', + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.nested.property('channel._id'); + expect(res.body).to.have.nested.property('channel.name', apiPublicChannelName); + expect(res.body).to.have.nested.property('channel.t', 'c'); + expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs + 1); + }); + }); + + it('/channels.addOwner', (done) => { + request + .post(api('channels.addOwner')) + .set(credentials) + .send({ + roomId: channel._id, + userId: 'rocket.cat', + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + + it('/channels.archive', (done) => { + request + .post(api('channels.archive')) + .set(credentials) + .send({ + roomId: channel._id, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + + it('/channels.unarchive', (done) => { + request + .post(api('channels.unarchive')) + .set(credentials) + .send({ + roomId: channel._id, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + + it('/channels.close', (done) => { + request + .post(api('channels.close')) + .set(credentials) + .send({ + roomId: channel._id, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + + it('/channels.close', (done) => { + request + .post(api('channels.close')) + .set(credentials) + .send({ + roomName: apiPublicChannelName, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('error', `The channel, ${apiPublicChannelName}, is already closed to the sender`); + }) + .end(done); + }); + + it('/channels.open', (done) => { + request + .post(api('channels.open')) + .set(credentials) + .send({ + roomId: channel._id, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + + it('/channels.list', (done) => { + request + .get(api('channels.list')) + .set(credentials) + .query({ + roomId: channel._id, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('count'); + expect(res.body).to.have.property('total'); + }) + .end(done); + }); + + it('/channels.list.joined', (done) => { + request + .get(api('channels.list.joined')) + .set(credentials) + .query({ + roomId: channel._id, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('count'); + expect(res.body).to.have.property('total'); + }) + .end(done); + }); + it('/channels.counters', (done) => { + request + .get(api('channels.counters')) + .set(credentials) + .query({ + roomId: channel._id, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('joined', true); + expect(res.body).to.have.property('members'); + expect(res.body).to.have.property('unreads'); + expect(res.body).to.have.property('unreadsFrom'); + expect(res.body).to.have.property('msgs'); + expect(res.body).to.have.property('latest'); + expect(res.body).to.have.property('userMentions'); + }) + .end(done); + }); + + it('/channels.rename', async () => { + const roomInfo = await getRoomInfo(channel._id); + + function failRenameChannel(name) { + it(`should not rename a channel to the reserved name ${name}`, (done) => { + request + .post(api('channels.rename')) + .set(credentials) + .send({ + roomId: channel._id, + name, + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('error', `${name} is already in use :( [error-field-unavailable]`); + }) + .end(done); + }); + } + + reservedWords.forEach((name) => { + failRenameChannel(name); + }); + + return request + .post(api('channels.rename')) + .set(credentials) + .send({ + roomId: channel._id, + name: `EDITED${apiPublicChannelName}`, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.nested.property('channel._id'); + expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); + expect(res.body).to.have.nested.property('channel.t', 'c'); + expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs + 1); + }); + }); + + it('/channels.addAll', (done) => { + request + .post(api('channels.addAll')) + .set(credentials) + .send({ + roomId: channel._id, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.nested.property('channel._id'); + expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); + expect(res.body).to.have.nested.property('channel.t', 'c'); + }) + .end(done); + }); + + it('/channels.addLeader', (done) => { + request + .post(api('channels.addLeader')) + .set(credentials) + .send({ + roomId: channel._id, + userId: 'rocket.cat', + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.a.property('success', true); + }) + .end(done); + }); + it('/channels.removeLeader', (done) => { + request + .post(api('channels.removeLeader')) + .set(credentials) + .send({ + roomId: channel._id, + userId: 'rocket.cat', + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + + it('/channels.setJoinCode', async () => { + const roomInfo = await getRoomInfo(channel._id); + + return request + .post(api('channels.setJoinCode')) + .set(credentials) + .send({ + roomId: channel._id, + joinCode: '123', + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.nested.property('channel._id'); + expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); + expect(res.body).to.have.nested.property('channel.t', 'c'); + expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs); + }); + }); + + it('/channels.setReadOnly', async () => { + const roomInfo = await getRoomInfo(channel._id); + + return request + .post(api('channels.setReadOnly')) + .set(credentials) + .send({ + roomId: channel._id, + readOnly: true, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.nested.property('channel._id'); + expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); + expect(res.body).to.have.nested.property('channel.t', 'c'); + expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs + 1); + }); + }); + it('/channels.leave', async () => { + const roomInfo = await getRoomInfo(channel._id); + + return request + .post(api('channels.leave')) + .set(credentials) + .send({ + roomId: channel._id, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.nested.property('channel._id'); + expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); + expect(res.body).to.have.nested.property('channel.t', 'c'); + expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs + 1); + }); + }); + describe('[/channels.create]', () => { let guestUser; let room; @@ -93,7 +589,7 @@ describe('[Channels]', function () { }), ); } - await Promise.all(promises); + const channelIds = (await Promise.all(promises)).map((r) => r.body.channel).map((channel) => channel._id); request .post(api('channels.create')) @@ -123,17 +619,25 @@ describe('[Channels]', function () { expect(res.body.members).to.have.lengthOf(1); }); }); + + await Promise.all(channelIds.map((id) => deleteRoom({ type: 'c', roomId: id }))); }); }); describe('[/channels.info]', () => { + const testChannelName = `api-channel-test-${Date.now()}`; let testChannel = {}; let channelMessage = {}; + + after(async () => { + await deleteRoom({ type: 'c', roomId: testChannel._id }); + }); + it('creating new channel...', (done) => { request .post(api('channels.create')) .set(credentials) .send({ - name: apiPublicChannelName, + name: testChannelName, }) .expect('Content-Type', 'application/json') .expect(200) @@ -147,7 +651,7 @@ describe('[Channels]', function () { .post(api('channels.create')) .set(credentials) .send({ - name: apiPublicChannelName, + name: testChannelName, }) .expect('Content-Type', 'application/json') .expect(400) @@ -169,7 +673,7 @@ describe('[Channels]', function () { .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', apiPublicChannelName); + expect(res.body).to.have.nested.property('channel.name', testChannelName); expect(res.body).to.have.nested.property('channel.t', 'c'); expect(res.body).to.have.nested.property('channel.msgs', 0); }) @@ -301,9 +805,13 @@ describe('[Channels]', function () { }); describe('[/channels.online]', () => { + const createdChannels = []; + const createdUsers = []; + const createUserAndChannel = async () => { const testUser = await createUser(); const testUserCredentials = await login(testUser.username, password); + createdUsers.push(testUser); await request.post(api('users.setStatus')).set(testUserCredentials).send({ message: '', @@ -317,6 +825,7 @@ describe('[Channels]', function () { type: 'c', members: [testUser.username], }); + createdChannels.push(roomResponse.body.channel); return { testUser, @@ -325,6 +834,13 @@ describe('[Channels]', function () { }; }; + after(async () => { + await Promise.all([ + createdUsers.map((user) => deleteUser(user)), + createdChannels.map((channel) => deleteRoom({ type: 'c', roomId: channel._id })), + ]); + }); + it('should return an error if no query', () => request .get(api('channels.online')) @@ -395,7 +911,7 @@ describe('[Channels]', function () { }); describe('[/channels.files]', async () => { - await testFileUploads('channels.files', channel); + await testFileUploads('channels.files', 'c'); }); describe('[/channels.join]', () => { @@ -403,62 +919,28 @@ describe('[Channels]', function () { let testChannelWithCode; let testUser; let testUserCredentials; - before('Create test user', (done) => { - const username = `user.test.${Date.now()}`; - const email = `${username}@rocket.chat`; - request - .post(api('users.create')) - .set(credentials) - .send({ email, name: username, username, password }) - .end((err, res) => { - testUser = res.body.user; - done(); - }); - }); - before('Login as test user', (done) => { - request - .post(api('login')) - .send({ - user: testUser.username, - password, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - testUserCredentials = {}; - testUserCredentials['X-Auth-Token'] = res.body.data.authToken; - testUserCredentials['X-User-Id'] = res.body.data.userId; - }) - .end(done); - }); - before('Create no code channel', (done) => { - request - .post(api('channels.create')) - .set(testUserCredentials) - .send({ - name: `${apiPublicChannelName}-nojoincode`, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - testChannelNoCode = res.body.channel; - }) - .end(done); + + before('Create test user', async () => { + testUser = await createUser(); + testUserCredentials = await login(testUser.username, password); + testChannelNoCode = (await createRoom({ type: 'c', credentials: testUserCredentials, name: `${apiPublicChannelName}-nojoincode` })) + .body.channel; + testChannelWithCode = ( + await createRoom({ type: 'c', credentials: testUserCredentials, name: `${apiPublicChannelName}-withjoincode` }) + ).body.channel; + await updatePermission('edit-room', ['admin', 'owner', 'moderator']); }); - before('Create code channel', (done) => { - request - .post(api('channels.create')) - .set(testUserCredentials) - .send({ - name: `${apiPublicChannelName}-withjoincode`, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - testChannelWithCode = res.body.channel; - }) - .end(done); + + after(async () => { + await Promise.all([ + deleteRoom({ type: 'c', roomId: testChannelNoCode._id }), + deleteRoom({ type: 'c', roomId: testChannelWithCode._id }), + deleteUser(testUser), + updatePermission('edit-room', ['admin', 'owner', 'moderator']), + updatePermission('join-without-join-code', ['admin', 'bot', 'app']), + ]); }); + before('Set code for channel', (done) => { request .post(api('channels.setJoinCode')) @@ -552,260 +1034,57 @@ describe('[Channels]', function () { request .post(api('channels.join')) .set(credentials) - .send({ - roomId: testChannelWithCode._id, - joinCode: '123', - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('channel._id', testChannelWithCode._id); - }) - .end(done); - }); - }); - - describe('with join-without-join-code permission', () => { - before('set join-without-join-code permission to true', async () => { - await updatePermission('join-without-join-code', ['admin']); - }); - - before('leave channel', (done) => { - request - .post(api('channels.leave')) - .set(credentials) - .send({ - roomId: testChannelWithCode._id, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); - }); - - it('should succeed when joining code-needed channel without join code and with join-without-join-code permission', (done) => { - request - .post(api('channels.join')) - .set(credentials) - .send({ - roomId: testChannelWithCode._id, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('channel._id', testChannelWithCode._id); - }) - .end(done); - }); - }); - }); - }); - - it('/channels.invite', async () => { - const roomInfo = await getRoomInfo(channel._id); - - return request - .post(api('channels.invite')) - .set(credentials) - .send({ - roomId: channel._id, - userId: 'rocket.cat', - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', apiPublicChannelName); - expect(res.body).to.have.nested.property('channel.t', 'c'); - expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs + 1); - }); - }); - - it('/channels.addModerator', (done) => { - request - .post(api('channels.addModerator')) - .set(credentials) - .send({ - roomId: channel._id, - userId: 'rocket.cat', - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); - }); - - it('/channels.addModerator should fail with missing room Id', (done) => { - request - .post(api('channels.addModerator')) - .set(credentials) - .send({ - userId: 'rocket.cat', - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - }) - .end(done); - }); - - it('/channels.addModerator should fail with missing user Id', (done) => { - request - .post(api('channels.addModerator')) - .set(credentials) - .send({ - roomId: channel._id, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - }) - .end(done); - }); - - it('/channels.removeModerator', (done) => { - request - .post(api('channels.removeModerator')) - .set(credentials) - .send({ - roomId: channel._id, - userId: 'rocket.cat', - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); - }); - - it('/channels.removeModerator should fail on invalid room id', (done) => { - request - .post(api('channels.removeModerator')) - .set(credentials) - .send({ - userId: 'rocket.cat', - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - }) - .end(done); - }); - - it('/channels.removeModerator should fail on invalid user id', (done) => { - request - .post(api('channels.removeModerator')) - .set(credentials) - .send({ - roomId: channel._id, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - }) - .end(done); - }); - - it('/channels.addOwner', (done) => { - request - .post(api('channels.addOwner')) - .set(credentials) - .send({ - roomId: channel._id, - userId: 'rocket.cat', - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); - }); - - it('/channels.removeOwner', (done) => { - request - .post(api('channels.removeOwner')) - .set(credentials) - .send({ - roomId: channel._id, - userId: 'rocket.cat', - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); - }); - - it('/channels.kick', async () => { - const roomInfo = await getRoomInfo(channel._id); - - return request - .post(api('channels.kick')) - .set(credentials) - .send({ - roomId: channel._id, - userId: 'rocket.cat', - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', apiPublicChannelName); - expect(res.body).to.have.nested.property('channel.t', 'c'); - expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs + 1); + .send({ + roomId: testChannelWithCode._id, + joinCode: '123', + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.nested.property('channel._id', testChannelWithCode._id); + }) + .end(done); + }); }); - }); - it('/channels.invite', async () => { - const roomInfo = await getRoomInfo(channel._id); + describe('with join-without-join-code permission', () => { + before('set join-without-join-code permission to true', async () => { + await updatePermission('join-without-join-code', ['admin']); + }); - return request - .post(api('channels.invite')) - .set(credentials) - .send({ - roomId: channel._id, - userId: 'rocket.cat', - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', apiPublicChannelName); - expect(res.body).to.have.nested.property('channel.t', 'c'); - expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs + 1); - }); - }); + before('leave channel', (done) => { + request + .post(api('channels.leave')) + .set(credentials) + .send({ + roomId: testChannelWithCode._id, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); - it('/channels.addOwner', (done) => { - request - .post(api('channels.addOwner')) - .set(credentials) - .send({ - roomId: channel._id, - userId: 'rocket.cat', - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); + it('should succeed when joining code-needed channel without join code and with join-without-join-code permission', (done) => { + request + .post(api('channels.join')) + .set(credentials) + .send({ + roomId: testChannelWithCode._id, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.nested.property('channel._id', testChannelWithCode._id); + }) + .end(done); + }); + }); + }); }); describe('/channels.setDescription', () => { @@ -984,138 +1263,27 @@ describe('[Channels]', function () { }); }); - it('/channels.archive', (done) => { - request - .post(api('channels.archive')) - .set(credentials) - .send({ - roomId: channel._id, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); - }); - - it('/channels.unarchive', (done) => { - request - .post(api('channels.unarchive')) - .set(credentials) - .send({ - roomId: channel._id, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); - }); - - it('/channels.close', (done) => { - request - .post(api('channels.close')) - .set(credentials) - .send({ - roomId: channel._id, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); - }); - - it('/channels.close', (done) => { - request - .post(api('channels.close')) - .set(credentials) - .send({ - roomName: apiPublicChannelName, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('error', `The channel, ${apiPublicChannelName}, is already closed to the sender`); - }) - .end(done); - }); - - it('/channels.open', (done) => { - request - .post(api('channels.open')) - .set(credentials) - .send({ - roomId: channel._id, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); - }); + describe('/channels.members', () => { + let testUser; - it('/channels.list', (done) => { - request - .get(api('channels.list')) - .set(credentials) - .query({ - roomId: channel._id, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.property('count'); - expect(res.body).to.have.property('total'); - }) - .end(done); - }); + before(async () => { + testUser = await createUser(); + await updateSetting('Accounts_SearchFields', 'username, name, bio, nickname'); + await request + .post(api('channels.invite')) + .set(credentials) + .send({ + roomId: channel._id, + userId: testUser._id, + }) + .expect('Content-Type', 'application/json') + .expect(200); + }); - it('/channels.list.joined', (done) => { - request - .get(api('channels.list.joined')) - .set(credentials) - .query({ - roomId: channel._id, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.property('count'); - expect(res.body).to.have.property('total'); - }) - .end(done); - }); - it('/channels.counters', (done) => { - request - .get(api('channels.counters')) - .set(credentials) - .query({ - roomId: channel._id, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.property('joined', true); - expect(res.body).to.have.property('members'); - expect(res.body).to.have.property('unreads'); - expect(res.body).to.have.property('unreadsFrom'); - expect(res.body).to.have.property('msgs'); - expect(res.body).to.have.property('latest'); - expect(res.body).to.have.property('userMentions'); - }) - .end(done); - }); + after(async () => { + await Promise.all([updateSetting('Accounts_SearchFields', 'username, name, bio, nickname'), deleteUser(testUser)]); + }); - describe('/channels.members', () => { it('should return an array of members by channel', (done) => { request .get(api('channels.members')) @@ -1162,75 +1330,34 @@ describe('[Channels]', function () { .set(credentials) .query({ roomId: channel._id, - filter: 'rocket.cat', + filter: testUser.username, }) .expect('Content-Type', 'application/json') .expect(200) .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.property('members').and.to.be.an('array'); - expect(res.body).to.have.property('count'); expect(res.body).to.have.property('count', 1); + expect(res.body.members[0]._id).to.be.equal(testUser._id); + expect(res.body).to.have.property('count'); expect(res.body).to.have.property('total'); - expect(res.body).to.have.property('offset'); - }) - .end(done); - }); - }); - - it('/channels.rename', async () => { - const roomInfo = await getRoomInfo(channel._id); - - function failRenameChannel(name) { - it(`should not rename a channel to the reserved name ${name}`, (done) => { - request - .post(api('channels.rename')) - .set(credentials) - .send({ - roomId: channel._id, - name, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('error', `${name} is already in use :( [error-field-unavailable]`); - }) - .end(done); - }); - } - - reservedWords.forEach((name) => { - failRenameChannel(name); + expect(res.body).to.have.property('offset'); + }) + .end(done); }); - - return request - .post(api('channels.rename')) - .set(credentials) - .send({ - roomId: channel._id, - name: `EDITED${apiPublicChannelName}`, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); - expect(res.body).to.have.nested.property('channel.t', 'c'); - expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs + 1); - }); }); describe('/channels.getIntegrations', () => { let integrationCreatedByAnUser; let userCredentials; let createdChannel; + let user; + before((done) => { createRoom({ name: `test-integration-channel-${Date.now()}`, type: 'c' }).end((err, res) => { createdChannel = res.body.channel; createUser().then((createdUser) => { - const user = createdUser; + user = createdUser; login(user.username, password).then((credentials) => { userCredentials = credentials; updatePermission('manage-incoming-integrations', ['user']).then(() => { @@ -1258,8 +1385,14 @@ describe('[Channels]', function () { }); }); - after((done) => { - removeIntegration(integrationCreatedByAnUser._id, 'incoming').then(done); + after(async () => { + await Promise.all([ + deleteRoom({ type: 'c', roomId: createdChannel._id }), + removeIntegration(integrationCreatedByAnUser._id, 'incoming'), + updatePermission('manage-incoming-integrations', ['admin']), + updatePermission('manage-own-incoming-integrations', ['admin']), + deleteUser(user), + ]); }); it('should return the list of integrations of created channel and it should contain the integration created by user when the admin DOES have the permission', (done) => { @@ -1336,57 +1469,14 @@ describe('[Channels]', function () { }); }); - it('/channels.addAll', (done) => { - request - .post(api('channels.addAll')) - .set(credentials) - .send({ - roomId: channel._id, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); - expect(res.body).to.have.nested.property('channel.t', 'c'); - }) - .end(done); - }); + describe('/channels.setCustomFields:', () => { + let withCFChannel; + let withoutCFChannel; - it('/channels.addLeader', (done) => { - request - .post(api('channels.addLeader')) - .set(credentials) - .send({ - roomId: channel._id, - userId: 'rocket.cat', - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.a.property('success', true); - }) - .end(done); - }); - it('/channels.removeLeader', (done) => { - request - .post(api('channels.removeLeader')) - .set(credentials) - .send({ - roomId: channel._id, - userId: 'rocket.cat', - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); - }); + after(async () => { + await deleteRoom({ type: 'c', roomId: withCFChannel._id }); + }); - describe('/channels.setCustomFields:', () => { - let cfchannel; it('create channel with customFields', (done) => { const customFields = { field0: 'value0' }; request @@ -1397,7 +1487,7 @@ describe('[Channels]', function () { customFields, }) .end((err, res) => { - cfchannel = res.body.channel; + withCFChannel = res.body.channel; done(); }); }); @@ -1406,7 +1496,7 @@ describe('[Channels]', function () { .get(api('channels.info')) .set(credentials) .query({ - roomId: cfchannel._id, + roomId: withCFChannel._id, }) .expect('Content-Type', 'application/json') .expect(200) @@ -1422,7 +1512,7 @@ describe('[Channels]', function () { .post(api('channels.setCustomFields')) .set(credentials) .send({ - roomId: cfchannel._id, + roomId: withCFChannel._id, customFields, }) .expect('Content-Type', 'application/json') @@ -1430,7 +1520,7 @@ describe('[Channels]', function () { .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', cfchannel.name); + expect(res.body).to.have.nested.property('channel.name', withCFChannel.name); expect(res.body).to.have.nested.property('channel.t', 'c'); expect(res.body).to.have.nested.property('channel.customFields.field9', 'value9'); expect(res.body).to.have.not.nested.property('channel.customFields.field0', 'value0'); @@ -1441,7 +1531,7 @@ describe('[Channels]', function () { .get(api('channels.info')) .set(credentials) .query({ - roomId: cfchannel._id, + roomId: withCFChannel._id, }) .expect('Content-Type', 'application/json') .expect(200) @@ -1456,7 +1546,7 @@ describe('[Channels]', function () { .post(api('channels.delete')) .set(credentials) .send({ - roomName: cfchannel.name, + roomName: withCFChannel.name, }) .expect('Content-Type', 'application/json') .expect(200) @@ -1473,7 +1563,7 @@ describe('[Channels]', function () { name: `channel.cf.${Date.now()}`, }) .end((err, res) => { - cfchannel = res.body.channel; + withoutCFChannel = res.body.channel; done(); }); }); @@ -1483,7 +1573,7 @@ describe('[Channels]', function () { .post(api('channels.setCustomFields')) .set(credentials) .send({ - roomId: cfchannel._id, + roomId: withoutCFChannel._id, customFields, }) .expect('Content-Type', 'application/json') @@ -1491,7 +1581,7 @@ describe('[Channels]', function () { .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', cfchannel.name); + expect(res.body).to.have.nested.property('channel.name', withoutCFChannel.name); expect(res.body).to.have.nested.property('channel.t', 'c'); expect(res.body).to.have.nested.property('channel.customFields.field1', 'value1'); }); @@ -1503,7 +1593,7 @@ describe('[Channels]', function () { .post(api('channels.setCustomFields')) .set(credentials) .send({ - roomName: cfchannel.name, + roomName: withoutCFChannel.name, customFields, }) .expect('Content-Type', 'application/json') @@ -1511,7 +1601,7 @@ describe('[Channels]', function () { .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', cfchannel.name); + expect(res.body).to.have.nested.property('channel.name', withoutCFChannel.name); expect(res.body).to.have.nested.property('channel.t', 'c'); expect(res.body).to.have.nested.property('channel.customFields.field2', 'value2'); expect(res.body).to.have.nested.property('channel.customFields.field3', 'value3'); @@ -1526,7 +1616,7 @@ describe('[Channels]', function () { .post(api('channels.setCustomFields')) .set(credentials) .send({ - roomName: cfchannel.name, + roomName: withoutCFChannel.name, customFields, }) .expect('Content-Type', 'application/json') @@ -1534,7 +1624,7 @@ describe('[Channels]', function () { .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', cfchannel.name); + expect(res.body).to.have.nested.property('channel.name', withoutCFChannel.name); expect(res.body).to.have.nested.property('channel.t', 'c'); expect(res.body).to.have.not.nested.property('channel.customFields.field2', 'value2'); expect(res.body).to.have.not.nested.property('channel.customFields.field3', 'value3'); @@ -1549,7 +1639,7 @@ describe('[Channels]', function () { .post(api('channels.setCustomFields')) .set(credentials) .send({ - roomName: cfchannel.name, + roomName: withoutCFChannel.name, customFields, }) .expect('Content-Type', 'application/json') @@ -1564,7 +1654,7 @@ describe('[Channels]', function () { .post(api('channels.delete')) .set(credentials) .send({ - roomName: cfchannel.name, + roomName: withoutCFChannel.name, }) .expect('Content-Type', 'application/json') .expect(200) @@ -1575,57 +1665,26 @@ describe('[Channels]', function () { }); }); - it('/channels.setJoinCode', async () => { - const roomInfo = await getRoomInfo(channel._id); - - return request - .post(api('channels.setJoinCode')) - .set(credentials) - .send({ - roomId: channel._id, - joinCode: '123', - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); - expect(res.body).to.have.nested.property('channel.t', 'c'); - expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs); - }); - }); + describe('/channels.setDefault', () => { + let testChannel; + const name = `setDefault-${Date.now()}`; - it('/channels.setReadOnly', async () => { - const roomInfo = await getRoomInfo(channel._id); + before(async () => { + testChannel = (await createRoom({ type: 'c', name })).body.channel; + }); - return request - .post(api('channels.setReadOnly')) - .set(credentials) - .send({ - roomId: channel._id, - readOnly: true, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); - expect(res.body).to.have.nested.property('channel.t', 'c'); - expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs + 1); - }); - }); + after(async () => { + await deleteRoom({ type: 'c', roomId: testChannel._id }); + }); - describe('/channels.setDefault', () => { it('should set channel as default', async () => { - const roomInfo = await getRoomInfo(channel._id); + const roomInfo = await getRoomInfo(testChannel._id); return request .post(api('channels.setDefault')) .set(credentials) .send({ - roomId: channel._id, + roomId: testChannel._id, default: true, }) .expect('Content-Type', 'application/json') @@ -1633,20 +1692,20 @@ describe('[Channels]', function () { .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); + expect(res.body).to.have.nested.property('channel.name', name); expect(res.body).to.have.nested.property('channel.t', 'c'); expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs); expect(res.body).to.have.nested.property('channel.default', true); }); }); it('should unset channel as default', async () => { - const roomInfo = await getRoomInfo(channel._id); + const roomInfo = await getRoomInfo(testChannel._id); return request .post(api('channels.setDefault')) .set(credentials) .send({ - roomId: channel._id, + roomId: testChannel._id, default: false, }) .expect('Content-Type', 'application/json') @@ -1654,7 +1713,7 @@ describe('[Channels]', function () { .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); + expect(res.body).to.have.nested.property('channel.name', name); expect(res.body).to.have.nested.property('channel.t', 'c'); expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs); expect(res.body).to.have.nested.property('channel.default', false); @@ -1662,29 +1721,20 @@ describe('[Channels]', function () { }); }); - it('/channels.leave', async () => { - const roomInfo = await getRoomInfo(channel._id); + describe('/channels.setType', () => { + let testChannel; + const name = `setType-${Date.now()}`; - return request - .post(api('channels.leave')) - .set(credentials) - .send({ - roomId: channel._id, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); - expect(res.body).to.have.nested.property('channel.t', 'c'); - expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs + 1); - }); - }); + before(async () => { + testChannel = (await createRoom({ type: 'c', name })).body.channel; + }); + + after(async () => { + await deleteRoom({ type: 'c', roomId: testChannel._id }); + }); - describe('/channels.setType', () => { it('should change the type public channel to private', async () => { - const roomInfo = await getRoomInfo(channel._id); + const roomInfo = await getRoomInfo(testChannel._id); request .post(api('channels.setType')) @@ -1698,7 +1748,7 @@ describe('[Channels]', function () { .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.nested.property('channel._id'); - expect(res.body).to.have.nested.property('channel.name', `EDITED${apiPublicChannelName}`); + expect(res.body).to.have.nested.property('channel.name', name); expect(res.body).to.have.nested.property('channel.t', 'p'); expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs + 1); }); @@ -1707,18 +1757,15 @@ describe('[Channels]', function () { describe('/channels.delete:', () => { let testChannel; - it('/channels.create', (done) => { - request - .post(api('channels.create')) - .set(credentials) - .send({ - name: `channel.test.${Date.now()}`, - }) - .end((err, res) => { - testChannel = res.body.channel; - done(); - }); + + before(async () => { + testChannel = (await createRoom({ type: 'c', name: `channel.test.${Date.now()}` })).body.channel; }); + + after(async () => { + await deleteRoom({ type: 'c', roomId: testChannel._id }); + }); + it('/channels.delete', (done) => { request .post(api('channels.delete')) @@ -1793,18 +1840,15 @@ describe('[Channels]', function () { describe('/channels.roles', () => { let testChannel; - it('/channels.create', (done) => { - request - .post(api('channels.create')) - .set(credentials) - .send({ - name: `channel.roles.test.${Date.now()}`, - }) - .end((err, res) => { - testChannel = res.body.channel; - done(); - }); + + before(async () => { + testChannel = (await createRoom({ type: 'c', name: `channel.roles.test.${Date.now()}` })).body.channel; + }); + + after(async () => { + await deleteRoom({ type: 'c', roomId: testChannel._id }); }); + it('/channels.invite', (done) => { request .post(api('channels.invite')) @@ -1868,18 +1912,15 @@ describe('[Channels]', function () { describe('/channels.moderators', () => { let testChannel; - it('/channels.create', (done) => { - request - .post(api('channels.create')) - .set(credentials) - .send({ - name: `channel.roles.test.${Date.now()}`, - }) - .end((err, res) => { - testChannel = res.body.channel; - done(); - }); + + before(async () => { + testChannel = (await createRoom({ type: 'c', name: `channel.moderators.test.${Date.now()}` })).body.channel; + }); + + after(async () => { + await deleteRoom({ type: 'c', roomId: testChannel._id }); }); + it('/channels.invite', (done) => { request .post(api('channels.invite')) @@ -1917,14 +1958,24 @@ describe('[Channels]', function () { .end(done); }); }); + describe('/channels.anonymousread', () => { - after(() => updateSetting('Accounts_AllowAnonymousRead', false)); + let testChannel; + + before(async () => { + testChannel = (await createRoom({ type: 'c', name: `channel.anonymousread.test.${Date.now()}` })).body.channel; + }); + + after(async () => { + await Promise.all([updateSetting('Accounts_AllowAnonymousRead', false), deleteRoom({ type: 'c', roomId: testChannel._id })]); + }); + it('should return an error when the setting "Accounts_AllowAnonymousRead" is disabled', (done) => { updateSetting('Accounts_AllowAnonymousRead', false).then(() => { request .get(api('channels.anonymousread')) .query({ - roomId: 'GENERAL', + roomId: testChannel._id, }) .expect('Content-Type', 'application/json') .expect(400) @@ -1943,7 +1994,7 @@ describe('[Channels]', function () { request .get(api('channels.anonymousread')) .query({ - roomId: 'GENERAL', + roomId: testChannel._id, }) .expect('Content-Type', 'application/json') .expect(200) @@ -1959,7 +2010,7 @@ describe('[Channels]', function () { request .get(api('channels.anonymousread')) .query({ - roomId: 'GENERAL', + roomId: testChannel._id, count: 5, offset: 0, }) @@ -1975,15 +2026,18 @@ describe('[Channels]', function () { }); describe('/channels.convertToTeam', () => { - before((done) => { - request - .post(api('channels.create')) - .set(credentials) - .send({ name: `channel-${Date.now()}` }) - .then((response) => { - this.newChannel = response.body.channel; - }) - .then(() => done()); + let testChannel; + + before(async () => { + testChannel = (await createRoom({ type: 'c', name: `channel.convertToTeam.test.${Date.now()}` })).body.channel; + }); + + after(async () => { + await Promise.all([ + updatePermission('create-team', ['admin', 'user']), + updatePermission('edit-room', ['admin', 'owner', 'moderator']), + deleteRoom({ type: 'c', roomId: testChannel._id }), + ]); }); it('should fail to convert channel if lacking edit-room permission', async () => { @@ -1993,7 +2047,7 @@ describe('[Channels]', function () { await request .post(api('channels.convertToTeam')) .set(credentials) - .send({ channelId: this.newChannel._id }) + .send({ channelId: testChannel._id }) .expect(403) .expect((res) => { expect(res.body).to.have.a.property('success', false); @@ -2007,7 +2061,7 @@ describe('[Channels]', function () { await request .post(api('channels.convertToTeam')) .set(credentials) - .send({ channelId: this.newChannel._id }) + .send({ channelId: testChannel._id }) .expect(403) .expect((res) => { expect(res.body).to.have.a.property('success', false); @@ -2019,8 +2073,8 @@ describe('[Channels]', function () { .post(api('channels.convertToTeam')) .set(credentials) .send({ - channelName: this.newChannel.name, - channelId: this.newChannel._id, + channelName: testChannel.name, + channelId: testChannel._id, }) .expect(400) .expect((res) => { @@ -2037,7 +2091,7 @@ describe('[Channels]', function () { await request .post(api('channels.convertToTeam')) .set(credentials) - .send({ channelId: this.newChannel._id }) + .send({ channelId: testChannel._id }) .expect(200) .expect((res) => { expect(res.body).to.have.a.property('success', true); @@ -2048,7 +2102,7 @@ describe('[Channels]', function () { await request .post(api('teams.convertToChannel')) .set(credentials) - .send({ teamName: this.newChannel.name }) + .send({ teamName: testChannel.name }) .expect(200) .expect((res) => { expect(res.body).to.have.a.property('success', true); @@ -2057,7 +2111,7 @@ describe('[Channels]', function () { await request .post(api('channels.convertToTeam')) .set(credentials) - .send({ channelName: this.newChannel.name }) + .send({ channelName: testChannel.name }) .expect(200) .expect((res) => { expect(res.body).to.have.a.property('success', true); @@ -2072,7 +2126,7 @@ describe('[Channels]', function () { request .post(api('channels.convertToTeam')) .set(credentials) - .send({ channelId: this.newChannel._id }) + .send({ channelId: testChannel._id }) .expect(400) .expect((res) => { expect(res.body).to.have.a.property('success', false); @@ -2081,64 +2135,12 @@ describe('[Channels]', function () { }); }); - describe.skip('/channels.setAutojoin', () => { - // let testTeam; + describe("Setting: 'Use Real Name': true", () => { let testChannel; - // let testUser1; - // let testUser2; - before(async () => { - const teamCreateRes = await request - .post(api('teams.create')) - .set(credentials) - .send({ name: `team-${Date.now()}` }); - - const { team } = teamCreateRes.body; - - const user1 = await createUser(); - const user2 = await createUser(); - - const channelCreateRes = await request - .post(api('channels.create')) - .set(credentials) - .send({ - name: `team-channel-${Date.now()}`, - extraData: { - teamId: team._id, - }, - }); - - const { channel } = channelCreateRes.body; - - // testTeam = team; - testChannel = channel; - // testUser1 = user1; - // testUser2 = user2; - - await request - .post(api('teams.addMembers')) - .set(credentials) - .send({ - name: team.name, - members: [{ userId: user1._id }, { userId: user2._id }], - }); - }); - - it('should add all existing team members', async () => { - const resAutojoin = await request - .post(api('channels.setAutojoin')) - .set(credentials) - .send({ roomName: testChannel.name, autojoin: true }) - .expect(200); - expect(resAutojoin.body).to.have.a.property('success', true); - const channelInfoResponse = await request.get(api('channels.info')).set(credentials).query({ roomId: testChannel._id }); - const { channel } = channelInfoResponse.body; - - return expect(channel.usersCount).to.be.equals(3); + before(async () => { + testChannel = (await createRoom({ type: 'c', name: `channel.anonymousread.test.${Date.now()}` })).body.channel; }); - }); - - describe("Setting: 'Use Real Name': true", () => { before(async () => { await updateSetting('UI_Use_Real_Name', true); @@ -2146,13 +2148,13 @@ describe('[Channels]', function () { .post(api('channels.join')) .set(credentials) .send({ - roomId: channel._id, + roomId: testChannel._id, }) .expect('Content-Type', 'application/json') .expect(200) .expect((res) => { expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('channel._id', channel._id); + expect(res.body).to.have.nested.property('channel._id', testChannel._id); }); await request @@ -2161,7 +2163,7 @@ describe('[Channels]', function () { .send({ message: { text: 'Sample message', - rid: channel._id, + rid: testChannel._id, }, }) .expect('Content-Type', 'application/json') @@ -2170,21 +2172,13 @@ describe('[Channels]', function () { expect(res.body).to.have.property('success', true); }); }); - after(async () => { - await updateSetting('UI_Use_Real_Name', false); - await request - .post(api('channels.leave')) - .set(credentials) - .send({ - roomId: channel._id, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('channel._id', channel._id); - }); + after(async () => { + await Promise.all([ + updateSetting('Accounts_AllowAnonymousRead', false), + updateSetting('UI_Use_Real_Name', false), + deleteRoom({ type: 'c', roomId: testChannel._id }), + ]); }); it('/channels.list', (done) => { @@ -2199,7 +2193,7 @@ describe('[Channels]', function () { expect(res.body).to.have.property('total'); expect(res.body).to.have.property('channels').and.to.be.an('array'); - const retChannel = res.body.channels.find(({ _id }) => _id === channel._id); + const retChannel = res.body.channels.find(({ _id }) => _id === testChannel._id); expect(retChannel).to.have.nested.property('lastMessage.u.name', 'RocketChat Internal Admin Test'); }) @@ -2218,7 +2212,7 @@ describe('[Channels]', function () { expect(res.body).to.have.property('total'); expect(res.body).to.have.property('channels').and.to.be.an('array'); - const retChannel = res.body.channels.find(({ _id }) => _id === channel._id); + const retChannel = res.body.channels.find(({ _id }) => _id === testChannel._id); expect(retChannel).to.have.nested.property('lastMessage.u.name', 'RocketChat Internal Admin Test'); }) diff --git a/apps/meteor/tests/end-to-end/api/03-groups.js b/apps/meteor/tests/end-to-end/api/03-groups.js index df736ecbeb86..07b03494900f 100644 --- a/apps/meteor/tests/end-to-end/api/03-groups.js +++ b/apps/meteor/tests/end-to-end/api/03-groups.js @@ -1066,7 +1066,7 @@ describe('[Groups]', function () { }); describe('/groups.files', async () => { - await testFileUploads('groups.files', group); + await testFileUploads('groups.files', 'p'); }); describe('/groups.listAll', () => { diff --git a/apps/meteor/tests/end-to-end/api/04-direct-message.js b/apps/meteor/tests/end-to-end/api/04-direct-message.js index a8ea87e2eddc..be8868ef6b48 100644 --- a/apps/meteor/tests/end-to-end/api/04-direct-message.js +++ b/apps/meteor/tests/end-to-end/api/04-direct-message.js @@ -333,7 +333,7 @@ describe('[Direct Messages]', function () { }); describe('[/im.files]', async () => { - await testFileUploads('im.files', directMessage, 'invalid-channel'); + await testFileUploads('im.files', 'd', 'invalid-channel'); }); describe('/im.messages', () => {