forked from mattermost/mattermost-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helper.js
140 lines (110 loc) · 3.39 KB
/
test_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import assert from 'assert';
import Config from 'assets/config.json';
import Client from 'mattermost-redux/client/client';
const PASSWORD = 'password1';
class TestHelper {
constructor() {
this.basicClient = null;
this.basicUser = null;
this.basicTeam = null;
this.basicChannel = null;
this.basicPost = null;
}
assertStatusOkay = (data) => {
assert(data);
assert(data.status === 'OK');
};
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;
};
createClient = () => {
const client = new Client();
client.setUrl(Config.DefaultServerUrl);
return client;
};
fakeEmail = () => {
return 'success' + this.generateId() + '@simulator.amazonses.com';
};
fakeUser = () => {
return {
email: this.fakeEmail(),
allow_marketing: true,
password: PASSWORD,
username: this.generateId()
};
};
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
};
};
fakeChannel = (teamId) => {
const name = this.generateId();
return {
name,
team_id: teamId,
display_name: `Unit Test ${name}`,
type: 'O'
};
};
fakeChannelMember = (userId, channelId) => {
return {
user_id: userId,
channel_id: channelId,
notify_props: {},
roles: 'system_user'
};
};
fakePost = (channelId) => {
return {
channel_id: channelId,
message: `Unit Test ${this.generateId()}`
};
};
initBasic = async (client = this.createClient()) => {
client.setUrl(Config.TestServerUrl || Config.DefaultServerUrl);
this.basicClient = client;
this.basicUser = await client.createUser(this.fakeUser());
await client.login(this.basicUser.email, PASSWORD);
this.basicTeam = await client.createTeam(this.fakeTeam());
this.basicChannel = await client.createChannel(this.fakeChannel(this.basicTeam.id));
this.basicPost = await client.createPost(this.basicTeam.id, this.fakePost(this.basicChannel.id));
return {
client: this.basicClient,
user: this.basicUser,
team: this.basicTeam,
channel: this.basicChannel,
post: this.basicPost
};
};
wait = () => {
return new Promise((resolve) => {
setTimeout(() => resolve(), 1000);
});
}
}
export default new TestHelper();