Selaa lähdekoodia

:pencil: backend | added some jsdoc style comments to start

master
TOJ 5 vuotta sitten
vanhempi
commit
7bb9b5e54a

+ 4
- 1
backend/README.md Näytä tiedosto

@@ -50,7 +50,8 @@ TBD
50 50
 
51 51
 ## :pill: Tests & Code Quality
52 52
 
53
-TBD
53
+Tests are run with AVA with code coverage reporting via nyc. Look at the example test for ideas, as well as the [ava documentation](https://github.com/avajs/ava/blob/main/docs/01-writing-tests.md)
54
+* Run tests with coverage report with `npm test`
54 55
 
55 56
 ## :heart: Built With
56 57
 
@@ -58,3 +59,5 @@ TBD
58 59
 * [Objection](https://vincit.github.io/objection.js/) - Light ORM
59 60
 * [Knex.js](https://knexjs.org/) - Query builder
60 61
 * [Schiwfty](https://hapipal.com/docs/schwifty) - Model layer
62
+* [Ava](https://github.com/avajs/ava) - Easy testing
63
+* [Nyc](https://github.com/istanbuljs/nyc) - Test coverage with Istanbul's CLI

+ 17
- 0
backend/lib/index.js Näytä tiedosto

@@ -2,8 +2,25 @@ const UserPlugin = require('./plugins/user');
2 2
 const MembershipPlugin = require('./plugins/membership');
3 3
 const TestPlugin = require('./plugins/example');
4 4
 
5
+/**
6
+ * A Hapi server instance
7
+ * @typedef {Object} Server
8
+ */
9
+
10
+/**
11
+ * A plugin for Hapi
12
+ * @typedef {Object} Plugin
13
+ */
14
+
5 15
 exports.plugin = {
6 16
     name: 'main-app-plugin',
17
+
18
+    /**
19
+     * Main Hapi plugin that grabs all our
20
+     * other plugins to create the API app
21
+     * @param {Server} server
22
+     * @param {Object} options
23
+     */
7 24
     register: async (server, options) => {
8 25
 
9 26
         await server.register(TestPlugin, {

+ 6
- 0
backend/lib/plugins/example.js Näytä tiedosto

@@ -3,6 +3,12 @@ const example = require('../routes/example/base')
3 3
 module.exports = {
4 4
     name: 'myPlugin',
5 5
     version: '1.0.0',
6
+
7
+    /**
8
+     * Example route registration
9
+     * @param {Server} server
10
+     * @param {Object} options
11
+     */
6 12
     register: async (server, options) => {
7 13
         // Create a route for example
8 14
         server.route(example)

+ 15
- 0
backend/lib/routes/example/base.js Näytä tiedosto

@@ -1,9 +1,24 @@
1 1
 module.exports = {
2 2
     method: 'GET',
3 3
     path: '/',
4
+
5
+    /**
6
+     * Our request hanlder callback
7
+     * @param {*} request
8
+     * @param {*} h
9
+     * @returns {Object}
10
+     */
4 11
     handler: async function (request, h) {
5 12
         return { test: 'hello, world' }
6 13
     },
14
+
15
+    /**
16
+     * Our route options
17
+     * @param {string} description - Title for our swagger docs
18
+     * @param {string} notes - Subtitle for our swagger docs
19
+     * @param {Array} tags - Our tags
20
+     * @param {boolean} auth - The authentication scheme
21
+     */
7 22
     options: {
8 23
         description: 'Test',
9 24
         notes: 'This is an example route',

+ 56
- 1
backend/lib/services/user.js Näytä tiedosto

@@ -5,7 +5,12 @@ const Jwt = require('@hapi/jwt');
5 5
 const Schmervice = require('@hapipal/schmervice');
6 6
 const SecurePassword = require('secure-password');
7 7
 
8
+/** Class for methods used in the User plugin */
8 9
 module.exports = class UserService extends Schmervice.Service {
10
+    /**
11
+     * Unsure of what our constructor does
12
+     * @param  {...any} args
13
+     */
9 14
     constructor(...args) {
10 15
         super(...args)
11 16
         const pwd = new SecurePassword()
@@ -14,21 +19,50 @@ module.exports = class UserService extends Schmervice.Service {
14 19
             verify: Util.promisify(pwd.verify.bind(pwd))
15 20
         }
16 21
     }
22
+
23
+    /**
24
+     * Use knex to find users with id column
25
+     * @param {number} id
26
+     * @param {*} txn
27
+     * @returns
28
+     */
17 29
     async findById(id, txn) {
18 30
         const { User } = this.server.models()
19 31
         return await User.query(txn).throwIfNotFound().findById(id)
20 32
     }
33
+
34
+    /**
35
+     * Use knew to find first user with username
36
+     * @param {*} username
37
+     * @param {*} txn
38
+     * @returns
39
+     */
21 40
     async findByUsername(username, txn) {
22 41
         const { User } = this.server.models()
23 42
 
24 43
         return await User.query(txn).throwIfNotFound().first().where({ username })
25 44
     }
45
+
46
+    /**
47
+     * Signup function
48
+     * @param {*} param0
49
+     * @param {*} txn
50
+     * @returns
51
+     */
26 52
     async signup({ password, ...userInfo }, txn) {
27 53
         const { User } = this.server.models()
28 54
         const { id } = await User.query(txn).insert(userInfo)
29 55
         await this.changePassword(id, password, txn)
30 56
         return id
31 57
     }
58
+
59
+    /**
60
+     * Updates user's info
61
+     * @param {number} id
62
+     * @param {*} param1
63
+     * @param {*} txn
64
+     * @returns
65
+     */
32 66
     async update(id, { password, ...userInfo }, txn) {
33 67
         const { User } = this.server.models()
34 68
 
@@ -42,6 +76,13 @@ module.exports = class UserService extends Schmervice.Service {
42 76
 
43 77
         return id
44 78
     }
79
+
80
+    /**
81
+     * Self explanatory
82
+     * @param {*} param0
83
+     * @param {*} txn
84
+     * @returns
85
+     */
45 86
     async login({ email, password }, txn) {
46 87
         const { User } = this.server.models()
47 88
 
@@ -51,7 +92,7 @@ module.exports = class UserService extends Schmervice.Service {
51 92
             .where({ user_email: email })
52 93
 
53 94
 
54
-        // Uncomment to run password check using SecurePassword
95
+        /** Uncomment to run password check using SecurePassword */
55 96
         // const passwordCheck = await this.pwd.verify(Buffer.from(password), user.password)
56 97
         // if (passwordCheck === SecurePassword.VALID_NEEDS_REHASH) {
57 98
         //     await this.changePassword(user.id, password, txn)
@@ -62,6 +103,12 @@ module.exports = class UserService extends Schmervice.Service {
62 103
 
63 104
         return user
64 105
     }
106
+
107
+    /**
108
+     * Create a token to be sent in request headers
109
+     * @param {User} user
110
+     * @returns {Token}
111
+     */
65 112
     createToken(user) {
66 113
         const key = this.server.registrations['main-app-plugin'].options.jwtKey
67 114
 
@@ -77,6 +124,14 @@ module.exports = class UserService extends Schmervice.Service {
77 124
             ttlSec: 4 * 60 * 60 // 7 days
78 125
         })
79 126
     }
127
+
128
+    /**
129
+     * Use knex to try to change password entry
130
+     * @param {number} id
131
+     * @param {string} password
132
+     * @param {*} txn
133
+     * @returns {number}
134
+     */
80 135
     async changePassword(id, password, txn) {
81 136
         const { User } = this.server.models()
82 137
 

+ 1
- 1
backend/package.json Näytä tiedosto

@@ -8,7 +8,7 @@
8 8
     "migrate": "knex migrate:latest",
9 9
     "unmigrate": "knex migrate:down",
10 10
     "seed": "knex seed:run",
11
-    "test": "nyc ava"
11
+    "test": "nyc ava --timeout=3000"
12 12
   },
13 13
   "author": "TOJ",
14 14
   "license": "UNLICENSED",

+ 11
- 7
backend/server/manifest.js Näytä tiedosto

@@ -5,10 +5,10 @@ const Vision = require('@hapi/vision');
5 5
 const Schwifty = require('@hapipal/schwifty');
6 6
 const HapiSwagger = require('hapi-swagger');
7 7
 
8
-// Pull .env into process.env
8
+/** Pull .env into process.env */
9 9
 Dotenv.config({ path: `${__dirname}/.env` });
10 10
 
11
-// Glue manifest as a confidence store
11
+/** Glue manifest as a confidence store */
12 12
 module.exports = new Confidence.Store({
13 13
     server: {
14 14
         host: process.env.API_HOST,
@@ -34,8 +34,10 @@ module.exports = new Confidence.Store({
34 34
     },
35 35
     register: {
36 36
         plugins: [
37
+
38
+            /** Main app */
37 39
             {
38
-                plugin: '../lib', // Main plugin
40
+                plugin: '../lib',
39 41
                 routes: {
40 42
                     prefix: '/api'
41 43
                 },
@@ -46,21 +48,23 @@ module.exports = new Confidence.Store({
46 48
                             $param: 'APP_SECRET',
47 49
                             $default: 'app-secret'
48 50
                         },
49
-                        production: { // In production do not default to "app-secret"
51
+                        // Use .env file in production
52
+                        production: {
50 53
                             $param: 'APP_SECRET'
51 54
                         }
52 55
                     }
53 56
                 }
54 57
             },
55
-            // Documentaion deps
56
-            Inert,
57
-            Vision,
58
+
59
+            /** Documentaion plugins */
60
+            Inert, Vision,
58 61
             {
59 62
                 plugin: HapiSwagger,
60 63
                 options: {
61 64
                     info: { title: 'Test API Documentation' }
62 65
                 }
63 66
             },
67
+            /** Model and knex integration */
64 68
             {
65 69
                 plugin: Schwifty,
66 70
                 options: {

Loading…
Peruuta
Tallenna