|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+'use strict'
|
|
|
2
|
+
|
|
|
3
|
+const test = require('ava')
|
|
|
4
|
+const { stub } = require('sinon')
|
|
|
5
|
+const Hapi = require('@hapi/hapi')
|
|
|
6
|
+
|
|
|
7
|
+const plugin = require('../lib/plugins/profile')
|
|
|
8
|
+
|
|
|
9
|
+const MatchQueue = require('../lib/models/matchqueue')
|
|
|
10
|
+
|
|
|
11
|
+test('path / should return ok on GET', async t => {
|
|
|
12
|
+ /**
|
|
|
13
|
+ * Create a new server and register services,
|
|
|
14
|
+ * models and routes for testing
|
|
|
15
|
+ * -
|
|
|
16
|
+ * NOTE: We use a mocked registerModel() and register
|
|
|
17
|
+ * models manually. Normally this is handled by
|
|
|
18
|
+ * Schwifty at runtime.
|
|
|
19
|
+ */
|
|
|
20
|
+ const server = Hapi.server()
|
|
|
21
|
+ server.registerModel = () => {}
|
|
|
22
|
+ server.models = () => ({ MatchQueue })
|
|
|
23
|
+ await plugin.register(server)
|
|
|
24
|
+
|
|
|
25
|
+ /**
|
|
|
26
|
+ * Route parameters
|
|
|
27
|
+ */
|
|
|
28
|
+ const profile_id = 1
|
|
|
29
|
+ const target_id = 2
|
|
|
30
|
+ const reinsert = true
|
|
|
31
|
+
|
|
|
32
|
+ /**
|
|
|
33
|
+ * Replace Objection model methods with our own custom functions
|
|
|
34
|
+ * !: Kinda janky - might be better to temp knex sqlite instance
|
|
|
35
|
+ */
|
|
|
36
|
+ stub(server.models()['MatchQueue'], 'query').returns({
|
|
|
37
|
+ // Mocked for markAsDeleted()
|
|
|
38
|
+ patch: () => ({
|
|
|
39
|
+ where: () => ({
|
|
|
40
|
+ andWhere: () => ({
|
|
|
41
|
+ first: () => ({ patch: 'bop' }),
|
|
|
42
|
+ }),
|
|
|
43
|
+ }),
|
|
|
44
|
+ }),
|
|
|
45
|
+ insert: queueRecord => {
|
|
|
46
|
+ t.is(queueRecord.profile_id, profile_id)
|
|
|
47
|
+ t.is(queueRecord.target_id, target_id)
|
|
|
48
|
+ t.is(queueRecord.is_deleted, !reinsert)
|
|
|
49
|
+ },
|
|
|
50
|
+ // Mocked for getQueue()
|
|
|
51
|
+ where: () => ({
|
|
|
52
|
+ andWhere: (cmd, val) => {
|
|
|
53
|
+ return [
|
|
|
54
|
+ { mocked_profile_id: 1, is_deleted: val },
|
|
|
55
|
+ { mocked_profile_id: 2, is_deleted: val },
|
|
|
56
|
+ { mocked_profile_id: 3, is_deleted: val },
|
|
|
57
|
+ ]
|
|
|
58
|
+ },
|
|
|
59
|
+ }),
|
|
|
60
|
+ })
|
|
|
61
|
+
|
|
|
62
|
+ /**
|
|
|
63
|
+ * Test the server with registered models and services
|
|
|
64
|
+ */
|
|
|
65
|
+ const { payload } = await server.inject({
|
|
|
66
|
+ method: 'PATCH',
|
|
|
67
|
+ url: `/${profile_id}/queue/${target_id}/delete?reinsert=${reinsert}`,
|
|
|
68
|
+ })
|
|
|
69
|
+ const res = JSON.parse(payload)
|
|
|
70
|
+
|
|
|
71
|
+ t.deepEqual(res, [
|
|
|
72
|
+ { mocked_profile_id: 1, is_deleted: false },
|
|
|
73
|
+ { mocked_profile_id: 2, is_deleted: false },
|
|
|
74
|
+ { mocked_profile_id: 3, is_deleted: false },
|
|
|
75
|
+ ])
|
|
|
76
|
+})
|