Procházet zdrojové kódy

Merge branch 'brian_tests' of fyindr/siimee into dev

brian_lifecycle_docs
maeda před 2 roky
rodič
revize
2a45594bb7

+ 35
- 28
backend/lib/routes/membership/active.js Zobrazit soubor

81
             const groupingIds = groupings.map(grouping => grouping.grouping_id)
81
             const groupingIds = groupings.map(grouping => grouping.grouping_id)
82
             const memberships =
82
             const memberships =
83
                 await membershipService.findMemberships(groupingIds)
83
                 await membershipService.findMemberships(groupingIds)
84
-            const profileIds = memberships
85
-                .filter(membership => membership.profile_id != profileId)
84
+            let profileIds = memberships
85
+                .filter(membership => membership.profile_id !== profileId)
86
                 .map(membership => membership.profile_id)
86
                 .map(membership => membership.profile_id)
87
-
87
+            profileIds =
88
+                !profileIds.length || profileIds[0] === undefined
89
+                    ? []
90
+                    : profileIds
88
             /** Assemble complete profiles to reference and pass */
91
             /** Assemble complete profiles to reference and pass */
89
             const completedProfiles = !profileIds.length
92
             const completedProfiles = !profileIds.length
90
                 ? []
93
                 ? []
91
                 : await profileService.getProfilesFor(profileIds, 'participant')
94
                 : await profileService.getProfilesFor(profileIds, 'participant')
92
-
93
             /**
95
             /**
94
              * Heavily process the result by storing just a profile_id
96
              * Heavily process the result by storing just a profile_id
95
              * and attach complete profiles
97
              * and attach complete profiles
96
              * !: This still assumes only ONE other profile
98
              * !: This still assumes only ONE other profile
97
              * TODO: should be refactored to many other profiles
99
              * TODO: should be refactored to many other profiles
98
              */
100
              */
99
-            const reformattedGroupings = groupings.length
100
-                ? groupings.map(grouping => {
101
-                      const otherPid = grouping.profiles.find(
102
-                          p => p.profile_id != profileId,
103
-                      ).profile_id
104
-                      grouping.profile = completedProfiles.find(
105
-                          p => otherPid == p.profile_id,
106
-                      )
107
-                      grouping.is_paired = _activeGroupingIds(
108
-                          memberships,
109
-                      ).includes(grouping.grouping_id)
110
-                      delete grouping.profiles
111
-                      return grouping
112
-                  })
113
-                : []
114
-
101
+            const reformattedGroupings =
102
+                groupings.length && completedProfiles.length
103
+                    ? groupings.map(grouping => {
104
+                          const otherPid = grouping.profiles.find(
105
+                              p => p.profile_id != profileId,
106
+                          ).profile_id
107
+                          grouping.profile = completedProfiles.find(
108
+                              p => otherPid == p.profile_id,
109
+                          )
110
+                          grouping.is_paired = _activeGroupingIds(
111
+                              memberships,
112
+                          ).includes(grouping.grouping_id)
113
+                          delete grouping.profiles
114
+                          return grouping
115
+                      })
116
+                    : []
115
             /** Grabs revealTags */
117
             /** Grabs revealTags */
116
-            const revealTags = await profileService.getTagsFor(
117
-                profileIds,
118
-                groupingIds,
119
-                'reveal',
120
-            )
118
+            const revealTags =
119
+                profileIds.length && groupingIds.length
120
+                    ? await profileService.getTagsFor(
121
+                          profileIds,
122
+                          groupingIds,
123
+                          'reveal',
124
+                      )
125
+                    : undefined
121
 
126
 
122
             /** If the revealTags exist, the completedProfile's hidden info is
127
             /** If the revealTags exist, the completedProfile's hidden info is
123
              * removed and replaced with the completedProfile's user information
128
              * removed and replaced with the completedProfile's user information
124
              * Otherwise the completedProfiles remain unchanged
129
              * Otherwise the completedProfiles remain unchanged
125
              */
130
              */
126
-            const user = await userService.findById(
127
-                completedProfiles.map(p => p.user_id),
128
-            )
131
+            const user = completedProfiles.length
132
+                ? await userService.findById(
133
+                      completedProfiles.map(p => p.user_id),
134
+                  )
135
+                : undefined
129
 
136
 
130
             // TODO: Refactor this. Is it safe to always use completedProfiles[0]?
137
             // TODO: Refactor this. Is it safe to always use completedProfiles[0]?
