'use strict' const test = require('ava') const { stub } = require('sinon') const Hapi = require('@hapi/hapi') const UserService = require('../lib/services/user.js') const plugin = require('../lib/plugins/user.js') const Auth = require('../lib/models/authentication.js') const params = { user_email: 'test@testemail.com', } const mockReturn = { password: 'a;slkdfja;ldfjk;alkdsfja;lsdkfj', } const pathToTest = { method: 'GET', url: `/${params.user_email}/password`, } test(`path /${params.user_email}/password should return OK on GET`, 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.models = () => ({ Auth }) // TODO: Apply server.registrations to other test specs server.registrations = { 'main-app-plugin': { options: {}, }, } /** * Register Routes and Services as usual */ await plugin.register(server) server.services()['userService'] = new UserService(server) /** * Replace Objection model methods with our own mock functions * !: Janky - might be better to temp knex sqlite instance */ stub(server.models()['Auth'], 'query').returns({ where: () => ({ first: () => ({ token: 'a;slkdfja;ldfjk;alkdsfja;lsdkfj', }), }), }) /** * 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, mockReturn) server.stop() })