-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathQB-CoreSpec.js
381 lines (318 loc) · 11.9 KB
/
QB-CoreSpec.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
'use strict';
var REST_REQUESTS_TIMEOUT = 10000;
var isNodeEnv = typeof window === 'undefined' && typeof exports === 'object';
var QuickBlox = isNodeEnv ? require('../src/qbMain') : window.QB;
var QB;
var QBtmp;
var CREDS = isNodeEnv ? require('./config').CREDS : window.CREDS;
var CONFIG = isNodeEnv ? require('./config').CONFIG : window.CONFIG;
var QBUser1 = isNodeEnv ? require('./config').QBUser1 : window.QBUser1;
describe('QB init tests', function () {
beforeEach(function () {
QB = new QuickBlox.QuickBlox();
});
it('1. can init SDK with session token and appId (without accountKey)', function () {
QB.init('56655ac9a0eb476d92002b66', CREDS.appId);
expect(QB.service.qbInst.config.creds.appId).toEqual(CREDS.appId);
});
///
it('2. can init SDK with appId, accountKey, config ', function () {
QB.initWithAppId(CREDS.appId, CREDS.accountKey, CONFIG);
expect(QB.service.qbInst.config.creds.appId).toEqual(CREDS.appId);
expect(QB.service.qbInst.config.creds.accountKey).toEqual(CREDS.accountKey);
expect(QB.service.qbInst.config.debug).toEqual(CONFIG.debug);
});
it('2.1. can init SDK with wrong type of appId', function () {
expect( function(){
QB.initWithAppId(CREDS.appId.toString(), CREDS.accountKey, CONFIG);
} ).toThrow(new Error('Type of appId must be a number'));
});
///
it('3. can init SDK with appId, authKey, authSecret, accountKey, config', function () {
QB.init(CREDS.appId, CREDS.authKey, CREDS.authSecret, CREDS.accountKey, CONFIG);
expect(QB.service.qbInst.config.creds.appId).toEqual(CREDS.appId);
expect(QB.service.qbInst.config.creds.authKey).toEqual(CREDS.authKey);
expect(QB.service.qbInst.config.creds.authSecret).toEqual(CREDS.authSecret);
expect(QB.service.qbInst.config.creds.accountKey).toEqual(CREDS.accountKey);
expect(QB.service.qbInst.config.debug).toEqual(CONFIG.debug);
});
it('4. can init SDK with appId, authKey, authSecret, config (without accountKey)', function () {
QB.init(CREDS.appId, CREDS.authKey, CREDS.authSecret, undefined, CONFIG);
expect(QB.service.qbInst.config.creds.appId).toEqual(CREDS.appId);
expect(QB.service.qbInst.config.creds.authKey).toEqual(CREDS.authKey);
expect(QB.service.qbInst.config.creds.authSecret).toEqual(CREDS.authSecret);
expect(QB.service.qbInst.config.debug).toEqual(CONFIG.debug);
});
});
describe('Ping tests', function () {
beforeAll(function (done) {
QB = new QuickBlox.QuickBlox();
QB.init(
CREDS.appId,
CREDS.authKey,
CREDS.authSecret,
CREDS.accountKey,
CONFIG
);
var chatConnectParams = {
userId: QBUser1.id,
password: QBUser1.password
};
console.log('Test SDK.... call QB-CoreSpec.js 62 line QB.chat.connect');
QB.chat.connect(chatConnectParams, function(err) {
if (err) {
done.fail('Connection to chat error: ' + JSON.stringify(err));
} else {
done();
}
});
});
afterAll(function () {
QB.chat.disconnect();
});
it('should ping shared chat server', function (done) {
QB.chat.ping(function (err) {
if (err) {
done.fail(err);
} else {
done();
}
});
});
it('should ping logged-in user and respond with pong', function (done) {
QB.chat.ping(QBUser1.id, function (err) {
if (err) {
done.fail(err);
} else {
done();
}
});
});
it('should return error within "pingTimeout"', function (done) {
QB.chat.ping(-1, function (err) {
expect(err).toEqual('No answer');
done();
});
});
});
describe('1. Session API', function () {
beforeAll(function () {
QB = new QuickBlox.QuickBlox();
QB.init(CREDS.appId, CREDS.authKey, CREDS.authSecret, CREDS.accountKey, CONFIG);
});
it('can create an App session', function (done) {
QB.createSession(function (err, session) {
if (err) {
done.fail('Create a session error: ' + JSON.stringify(err));
} else {
expect(session).not.toBeNull();
expect(session.application_id).toEqual(CREDS.appId);
done();
}
});
}, REST_REQUESTS_TIMEOUT);
it('can create a User session', function (done) {
QB.createSession(QBUser1, function (err, session) {
if (err) {
done.fail("Create a User session error: " + JSON.stringify(err));
} else {
expect(session).not.toBeNull();
expect(session.application_id).toEqual(CREDS.appId);
expect(session.user_id).toEqual(QBUser1.id);
done();
}
});
}, REST_REQUESTS_TIMEOUT);
it('can login a user', function (done) {
QB.login(QBUser1, function (err, user) {
if (err) {
done.fail("Login user error2: " + JSON.stringify(err));
} else {
expect(user).not.toBeNull();
expect(user.login).toEqual(QBUser1.login);
expect(user.id).toEqual(QBUser1.id);
done();
}
});
}, REST_REQUESTS_TIMEOUT);
it('can logout a user', function (done) {
QB.logout(function (err, result) {
if (err) {
done.fail("Logout user error3: " + JSON.stringify(err));
} else {
expect(null).toBeNull(); /** we just have to have some expectations. */
done();
}
});
}, REST_REQUESTS_TIMEOUT);
it('can destroy a session', function (done) {
QB.destroySession(function (err, result) {
if (err) {
done.fail("Destroy session error2: " + JSON.stringify(err));
} else {
expect(QB.service.qbInst.session).toBeNull();
done();
}
});
}, REST_REQUESTS_TIMEOUT);
it('can connect to custom domains', function () {
/** Test old way to set domains */
var CUSTOMCONFIG = {
endpoints: {
api: 'apicustomdomain.quickblox.com',
chat: 'chatcustomdomain.quickblox.com',
muc: 'muc.chatcustomdomain.quickblox.com'
},
chatProtocol: {
bosh: 'https://chatcustomdomain.quickblox.com:5281',
websocket: 'wss://chatcustomdomain.quickblox.com:5291'
}
};
QB.init(CREDS.appId, CREDS.authKey, CREDS.authSecret, CREDS.accountKey, CUSTOMCONFIG);
expect(QB.service.qbInst.config.endpoints.api).toEqual('apicustomdomain.quickblox.com');
expect(QB.service.qbInst.config.endpoints.chat).toEqual('chatcustomdomain.quickblox.com');
expect(QB.service.qbInst.config.endpoints.muc).toEqual('muc.chatcustomdomain.quickblox.com');
expect(QB.service.qbInst.config.chatProtocol.bosh).toEqual('https://chatcustomdomain.quickblox.com:5281');
expect(QB.service.qbInst.config.chatProtocol.websocket).toEqual('wss://chatcustomdomain.quickblox.com:5291');
/** Test new way to set domains */
var CUSTOMCONFIG2 = {
endpoints: {
api: 'apicustomdomain2.quickblox.com',
chat: 'chatcustomdomain2.quickblox.com'
}
};
QB.init(CREDS.appId, CREDS.authKey, CREDS.authSecret, CREDS.accountKey, CUSTOMCONFIG2);
expect(QB.service.qbInst.config.endpoints.api).toEqual('apicustomdomain2.quickblox.com');
expect(QB.service.qbInst.config.endpoints.chat).toEqual('chatcustomdomain2.quickblox.com');
expect(QB.service.qbInst.config.endpoints.muc).toEqual('muc.chatcustomdomain2.quickblox.com');
expect(QB.service.qbInst.config.chatProtocol.bosh).toEqual('https://chatcustomdomain2.quickblox.com:5281');
expect(QB.service.qbInst.config.chatProtocol.websocket).toEqual('wss://chatcustomdomain2.quickblox.com:5291');
/** return back to default domains */
var DEFAULTCONFIG = {
endpoints: {
api: 'api.quickblox.com',
chat: 'chat.quickblox.com'
}
};
QB.init(CREDS.appId, CREDS.authKey, CREDS.authSecret, CREDS.accountKey, DEFAULTCONFIG);
expect(QB.service.qbInst.config.endpoints.api).toEqual('api.quickblox.com');
expect(QB.service.qbInst.config.endpoints.chat).toEqual('chat.quickblox.com');
expect(QB.service.qbInst.config.endpoints.muc).toEqual('muc.chat.quickblox.com');
expect(QB.service.qbInst.config.chatProtocol.bosh).toEqual('https://chat.quickblox.com:5281');
expect(QB.service.qbInst.config.chatProtocol.websocket).toEqual('wss://chat.quickblox.com:5291');
});
});
describe('2. Session API. Init with User Session token tests', function () {
beforeAll(function () {
QB = new QuickBlox.QuickBlox();
QB.initWithAppId(CREDS.appId, CREDS.accountKey, CONFIG);
});
// afterAll(function () {
// QB.chat.disconnect();
// QB.destroySession(function (err, result) {
//
// });
// });
it(`0.0. get session token for tests token value is ${CREDS.sessionToken}`, function () {
//
//
QBtmp = new QuickBlox.QuickBlox();
QBtmp.init(
CREDS.appId,
CREDS.authKey,
CREDS.authSecret,
CREDS.accountKey,
CONFIG
);
//get the user session token
QBtmp.createSession(QBUser1, function (err, session) {
if (err) {
done.fail("Create a User session error: " + JSON.stringify(err));
} else {
expect(session).not.toBeNull();
expect(session.application_id).toEqual(CREDS.appId);
expect(session.user_id).toEqual(QBUser1.id);
expect(session.token).not.toBeUndefined();
}
});
//
//
});
it('1.1. can not call startSessionWithToken without callback function', function () {
expect( function(){
QB.startSessionWithToken('56655ac9a0eb476d92002b66' , null);
} ).toThrow(new Error('Cannot start session without callback function'));
});
it('1.2. can not call startSessionWithToken with null value for session token', function () {
expect( function(){
QB.startSessionWithToken(null, function (err, session){});
} ).toThrow(new Error('Cannot start session with null value token'));
});
it('1.3. can not call startSessionWithToken with empty string value for session token', function () {
expect( function(){
QB.startSessionWithToken('', function (err, session){});
} ).toThrow(new Error('Cannot start session with empty string token'));
});
it(`2.can start a session with token from user session`, function () {
//
//
QBtmp = new QuickBlox.QuickBlox();
QBtmp.init(
CREDS.appId,
CREDS.authKey,
CREDS.authSecret,
CREDS.accountKey,
CONFIG
);
//get the user session token
QBtmp.createSession(QBUser1, function (err, usrSession) {
if (err) {
done.fail("Create a User session error: " + JSON.stringify(err));
} else {
expect(usrSession).not.toBeNull();
expect(usrSession.application_id).toEqual(CREDS.appId);
expect(usrSession.user_id).toEqual(QBUser1.id);
expect(usrSession.token).not.toBeUndefined();
//
QB.startSessionWithToken(usrSession.token, function (err, openSession) {
if (err) {
done.fail('Start a session with token error: ' + JSON.stringify(err));
} else {
expect(openSession).not.toBeNull();
expect(openSession.application_id).toEqual(CREDS.appId);
expect(openSession.user_id).toEqual(QBUser1.id);
expect(openSession.token).not.toBeUndefined();
//connect to chat
var chatConnectParams = {
userId: QBUser1.id,
password: openSession.token
};
console.log('Test SDK.... call QB-CoreSpec.js 296 line QB.chat.connect');
QB.chat.connect(chatConnectParams, function(err) {
if (err) {
done.fail('Connection to chat error: ' + JSON.stringify(err));
} else {
done('Test SDK.... call QB-CoreSpec.js 410 line QB.chat.connect');
}
});
//
}
});
//
done();
}
});
//
//
});
xit('3. can destroy a session', function (done) {
QB.destroySession(function (err, result) {
if (err) {
done.fail("Destroy session error: " + JSON.stringify(err));
} else {
expect(QB.service.qbInst.session).toBeNull();
done();
}
});
}, REST_REQUESTS_TIMEOUT);
});