131
             if (revealTags && user) {
138
             if (revealTags && user) {

+ 2
- 1
backend/lib/routes/profile/get.js Zobrazit soubor

28
     options: {
28
     options: {
29
         ...pluginConfig.docs,
29
         ...pluginConfig.docs,
30
         tags: ['api'],
30
         tags: ['api'],
31
-        auth: 'default_jwt',
31
+        // auth: 'default_jwt',
32
+        auth: false,
32
         cors: true,
33
         cors: true,
33
         handler: async function (request, h) {
34
         handler: async function (request, h) {
34
             const { profile_id } = request.params
35
             const { profile_id } = request.params

+ 1
- 3
backend/lib/routes/profile/score.js Zobrazit soubor

53
             const distanceUnit = request.query.unit
53
             const distanceUnit = request.query.unit
54
                 ? request.query.unit
54
                 ? request.query.unit
55
                 : 'mile'
55
                 : 'mile'
56
-            const duration = request.query.duration.includes('-')
57
-                ? request.query.duration.split('-')[0]
58
-                : request.query.duration
56
+            const duration = request.query.duration
59
             const presence =
57
             const presence =
60
                 request.query.presence === 'in_person'
58
                 request.query.presence === 'in_person'
61
                     ? 'onsite'
59
                     ? 'onsite'

+ 14
- 1
backend/lib/services/profile/index.js Zobrazit soubor

34
                 if (!desc.is_active) return
34
                 if (!desc.is_active) return
35
                 this.tagLookup[desc.tag_id] = desc
35
                 this.tagLookup[desc.tag_id] = desc
36
             })
36
             })
37
+            /* EXAMPLE:
38
+             * this.tagLookup : {
39
+             * '1' : {
40
+             * tag_id: 1,
41
+             * tag_category: 'reveal',
42
+             * tag_description: 'verified',
43
+             * is_active: 1
44
+             * }
45
+             * }
46
+             * */
37
         } else return
47
         } else return
38
     }
48
     }
