Explorar el Código

:white_check_mark: Continued writing tests for user routes

tags/0.0.4
tomit4 hace 2 años
padre
commit
9fc7d2225a

+ 9
- 10
backend/lib/services/profile/index.js Ver fichero

28
         if (Object.keys(this.tagLookup).length) return
28
         if (Object.keys(this.tagLookup).length) return
29
         const { Tag } = this.server.models()
29
         const { Tag } = this.server.models()
30
         const allTagDescriptions = await Tag.query()
30
         const allTagDescriptions = await Tag.query()
31
-        allTagDescriptions.forEach(desc => {
32
-            if (!desc.is_active) return
33
-            this.tagLookup[desc.tag_id] = desc
34
-        })
31
+        if (allTagDescriptions.length) {
32
+            allTagDescriptions.forEach(desc => {
33
+                if (!desc.is_active) return
34
+                this.tagLookup[desc.tag_id] = desc
35
+            })
36
+        } else return
35
     }
37
     }
36
     /**
38
     /**
37
      * Internal method to get list of profile_ids for this user
39
      * Internal method to get list of profile_ids for this user
40
      */
42
      */
41
     async _getProfileIdsForUserId(userId) {
43
     async _getProfileIdsForUserId(userId) {
42
         const { Profile } = this.server.models()
44
         const { Profile } = this.server.models()
43
-
44
         /** Grab every Profile associated with this id */
45
         /** Grab every Profile associated with this id */
45
         const allProfiles = await Profile.query().where('user_id', userId)
46
         const allProfiles = await Profile.query().where('user_id', userId)
46
-
47
         /** Copy a list of the just the Profiles */
47
         /** Copy a list of the just the Profiles */
48
-        const profileIdsToGrab = allProfiles.map(profile => profile.profile_id)
49
-
50
-        /** Uncomment to dedupe the list just in case */
51
-        return [...new Set(profileIdsToGrab)]
48
+        return typeof allProfiles === 'object'
49
+            ? [...new Set(allProfiles.map(profile => profile.profile_id))]
50
+            : [allProfiles]
52
     }
51
     }
53
 
52
 
54
     /**
53
     /**

+ 7
- 3
backend/lib/services/profile/profiler.js Ver fichero

105
  * @returns {Array}
105
  * @returns {Array}
106
  */
106
  */
107
 const makeCompleteFromProfileEntries = (profilesEntries, type, tagLookup) => {
107
 const makeCompleteFromProfileEntries = (profilesEntries, type, tagLookup) => {
108
-    return profilesEntries.map(entry =>
109
-        _makeCompleteProfile(entry, type, tagLookup),
110
-    )
108
+    try {
109
+        return profilesEntries.map(entry =>
110
+            _makeCompleteProfile(entry, type, tagLookup),
111
+        )
112
+    } catch (err) {
113
+        throw new Error(err)
114
+    }
111
 }
115
 }
112
 
116
 
