2022-09-12 14:51:33 +00:00
|
|
|
import axios from 'axios';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2023-03-07 12:28:51 +00:00
|
|
|
import data from '../data';
|
2022-09-12 14:51:33 +00:00
|
|
|
import random from './random';
|
2021-05-20 13:50:42 +00:00
|
|
|
|
|
|
|
const TEAM_TYPE = {
|
|
|
|
PUBLIC: 0,
|
|
|
|
PRIVATE: 1
|
|
|
|
};
|
2020-07-15 16:28:34 +00:00
|
|
|
|
2021-07-02 17:39:39 +00:00
|
|
|
const { server } = data;
|
2020-07-15 16:28:34 +00:00
|
|
|
|
|
|
|
const rocketchat = axios.create({
|
2021-09-13 20:41:05 +00:00
|
|
|
baseURL: `${server}/api/v1/`,
|
2021-07-02 17:39:39 +00:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json;charset=UTF-8'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-03-07 12:28:51 +00:00
|
|
|
export const login = async (username: string, password: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
console.log(`Logging in as user ${username}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
const response = await rocketchat.post('login', {
|
|
|
|
user: username,
|
|
|
|
password
|
|
|
|
});
|
2023-03-07 12:28:51 +00:00
|
|
|
const { authToken, userId } = response.data.data;
|
2021-07-02 17:39:39 +00:00
|
|
|
rocketchat.defaults.headers.common['X-User-Id'] = userId;
|
|
|
|
rocketchat.defaults.headers.common['X-Auth-Token'] = authToken;
|
|
|
|
return { authToken, userId };
|
|
|
|
};
|
|
|
|
|
2023-03-07 12:28:51 +00:00
|
|
|
export interface ITestUser {
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
name: string;
|
|
|
|
email: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const createRandomUser = async (): Promise<ITestUser> => {
|
2021-07-02 17:39:39 +00:00
|
|
|
try {
|
2023-03-07 12:28:51 +00:00
|
|
|
await login(data.adminUser, data.adminPassword);
|
|
|
|
const user = data.randomUser();
|
|
|
|
console.log(`Creating user ${user.username}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
await rocketchat.post('users.create', {
|
2023-03-07 12:28:51 +00:00
|
|
|
username: user.username,
|
|
|
|
name: user.name,
|
|
|
|
password: user.password,
|
|
|
|
email: user.email
|
2021-07-02 17:39:39 +00:00
|
|
|
});
|
2023-03-07 12:28:51 +00:00
|
|
|
return user;
|
2021-07-02 17:39:39 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(JSON.stringify(error));
|
|
|
|
throw new Error('Failed to create user');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-07 12:28:51 +00:00
|
|
|
export const createRandomRoom = async (
|
|
|
|
user: { username: string; password: string },
|
|
|
|
type: 'p' | 'c' = 'c'
|
|
|
|
): Promise<{ _id: string; name: string }> => {
|
2021-07-02 17:39:39 +00:00
|
|
|
try {
|
2023-03-07 12:28:51 +00:00
|
|
|
await login(user.username, user.password);
|
|
|
|
const room = `room${random()}`;
|
|
|
|
console.log(`Creating room ${room}`);
|
|
|
|
const result = await rocketchat.post(type === 'c' ? 'channels.create' : 'groups.create', {
|
|
|
|
name: room
|
2021-07-02 17:39:39 +00:00
|
|
|
});
|
2023-03-07 12:28:51 +00:00
|
|
|
return {
|
|
|
|
_id: type === 'c' ? result.data.channel._id : result.data.group._id,
|
|
|
|
name: type === 'c' ? result.data.channel.name : result.data.group.name
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
console.log(JSON.stringify(e));
|
|
|
|
throw new Error('Failed to create room');
|
2021-07-02 17:39:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-07 12:28:51 +00:00
|
|
|
export const createRandomTeam = async (user: { username: string; password: string }) => {
|
2021-07-02 17:39:39 +00:00
|
|
|
try {
|
2023-03-07 12:28:51 +00:00
|
|
|
await login(user.username, user.password);
|
|
|
|
const team = `team${random()}`;
|
|
|
|
console.log(`Creating team ${team}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
await rocketchat.post('teams.create', {
|
2023-03-07 12:28:51 +00:00
|
|
|
name: team,
|
2021-07-02 17:39:39 +00:00
|
|
|
type: TEAM_TYPE.PRIVATE
|
|
|
|
});
|
2023-03-07 12:28:51 +00:00
|
|
|
return team;
|
|
|
|
} catch (e) {
|
|
|
|
console.log(JSON.stringify(e));
|
|
|
|
throw new Error('Failed create team');
|
2021-07-02 17:39:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-07 12:28:51 +00:00
|
|
|
export const sendRandomMessage = async ({
|
|
|
|
user,
|
|
|
|
room,
|
|
|
|
messageEnd,
|
|
|
|
tmid
|
|
|
|
}: {
|
|
|
|
user: { username: string; password: string };
|
|
|
|
room: string;
|
|
|
|
messageEnd: string;
|
|
|
|
tmid?: string;
|
|
|
|
}) => {
|
2021-07-02 17:39:39 +00:00
|
|
|
try {
|
2023-03-07 12:28:51 +00:00
|
|
|
const msg = `${random()}${messageEnd}`;
|
|
|
|
console.log(`Sending message ${msg} to ${room}`);
|
|
|
|
await login(user.username, user.password);
|
|
|
|
const response = await rocketchat.post('chat.postMessage', { channel: room, msg, tmid });
|
|
|
|
return response.data;
|
|
|
|
} catch (infoError) {
|
|
|
|
console.log(JSON.stringify(infoError));
|
|
|
|
throw new Error('Failed to find or create private group');
|
2021-07-02 17:39:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-07 12:28:51 +00:00
|
|
|
export const sendMessage = async (user: { username: string; password: string }, channel: string, msg: string, tmid?: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
console.log(`Sending message to ${channel}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
try {
|
|
|
|
await login(user.username, user.password);
|
2021-10-26 16:11:50 +00:00
|
|
|
const response = await rocketchat.post('chat.postMessage', { channel, msg, tmid });
|
|
|
|
return response.data;
|
2021-07-02 17:39:39 +00:00
|
|
|
} catch (infoError) {
|
|
|
|
console.log(JSON.stringify(infoError));
|
|
|
|
throw new Error('Failed to find or create private group');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-07 12:28:51 +00:00
|
|
|
export const get = (endpoint: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
console.log(`GET /${endpoint}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
return rocketchat.get(endpoint);
|
|
|
|
};
|
2021-03-31 21:01:20 +00:00
|
|
|
|
2023-03-07 12:28:51 +00:00
|
|
|
export const post = async (endpoint: string, body: any, user: ITestUser) => {
|
|
|
|
await login(user.username, user.password);
|
2021-09-13 20:41:05 +00:00
|
|
|
console.log(`POST /${endpoint} ${JSON.stringify(body)}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
return rocketchat.post(endpoint, body);
|
|
|
|
};
|