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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import Client from 'client/client.jsx';
import WebSocketClient from 'client/websocket_client.jsx';
import jqd from 'jquery-deferred';
var HEADER_TOKEN = 'token';
class TestHelperClass {
basicClient = () => {
return this.basicc;
}
basicWebSocketClient = () => {
return this.basicwsc;
}
basicTeam = () => {
return this.basict;
}
basicUser = () => {
return this.basicu;
}
basicChannel = () => {
return this.basicch;
}
basicPost = () => {
return this.basicp;
}
generateId = () => {
// implementation taken from http://stackoverflow.com/a/2117523
var id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
id = id.replace(/[xy]/g, function replaceRandom(c) {
var r = Math.floor(Math.random() * 16);
var v;
if (c === 'x') {
v = r;
} else {
v = (r & 0x3) | 0x8;
}
return v.toString(16);
});
return 'uid' + id;
}
createClient() {
var c = new Client();
c.setUrl('http://localhost:8065');
c.useHeaderToken();
c.enableLogErrorsToConsole(true);
return c;
}
createWebSocketClient(token) {
var ws = new WebSocketClient();
ws.initialize('http://localhost:8065/api/v3/users/websocket', token);
return ws;
}
fakeEmail = () => {
return 'success' + this.generateId() + '@simulator.amazonses.com';
}
fakeUser = () => {
var user = {};
user.email = this.fakeEmail();
user.allow_marketing = true;
user.password = 'password1';
user.username = this.generateId();
return user;
}
fakeTeam = () => {
var team = {};
team.name = this.generateId();
team.display_name = `Unit Test ${team.name}`;
team.type = 'O';
team.email = this.fakeEmail();
team.allowed_domains = '';
return team;
}
fakeChannel = () => {
var channel = {};
channel.name = this.generateId();
channel.display_name = `Unit Test ${channel.name}`;
channel.type = 'O'; // open channel
return channel;
}
fakePost = () => {
var post = {};
post.message = `Unit Test ${this.generateId()}`;
return post;
}
initBasic = (callback, connectWS) => {
this.basicc = this.createClient();
function throwerror(err) {
throw err;
}
var d1 = jqd.Deferred();
var email = this.fakeEmail();
var user = this.fakeUser();
var team = this.fakeTeam();
team.email = email;
user.email = email;
var self = this;
this.basicClient().createUser(
user,
function(ruser) {
self.basicu = ruser;
self.basicu.password = user.password;
self.basicClient().login(
self.basicu.email,
self.basicu.password,
null,
function(data, res) {
if (connectWS) {
self.basicwsc = self.createWebSocketClient(res.header[HEADER_TOKEN]);
}
self.basicClient().useHeaderToken();
self.basicClient().createTeam(team,
function(rteam) {
self.basict = rteam;
self.basicClient().setTeamId(rteam.id);
var channel = self.fakeChannel();
channel.team_id = self.basicTeam().id;
self.basicClient().createChannel(
channel,
function(rchannel) {
self.basicch = rchannel;
var post = self.fakePost();
post.channel_id = rchannel.id;
self.basicClient().createPost(
post,
function(rpost) {
self.basicp = rpost;
d1.resolve();
},
throwerror
);
},
throwerror
);
},
throwerror
);
},
throwerror
);
},
throwerror
);
jqd.when(d1).done(() => {
callback();
});
}
}
var TestHelper = new TestHelperClass();
export default TestHelper;
|