mattermost-mobile/test/test_helper.js
Elias Nahum 03d406021f Port WebSocket from mm-redux and batch actions (#4060)
* Port WebSocket from mm-redux and batch actions

* Update mm-redux and fix tests

* Change action name

* Naming batch actions

* Fix unit tests

* Dispatch connection change only if its different

* Remove comment

* Add Lint to TypeScript and fix linting errors

* Add WebSocket Unit Tests

* Revert from unwanted RN 0.62
2020-04-17 20:44:25 -07:00

306 lines
8.5 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import assert from 'assert';
import nock from 'nock';
import Config from 'assets/config.json';
import Client from 'mattermost-redux/client/client4';
const PASSWORD = 'password1';
class TestHelper {
constructor() {
this.basicClient = null;
this.basicUser = null;
this.basicTeam = null;
this.basicChannel = null;
this.basicPost = null;
}
activateMocking() {
if (!nock.isActive()) {
nock.activate();
}
}
assertStatusOkay = (data) => {
assert(data);
assert(data.status === 'OK');
};
createClient = () => {
const client = new Client();
client.setUrl(Config.DefaultServerUrl);
return client;
};
fakeChannel = (teamId) => {
const name = this.generateId();
return {
name,
team_id: teamId,
display_name: `Unit Test ${name}`,
type: 'O',
};
};
fakeChannelWithId = (teamId) => {
return {
...this.fakeChannel(teamId),
id: this.generateId(),
create_at: 1507840900004,
update_at: 1507840900004,
delete_at: 0,
};
};
fakeChannelMember = (userId, channelId) => {
return {
user_id: userId,
channel_id: channelId,
notify_props: {},
roles: 'system_user',
};
};
fakeEmail = () => {
return 'success' + this.generateId() + '@simulator.amazonses.com';
};
fakePost = (channelId) => {
const time = Date.now();
return {
id: this.generateId(),
channel_id: channelId,
create_at: time,
update_at: time,
message: `Unit Test ${this.generateId()}`,
};
};
fakePostWithId = (channelId) => {
return {
...this.fakePost(channelId),
id: this.generateId(),
create_at: 1507840900004,
update_at: 1507840900004,
delete_at: 0,
};
};
fakeTeam = () => {
const name = this.generateId();
let inviteId = this.generateId();
if (inviteId.length > 32) {
inviteId = inviteId.substring(0, 32);
}
return {
name,
display_name: `Unit Test ${name}`,
type: 'O',
email: this.fakeEmail(),
allowed_domains: '',
invite_id: inviteId,
};
};
fakeTeamWithId = () => {
return {
...this.fakeTeam(),
id: this.generateId(),
create_at: 1507840900004,
update_at: 1507840900004,
delete_at: 0,
};
};
fakeTeamMember = (userId, teamId) => {
return {
user_id: userId,
team_id: teamId,
roles: 'team_user',
delete_at: 0,
scheme_user: false,
scheme_admin: false,
};
};
fakeUser = () => {
return {
email: this.fakeEmail(),
allow_marketing: true,
password: PASSWORD,
username: this.generateId(),
roles: 'system_user',
};
};
fakeUserWithId = (id = this.generateId()) => {
return {
...this.fakeUser(),
id,
create_at: 1507840900004,
update_at: 1507840900004,
delete_at: 0,
};
};
generateId = () => {
// Implementation taken from http://stackoverflow.com/a/2117523
let id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
id = id.replace(/[xy]/g, (c) => {
const r = Math.floor(Math.random() * 16);
let v;
if (c === 'x') {
v = r;
} else {
v = (r & 0x3) | 0x8;
}
return v.toString(16);
});
return 'uid' + id;
};
initMockEntities = () => {
this.basicUser = this.fakeUserWithId();
this.basicUser.roles = 'system_user system_admin';
this.basicTeam = this.fakeTeamWithId();
this.basicTeamMember = this.fakeTeamMember(this.basicUser.id, this.basicTeam.id);
this.basicChannel = this.fakeChannelWithId(this.basicTeam.id);
this.basicChannelMember = this.fakeChannelMember(this.basicUser.id, this.basicChannel.id);
this.basicPost = {...this.fakePostWithId(this.basicChannel.id), create_at: 1507841118796};
this.basicRoles = {
system_admin: {
id: this.generateId(),
name: 'system_admin',
display_name: 'authentication.roles.global_admin.name',
description: 'authentication.roles.global_admin.description',
permissions: [
'system_admin_permission',
],
scheme_managed: true,
built_in: true,
},
system_user: {
id: this.generateId(),
name: 'system_user',
display_name: 'authentication.roles.global_user.name',
description: 'authentication.roles.global_user.description',
permissions: [
'system_user_permission',
],
scheme_managed: true,
built_in: true,
},
team_admin: {
id: this.generateId(),
name: 'team_admin',
display_name: 'authentication.roles.team_admin.name',
description: 'authentication.roles.team_admin.description',
permissions: [
'team_admin_permission',
],
scheme_managed: true,
built_in: true,
},
team_user: {
id: this.generateId(),
name: 'team_user',
display_name: 'authentication.roles.team_user.name',
description: 'authentication.roles.team_user.description',
permissions: [
'team_user_permission',
],
scheme_managed: true,
built_in: true,
},
channel_admin: {
id: this.generateId(),
name: 'channel_admin',
display_name: 'authentication.roles.channel_admin.name',
description: 'authentication.roles.channel_admin.description',
permissions: [
'channel_admin_permission',
],
scheme_managed: true,
built_in: true,
},
channel_user: {
id: this.generateId(),
name: 'channel_user',
display_name: 'authentication.roles.channel_user.name',
description: 'authentication.roles.channel_user.description',
permissions: [
'channel_user_permission',
],
scheme_managed: true,
built_in: true,
},
};
this.basicScheme = this.mockSchemeWithId();
}
initBasic = async (client = this.createClient()) => {
client.setUrl(Config.TestServerUrl || Config.DefaultServerUrl);
this.basicClient = client;
this.initMockEntities();
this.activateMocking();
return {
client: this.basicClient,
user: this.basicUser,
team: this.basicTeam,
channel: this.basicChannel,
post: this.basicPost,
};
};
mockScheme = () => {
return {
name: this.generateId(),
description: this.generateId(),
scope: 'channel',
defaultchanneladminrole: false,
defaultchanneluserrole: false,
};
};
mockSchemeWithId = () => {
return {
...this.mockScheme(),
id: this.generateId(),
create_at: 1507840900004,
update_at: 1507840900004,
delete_at: 0,
};
};
tearDown = async () => {
nock.restore();
this.basicClient4 = null;
this.basicUser = null;
this.basicTeam = null;
this.basicTeamMember = null;
this.basicChannel = null;
this.basicChannelMember = null;
this.basicPost = null;
}
wait = (time) => new Promise((resolve) => setTimeout(resolve, time))
}
export default new TestHelper();