Cleanup data_setup

This commit is contained in:
Diego Mello 2023-02-23 09:53:02 -03:00
parent 26fe36a961
commit a1f8ef6c33
1 changed files with 4 additions and 144 deletions

View File

@ -1,6 +1,6 @@
import axios from 'axios';
import data, { TDataChannels, TDataGroups, TDataTeams, TDataUsers, TUserRegularChannels } from '../data';
import data from '../data';
import random from './random';
const TEAM_TYPE = {
@ -54,41 +54,6 @@ export const createRandomUser = async (): Promise<ITestUser> => {
}
};
const createUser = async (username: string, password: string, name: string, email: string) => {
console.log(`Creating user ${username}`);
try {
await rocketchat.post('users.create', {
username,
password,
name,
email
});
} catch (error) {
console.log(JSON.stringify(error));
throw new Error('Failed to create user');
}
};
const createChannelIfNotExists = async (channelname: string) => {
console.log(`Creating public channel ${channelname}`);
try {
const room = await rocketchat.post('channels.create', {
name: channelname
});
return room;
} catch (createError) {
try {
// Maybe it exists already?
const room = rocketchat.get(`channels.info?roomName=${channelname}`);
return room;
} catch (infoError) {
console.log(JSON.stringify(createError));
console.log(JSON.stringify(infoError));
throw new Error('Failed to find or create public channel');
}
}
};
export const createRandomRoom = async (
user: { username: string; password: string },
type: 'p' | 'c' = 'c'
@ -126,60 +91,6 @@ export const createRandomTeam = async (user: { username: string; password: strin
}
};
const createTeamIfNotExists = async (teamname: string) => {
console.log(`Creating private team ${teamname}`);
try {
await rocketchat.post('teams.create', {
name: teamname,
type: TEAM_TYPE.PRIVATE
});
} catch (createError) {
try {
// Maybe it exists already?
await rocketchat.get(`teams.info?teamName=${teamname}`);
} catch (infoError) {
console.log(JSON.stringify(createError));
console.log(JSON.stringify(infoError));
throw new Error('Failed to find or create private team');
}
}
};
const createGroupIfNotExists = async (groupname: string) => {
console.log(`Creating private group ${groupname}`);
try {
await rocketchat.post('groups.create', {
name: groupname
});
} catch (createError) {
try {
// Maybe it exists already?
await rocketchat.get(`groups.info?roomName=${groupname}`);
} catch (infoError) {
console.log(JSON.stringify(createError));
console.log(JSON.stringify(infoError));
throw new Error('Failed to find or create private group');
}
}
};
const changeChannelJoinCode = async (roomId: string, joinCode: string) => {
console.log(`Changing channel Join Code ${roomId}`);
try {
await rocketchat.post('method.call/saveRoomSettings', {
message: JSON.stringify({
msg: 'method',
id: process.env.TEST_SESSION,
method: 'saveRoomSettings',
params: [roomId, { joinCode }]
})
});
} catch (createError) {
console.log(JSON.stringify(createError));
throw new Error('Failed to create protected channel');
}
};
export const sendRandomMessage = async ({
user,
room,
@ -203,7 +114,7 @@ export const sendRandomMessage = async ({
}
};
const sendMessage = async (user: { username: string; password: string }, channel: string, msg: string, tmid?: string) => {
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);
@ -215,64 +126,13 @@ const sendMessage = async (user: { username: string; password: string }, channel
}
};
const setup = async () => {
await login(data.adminUser, data.adminPassword);
for (const userKey in data.users) {
if (Object.prototype.hasOwnProperty.call(data.users, userKey)) {
const user = data.users[userKey as TDataUsers];
await createUser(user.username, user.password, user.username, user.email);
}
}
for (const channelKey in data.channels) {
if (Object.prototype.hasOwnProperty.call(data.channels, channelKey)) {
const channel = data.channels[channelKey as TDataChannels];
const {
data: {
channel: { _id }
}
} = await createChannelIfNotExists(channel.name);
if ('joinCode' in channel) {
await changeChannelJoinCode(_id, channel.joinCode);
}
}
}
await login(data.users.regular.username, data.users.regular.password);
for (const channelKey in data.userRegularChannels) {
if (Object.prototype.hasOwnProperty.call(data.userRegularChannels, channelKey)) {
const channel = data.userRegularChannels[channelKey as TUserRegularChannels];
await createChannelIfNotExists(channel.name);
}
}
for (const groupKey in data.groups) {
if (Object.prototype.hasOwnProperty.call(data.groups, groupKey)) {
const group = data.groups[groupKey as TDataGroups];
await createGroupIfNotExists(group.name);
}
}
for (const teamKey in data.teams) {
if (Object.prototype.hasOwnProperty.call(data.teams, teamKey)) {
const team = data.teams[teamKey as TDataTeams];
await createTeamIfNotExists(team.name);
}
}
};
const get = (endpoint: string) => {
export const get = (endpoint: string) => {
console.log(`GET /${endpoint}`);
return rocketchat.get(endpoint);
};
const post = async (endpoint: string, body: any, user = data.users.regular) => {
export const post = async (endpoint: string, body: any, user = data.users.regular) => {
await login(user.username, user.password);
console.log(`POST /${endpoint} ${JSON.stringify(body)}`);
return rocketchat.post(endpoint, body);
};
export { setup, sendMessage, get, post, login };