39
     /**
49
     /**
368
         if (!profileId.length || !groupingId.length) {
378
         if (!profileId.length || !groupingId.length) {
369
             return associations
379
             return associations
370
         } else {
380
         } else {
381
+            // NOTE: all queries do not fire in Swagger API (whereIn clauses??)
371
             associations = groupingId
382
             associations = groupingId
372
                 ? await TagAssociation.query()
383
                 ? await TagAssociation.query()
373
                       .where('grouping_id', groupingId)
384
                       .where('grouping_id', groupingId)
376
             return associations
387
             return associations
377
                 .map(assoc => ({
388
                 .map(assoc => ({
378
                     ...assoc,
389
                     ...assoc,
379
-                    tag: this.tagLookup[assoc.tag_id],
390
+                    tag: !this.tagLookup[assoc.tag_id]
391
+                        ? {}
392
+                        : this.tagLookup[assoc.tag_id],
380
                 }))
393
                 }))
381
                 .filter(tagWithAssoc => {
394
                 .filter(tagWithAssoc => {
382
                     return category
395
                     return category

+ 1
- 1
backend/lib/services/user.js Zobrazit soubor

9
 // Configuration for Brevo
9
 // Configuration for Brevo
10
 const Brevo = require('@getbrevo/brevo')
10
 const Brevo = require('@getbrevo/brevo')
11
 const apiInstance = new Brevo.TransactionalEmailsApi()
11
 const apiInstance = new Brevo.TransactionalEmailsApi()
12
-const apiKey = apiInstance.authentications.apiKey
12
+const apiKey = apiInstance.apiClient.authentications['api-key']
13
 apiKey.apiKey = process.env.BREVO_KEY
13
 apiKey.apiKey = process.env.BREVO_KEY
14
 const sendSmtpEmail = new Brevo.SendSmtpEmail()
14
 const sendSmtpEmail = new Brevo.SendSmtpEmail()
15
 
15
 

+ 69
- 4
backend/tests/membership.spec.js Zobrazit soubor

7
 const plugin = require('../lib/plugins/membership')
7
 const plugin = require('../lib/plugins/membership')
8
 
8
 
9
 const ProfileService = require('../lib/services/profile')
9
 const ProfileService = require('../lib/services/profile')
10
+const UserService = require('../lib/services/user')
10
 const MembershipService = require('../lib/services/membership')
11
 const MembershipService = require('../lib/services/membership')
11
 
12
 
12
 const Profile = require('../lib/models/profile')
13
 const Profile = require('../lib/models/profile')
13
 const Grouping = require('../lib/models/grouping')
14
 const Grouping = require('../lib/models/grouping')
14
 const Membership = require('../lib/models/membership')
15
 const Membership = require('../lib/models/membership')
15
 const Tag = require('../lib/models/tag')
16
 const Tag = require('../lib/models/tag')
16
-const ZipCode = require('../lib/models/zip-code')
17
+const TagAssociation = require('../lib/models/tag-association')
18
+const User = require('../lib/models/user')
17
 const Aspect = require('../lib/models/aspect')
19
 const Aspect = require('../lib/models/aspect')
18
 const AspectLabel = require('../lib/models/aspect_label')
20
 const AspectLabel = require('../lib/models/aspect_label')
19
 
21
 
34
         responses: [],
36
         responses: [],
35
         tags: [],
37
         tags: [],
36
     },
38
     },
37
-    memberships: [{ membership_id: 1, grouping_id: 1 }],
39
+    memberships: [
40
+        { membership_id: 1, grouping_id: 1, profile_id: 99 },
41
+        { membership_id: 1, grouping_id: 1, profile_id: 1 },
42
+        { membership_id: 1, grouping_id: 1, profile_id: 2 },
43
+    ],
38
     groupings: [
44
     groupings: [
39
         { grouping_id: 1, profiles: [{ profile_id: 1 }, { profile_id: 99 }] },
45
         { grouping_id: 1, profiles: [{ profile_id: 1 }, { profile_id: 99 }] },
40
     ],
46
     ],
79
             user: { user_name: 'jill' },
85
             user: { user_name: 'jill' },
80
         },
86
         },
81
     ],
87
     ],
82
-    tags: [],
88
+    tag_associations: [
89
+        {
90
+            tag_association_id: 1,
91
+            profile_id: 99,
92
+            grouping_id: 1,
93
+            tag_id: 7,
94
+            is_deleted: 0,
95
+        },
96
+        {
97
+            tag_association_id: 2,
98
+            profile_id: 1,
99
+            grouping_id: 1,
100
+            tag_id: 8,
101
+            is_deleted: 0,
102
+        },
103
+    ],
104
+    tags: [
105
+        {
106
+            tag_id: 7,
107
+            tag_category: 'reveal',
108
+            tag_description: 'user_name',
109
+            is_active: 1,
110
+        },
111
+        {
112
+            tag_id: 8,
113
+            tag_category: 'reveal',
114
+            tag_description: 'user_email',
115
+            is_active: 1,
116
+        },
117
+    ],
83
     labels: [
118
     labels: [
84
         { aspect_id: 1, a: 100, b: 100 },
119
         { aspect_id: 1, a: 100, b: 100 },
85
         { aspect_id: 2, a: 100, b: 200 },
120
         { aspect_id: 2, a: 100, b: 200 },
92
         { aspect_id: 3, 1: 111, 2: 112, 3: 113, 4: 114 },
127
         { aspect_id: 3, 1: 111, 2: 112, 3: 113, 4: 114 },
93
         { aspect_id: 4, 1: 111, 2: 111, 3: 111, 4: 111 },
128
         { aspect_id: 4, 1: 111, 2: 111, 3: 111, 4: 111 },
94
     ],
129
     ],
130
+    user: [
131
+        {
132
+            user_id: 99,
133
+            user_name: 'bob',
134
+            user_email: 'bob@testemail.com',
135
+            is_admin: 0,
136
+            is_poster: 0,
137
+            is_verified: 0,
138
+        },
139
+    ],
95
 }
140
 }
96
 const pathToTest = {
141
 const pathToTest = {
97
     method: 'GET',
142
     method: 'GET',
121
         Membership,
166
         Membership,
122
         Grouping,
167
         Grouping,
123
         Tag,
168
         Tag,
169
+        TagAssociation,
170
+        User,
124
     })
171
     })
125
 
172
 
126
-    // server.registerService(ProfileService)
127
     /**
173
     /**
128
      * Register Routes and Services as usual
174
      * Register Routes and Services as usual
129
      */
175
      */
133
      * !: Janky - might be better to temp knex sqlite instance
179
      * !: Janky - might be better to temp knex sqlite instance
134
      */
180
      */
135
     server.services()['profileService'] = new ProfileService(server)
181
     server.services()['profileService'] = new ProfileService(server)
182
+    server.services()['membershipService'] = new MembershipService(server)
183
+    server.services()['userService'] = new UserService(server)
184
+
136
     stub(server.models()['Tag'], 'query').returns(mockReturn.tags)
185
     stub(server.models()['Tag'], 'query').returns(mockReturn.tags)
137
     stub(server.models()['AspectLabel'], 'query').returns(mockReturn.labels)
