|
|
@@ -0,0 +1,128 @@
|
|
|
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 Profile = require('../lib/models/profile')
|
|
|
10
|
+const ZipCode = require('../lib/models/zip-code')
|
|
|
11
|
+const MatchQueue = require('../lib/models/matchqueue')
|
|
|
12
|
+/**
|
|
|
13
|
+ * Route parameters
|
|
|
14
|
+ */
|
|
|
15
|
+const params = {
|
|
|
16
|
+ profile_id: 1,
|
|
|
17
|
+ max_distance: 1000,
|
|
|
18
|
+}
|
|
|
19
|
+const mockReturn = {
|
|
|
20
|
+ user: [
|
|
|
21
|
+ {
|
|
|
22
|
+ profile_id: 1,
|
|
|
23
|
+ user: { is_poster: 1 },
|
|
|
24
|
+ responses: [
|
|
|
25
|
+ { val: '120' },
|
|
|
26
|
+ { val: '200' },
|
|
|
27
|
+ { response_key_id: 16, val: '90065' },
|
|
|
28
|
+ ],
|
|
|
29
|
+ },
|
|
|
30
|
+ {
|
|
|
31
|
+ profile_id: 2,
|
|
|
32
|
+ user: { is_poster: 0 },
|
|
|
33
|
+ responses: [
|
|
|
34
|
+ { val: '120' },
|
|
|
35
|
+ { val: '200' },
|
|
|
36
|
+ { response_key_id: 16, val: '96741' },
|
|
|
37
|
+ ],
|
|
|
38
|
+ },
|
|
|
39
|
+ {
|
|
|
40
|
+ profile_id: 3,
|
|
|
41
|
+ user: { is_poster: 0 },
|
|
|
42
|
+ responses: [
|
|
|
43
|
+ { val: '180' },
|
|
|
44
|
+ { val: '140' },
|
|
|
45
|
+ { response_key_id: 16, val: '97002' },
|
|
|
46
|
+ ],
|
|
|
47
|
+ },
|
|
|
48
|
+ ],
|
|
|
49
|
+}
|
|
|
50
|
+const pathToTest = {
|
|
|
51
|
+ method: 'GET',
|
|
|
52
|
+ url: `/${params.profile_id}/score?max_distance=${params.max_distance}`,
|
|
|
53
|
+}
|
|
|
54
|
+
|
|
|
55
|
+test(`path ${pathToTest.path} should return ok on GET`, async t => {
|
|
|
56
|
+ /**
|
|
|
57
|
+ * Create a new server and register services,
|
|
|
58
|
+ * models and routes for testing
|
|
|
59
|
+ * -
|
|
|
60
|
+ * NOTE: We use a mocked registerModel() and register
|
|
|
61
|
+ * models manually. Normally this is handled by
|
|
|
62
|
+ * Schwifty at runtime.
|
|
|
63
|
+ */
|
|
|
64
|
+ const server = Hapi.server()
|
|
|
65
|
+ /**
|
|
|
66
|
+ * Overload so we don't register any models
|
|
|
67
|
+ * using the plugin call (see plugins/profile.js)
|
|
|
68
|
+ * and Manually load the model we need for the test
|
|
|
69
|
+ */
|
|
|
70
|
+ server.registerModel = () => {}
|
|
|
71
|
+ server.models = () => ({ MatchQueue, Profile, ZipCode })
|
|
|
72
|
+ /**
|
|
|
73
|
+ * Register Routes and Services as usual
|
|
|
74
|
+ */
|
|
|
75
|
+ await plugin.register(server)
|
|
|
76
|
+ /**
|
|
|
77
|
+ * Replace Objection model methods with our own mock functions
|
|
|
78
|
+ * !: Janky - might be better to temp knex sqlite instance
|
|
|
79
|
+ */
|
|
|
80
|
+ stub(server.models()['Profile'], 'query').returns({
|
|
|
81
|
+ // Mocked for pathToTest(), scoreProfilesFor()
|
|
|
82
|
+ findOne: () => ({
|
|
|
83
|
+ withGraphFetched: () => ({
|
|
|
84
|
+ withGraphFetched: () => mockReturn.user[0],
|
|
|
85
|
+ }),
|
|
|
86
|
+ }),
|
|
|
87
|
+ // Mocked for scoreProfilesFor()
|
|
|
88
|
+ withGraphFetched: () => ({
|
|
|
89
|
+ withGraphFetched: () => [mockReturn.user[1], mockReturn.user[2]],
|
|
|
90
|
+ }),
|
|
|
91
|
+ // Mocked for _getProfileIdsForUserId()
|
|
|
92
|
+ where: () => ({}),
|
|
|
93
|
+ // Mocked for getCompleteProfilesFor(), getProfilesFor()
|
|
|
94
|
+ whereIn: () => ({
|
|
|
95
|
+ withGraphFetched: () => ({}),
|
|
|
96
|
+ }),
|
|
|
97
|
+ // Mocked for deleteProfile()
|
|
|
98
|
+ delete: () => ({
|
|
|
99
|
+ where: () => ({}),
|
|
|
100
|
+ }),
|
|
|
101
|
+ })
|
|
|
102
|
+ stub(server.models()['ZipCode'], 'query').returns({
|
|
|
103
|
+ // Mocked for _latLonForZip()
|
|
|
104
|
+ findOne: () => ({
|
|
|
105
|
+ latitude: 38.0 + Math.random(),
|
|
|
106
|
+ longitude: -121.0 + Math.random(),
|
|
|
107
|
+ }),
|
|
|
108
|
+ })
|
|
|
109
|
+ stub(server.models()['MatchQueue'], 'query').returns({
|
|
|
110
|
+ patch: () => ({
|
|
|
111
|
+ where: () => ({}),
|
|
|
112
|
+ }),
|
|
|
113
|
+ insert: () => ({}),
|
|
|
114
|
+ where: () => ({
|
|
|
115
|
+ andWhere: () => ({}),
|
|
|
116
|
+ }),
|
|
|
117
|
+ })
|
|
|
118
|
+
|
|
|
119
|
+ /**
|
|
|
120
|
+ * Test the server with registered models and services
|
|
|
121
|
+ */
|
|
|
122
|
+ const { payload } = await server.inject(pathToTest)
|
|
|
123
|
+ const res = JSON.parse(payload)
|
|
|
124
|
+
|
|
|
125
|
+ t.deepEqual(res.ok, true)
|
|
|
126
|
+ t.is(res.data.length, 2)
|
|
|
127
|
+ t.is(res.data[0].profile_id, 3)
|
|
|
128
|
+})
|