This repository was archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
380 lines (323 loc) · 10.8 KB
/
test.js
File metadata and controls
380 lines (323 loc) · 10.8 KB
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';
let http = require('http');
let assert = require('chai').assert;
let koa = require('koa');
let koaRouter = require('koa-router');
let body = require('koa-body');
let util = require('kinda-util').create();
let Collection = require('kinda-collection');
let KindaRemoteRepository = require('./src');
suite('KindaRemoteRepository', function() {
let httpServer, repository, users;
let catchError = async function(fn) {
let err;
try {
await fn();
} catch (e) {
err = e;
}
return err;
};
suiteSetup(async function() {
let serverPort = 8888;
let server = koa();
server.use(body());
let router = koaRouter();
server.use(router.routes());
server.use(router.allowedMethods());
router.post('/authorizations', function *() {
let credentials = this.request.body;
if (!credentials) {
this.status = 403;
return;
}
if (credentials.username !== 'mvila@3base.com') {
this.status = 403;
return;
}
if (credentials.password !== 'password') {
this.status = 403;
return;
}
this.status = 201;
this.type = 'application/json';
this.body = JSON.stringify('12345678');
});
router.get('/authorizations/12345678', function *() {
this.status = 204;
});
router.del('/authorizations/12345678', function *() {
this.status = 204;
});
router.get('/authorizations/abcdefgh', function *() {
this.status = 403;
});
router.get('/', function *() {
this.body = {
repositoryId: 'a1b2c3d4e5'
};
});
router.get('/users/007', function *() {
let query = util.decodeValue(this.query);
if (query.authorization !== '12345678') {
this.status = 403;
return;
}
this.body = {
class: 'User',
value: { id: '007', firstName: 'James', age: 39 }
};
});
router.get('/users/aaa', function *() {
this.body = {
class: 'Superuser',
value: { id: 'aaa', firstName: 'Manu', age: 42, superpower: 'telepathy' }
};
});
router.get('/users/xyz', function *() {
let query = util.decodeValue(this.query);
if (query.errorIfMissing == null) query.errorIfMissing = true;
this.status = query.errorIfMissing ? 404 : 204;
});
router.post('/users', function *() {
let user = this.request.body;
user.id = 'bbb';
this.status = 201;
this.body = { class: 'User', value: user };
});
router.put('/users/bbb', function *() {
let user = this.request.body;
this.body = { class: 'User', value: user };
});
router.del('/users/ccc', function *() {
this.body = 1;
});
router.del('/users/xyz', function *() {
let query = util.decodeValue(this.query);
if (query.errorIfMissing == null) query.errorIfMissing = true;
if (query.errorIfMissing) {
this.status = 404;
} else {
this.body = 0;
}
});
router.post('/users/get-items', function *() {
let ids = this.request.body;
let results = ids.map(function(id) {
switch (id) {
case 'aaa':
return {
class: 'Superuser',
value: { id: 'aaa', firstName: 'Manu', age: 42, superpower: 'telepathy' }
};
case 'bbb':
return {
class: 'User',
value: { id: 'bbb', firstName: 'Vince', age: 43 }
};
default:
throw new Error('item not found');
}
});
this.status = 201;
this.body = results;
});
router.get('/users', function *() {
this.body = [
{
class: 'Superuser',
value: { id: 'aaa', firstName: 'Manu', age: 42, superpower: 'telepathy' }
},
{
class: 'User',
value: { id: 'bbb', firstName: 'Vince', age: 43 }
}
];
});
router.del('/users', function *() {
this.body = 3;
});
router.get('/users/count', function *() {
this.body = 2;
});
router.get('/users/count-retired', function *() {
this.body = 3;
});
router.get('/superusers/aaa/archive', function *() {
this.body = { ok: true };
});
router.post('/users/restore', function *() {
this.status = 201;
this.body = this.request.body;
});
httpServer = http.createServer(server.callback());
httpServer.listen(serverPort);
let Users = Collection.extend('Users', function() {
this.Item = this.Item.extend('User', function() {
this.addPrimaryKeyProperty('id', String);
this.addProperty('firstName', String);
this.addProperty('age', Number);
this.archive = async function() {
return await this.call('archive');
};
});
this.countRetired = async function() {
return await this.call('countRetired');
};
this.restore = async function(archive) {
return await this.call('restore', undefined, archive);
};
});
let Superusers = Users.extend('Superusers', function() {
this.Item = this.Item.extend('Superuser', function() {
this.addProperty('superpower', String);
});
});
let serverURL = 'http://localhost:' + serverPort;
repository = KindaRemoteRepository.create({
name: 'Test',
url: serverURL,
collections: [Users, Superusers]
});
users = repository.createCollection('Users');
});
suiteTeardown(async function() {
httpServer.close();
});
test('test authorization', async function() {
assert.isFalse(repository.isSignedIn);
let credentials = { username: 'mvila@3base.com', password: 'wrongpass' };
let authorization = await repository.signInWithCredentials(credentials);
assert.isUndefined(authorization);
assert.isFalse(repository.isSignedIn);
let err = await catchError(async function() {
await users.getItem('007');
});
assert.instanceOf(err, Error);
assert.strictEqual(err.statusCode, 403);
assert.isFalse(repository.isSignedIn);
credentials = { username: 'mvila@3base.com', password: 'password' };
authorization = await repository.signInWithCredentials(credentials);
assert.ok(authorization);
assert.isTrue(repository.isSignedIn);
let item = await users.getItem('007');
assert.strictEqual(item.id, '007');
assert.strictEqual(item.firstName, 'James');
assert.strictEqual(item.age, 39);
assert.isTrue(repository.isSignedIn);
await repository.signOut();
assert.isFalse(repository.isSignedIn);
err = await catchError(async function() {
await users.getItem('007');
});
assert.instanceOf(err, Error);
assert.strictEqual(err.statusCode, 403);
assert.isFalse(repository.isSignedIn);
authorization = await repository.signInWithAuthorization('abcdefgh');
assert.isFalse(authorization);
assert.isFalse(repository.isSignedIn);
assert.isFalse(repository.isSignedIn);
authorization = await repository.signInWithAuthorization('12345678');
assert.isTrue(authorization);
assert.isTrue(repository.isSignedIn);
item = await users.getItem('007');
assert.ok(item);
await repository.signOut();
});
test('get repository id', async function() {
let id = await repository.getRepositoryId();
assert.strictEqual(id, 'a1b2c3d4e5');
});
test('get an item', async function() {
let item = await users.getItem('aaa');
assert.strictEqual(item.class.name, 'Superuser');
assert.strictEqual(item.id, 'aaa');
assert.strictEqual(item.firstName, 'Manu');
assert.strictEqual(item.age, 42);
assert.strictEqual(item.superpower, 'telepathy');
let err = await catchError(async function() {
await users.getItem('xyz');
});
assert.instanceOf(err, Error);
assert.strictEqual(err.statusCode, 404);
item = await users.getItem('xyz', { errorIfMissing: false });
assert.isUndefined(item);
});
test('put an item', async function() {
let item = users.createItem({ firstName: 'Vince', age: 43 });
await item.save();
assert.strictEqual(item.id, 'bbb');
assert.strictEqual(item.firstName, 'Vince');
assert.strictEqual(item.age, 43);
item.age++;
await item.save();
assert.strictEqual(item.id, 'bbb');
assert.strictEqual(item.firstName, 'Vince');
assert.strictEqual(item.age, 44);
});
test('delete an item', async function() {
let err = await catchError(async function() {
await users.deleteItem('ccc');
});
assert.isUndefined(err);
err = await catchError(async function() {
await users.deleteItem('xyz');
});
assert.instanceOf(err, Error);
assert.strictEqual(err.statusCode, 404);
err = await catchError(async function() {
await users.deleteItem('xyz', { errorIfMissing: false });
});
assert.isUndefined(err);
});
test('get several items at once', async function() {
let items = await users.getItems(['aaa', 'bbb']);
assert.strictEqual(items.length, 2);
assert.strictEqual(items[0].class.name, 'Superuser');
assert.strictEqual(items[0].id, 'aaa');
assert.strictEqual(items[0].firstName, 'Manu');
assert.strictEqual(items[0].age, 42);
assert.strictEqual(items[0].superpower, 'telepathy');
assert.strictEqual(items[1].class.name, 'User');
assert.strictEqual(items[1].id, 'bbb');
assert.strictEqual(items[1].firstName, 'Vince');
assert.strictEqual(items[1].age, 43);
});
test('find items', async function() {
let items = await users.findItems();
assert.strictEqual(items.length, 2);
assert.strictEqual(items[0].class.name, 'Superuser');
assert.strictEqual(items[0].id, 'aaa');
assert.strictEqual(items[0].firstName, 'Manu');
assert.strictEqual(items[0].age, 42);
assert.strictEqual(items[0].superpower, 'telepathy');
assert.strictEqual(items[1].class.name, 'User');
assert.strictEqual(items[1].id, 'bbb');
assert.strictEqual(items[1].firstName, 'Vince');
assert.strictEqual(items[1].age, 43);
});
test('count items', async function() {
let count = await users.countItems();
assert.strictEqual(count, 2);
});
test('find and delete items', async function() {
let deletedItemsCount = await users.findAndDeleteItems({
start: 'bbb', end: 'ddd'
});
assert.strictEqual(deletedItemsCount, 3);
});
test('call custom method on a collection', async function() {
let count = await users.countRetired();
assert.strictEqual(count, 3);
});
test('call custom method on an item', async function() {
let item = await users.getItem('aaa');
let result = await item.archive();
assert.isTrue(result.ok);
});
test('call custom method with a body', async function() {
let archive = [{ id: 'aaa', firstName: 'Manu', age: 42 }];
let result = await users.restore(archive);
assert.deepEqual(result, archive);
});
});