186
     stub(server.models()['AspectLabel'], 'query').returns(mockReturn.labels)
138
     stub(server.models()['Aspect'], 'query').returns(mockReturn.aspects)
187
     stub(server.models()['Aspect'], 'query').returns(mockReturn.aspects)
188
+    stub(server.models()['User'], 'query').returns({
189
+        throwIfNotFound: () => ({
190
+            first: () => ({
191
+                where: () => {
192
+                    return mockReturn.user
193
+                },
194
+            }),
195
+        }),
196
+    })
139
     stub(server.models()['Grouping'], 'query').returns({
197
     stub(server.models()['Grouping'], 'query').returns({
140
         whereIn: () => ({
198
         whereIn: () => ({
141
             withGraphFetched: () => {
199
             withGraphFetched: () => {
151
             return mockReturn.memberships
209
             return mockReturn.memberships
152
         },
210
         },
153
     })
211
     })
212
+    stub(server.models()['TagAssociation'], 'query').returns({
213
+        where: () => ({
214
+            andWhere: () => {
215
+                return mockReturn.tag_associations
216
+            },
217
+        }),
218
+    })
154
     stub(server.models()['Profile'], 'query').returns({
219
     stub(server.models()['Profile'], 'query').returns({
155
         // Mocked for getProfile()
220
         // Mocked for getProfile()
156
         where: () => ({
221
         where: () => ({

+ 14
- 3
backend/tests/score.spec.js Zobrazit soubor

12
 const Aspect = require('../lib/models/aspect')
12
 const Aspect = require('../lib/models/aspect')
13
 const AspectLabel = require('../lib/models/aspect_label')
13
 const AspectLabel = require('../lib/models/aspect_label')
14
 
14
 
15
-// !: Must match the key set in servives/profile.js
16
-const zipcodeKey = 7
15
+// !: Must match the key set in db/data-generator/config.json
16
+const zipcodeKey = 10
17
+const durationKey = 14
18
+const presenceKey = 15
17
 
19
 
18
 /**
20
 /**
19
  * Route parameters
21
  * Route parameters
21
 const params = {
23
 const params = {
22
     profile_id: 1,
24
     profile_id: 1,
23
     max_distance: 1000,
25
     max_distance: 1000,
26
+    duration: 'full-time',
27
+    presence: 'remote',
24
 }
28
 }
29
+
25
 const mockReturn = {
30
 const mockReturn = {
26
     user: [
31
     user: [
27
         {
32
         {
31
                 { response_key_id: 1, val: '100' },
36
                 { response_key_id: 1, val: '100' },
32
                 { response_key_id: 2, val: '200' },
37
                 { response_key_id: 2, val: '200' },
33
                 { response_key_id: zipcodeKey, val: '90065' },
38
                 { response_key_id: zipcodeKey, val: '90065' },
39
+                { response_key_id: durationKey, val: 'full-time' },
40
+                { response_key_id: presenceKey, val: 'remote' },
34
             ],
41
             ],
35
         },
42
         },
36
         {
43
         {
40
                 { response_key_id: 1, val: '100' },
47
                 { response_key_id: 1, val: '100' },
41
                 { response_key_id: 2, val: '200' },
48
                 { response_key_id: 2, val: '200' },
42
                 { response_key_id: zipcodeKey, val: '97002' },
49
                 { response_key_id: zipcodeKey, val: '97002' },
50
+                { response_key_id: durationKey, val: 'full-time' },
51
+                { response_key_id: presenceKey, val: 'remote' },
43
             ],
52
             ],
44
         },
53
         },
45
         {
54
         {
49
                 { response_key_id: 1, val: '200' },
58
                 { response_key_id: 1, val: '200' },
50
                 { response_key_id: 2, val: '200' },
59
                 { response_key_id: 2, val: '200' },
51
                 { response_key_id: zipcodeKey, val: '96741' },
60
                 { response_key_id: zipcodeKey, val: '96741' },
61
+                { response_key_id: durationKey, val: 'full-time' },
62
+                { response_key_id: presenceKey, val: 'remote' },
52
             ],
63
             ],
53
         },
64
         },
54
     ],
65
     ],
67
 }
78
 }
68
 const pathToTest = {
79
 const pathToTest = {
69
     method: 'GET',
80
     method: 'GET',
70
-    url: `/${params.profile_id}/score?max_distance=${params.max_distance}`,
81
+    url: `/${params.profile_id}/score?max_distance=${params.max_distance}&duration=${params.duration}&presence=${params.presence}`,
71
 }
82
 }
72
 
83
 
73
 test(`path ${pathToTest.url} should return ok on GET`, async t => {
84
 test(`path ${pathToTest.url} should return ok on GET`, async t => {

+ 24
- 15
backend/tests/user-send-email.spec.js Zobrazit soubor

6
  */
6
  */
7
 
7
 
8
 // Change email here to your actual email
8
 // Change email here to your actual email
9
-// const email = 'myalias@myactualemail.com'
9
+let email = undefined
10
+// email = 'myalias@myactualemail.com'
10
 
11
 
11
 const test = require('ava')
12
 const test = require('ava')
12
 const { stub } = require('sinon')
13
 const { stub } = require('sinon')
16
 
17
 
17
 // Necessary Dependencies/Configurations for Brevo Transac Email
18
 // Necessary Dependencies/Configurations for Brevo Transac Email
18
 const crypto = require('crypto')
19
 const crypto = require('crypto')
19
-const SibApiV3Sdk = require('sib-api-v3-sdk')
20
-const defaultClient = SibApiV3Sdk.ApiClient.instance
21
-const apiKey = defaultClient.authentications['api-key']
20
+const Brevo = require('@getbrevo/brevo')
21
+const apiInstance = new Brevo.TransactionalEmailsApi()
22
+const apiKey = apiInstance.apiClient.authentications['api-key']
22
 apiKey.apiKey = process.env.BREVO_KEY
23
 apiKey.apiKey = process.env.BREVO_KEY
23
-const apiInstance = new SibApiV3Sdk.TransactionalEmailsApi()
24
+const sendSmtpEmail = new Brevo.SendSmtpEmail()
24
 
25
 
25
 // Existing activeSession to test against (should not match)
26
 // Existing activeSession to test against (should not match)
26
 const activeSessions = {
27
 const activeSessions = {
53
 }
54
 }
54
 
55
 
55
 test('path /send-email should send test transac email', async t => {
56
 test('path /send-email should send test transac email', async t => {
57
+    /* If email is left undefined, simply pass the test */
58
+    if (!email) {
59
+        t.log('no actual email was given, bypassing test...')
60
+        return t.pass()
61
+    }
56
     /**
62
     /**
57
      * Create a new server and register services,
63
      * Create a new server and register services,
58
      * models and routes for testing
64
      * models and routes for testing
80
         }
86
         }
81
     }
87
     }
82
 
88
 
89
+    hashedSessionToken = Object.keys(activeSessions).find(hashedToken => {
90
+        return activeSessions[`${hashedToken}`].email === userCredentials.email
91
+    })
83
     /**
92
     /**
84
      * Sends a Transactional Email via Brevo
93
      * Sends a Transactional Email via Brevo
85
      * @ returns {Object}
94
      * @ returns {Object}
101
             emailWasRespondedTo: false,
110
             emailWasRespondedTo: false,
102
             accessToken: null,
111
             accessToken: null,
103
         }
112
         }
104
-        const sendSmtpEmail = {
113
+        sendSmtpEmail.sender = {
114
+            name: 'My Test Company',
115
+            email: 'mytestemail@email.com',
116
+        }
117
+        sendSmtpEmail.subject = 'My Test Company'
118
+        sendSmtpEmail = {
105
             to: [
119
             to: [
106
                 {
120
                 {
107
                     email: userCredentials.email,
121
                     email: userCredentials.email,
108
                 },
122
                 },
109
             ],
123
             ],
110
-            templateId: 1,
111
-            params: {
112
-                // TODO: Change this in production...
113
-                link: `localhost:3000/verify/${hashedSessionToken}`,
114
-            },
124
+        }
125
+        sendSmtpEmail.templateId = Number(process.env.BREVO_TEMPLATE_ID)
126
+        sendSmtpEmail.params = {
127
+            link: `${process.env.BREVO_LINK}/verify/${hashedSessionToken}`,
115
         }
128
         }
116
         return await apiInstance.sendTransacEmail(sendSmtpEmail).then(
129
         return await apiInstance.sendTransacEmail(sendSmtpEmail).then(
117
             data => {
130
             data => {
123
         )
136
         )
124
     }
137
     }
125
 
138
 
126
-    hashedSessionToken = Object.keys(activeSessions).find(hashedToken => {
127
-        return activeSessions[`${hashedToken}`].email === userCredentials.email
128
-    })
129
-
130
     stub(server.services()['userService'], 'emailSent').returns(
139
     stub(server.services()['userService'], 'emailSent').returns(
131
         await emailSent(userCredentials),
140
         await emailSent(userCredentials),
132
     )
141
     )

Načítá se…
Zrušit
Uložit