Rocket.Chat.ReactNative/e2e/helpers/data_setup.ts

139 lines
3.7 KiB
TypeScript
Raw Normal View History

import axios from 'axios';
2023-03-07 12:28:51 +00:00
import data from '../data';
import random from './random';
const TEAM_TYPE = {
PUBLIC: 0,
PRIVATE: 1
};
const { server } = data;
const rocketchat = axios.create({
baseURL: `${server}/api/v1/`,
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
});
2023-03-07 12:28:51 +00:00
export const login = async (username: string, password: string) => {
console.log(`Logging in as user ${username}`);
const response = await rocketchat.post('login', {
user: username,
password
});
2023-03-07 12:28:51 +00:00
const { authToken, userId } = response.data.data;
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> => {
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}`);
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
});
2023-03-07 12:28:51 +00:00
return user;
} 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 }> => {
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
});
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');
}
};
2023-03-07 12:28:51 +00:00
export const createRandomTeam = async (user: { username: string; password: string }) => {
try {
2023-03-07 12:28:51 +00:00
await login(user.username, user.password);
const team = `team${random()}`;
console.log(`Creating team ${team}`);
await rocketchat.post('teams.create', {
2023-03-07 12:28:51 +00:00
name: team,
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');
}
};
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;
}) => {
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');
}
};
2023-03-07 12:28:51 +00:00
export const sendMessage = async (user: { username: string; password: string }, channel: string, msg: string, tmid?: string) => {
console.log(`Sending message to ${channel}`);
try {
await login(user.username, user.password);
const response = await rocketchat.post('chat.postMessage', { channel, msg, tmid });
return response.data;
} 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) => {
console.log(`GET /${endpoint}`);
return rocketchat.get(endpoint);
};
2023-03-07 12:28:51 +00:00
export const post = async (endpoint: string, body: any, user: ITestUser) => {
await login(user.username, user.password);
console.log(`POST /${endpoint} ${JSON.stringify(body)}`);
return rocketchat.post(endpoint, body);
};