Skip to content

Commit

Permalink
perform tests on all files endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Dec 22, 2022
1 parent a969bc7 commit 102f3f6
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 204 deletions.
224 changes: 224 additions & 0 deletions apps/meteor/tests/data/uploads.helper.ts
@@ -0,0 +1,224 @@
import type { Response } from 'supertest';
import { expect } from 'chai';

import { api, request, credentials } from './api-data.js';
import { password } from './user.js';
import { createUser, login } from './users.helper';
import { imgURL } from './interactions.js';
import { updateSetting } from './permissions.helper';
import { createRoom } 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') {
before(async function () {
await updateSetting('VoIP_Enabled', true);
await updateSetting('Message_KeepHistory', true);
});

after(async function () {
await updateSetting('VoIP_Enabled', false);
await updateSetting('Message_KeepHistory', false);
});

console.log('filesEndpoint', filesEndpoint, room);

const createVoipRoom = async function () {
const testUser = await createUser({ roles: ['user', 'livechat-agent'] });
const testUserCredentials = await login(testUser.username, password);
const visitor = await createVisitor();
const roomResponse = await createRoom({
token: visitor.token,
type: 'v',
agentId: testUser._id,
credentials: testUserCredentials,
name: null,
username: null,
members: null,
});
return roomResponse.body.room;
};

