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

198 lines
5.4 KiB
TypeScript

import axios from 'axios';
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'
}
});
export const login = async (username: string, password: string) => {
console.log(`Logging in as user ${username}`);
const response = await rocketchat.post('login', {
user: username,
password
});
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 };
};
export interface ITestUser {
username: string;
password: string;
name: string;
email: string;
}
export const createRandomUser = async (): Promise<ITestUser> => {
try {
await login(data.adminUser, data.adminPassword);
const user = data.randomUser();
console.log(`Creating user ${user.username}`);
await rocketchat.post('users.create', {
username: user.username,
name: user.name,
password: user.password,
email: user.email
});
return user;
} catch (error) {
console.log(JSON.stringify(error));
throw new Error('Failed to create user');
}
};
export const createRandomRoom = async (
user: { username: string; password: string },
type: 'p' | 'c' = 'c'
): Promise<{ _id: string; name: string }> => {
try {
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
});
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');
}
};
export const createRandomTeam = async (user: { username: string; password: string }) => {
try {
await login(user.username, user.password);
const team = `team${random()}`;
console.log(`Creating team ${team}`);
await rocketchat.post('teams.create', {
name: team,
type: TEAM_TYPE.PRIVATE
});
return team;
} catch (e) {
console.log(JSON.stringify(e));
throw new Error('Failed create team');
}
};
export const sendRandomMessage = async ({
user,
room,
messageEnd,
tmid
}: {
user: { username: string; password: string };
room: string;
messageEnd: string;
tmid?: string;
}) => {
try {
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');
}
};
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');
}
};
export const get = (endpoint: string) => {
console.log(`GET /${endpoint}`);
return rocketchat.get(endpoint);
};
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);
};
interface UserId {
userId: string;
}
interface UserName {
username: string;
}
export const getProfileInfo = async (param: UserId | UserName) => {
let query = '';
if ('userId' in param) {
query += `userId=${param.userId}`;
} else if ('username' in param) {
query += `username=${param.username}`;
}
const result = await get(`users.info?${query}`);
return result.data.user;
};
export interface IDeleteCreateUser {
server: string;
username: string;
}
const deleteCreatedUser = async ({ server, username: usernameToDelete }: IDeleteCreateUser) => {
const serverConnection = axios.create({
baseURL: `${server}/api/v1/`,
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
});
console.log(`Logging in as admin in ${server}`);
const response = await serverConnection.post('login', {
user: data.adminUser,
password: data.adminPassword
});
const { authToken, userId } = response.data.data;
serverConnection.defaults.headers.common['X-User-Id'] = userId;
serverConnection.defaults.headers.common['X-Auth-Token'] = authToken;
console.log(`Get user info: users.info?username=${usernameToDelete}`);
const result = await serverConnection.get(`users.info?username=${usernameToDelete}`);
const userIdToDelete = result.data.user._id;
const body = { userId: userIdToDelete, confirmRelinquish: false };
console.log(`Delete user: users.delete ${JSON.stringify(body)}`);
const responsePost = await serverConnection.post('users.delete', body);
return responsePost.data;
};
// Delete created users to avoid use all the Seats Available on the server
export const deleteCreatedUsers = async (deleteUsersAfterAll: IDeleteCreateUser[]) => {
if (deleteUsersAfterAll.length) {
for await (const deleteUser of deleteUsersAfterAll) {
await deleteCreatedUser(deleteUser);
}
}
};