Skip to content

Commit

Permalink
Split autotranslate endpoint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed Jun 23, 2024
1 parent 191e534 commit b654f09
Show file tree
Hide file tree
Showing 4 changed files with 419 additions and 365 deletions.
368 changes: 3 additions & 365 deletions apps/meteor/tests/end-to-end/api/00-autotranslate.ts
Original file line number Diff line number Diff line change
@@ -1,381 +1,19 @@
import type { Credentials } from '@rocket.chat/api-client';
import type { IMessage, IRoom, IUser } from '@rocket.chat/core-typings';
import type { IRoom, IUser } from '@rocket.chat/core-typings';
import { expect } from 'chai';
import { before, describe, after, it } from 'mocha';

import { getCredentials, api, request, credentials } from '../../data/api-data';
import { sendSimpleMessage } from '../../data/chat.helper';
import { updatePermission, updateSetting } from '../../data/permissions.helper';
import { getCredentials, api, request } from '../../data/api-data';
import { updateSetting } from '../../data/permissions.helper';
import { createRoom, deleteRoom } from '../../data/rooms.helper';
import { password } from '../../data/user';
import type { TestUser } from '../../data/users.helper';
import { createUser, deleteUser, login } from '../../data/users.helper';

const resetAutoTranslateDefaults = async () => {
await Promise.all([
updateSetting('AutoTranslate_Enabled', false),
updateSetting('AutoTranslate_AutoEnableOnJoinRoom', false),
updateSetting('Language', ''),
updatePermission('auto-translate', ['admin']),
]);
};

const resetE2EDefaults = async () => {
await Promise.all([updateSetting('E2E_Enabled_Default_PrivateRooms', false), updateSetting('E2E_Enable', false)]);
};

describe('AutoTranslate', () => {
before((done) => getCredentials(done));

describe('[AutoTranslate]', () => {
describe('[/autotranslate.getSupportedLanguages', () => {
before(() => resetAutoTranslateDefaults());
after(() => resetAutoTranslateDefaults());

it('should throw an error when the "AutoTranslate_Enabled" setting is disabled', (done) => {
void request
.get(api('autotranslate.getSupportedLanguages'))
.set(credentials)
.query({
targetLanguage: 'en',
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.a.property('success', false);
expect(res.body.error).to.be.equal('AutoTranslate is disabled.');
})
.end(done);
});
it('should throw an error when the user does not have the "auto-translate" permission', (done) => {
void updateSetting('AutoTranslate_Enabled', true).then(() => {
void updatePermission('auto-translate', []).then(() => {
void request
.get(api('autotranslate.getSupportedLanguages'))
.set(credentials)
.query({
targetLanguage: 'en',
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.a.property('success', false);
expect(res.body.errorType).to.be.equal('error-action-not-allowed');
expect(res.body.error).to.be.equal('Auto-Translate is not allowed [error-action-not-allowed]');
})
.end(done);
});
});
});

it('should return a list of languages', async () => {
await updatePermission('auto-translate', ['admin']);
await request
.get(api('autotranslate.getSupportedLanguages'))
.set(credentials)
.query({
targetLanguage: 'en',
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.a.property('success', true);
expect(res.body.languages).to.be.an('array');
});
});
});

describe('[/autotranslate.saveSettings', () => {
let testGroupId: IRoom['_id'];
let testChannelId: IRoom['_id'];

before(async () => {
await Promise.all([
resetAutoTranslateDefaults(),
updateSetting('E2E_Enable', true),
updateSetting('E2E_Enabled_Default_PrivateRooms', true),
]);

testGroupId = (await createRoom({ type: 'p', name: `e2etest-autotranslate-${Date.now()}` })).body.group._id;
testChannelId = (await createRoom({ type: 'c', name: `test-autotranslate-${Date.now()}` })).body.channel._id;
});

after(async () => {
await Promise.all([
resetAutoTranslateDefaults(),
resetE2EDefaults(),
deleteRoom({ type: 'p', roomId: testGroupId }),
deleteRoom({ type: 'c', roomId: testChannelId }),
]);
});

it('should throw an error when the "AutoTranslate_Enabled" setting is disabled', (done) => {
void request
.post(api('autotranslate.saveSettings'))
.set(credentials)
.send({
roomId: testChannelId,
field: 'autoTranslate',
defaultLanguage: 'en',
value: true,
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.a.property('success', false);
expect(res.body.error).to.be.equal('AutoTranslate is disabled.');
})
.end(done);
});
it('should throw an error when the user does not have the "auto-translate" permission', (done) => {
void updateSetting('AutoTranslate_Enabled', true).then(() => {
void updatePermission('auto-translate', []).then(() => {
void request
.post(api('autotranslate.saveSettings'))
.set(credentials)
.send({
roomId: testChannelId,
defaultLanguage: 'en',
field: 'autoTranslateLanguage',
value: 'en',
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.a.property('success', false);
expect(res.body.errorType).to.be.equal('error-action-not-allowed');
expect(res.body.error).to.be.equal('Auto-Translate is not allowed [error-action-not-allowed]');
})
.end(done);
});
});
});
it('should throw an error when the bodyParam "roomId" is not provided', (done) => {
void updatePermission('auto-translate', ['admin']).then(() => {
void request
.post(api('autotranslate.saveSettings'))
.set(credentials)
.send({})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.a.property('success', false);
})
.end(done);
});
});
it('should throw an error when the bodyParam "field" is not provided', (done) => {
void request
.post(api('autotranslate.saveSettings'))
.set(credentials)
.send({
roomId: testChannelId,
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.a.property('success', false);
})
.end(done);
});
it('should throw an error when the bodyParam "value" is not provided', (done) => {
void request
.post(api('autotranslate.saveSettings'))
.set(credentials)
.send({
roomId: testChannelId,
field: 'autoTranslate',
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.a.property('success', false);
})
.end(done);
});
it('should throw an error when the bodyParam "autoTranslate" is not a boolean', (done) => {
void request
.post(api('autotranslate.saveSettings'))
.set(credentials)
.send({
roomId: testChannelId,
field: 'autoTranslate',
value: 'test',
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.a.property('success', false);
})
.end(done);
});
it('should throw an error when the bodyParam "autoTranslateLanguage" is not a string', (done) => {
void request
.post(api('autotranslate.saveSettings'))
.set(credentials)
.send({
roomId: testChannelId,
field: 'autoTranslateLanguage',
value: 12,
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.a.property('success', false);
})
.end(done);
});
it('should throw an error when the bodyParam "field" is invalid', (done) => {
void request
.post(api('autotranslate.saveSettings'))
.set(credentials)
.send({
roomId: testChannelId,
field: 'invalid',
value: 12,
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.a.property('success', false);
})
.end(done);
});
it('should throw an error when the bodyParam "roomId" is invalid or the user is not subscribed', (done) => {
void request
.post(api('autotranslate.saveSettings'))
.set(credentials)
.send({
roomId: 'invalid',
field: 'autoTranslateLanguage',
value: 'en',
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.a.property('success', false);
expect(res.body.errorType).to.be.equal('error-invalid-subscription');
expect(res.body.error).to.be.equal('Invalid subscription [error-invalid-subscription]');
})
.end(done);
});
it('should throw an error when E2E encryption is enabled', async () => {
await request
.post(api('autotranslate.saveSettings'))
.set(credentials)
.send({
roomId: testGroupId,
field: 'autoTranslate',
defaultLanguage: 'en',
value: true,
})
.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-e2e-enabled');
});
});
it('should return success when the setting is saved correctly', (done) => {
void request
.post(api('autotranslate.saveSettings'))
.set(credentials)
.send({
roomId: testChannelId,
field: 'autoTranslateLanguage',
value: 'en',
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.a.property('success', true);
})
.end(done);
});
});

