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.

user-credential.spec.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. test('path /login should return ok', async t => {
  31. /**
  32. * Create a new server and register services,
  33. * models and routes for testing
  34. * -
  35. * NOTE: We use a mocked registerModel() and register
  36. * models manually. Normally this is handled by
  37. * Schwifty at runtime.
  38. */
  39. const server = Hapi.server()
  40. /**
  41. * Overload so we don't register any models
  42. * using the plugin call (see plugins/profile.js)
  43. * and Manually load the model we need for the test
  44. */
  45. server.registerModel = () => {}
  46. server.models = () => ({ User, Auth })
  47. server.registrations = {
  48. 'main-app-plugin': {
  49. options: {},
  50. },
  51. }
  52. server.registrations['main-app-plugin'].options.jwtKey = {
  53. $filter: 'NODE_ENV',
  54. $default: {
  55. $param: 'APP_SECRET',
  56. $default: 'app-secret',
  57. },
  58. // Use .env file in production
  59. production: {
  60. $param: 'APP_SECRET',
  61. },
  62. }
  63. stub(Objection, 'transaction').returns({})
  64. /**
  65. * Register Routes and Services as usual
  66. */
  67. await plugin.register(server)
  68. server.services()['userService'] = new UserService(server)
  69. server.services()['userService'].createToken = () =>
  70. 'a;slkdf;asdfa;sdfkja;lsdfj;askdfj;laskdjf;laskjdf'
  71. /**
  72. * Replace Objection model methods with our own mock functions
  73. * !: Janky - might be better to temp knex sqlite instance
  74. */
  75. stub(server.models()['Auth'], 'query').returns({
  76. throwIfNotFound: () => ({
  77. first: () => ({
  78. where: () => ({ ...mockReturn }),
  79. }),
  80. }),
  81. })
  82. stub(server.models()['User'], 'createNotFoundError').returns({})
  83. stub(server.models()['User'], 'query').returns({
  84. throwIfNotFound: () => ({
  85. first: () => ({
  86. where: () => ({ ...mockReturn }),
  87. }),
  88. }),
  89. })
  90. /**
  91. * Test the server with registered models and services
  92. */
  93. const { payload } = await server.inject(pathToTest)
  94. const res = JSON.parse(payload)
  95. t.deepEqual(res.ok, true)
  96. t.deepEqual(res.data.answered.email, mockReturn.user_email)
  97. t.deepEqual(res.data.answered.name, mockReturn.user_name)
  98. t.deepEqual(res.data.answered.seeking, 'poster')
  99. })