113
 module.exports = {
117
 module.exports = {

+ 74
- 0
backend/tests/user-auth-pass.spec.js Ver fichero

1
+'use strict'
2
+
3
+const test = require('ava')
4
+const { stub } = require('sinon')
5
+const Hapi = require('@hapi/hapi')
6
+const UserService = require('../lib/services/user.js')
7
+
8
+const plugin = require('../lib/plugins/user.js')
9
+const Auth = require('../lib/models/authentication.js')
10
+
11
+const params = {
12
+    user_email: 'test@testemail.com',
13
+}
14
+
15
+const mockReturn = {
16
+    password: 'a;slkdfja;ldfjk;alkdsfja;lsdkfj',
17
+}
18
+
19
+const pathToTest = {
20
+    method: 'GET',
21
+    url: `/${params.user_email}/password`,
22
+}
23
+
24
+test(`path /${params.user_email}/password should return OK on GET`, async t => {
25
+    /**
26
+     * Create a new server and register services,
27
+     * models and routes for testing
28
+     * -
29
+     * NOTE: We use a mocked registerModel() and register
30
+     * models manually. Normally this is handled by
31
+     * Schwifty at runtime.
32
+     */
33
+    const server = Hapi.server()
34
+    /**
35
+     * Overload so we don't register any models
36
+     * using the plugin call (see plugins/profile.js)
37
+     * and Manually load the model we need for the test
38
+     */
39
+
40
+    server.models = () => ({ Auth })
41
+    // TODO: Apply server.registrations to other test specs
42
+    server.registrations = {
43
+        'main-app-plugin': {
44
+            options: {},
45
+        },
46
+    }
47
+
48
+    /**
49
+     * Register Routes and Services as usual
50
+     */
51
+    await plugin.register(server)
52
+    server.services()['userService'] = new UserService(server)
53
+
54
+    /**
55
+     * Replace Objection model methods with our own mock functions
56
+     * !: Janky - might be better to temp knex sqlite instance
57
+     */
58
+    stub(server.models()['Auth'], 'query').returns({
59
+        where: () => ({
60
+            first: () => ({
61
+                token: 'a;slkdfja;ldfjk;alkdsfja;lsdkfj',
62
+            }),
63
+        }),
64
+    })
65
+
66
+    /**
67
+     * Test the server with registered models and services
68
+     */
69
+    const { payload } = await server.inject(pathToTest)
70
+    const res = JSON.parse(payload)
71
+    t.deepEqual(res.ok, true)
72
+    t.deepEqual(res.data, mockReturn)
73
+    server.stop()
74
+})

+ 112
- 0
backend/tests/user-create-profile.spec.js Ver fichero

1
+'use strict'
2
+
3
+const test = require('ava')
4
+const { stub } = require('sinon')
5
+const Hapi = require('@hapi/hapi')
6
+const UserService = require('../lib/services/user.js')
7
+const ProfileService = require('../lib/services/profile/index.js')
8
+
9
+const plugin = require('../lib/plugins/user.js')
10
+const User = require('../lib/models/user.js')
11
+const Profile = require('../lib/models/profile.js')
12
+const Tag = require('../lib/models/tag.js')
13
+const Response = require('../lib/models/response.js')
14
+
15
+const params = {
16
+    user_id: 102,
17
+}
18
+const payload = [
19
+    { response_key_id: 8, val: 'bobby@bob.com' },
20
+    { response_key_id: 9, val: 'password' },
21
+    { response_key_id: 7, val: 'fk' },
22
+    { response_key_id: 11, val: 'position' },
23
+]
24
+
25
+const mockProfile = {
26
+    user_id: 102,
27
+    id: 147,
28
+}
29
+
30
+const mockReturn = {
31
+    user_id: 102,
32
+    profile_id: 147,
33
+}
34
+
35
+const pathToTest = {
36
+    method: 'POST',
37
+    url: `/${params.user_id}/profile`,
38
+    payload: JSON.stringify(payload),
39
+}
40
+
41
+test(`path /${params.user_id}/profile should return new profile info`, async t => {
42
+    /**
43
+     * Create a new server and register services,
44
+     * models and routes for testing
45
+     * -
46
+     * NOTE: We use register models manually.
47
+     * Normally this is handled by
48
+     * Schwifty at runtime.
49
+     */
50
+    const server = Hapi.server()
51
+    server.registrations = {
52
+        'main-app-plugin': {
53
+            options: {},
54
+        },
55
+    }
56
+    /**
57
+     * Register Routes and Services as usual
58
+     */
59
+    await plugin.register(server)
60
+
61
+    server.models = () => ({
62
+        User,
63
+        Profile,
64
+        Tag,
65
+        Response,
66
+    })
67
+
68
+    server.services()['userService'] = new UserService(server)
69
+    server.services()['profileService'] = new ProfileService(server)
70
+
71
+    /**
72
+     * Replace Objection model methods with our own mock functions
73
+     * !: Janky - might be better to temp knex sqlite instance
74
+     */
75
+    stub(server.models()['User'], 'query').returns({
76
+        throwIfNotFound: () => ({
77
+            first: () => ({
78
+                where: () => mockProfile.user_id,
79
+            }),
80
+        }),
81
+    })
82
+
83
+    stub(server.models()['Tag'], 'query').returns({})
84
+    stub(server.models()['Profile'], 'query').returns({
85
+        where: () => mockProfile.user_id,
86
+        whereIn: () => ({
87
+            withGraphFetched: () => ({
88
+                withGraphFetched: () => ({
89
+                    withGraphFetched: () => [],
90
+                }),
91
+            }),
92
+        }),
93
+        insert: () => mockProfile,
94
+    })
95
+
96
+    stub(server.models()['Response'], 'query').returns({
97
+        insert: () => ({
98
+            profile_id: 147,
99
+            response_key_id: 11,
100
+            val: 'position',
101
+            id: 3,
102
+        }),
103
+    })
104
+    /**
105
+     * Test the server with registered models and services
106
+     */
107
+    const { payload } = await server.inject(pathToTest)
108
+    const res = JSON.parse(payload)
109
+    t.deepEqual(res.ok, true)
110
+    t.deepEqual(res.data, mockReturn)
111
+    server.stop()
112
+})

backend/tests/usercredential.spec.js → backend/tests/user-credential.spec.js Ver fichero


backend/tests/usersignup.spec.js → backend/tests/user-signup.spec.js Ver fichero


Loading…
Cancelar
Guardar