| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 'use strict'
-
- const test = require('ava')
- const { stub } = require('sinon')
- const Hapi = require('@hapi/hapi')
- const Objection = require('objection')
- const UserService = require('../lib/services/user.js')
-
- const plugin = require('../lib/plugins/user.js')
-
- const Auth = require('../lib/models/authentication.js')
- const User = require('../lib/models/user.js')
-
- /**
- * Route parameters
- */
- const payload = {
- user_email: 'test@testemail.com',
- password: 'abcd123',
- }
-
- const mockReturn = {
- user_id: 1234,
- user_name: 'brian',
- user_email: 'test@testemail.com',
- is_poster: 1,
- is_admin: 0,
- is_verified: 1,
- }
-
- const pathToTest = {
- method: 'POST',
- url: `/login`,
- payload: JSON.stringify(payload),
- }
-
- // NOTE: how does hapi/ava expect payload from 'POST' request??
- test('path /login should return ok', async t => {
- /**
- * Create a new server and register services,
- * models and routes for testing
- * -
- * NOTE: We use a mocked registerModel() and register
- * models manually. Normally this is handled by
- * Schwifty at runtime.
- */
- const server = Hapi.server()
- /**
- * Overload so we don't register any models
- * using the plugin call (see plugins/profile.js)
- * and Manually load the model we need for the test
- */
- server.registerModel = () => {}
-
- server.models = () => ({ User, Auth })
- server.registrations = {
- 'main-app-plugin': {
- options: {},
- },
- }
- server.registrations['main-app-plugin'].options.jwtKey = {
- $filter: 'NODE_ENV',
- $default: {
- $param: 'APP_SECRET',
- $default: 'app-secret',
- },
- // Use .env file in production
- production: {
- $param: 'APP_SECRET',
- },
- }
- stub(Objection, 'transaction').returns({})
- /**
- * Register Routes and Services as usual
- */
- await plugin.register(server)
- server.services()['userService'] = new UserService(server)
- server.services()['userService'].createToken = () =>
- 'a;slkdf;asdfa;sdfkja;lsdfj;askdfj;laskdjf;laskjdf'
-
- /**
- * Replace Objection model methods with our own mock functions
- * !: Janky - might be better to temp knex sqlite instance
- */
-
- stub(server.models()['Auth'], 'query').returns({
- throwIfNotFound: () => ({
- first: () => ({
- where: () => ({ ...mockReturn }),
- }),
- }),
- })
- stub(server.models()['User'], 'createNotFoundError').returns({})
- stub(server.models()['User'], 'query').returns({
- throwIfNotFound: () => ({
- first: () => ({
- where: () => ({ ...mockReturn }),
- }),
- }),
- })
-
- /**
- * Test the server with registered models and services
- */
- const { payload } = await server.inject(pathToTest)
- const res = JSON.parse(payload)
-
- t.deepEqual(res.ok, true)
- t.deepEqual(res.data.answered.email, mockReturn.user_email)
- t.deepEqual(res.data.answered.name, mockReturn.user_name)
- t.deepEqual(res.data.answered.seeking, 'poster')
- })
|