describe('[/autotranslate.translateMessage', () => {
let messageSent: IMessage;
let testChannelId: IRoom['_id'];

before(async () => {
await resetAutoTranslateDefaults();

testChannelId = (await createRoom({ type: 'c', name: `test-autotranslate-message-${Date.now()}` })).body.channel._id;
const res = await sendSimpleMessage({
roomId: testChannelId,
text: 'Isso é um teste',
});
messageSent = res.body.message;
});

after(async () => {
await Promise.all([resetAutoTranslateDefaults(), deleteRoom({ type: 'c', roomId: testChannelId })]);
});

it('should throw an error when the "AutoTranslate_Enabled" setting is disabled', (done) => {
void request
.post(api('autotranslate.translateMessage'))
.set(credentials)
.send({
messageId: 'test',
targetLanguage: 'en',
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.a.property('success', false);
expect(res.body.error).to.be.equal('AutoTranslate is disabled.');
})
.end(done);
});
it('should throw an error when the bodyParam "messageId" is not provided', (done) => {
void updateSetting('AutoTranslate_Enabled', true).then(() => {
void updatePermission('auto-translate', ['admin']).then(() => {
void request
.post(api('autotranslate.translateMessage'))
.set(credentials)
.send({})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.a.property('success', false);
})
.end(done);
});
});
});
it('should throw an error when the bodyParam "messageId" is invalid', (done) => {
void request
.post(api('autotranslate.translateMessage'))
.set(credentials)
.send({
messageId: 'invalid',
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.a.property('success', false);
expect(res.body.error).to.be.equal('Message not found.');
})
.end(done);
});
it('should return success when the translate is successful', (done) => {
void request
.post(api('autotranslate.translateMessage'))
.set(credentials)
.send({
messageId: messageSent._id,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.a.property('success', true);
})
.end(done);
});
});

describe('Autoenable setting', () => {
let userA: TestUser<IUser>;
let userB: TestUser<IUser>;
Expand Down
Loading

0 comments on commit b654f09

Please sign in to comment.