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-create-profile.spec.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 ProfileService = require('../lib/services/profile/index.js')
  7. const plugin = require('../lib/plugins/user.js')
  8. const User = require('../lib/models/user.js')
  9. const Profile = require('../lib/models/profile.js')
  10. const Tag = require('../lib/models/tag.js')
  11. const Response = require('../lib/models/response.js')
  12. const params = {
  13. user_id: 102,
  14. }
  15. const payload = [
  16. { response_key_id: 8, val: 'bobby@bob.com' },
  17. { response_key_id: 9, val: 'password' },
  18. { response_key_id: 7, val: 'fk' },
  19. { response_key_id: 11, val: 'position' },
  20. ]
  21. const mockProfile = {
  22. user_id: 102,
  23. id: 147,
  24. }
  25. const mockReturn = {
  26. user_id: 102,
  27. profile_id: 147,
  28. }
  29. const pathToTest = {
  30. method: 'POST',
  31. url: `/${params.user_id}/profile`,
  32. payload: JSON.stringify(payload),
  33. }
  34. test('path /<user_id>/profile should return new profile info', async t => {
  35. /**
  36. * Create a new server and register services,
  37. * models and routes for testing
  38. * -
  39. * NOTE: We use register models manually.
  40. * Normally this is handled by
  41. * Schwifty at runtime.
  42. */
  43. const server = Hapi.server()
  44. server.registrations = {
  45. 'main-app-plugin': {
  46. options: {},
  47. },
  48. }
  49. /**
  50. * Register Routes and Services as usual
  51. */
  52. await plugin.register(server)
  53. server.models = () => ({
  54. User,
  55. Profile,
  56. Tag,
  57. Response,
  58. })
  59. server.services()['userService'] = new UserService(server)
  60. server.services()['profileService'] = new ProfileService(server)
  61. /**
  62. * Replace Objection model methods with our own mock functions
  63. * !: Janky - might be better to temp knex sqlite instance
  64. */
  65. stub(server.models()['User'], 'query').returns({
  66. throwIfNotFound: () => ({
  67. first: () => ({
  68. where: () => mockProfile.user_id,
  69. }),
  70. }),
  71. })
  72. stub(server.models()['Tag'], 'query').returns({})
  73. stub(server.models()['Profile'], 'query').returns({
  74. where: () => mockProfile.user_id,
  75. whereIn: () => ({
  76. withGraphFetched: () => ({
  77. withGraphFetched: () => ({
  78. withGraphFetched: () => [],
  79. }),
  80. }),
  81. }),
  82. insert: () => mockProfile,
  83. })
  84. stub(server.models()['Response'], 'query').returns({
  85. insert: () => ({
  86. profile_id: 147,
  87. response_key_id: 11,
  88. val: 'position',
  89. id: 3,
  90. }),
  91. })
  92. /**
  93. * Test the server with registered models and services
  94. */
  95. const { payload } = await server.inject(pathToTest)
  96. const res = JSON.parse(payload)
  97. t.deepEqual(res.ok, true)
  98. t.deepEqual(res.data, mockReturn)
  99. server.stop()
  100. })