it('should fail if invalid channel', function (done) {
request
.get(api(filesEndpoint))
.set(credentials)
.query({
roomId: 'invalid',
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect(function (res: Response) {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('errorType', invalidRoomError);
})
.end(done);
});

it('should fail for room type v', async function () {
const { _id } = await createVoipRoom();
request
.get(api(filesEndpoint))
.set(credentials)
.query({
roomId: _id,
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect(function (res: Response) {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('errorType', 'error-room-not-found');
});
});

it('should succeed when searching by roomId', function (done) {
console.log('room._id ->', room._id);
request
.get(api(filesEndpoint))
.set(credentials)
.query({
roomId: room._id,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect(function (res: Response) {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('files').and.to.be.an('array');
})
.end(done);
});

it('should succeed when searching by roomId even requested with count and offset params', function (done) {
request
.get(api(filesEndpoint))
.set(credentials)
.query({
roomId: room._id,
count: 5,
offset: 0,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect(function (res: Response) {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('files').and.to.be.an('array');
})
.end(done);
});

it('should succeed when searching by roomName', function (done) {
if (!room.name) {
this.skip();
}
request
.get(api(filesEndpoint))
.set(credentials)
.query({
roomName: room.name,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect(function (res: Response) {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('files').and.to.be.an('array');
})
.end(done);
});

it('should succeed when searching by roomName even requested with count and offset params', function (done) {
if (!room.name) {
this.skip();
}
request
.get(api(filesEndpoint))
.set(credentials)
.query({
roomName: room.name,
count: 5,
offset: 0,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect(function (res: Response) {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('files').and.to.be.an('array');
})
.end(done);
});

it('should not return thumbnails', async function () {
await request
.post(api(`rooms.upload/${room._id}`))
.set(credentials)
.attach('file', imgURL)
.expect('Content-Type', 'application/json')
.expect(200)
.expect(function (res: Response) {
expect(res.body).to.have.property('success', true);
});

await request
.get(api(filesEndpoint))
.set(credentials)
.query({
roomId: room._id,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect(function (res: Response) {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('files').and.to.be.an('array').with.lengthOf(1);

const { files } = res.body;

files.forEach(function (file: unknown) {
expect(file).to.not.have.property('originalFileId');
});
});
});

it('should not return hidden files', async function () {
let msgId;
let fileId: string;

await request
.post(api(`rooms.upload/${room._id}`))
.set(credentials)
.attach('file', imgURL)
.expect('Content-Type', 'application/json')
.expect(200)
.expect(function (res: Response) {
expect(res.body).to.have.property('success', true);

msgId = res.body.message._id;
fileId = res.body.message.file._id;
});

await request
.post(api('chat.delete'))
.set(credentials)
.send({
roomId: room._id,
msgId,
})
.expect('Content-Type', 'application/json')
.expect(200);

await request
.get(api(filesEndpoint))
.set(credentials)
.query({
roomId: room._id,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect(function (res: Response) {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('files').and.to.be.an('array').with.lengthOf(1);

const { files } = res.body;
files.forEach(function (file: unknown) {
expect(file).to.have.property('_id').to.not.be.equal(fileId);
});
});
});
}
149 changes: 3 additions & 146 deletions apps/meteor/tests/end-to-end/api/02-channels.js
Expand Up @@ -3,11 +3,10 @@ import { expect } from 'chai';
import { getCredentials, api, request, credentials, apiPublicChannelName, channel, reservedWords } from '../../data/api-data.js';
import { adminUsername, password } from '../../data/user.js';
import { createUser, login } from '../../data/users.helper';
import { imgURL } from '../../data/interactions.js';
import { updatePermission, updateSetting } from '../../data/permissions.helper';
import { createRoom } from '../../data/rooms.helper';
import { createVisitor } from '../../data/livechat/rooms';
import { createIntegration, removeIntegration } from '../../data/integration.helper';
import { testFileUploads } from '../../data/uploads.helper';

function getRoomInfo(roomId) {
return new Promise((resolve /* , reject*/) => {
Expand Down Expand Up @@ -303,150 +302,8 @@ describe('[Channels]', function () {
});
});

describe('[/channels.files]', () => {
before(() => updateSetting('VoIP_Enabled', true));
const createVoipRoom = async () => {
const testUser = await createUser({ roles: ['user', 'livechat-agent'] });
const testUserCredentials = await login(testUser.username, password);
const visitor = await createVisitor();
const roomResponse = await createRoom({
token: visitor.token,
type: 'v',
agentId: testUser._id,
credentials: testUserCredentials,
});
return roomResponse.body.room;
};
it('should fail if invalid channel', (done) => {
request
.get(api('channels.files'))
.set(credentials)
.query({
roomId: 'invalid',
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('errorType', 'error-room-not-found');
})
.end(done);
});

it('should fail for room type v', async () => {
const { _id } = await createVoipRoom();
request
.get(api('channels.files'))
.set(credentials)
.query({
roomId: _id,
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('errorType', 'error-room-not-found');
});
});

it('should succeed when searching by roomId', (done) => {
request
.get(api('channels.files'))
.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('files').and.to.be.an('array');
})
.end(done);
});

it('should succeed when searching by roomId even requested with count and offset params', (done) => {
request
.get(api('channels.files'))
.set(credentials)
.query({
roomId: channel._id,
count: 5,
offset: 0,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('files').and.to.be.an('array');
})
.end(done);
});

it('should succeed when searching by roomName', (done) => {
request
.get(api('channels.files'))
.set(credentials)
.query({
roomName: channel.name,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('files').and.to.be.an('array');
})
.end(done);
});

it('should succeed when searching by roomName even requested with count and offset params', (done) => {
request
.get(api('channels.files'))
.set(credentials)
.query({
roomName: channel.name,
count: 5,
offset: 0,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('files').and.to.be.an('array');
})
.end(done);
});

it('should not return thumbnails', async function () {
await request
.post(api(`rooms.upload/${channel._id}`))
.set(credentials)
.attach('file', imgURL)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
});

await request
.get(api('channels.files'))
.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('files').and.to.be.an('array').with.lengthOf(1);

const { files } = res.body;

files.forEach((file) => {
expect(file).to.not.have.property('originalFileId');
});
});
});
describe('[/channels.files]', async function () {
await testFileUploads('channels.files', channel);
});

describe('[/channels.join]', () => {
Expand Down

0 comments on commit 102f3f6

Please sign in to comment.