ソースを参照

:recycle: fix-up all backend tests

juan-auth-overpass
j 2年前
コミット
8711238839

+ 0
- 1
backend/lib/routes/membership/join.js ファイルの表示

@@ -49,7 +49,6 @@ module.exports = {
49 49
          */
50 50
         handler: async function (request, h) {
51 51
             try {
52
-                console.log('---')
53 52
                 const { membershipService } = request.server.services()
54 53
 
55 54
                 /** Grab payload info */

+ 3
- 2
backend/lib/services/profile/index.js ファイルの表示

@@ -119,12 +119,13 @@ module.exports = class ProfileService extends Schmervice.Service {
119 119
         // taking the info from profilesEntries
120 120
         // to repack into completeProfiles
121 121
         // in same order as profileIdArray
122
-        return profiler.makeOrderedCompleteProfiles(
122
+        const ordered = profiler.makeOrderedCompleteProfiles(
123 123
             profileIdArray,
124 124
             profilesEntries,
125 125
             type,
126 126
             this.tagLookup,
127 127
         )
128
+        return ordered
128 129
     }
129 130
 
130 131
     /**
@@ -293,7 +294,7 @@ module.exports = class ProfileService extends Schmervice.Service {
293 294
     }
294 295
 
295 296
     async calcProfileDistances(matchPool, distanceUnit, userZip) {
296
-        await Promise.all(
297
+        return await Promise.all(
297 298
             matchPool.map(async profile => {
298 299
                 const targetZip = zipcoder.getZipCodeFromProfile(profile)
299 300
                 if (!userZip || !targetZip)

+ 4
- 1
backend/lib/services/profile/profiler.js ファイルの表示

@@ -72,7 +72,10 @@ class CompleteProfile {
72 72
     }
73 73
 }
74 74
 const _makeCompleteProfile = (profileEntry, type, tagLookup) => {
75
-    profileEntry.tags = profileEntry.tags.map(tag => tagLookup[tag.tag_id])
75
+    // remap tags
76
+    if (profileEntry && profileEntry.tags.length) {
77
+        profileEntry.tags = profileEntry?.tags.map(tag => tagLookup[tag.tag_id])
78
+    }
76 79
     const complete = new CompleteProfile(profileEntry, type)
77 80
     return complete
78 81
 }

+ 11
- 0
backend/tests/matchqueue.spec.js ファイルの表示

@@ -60,6 +60,17 @@ test('path /<profile_id>/queue/<target_id> should return ok on PATCH', async t =
60 60
      */
61 61
     server.registerModel = () => {}
62 62
     server.models = () => ({ Aspect, AspectLabel, MatchQueue, Profile })
63
+
64
+    /**
65
+     * Register mock authentication just for testing
66
+     * -
67
+     * Profile plugin does not do this
68
+     */
69
+    server.auth.scheme('jwt', () => ({
70
+        authenticate: (req, h) => h.authenticated({ credentials: {} }),
71
+    }))
72
+    server.auth.strategy('default_jwt', 'jwt')
73
+
63 74
     /**
64 75
      * Register Routes and Services as usual
65 76
      */

+ 12
- 0
backend/tests/membership-join.spec.js ファイルの表示

@@ -56,7 +56,19 @@ test(`path ${pathToTest.url} should return two memberships`, async t => {
56 56
         Grouping,
57 57
         Membership,
58 58
     })
59
+
60
+    /**
61
+     * Register mock authentication just for testing
62
+     * -
63
+     * Profile plugin does not do this
64
+     */
65
+    server.auth.scheme('jwt', () => ({
66
+        authenticate: (req, h) => h.authenticated({ credentials: {} }),
67
+    }))
68
+    server.auth.strategy('default_jwt', 'jwt')
69
+
59 70
     await plugin.register(server)
71
+
60 72
     server.services()['profileService'] = new ProfileService(server)
61 73
     /**
62 74
      * TODO: adjust stubs to mock joinGrouping more closely

+ 32
- 18
backend/tests/membership.spec.js ファイルの表示

@@ -3,17 +3,17 @@
3 3
 const test = require('ava')
4 4
 const { stub } = require('sinon')
5 5
 const Hapi = require('@hapi/hapi')
6
+const Objection = require('objection')
6 7
 
7 8
 const plugin = require('../lib/plugins/membership')
8 9
 
9 10
 const ProfileService = require('../lib/services/profile')
10
-const MembershipService = require('../lib/services/membership')
11
+const UserService = require('../lib/services/user')
11 12
 
12 13
 const Profile = require('../lib/models/profile')
13 14
 const Grouping = require('../lib/models/grouping')
14 15
 const Membership = require('../lib/models/membership')
15 16
 const Tag = require('../lib/models/tag')
16
-const ZipCode = require('../lib/models/zip-code')
17 17
 const Aspect = require('../lib/models/aspect')
18 18
 const AspectLabel = require('../lib/models/aspect_label')
19 19
 
@@ -26,10 +26,10 @@ const params = {
26 26
 const mockReturn = {
27 27
     profile: {
28 28
         profile_id: 99,
29
-        user_id: 99,
30 29
         user: {
31
-            user_email: '',
32
-            user_name: 'bob',
30
+            user_id: 22,
31
+            user_email: 'jill@mail',
32
+            user_name: 'jill',
33 33
         },
34 34
         responses: [],
35 35
         tags: [],
@@ -50,7 +50,7 @@ const mockReturn = {
50 50
                 { val: 'foo', response_key_id: 11 },
51 51
                 { val: 'foo', response_key_id: 12 },
52 52
             ],
53
-            user: { user_name: 'phil' },
53
+            user: { user_id: 11, user_name: 'phil' },
54 54
         },
55 55
         {
56 56
             profile_id: 2,
@@ -63,7 +63,7 @@ const mockReturn = {
63 63
                 { val: 'foo', response_key_id: 11 },
64 64
                 { val: 'foo', response_key_id: 12 },
65 65
             ],
66
-            user: { user_name: 'gil' },
66
+            user: { user_id: 30, user_name: 'gil' },
67 67
         },
68 68
         {
69 69
             profile_id: 99,
@@ -76,7 +76,7 @@ const mockReturn = {
76 76
                 { val: 'foo', response_key_id: 11 },
77 77
                 { val: 'foo', response_key_id: 12 },
78 78
             ],
79
-            user: { user_name: 'jill' },
79
+            user: { user_id: 22, user_name: 'jill', user_email: 'jill@mail' },
80 80
         },
81 81
     ],
82 82
     tags: [],
@@ -123,7 +123,18 @@ test('path /<profile_id> should return ok', async t => {
123 123
         Tag,
124 124
     })
125 125
 
126
-    // server.registerService(ProfileService)
126
+    /**
127
+     * Register mock authentication just for testing
128
+     * -
129
+     * Profile plugin does not do this
130
+     */
131
+    server.auth.scheme('jwt', () => ({
132
+        authenticate: (req, h) => h.authenticated({ credentials: {} }),
133
+    }))
134
+    server.auth.strategy('default_jwt', 'jwt')
135
+
136
+    // stub(Objection, 'transaction').returns({})
137
+
127 138
     /**
128 139
      * Register Routes and Services as usual
129 140
      */
@@ -133,23 +144,26 @@ test('path /<profile_id> should return ok', async t => {
133 144
      * !: Janky - might be better to temp knex sqlite instance
134 145
      */
135 146
     server.services()['profileService'] = new ProfileService(server)
147
+    server.services()['profileService'].getTagsFor = () => []
148
+
149
+    // TODO: delete this and figure out stub for Profile
150
+    server.services()['profileService'].getProfilesFor = () =>
151
+        mockReturn.profiles
152
+
153
+    server.services()['userService'] = new UserService(server)
154
+    server.services()['userService'].findById = () => mockReturn.profile.user
155
+
136 156
     stub(server.models()['Tag'], 'query').returns(mockReturn.tags)
137 157
     stub(server.models()['AspectLabel'], 'query').returns(mockReturn.labels)
138 158
     stub(server.models()['Aspect'], 'query').returns(mockReturn.aspects)
139 159
     stub(server.models()['Grouping'], 'query').returns({
140 160
         whereIn: () => ({
141
-            withGraphFetched: () => {
142
-                return mockReturn.groupings
143
-            },
161
+            withGraphFetched: () => mockReturn.groupings,
144 162
         }),
145 163
     })
146 164
     stub(server.models()['Membership'], 'query').returns({
147
-        where: () => {
148
-            return mockReturn.memberships
149
-        },
150
-        whereIn: () => {
151
-            return mockReturn.memberships
152
-        },
165
+        where: () => mockReturn.memberships,
166
+        whereIn: () => mockReturn.memberships,
153 167
     })
154 168
     stub(server.models()['Profile'], 'query').returns({
155 169
         // Mocked for getProfile()

+ 13
- 1
backend/tests/profile.spec.js ファイルの表示

@@ -1,5 +1,4 @@
1 1
 'use strict'
2
-
3 2
 const test = require('ava')
4 3
 const { stub } = require('sinon')
5 4
 const Hapi = require('@hapi/hapi')
@@ -42,6 +41,7 @@ test('path /<profile_id> should return ok', async t => {
42 41
      * Schwifty at runtime.
43 42
      */
44 43
     const server = Hapi.server()
44
+
45 45
     /**
46 46
      * Overload so we don't register any models
47 47
      * using the plugin call (see plugins/profile.js)
@@ -49,10 +49,22 @@ test('path /<profile_id> should return ok', async t => {
49 49
      */
50 50
     server.registerModel = () => {}
51 51
     server.models = () => ({ Profile, Tag })
52
+
53
+    /**
54
+     * Register mock authentication just for testing
55
+     * -
56
+     * Profile plugin does not do this
57
+     */
58
+    server.auth.scheme('jwt', () => ({
59
+        authenticate: (req, h) => h.authenticated({ credentials: {} }),
60
+    }))
61
+    server.auth.strategy('default_jwt', 'jwt')
62
+
52 63
     /**
53 64
      * Register Routes and Services as usual
54 65
      */
55 66
     await plugin.register(server)
67
+
56 68
     /**
57 69
      * Replace Objection model methods with our own mock functions
58 70
      * !: Janky - might be better to temp knex sqlite instance

+ 11
- 0
backend/tests/respond.spec.js ファイルの表示

@@ -52,6 +52,17 @@ test('path /<profile_id>/respond should return ok on POST', async t => {
52 52
      */
53 53
     server.registerModel = () => {}
54 54
     server.models = () => ({ Response, ResponseKey })
55
+
56
+    /**
57
+     * Register mock authentication just for testing
58
+     * -
59
+     * Profile plugin does not do this
60
+     */
61
+    server.auth.scheme('jwt', () => ({
62
+        authenticate: (req, h) => h.authenticated({ credentials: {} }),
63
+    }))
64
+    server.auth.strategy('default_jwt', 'jwt')
65
+
55 66
     /**
56 67
      * Register Routes and Services as usual
57 68
      */

+ 33
- 1
backend/tests/score.spec.js ファイルの表示

@@ -13,7 +13,7 @@ const Aspect = require('../lib/models/aspect')
13 13
 const AspectLabel = require('../lib/models/aspect_label')
14 14
 
15 15
 // !: Must match the key set in servives/profile.js
16
-const zipcodeKey = 7
16
+const zipcodeKey = 10
17 17
 
18 18
 /**
19 19
  * Route parameters
@@ -30,6 +30,13 @@ const mockReturn = {
30 30
             responses: [
31 31
                 { response_key_id: 1, val: '100' },
32 32
                 { response_key_id: 2, val: '200' },
33
+                { response_key_id: 3, val: '200' },
34
+                { response_key_id: 4, val: '200' },
35
+                { response_key_id: 5, val: '200' },
36
+                { response_key_id: 6, val: '200' },
37
+                { response_key_id: 7, val: '200' },
38
+                { response_key_id: 8, val: '200' },
39
+                { response_key_id: 9, val: '200' },
33 40
                 { response_key_id: zipcodeKey, val: '90065' },
34 41
             ],
35 42
         },
@@ -39,6 +46,13 @@ const mockReturn = {
39 46
             responses: [
40 47
                 { response_key_id: 1, val: '100' },
41 48
                 { response_key_id: 2, val: '200' },
49
+                { response_key_id: 3, val: '200' },
50
+                { response_key_id: 4, val: '200' },
51
+                { response_key_id: 5, val: '200' },
52
+                { response_key_id: 6, val: '200' },
53
+                { response_key_id: 7, val: '200' },
54
+                { response_key_id: 8, val: '200' },
55
+                { response_key_id: 9, val: '200' },
42 56
                 { response_key_id: zipcodeKey, val: '97002' },
43 57
             ],
44 58
         },
@@ -48,6 +62,13 @@ const mockReturn = {
48 62
             responses: [
49 63
                 { response_key_id: 1, val: '200' },
50 64
                 { response_key_id: 2, val: '200' },
65
+                { response_key_id: 3, val: '200' },
66
+                { response_key_id: 4, val: '200' },
67
+                { response_key_id: 5, val: '200' },
68
+                { response_key_id: 6, val: '200' },
69
+                { response_key_id: 7, val: '200' },
70
+                { response_key_id: 8, val: '200' },
71
+                { response_key_id: 9, val: '200' },
51 72
                 { response_key_id: zipcodeKey, val: '96741' },
52 73
             ],
53 74
         },
@@ -93,6 +114,17 @@ test(`path ${pathToTest.url} should return ok on GET`, async t => {
93 114
         Profile,
94 115
         ZipCode,
95 116
     })
117
+
118
+    /**
119
+     * Register mock authentication just for testing
120
+     * -
121
+     * Profile plugin does not do this
122
+     */
123
+    server.auth.scheme('jwt', () => ({
124
+        authenticate: (req, h) => h.authenticated({ credentials: {} }),
125
+    }))
126
+    server.auth.strategy('default_jwt', 'jwt')
127
+
96 128
     /**
97 129
      * Register Routes and Services as usual
98 130
      */

+ 13
- 0
backend/tests/update.spec.js ファイルの表示

@@ -8,6 +8,7 @@ const plugin = require('../lib/plugins/profile')
8 8
 
9 9
 const Response = require('../lib/models/response')
10 10
 const ResponseKey = require('../lib/models/response-key')
11
+const ProfileService = require('../lib/services/profile')
11 12
 /**
12 13
  * Route parameters
13 14
  */
@@ -66,10 +67,22 @@ test(`path ${pathToTest.url} should return ok on ${pathToTest.method}`, async t
66 67
      */
67 68
     server.registerModel = () => {}
68 69
     server.models = () => ({ Response, ResponseKey })
70
+
71
+    /**
72
+     * Register mock authentication just for testing
73
+     * -
74
+     * Profile plugin does not do this
75
+     */
76
+    server.auth.scheme('jwt', () => ({
77
+        authenticate: (req, h) => h.authenticated({ credentials: {} }),
78
+    }))
79
+    server.auth.strategy('default_jwt', 'jwt')
80
+
69 81
     /**
70 82
      * Register Routes and Services as usual
71 83
      */
72 84
     await plugin.register(server)
85
+
73 86
     /**
74 87
      * Replace Objection model methods with our own mock functions
75 88
      * !: Janky - might be better to temp knex sqlite instance

読み込み中…
キャンセル
保存