Переглянути джерело

:white_check_mark: Added test for user signup

juan-filtering-match-pool
tomit4 2 роки тому
джерело
коміт
eac8a832b6

+ 0
- 1
backend/tests/usercredential.spec.js Переглянути файл

@@ -34,7 +34,6 @@ const pathToTest = {
34 34
     payload: JSON.stringify(payload),
35 35
 }
36 36
 
37
-// NOTE: how does hapi/ava expect payload from 'POST' request??
38 37
 test('path /login should return ok', async t => {
39 38
     /**
40 39
      * Create a new server and register services,

+ 101
- 0
backend/tests/usersignup.spec.js Переглянути файл

@@ -0,0 +1,101 @@
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 User = require('../lib/models/user.js')
10
+const Auth = require('../lib/models/authentication.js')
11
+
12
+const payload = {
13
+    user_name: 'john_doe',
14
+    user_email: 'test@testemail.com',
15
+    is_poster: 1,
16
+    user_pass: 'password',
17
+}
18
+
19
+const mockInsert = {
20
+    ...payload,
21
+    id: 15,
22
+    is_admin: 0,
23
+    is_verified: 1,
24
+}
25
+
26
+const mockReturn = {
27
+    is_admin: 0,
28
+    is_poster: 1,
29
+    is_verified: 1,
30
+    user_email: 'test@testemail.com',
31
+    user_id: 15,
32
+    user_name: 'john_doe',
33
+}
34
+
35
+const pathToTest = {
36
+    method: 'POST',
37
+    url: '/signup',
38
+    payload: JSON.stringify(payload),
39
+}
40
+
41
+test('path /signup should return new user info', async t => {
42
+    /**
43
+     * Create a new server and register services,
44
+     * models and routes for testing
45
+     * -
46
+     * NOTE: We use a mocked registerModel() and register
47
+     * models manually. Normally this is handled by
48
+     * Schwifty at runtime.
49
+     */
50
+    const server = Hapi.server()
51
+    /**
52
+     * Overload so we don't register any models
53
+     * using the plugin call (see plugins/profile.js)
54
+     * and Manually load the model we need for the test
55
+     */
56
+    server.registerModel = () => {}
57
+    server.models = () => ({ User, Auth })
58
+    // TODO: Apply server.registrations to other test specs
59
+    server.registrations = {
60
+        'main-app-plugin': {
61
+            options: {},
62
+        },
63
+    }
64
+
65
+    /**
66
+     * Register Routes and Services as usual
67
+     */
68
+    await plugin.register(server)
69
+    server.services()['userService'] = new UserService(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
+        where: () => {
77
+            if (mockInsert.user_email !== 'test@testemail.com') {
78
+                return [mockInsert.user_email]
79
+            } else return []
80
+        },
81
+        insert: () => mockInsert,
82
+    })
83
+
84
+    stub(server.models()['Auth'], 'query').returns({
85
+        insert: () => mockInsert,
86
+        throwIfNotFound: () => ({
87
+            where: () => ({
88
+                patch: () => ({}),
89
+            }),
90
+        }),
91
+    })
92
+
93
+    /**
94
+     * Test the server with registered models and services
95
+     */
96
+    const { payload } = await server.inject(pathToTest)
97
+    const res = JSON.parse(payload)
98
+    t.deepEqual(res.ok, true)
99
+    t.deepEqual(res.data, mockReturn)
100
+    server.stop()
101
+})

Завантаження…
Відмінити
Зберегти