Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tests): API tests #32643

Merged
merged 19 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion apps/meteor/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/node_modules/
#/tests/e2e/
/tests/data/
/packages/
/app/emoji-emojione/generateEmojiIndex.js
/public/
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/.mocharc.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ module.exports = {
...require('./.mocharc.base.json'), // see https://github.com/mochajs/mocha/issues/3916
timeout: 10000,
bail: true,
file: 'tests/end-to-end/teardown.js',
file: 'tests/end-to-end/teardown.ts',
spec: ['tests/end-to-end/api/**/*', 'tests/end-to-end/apps/*'],
};
2 changes: 1 addition & 1 deletion apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"@types/bcrypt": "^5.0.1",
"@types/body-parser": "^1.19.4",
"@types/busboy": "^1.5.2",
"@types/chai": "^4.3.9",
"@types/chai": "~4.3.16",
"@types/chai-as-promised": "^7.1.7",
"@types/chai-datetime": "0.0.38",
"@types/chai-dom": "1.11.2",
Expand Down
68 changes: 0 additions & 68 deletions apps/meteor/tests/data/api-data.js

This file was deleted.

73 changes: 73 additions & 0 deletions apps/meteor/tests/data/api-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type { Credentials } from '@rocket.chat/api-client';
import type { CallbackHandler, Response } from 'supertest';
import supertest from 'supertest';

import { adminUsername, adminPassword } from './user';

const apiUrl = process.env.TEST_API_URL || 'http://localhost:3000';

export const request = supertest(apiUrl);
const prefix = '/api/v1/';

export function wait(cb: () => void, time: number) {
return () => setTimeout(cb, time);
}

const privateChannelName = `private-channel-test-${Date.now()}` as const;

const username = 'user.test';
const email = `${username}@rocket.chat`;

export const apiUsername = `api${username}-${Date.now()}` as const;
export const apiEmail = `api${email}-${Date.now()}` as const;
export const apiPrivateChannelName = `api${privateChannelName}-${Date.now()}` as const;

const roleNameUsers = `role-name-test-users-${Date.now()}` as const;
const roleNameSubscriptions = `role-name-test-subscriptions-${Date.now()}` as const;
const roleScopeUsers = 'Users' as const;
const roleScopeSubscriptions = 'Subscriptions' as const;
const roleDescription = `role-description-test-${Date.now()}` as const;

export const apiRoleNameUsers = `api${roleNameUsers}` as const;
export const apiRoleNameSubscriptions = `api${roleNameSubscriptions}` as const;
export const apiRoleScopeUsers = `${roleScopeUsers}` as const;
export const apiRoleScopeSubscriptions = `${roleScopeSubscriptions}` as const;
export const apiRoleDescription = `api${roleDescription}` as const;
export const reservedWords = ['admin', 'administrator', 'system', 'user'] as const;

export const credentials: Credentials = {
'X-Auth-Token': undefined,
'X-User-Id': undefined,
} as unknown as Credentials; // FIXME

export function api<TPath extends string>(path: TPath) {
return `${prefix}${path}` as const;
}

export function methodCall<TMethodName extends string>(methodName: TMethodName) {
return api(`method.call/${methodName}`);
}

export function log(res: Response) {
console.log((res as { req?: any }).req.path); // FIXME
console.log({
body: res.body,
headers: res.headers,
});
}

