Skip to content

Commit

Permalink
refactor(tests): API tests (#32643)
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan authored Jun 24, 2024
1 parent 6f935c8 commit e07a515
Show file tree
Hide file tree
Showing 120 changed files with 3,633 additions and 3,368 deletions.
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
9 changes: 5 additions & 4 deletions apps/meteor/.mocharc.api.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict';

/**
/*
* Mocha configuration for REST API integration tests.
*/

module.exports = {
module.exports = /** @satisfies {import('mocha').MochaOptions} */ ({
...require('./.mocharc.base.json'), // see https://github.com/mochajs/mocha/issues/3916
timeout: 10000,
bail: true,
file: 'tests/end-to-end/teardown.js',
retries: 0,
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.

76 changes: 76 additions & 0 deletions apps/meteor/tests/data/api-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type { Credentials } from '@rocket.chat/api-client';
import type { Path } from '@rocket.chat/rest-typings';
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

type PathWithoutPrefix<TPath> = TPath extends `/v1/${infer U}` ? U : never;

export function api<TPath extends PathWithoutPrefix<Path>>(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);
}
4 changes: 0 additions & 4 deletions apps/meteor/tests/data/apps/apps-data.js

This file was deleted.

12 changes: 12 additions & 0 deletions apps/meteor/tests/data/apps/apps-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Path } from '@rocket.chat/rest-typings';

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';

type PathWithoutPrefix<TPath> = TPath extends `/apps${infer U}` ? U : never;

export function apps(path?: ''): `/api/apps`;
export function apps<TPath extends PathWithoutPrefix<Path>>(path: TPath): `/api/apps${TPath}`;
export function apps(path = '') {
return `/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,17 @@ 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
.get(api(`chat.getMessage?msgId=${msgId}`))
return new Promise<IMessage>((resolve) => {
void request
.get(api(`chat.getMessage`))
.query({ 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

0 comments on commit e07a515

Please sign in to comment.