'use strict' const test = require('ava') const { stub } = require('sinon') const Hapi = require('@hapi/hapi') const UserService = require('../lib/services/user.js') const ProfileService = require('../lib/services/profile/index.js') const plugin = require('../lib/plugins/user.js') const JWT = require('jsonwebtoken') const User = require('../lib/models/user.js') const Profile = require('../lib/models/profile.js') // Dummy Method So JWT can be verified const createToken = (data, expiration = 600) => { const key = process.env.APP_SECRET const obj = {} Object.assign(obj, { ...data }) return JWT.sign(obj, key, { expiresIn: expiration }) } // Dummy Data const payload = 'a;lsdkfja;ldfjka;ldfja;lskjdfa;dfjk' const email = 'test@testemail.com' const userData = { email, name: 'fk', seeking: 'position', sessionToken: createToken(this), } const userInDb = { user_id: 101, user_name: 'john_doe', user_email: email, is_admin: 0, is_poster: 0, is_verified: 0, } const allProfiles = [ { profile_id: 147, user_id: 101, }, ] // Existing activeSession const activeSessions = { 'a;lsdkfja;ldfjka;ldfja;lskjdfa;dfjk': { email, name: 'john_doe', seeking: 'position', sessionToken: userData.sessionToken, expiration: Date.now() + 600000, emailWasRespondedTo: true, accessToken: null, }, } const mockReturn = { profileId: allProfiles[0].profile_id, sessionToken: userData.sessionToken, } const pathToTest = { method: 'POST', url: '/validate-session', payload: JSON.stringify(payload), } test('path /validate-session should return validated session data and profileId', async t => { /** * Create a new server and register services, * models and routes for testing * - * NOTE: We use register models manually. * Normally this is handled by * Schwifty at runtime. */ const server = Hapi.server() /** * Register Routes and Services as usual */ server.registerModel = () => {} server.models = () => ({ User, Profile, }) 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', }, } await plugin.register(server) server.models = () => ({ User, Profile, }) server.services()['userService'] = new UserService(server) server.services()['userService']['activeSessions'] = activeSessions server.services()['profileService'] = new ProfileService(server) server.services()['profileService']['_setTagLookup'] = () => {} stub(server.models()['User'], 'query').returns({ throwIfNotFound: () => ({ first: () => ({ where: () => { if (userData.email === userInDb.user_email) { return userInDb } }, }), }), }) stub(server.models()['Profile'], 'query').returns({ where: () => { return [allProfiles.find(obj => obj.user_id === userInDb.user_id)] }, whereIn: () => ({ withGraphFetched: () => ({ withGraphFetched: () => ({ withGraphFetched: () => [ { profile_id: 147, user_id: 101, tags: [], responses: [], user: { user_id: 101, user_name: 'fk', user_email: email, is_admin: 0, is_poster: 0, is_verified: 0, }, }, ], }), }), }), }) /** * 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() })