export function getCredentials(done?: CallbackHandler) {
void request
.post(api('login'))
.send({
user: adminUsername,
password: adminPassword,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
credentials['X-Auth-Token'] = res.body.data.authToken;
credentials['X-User-Id'] = res.body.data.userId;
})
.end(done);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const APP_URL = 'https://github.com/RocketChat/Apps.RocketChat.Tester/blob/master/dist/appsrocketchattester_0.0.5.zip?raw=true';
export const APP_NAME = 'Apps.RocketChat.Tester';
export const APP_USERNAME = 'appsrocketchattester.bot';
export const apps = (path = '') => `/api/apps${path}`;
export const apps = (path = '') => `/api/apps${path}` as const;
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import type { App } from '@rocket.chat/core-typings';

import { request, credentials } from '../api-data';
import { apps, APP_URL, APP_NAME } from './apps-data';

export const getApps = () =>
new Promise((resolve) => {
request
const getApps = () =>
new Promise<App[]>((resolve) => {
void request
.get(apps())
.set(credentials)
.end((err, res) => {
.end((_err, res) => {
resolve(res.body.apps);
});
});

export const removeAppById = (id) =>
const removeAppById = (id: App['id']) =>
new Promise((resolve) => {
request
void request
.delete(apps(`/${id}`))
.set(credentials)
.end(resolve);
Expand All @@ -28,14 +30,14 @@ export const cleanupApps = async () => {
};

export const installTestApp = () =>
new Promise((resolve) => {
request
new Promise<App>((resolve) => {
void request
.post(apps())
.set(credentials)
.send({
url: APP_URL,
})
.end((err, res) => {
.end((_err, res) => {
resolve(res.body.app);
});
});
1 change: 0 additions & 1 deletion apps/meteor/tests/data/channel.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import type { IRoom, IMessage } from '@rocket.chat/core-typings';

import { api, credentials, request } from './api-data';

export const sendSimpleMessage = ({ roomId, text = 'test message', tmid }) => {
export const sendSimpleMessage = ({
roomId,
text = 'test message',
tmid,
}: {
roomId: IRoom['_id'];
text?: string;
tmid?: IMessage['_id'];
}) => {
if (!roomId) {
throw new Error('"roomId" is required in "sendSimpleMessage" test helper');
}
const message = {
const message: {
rid: IRoom['_id'];
text: string;
tmid?: IMessage['_id'];
} = {
rid: roomId,
text,
};
Expand All @@ -15,17 +29,7 @@ export const sendSimpleMessage = ({ roomId, text = 'test message', tmid }) => {
return request.post(api('chat.sendMessage')).set(credentials).send({ message });
};

export const pinMessage = ({ msgId }) => {
if (!msgId) {
throw new Error('"msgId" is required in "pinMessage" test helper');
}

return request.post(api('chat.pinMessage')).set(credentials).send({
messageId: msgId,
});
};

export const deleteMessage = ({ roomId, msgId }) => {
export const deleteMessage = ({ roomId, msgId }: { roomId: IRoom['_id']; msgId: IMessage['_id'] }) => {
if (!roomId) {
throw new Error('"roomId" is required in "deleteMessage" test helper');
}
Expand All @@ -39,16 +43,16 @@ export const deleteMessage = ({ roomId, msgId }) => {
});
};

export const getMessageById = ({ msgId }) => {
export const getMessageById = ({ msgId }: { msgId: IMessage['_id'] }) => {
if (!msgId) {
throw new Error('"msgId" is required in "getMessageById" test helper');
}

return new Promise((resolve) => {
request
return new Promise<IMessage>((resolve) => {
void request
.get(api(`chat.getMessage?msgId=${msgId}`))
.set(credentials)
.end((err, res) => {
.end((_err, res) => {
resolve(res.body.message);
});
});
Expand Down
15 changes: 0 additions & 15 deletions apps/meteor/tests/data/checks.js

This file was deleted.

2 changes: 0 additions & 2 deletions apps/meteor/tests/data/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export const CI_MAX_ROOMS_PER_GUEST = 10;
export const MAX_BIO_LENGTH = 260;
export const MAX_NICKNAME_LENGTH = 120;
18 changes: 0 additions & 18 deletions apps/meteor/tests/data/custom-fields.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { Credentials } from '@rocket.chat/api-client';
import type { IIntegration } from '@rocket.chat/core-typings';
import type { IntegrationsCreateProps } from '@rocket.chat/rest-typings';

import { api, credentials, request } from './api-data';

export const createIntegration = (integration, userCredentials) =>
new Promise((resolve, reject) => {
request
export const createIntegration = (integration: IntegrationsCreateProps, userCredentials: Credentials) =>
new Promise<IIntegration>((resolve, reject) => {
void request
.post(api('integrations.create'))
.set(userCredentials)
.send(integration)
Expand All @@ -21,9 +25,9 @@ export const createIntegration = (integration, userCredentials) =>
});
});

export const removeIntegration = (integrationId, type) =>
new Promise((resolve, reject) => {
request
export const removeIntegration = (integrationId: IIntegration['_id'], type: 'incoming' | 'outgoing') =>
new Promise<void>((resolve) => {
void request
.post(api('integrations.remove'))
.set(credentials)
.send({
Expand Down
5 changes: 0 additions & 5 deletions apps/meteor/tests/data/interactions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
export const targetUser = 'rocket.cat';
export const imgURL = './public/images/logo/1024x1024.png';
export const lstURL = './tests/e2e/fixtures/files/lst-test.lst';
export const drawioURL = './tests/e2e/fixtures/files/diagram.drawio';
export const svgLogoURL = './public/images/logo/logo.svg';
export const svgLogoFileName = 'logo.svg';
5 changes: 0 additions & 5 deletions apps/meteor/tests/data/licenses.helper.ts

This file was deleted.

Loading
Loading