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-signup.spec.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict'
  2. const test = require('ava')
  3. const { stub } = require('sinon')
  4. const Hapi = require('@hapi/hapi')
  5. const UserService = require('../lib/services/user.js')
  6. const plugin = require('../lib/plugins/user.js')
  7. const User = require('../lib/models/user.js')
  8. const Auth = require('../lib/models/authentication.js')
  9. const payload = {
  10. user_name: 'john_doe',
  11. user_email: 'test@testemail.com',
  12. is_poster: 1,
  13. user_pass: 'password',
  14. }
  15. const mockInsert = {
  16. ...payload,
  17. id: 15,
  18. is_admin: 0,
  19. is_verified: 1,
  20. }
  21. const mockReturn = {
  22. is_admin: 0,
  23. is_poster: 1,
  24. is_verified: 1,
  25. user_email: 'test@testemail.com',
  26. user_id: 15,
  27. user_name: 'john_doe',
  28. }
  29. const pathToTest = {
  30. method: 'POST',
  31. url: '/signup',
  32. payload: JSON.stringify(payload),
  33. }
  34. test('path /signup should return new user info', async t => {
  35. /**
  36. * Create a new server and register services,
  37. * models and routes for testing
  38. * -
  39. * NOTE: We use a mocked registerModel() and register
  40. * models manually. Normally this is handled by
  41. * Schwifty at runtime.
  42. */
  43. const server = Hapi.server()
  44. /**
  45. * Overload so we don't register any models
  46. * using the plugin call (see plugins/profile.js)
  47. * and Manually load the model we need for the test
  48. */
  49. server.registerModel = () => {}
  50. server.models = () => ({ User, Auth })
  51. // TODO: Apply server.registrations to other test specs
  52. server.registrations = {
  53. 'main-app-plugin': {
  54. options: {},
  55. },
  56. }
  57. /**
  58. * Register Routes and Services as usual
  59. */
  60. await plugin.register(server)
  61. server.services()['userService'] = new UserService(server)
  62. /**
  63. * Replace Objection model methods with our own mock functions
  64. * !: Janky - might be better to temp knex sqlite instance
  65. */
  66. stub(server.models()['User'], 'query').returns({
  67. where: () => {
  68. if (mockInsert.user_email !== 'test@testemail.com') {
  69. return [mockInsert.user_email]
  70. } else return []
  71. },
  72. insert: () => mockInsert,
  73. })
  74. stub(server.models()['Auth'], 'query').returns({
  75. insert: () => mockInsert,
  76. throwIfNotFound: () => ({
  77. where: () => ({
  78. patch: () => ({}),
  79. }),
  80. }),
  81. })
  82. /**
  83. * Test the server with registered models and services
  84. */
  85. const { payload } = await server.inject(pathToTest)
  86. const res = JSON.parse(payload)
  87. t.deepEqual(res.ok, true)
  88. t.deepEqual(res.data, mockReturn)
  89. server.stop()
  90. })