You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usercredential.spec.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict'
  2. const test = require('ava')
  3. const { stub } = require('sinon')
  4. const Hapi = require('@hapi/hapi')
  5. const Objection = require('objection')
  6. const UserService = require('../lib/services/user.js')
  7. const plugin = require('../lib/plugins/user.js')
  8. const Auth = require('../lib/models/authentication.js')
  9. const User = require('../lib/models/user.js')
  10. /**
  11. * Route parameters
  12. */
  13. const payload = {
  14. user_email: 'test@testemail.com',
  15. password: 'abcd123',
  16. }
  17. const mockReturn = {
  18. user_id: 1234,
  19. user_name: 'brian',
  20. user_email: 'test@testemail.com',
  21. is_poster: 1,
  22. is_admin: 0,
  23. is_verified: 1,
  24. }
  25. const pathToTest = {
  26. method: 'POST',
  27. url: `/login`,
  28. payload: JSON.stringify(payload),
  29. }
  30. // NOTE: how does hapi/ava expect payload from 'POST' request??
  31. test('path /login should return ok', async t => {
  32. /**
  33. * Create a new server and register services,
  34. * models and routes for testing
  35. * -
  36. * NOTE: We use a mocked registerModel() and register
  37. * models manually. Normally this is handled by
  38. * Schwifty at runtime.
  39. */
  40. const server = Hapi.server()
  41. /**
  42. * Overload so we don't register any models
  43. * using the plugin call (see plugins/profile.js)
  44. * and Manually load the model we need for the test
  45. */
  46. server.registerModel = () => {}
  47. server.models = () => ({ User, Auth })
  48. server.registrations = {
  49. 'main-app-plugin': {
  50. options: {},
  51. },
  52. }
  53. server.registrations['main-app-plugin'].options.jwtKey = {
  54. $filter: 'NODE_ENV',
  55. $default: {
  56. $param: 'APP_SECRET',
  57. $default: 'app-secret',
  58. },
  59. // Use .env file in production
  60. production: {
  61. $param: 'APP_SECRET',
  62. },
  63. }
  64. stub(Objection, 'transaction').returns({})
  65. /**
  66. * Register Routes and Services as usual
  67. */
  68. await plugin.register(server)
  69. server.services()['userService'] = new UserService(server)
  70. server.services()['userService'].createToken = () =>
  71. 'a;slkdf;asdfa;sdfkja;lsdfj;askdfj;laskdjf;laskjdf'
  72. /**
  73. * Replace Objection model methods with our own mock functions
  74. * !: Janky - might be better to temp knex sqlite instance
  75. */
  76. stub(server.models()['Auth'], 'query').returns({
  77. throwIfNotFound: () => ({
  78. first: () => ({
  79. where: () => ({ ...mockReturn }),
  80. }),
  81. }),
  82. })
  83. stub(server.models()['User'], 'createNotFoundError').returns({})
  84. stub(server.models()['User'], 'query').returns({
  85. throwIfNotFound: () => ({
  86. first: () => ({
  87. where: () => ({ ...mockReturn }),
  88. }),
  89. }),
  90. })
  91. /**
  92. * Test the server with registered models and services
  93. */
  94. const { payload } = await server.inject(pathToTest)
  95. const res = JSON.parse(payload)
  96. t.deepEqual(res.ok, true)
  97. t.deepEqual(res.data.answered.email, mockReturn.user_email)
  98. t.deepEqual(res.data.answered.name, mockReturn.user_name)
  99. t.deepEqual(res.data.answered.seeking, 'poster')
  100. })