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.

profile.spec.js 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict'
  2. const test = require('ava')
  3. const { stub } = require('sinon')
  4. const Hapi = require('@hapi/hapi')
  5. const plugin = require('../lib/plugins/profile')
  6. const Profile = require('../lib/models/profile')
  7. const Tag = require('../lib/models/tag')
  8. /**
  9. * Route parameters
  10. */
  11. const params = {
  12. profile_id: 1,
  13. target_id: 2,
  14. reinsert: true,
  15. include_profile: false,
  16. }
  17. const mockReturn = {
  18. profile: {
  19. profile_id: 99,
  20. user: {},
  21. responses: [],
  22. tags: [],
  23. },
  24. tags: [],
  25. }
  26. const pathToTest = {
  27. method: 'GET',
  28. url: `/${params.profile_id}`,
  29. }
  30. test('path /<profile_id> 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 = () => ({ Profile, Tag })
  47. /**
  48. * Register mock authentication just for testing
  49. * -
  50. * Profile plugin does not do this
  51. */
  52. server.auth.scheme('jwt', () => ({
  53. authenticate: (req, h) => h.authenticated({ credentials: {} }),
  54. }))
  55. server.auth.strategy('default_jwt', 'jwt')
  56. /**
  57. * Register Routes and Services as usual
  58. */
  59. await plugin.register(server)
  60. /**
  61. * Replace Objection model methods with our own mock functions
  62. * !: Janky - might be better to temp knex sqlite instance
  63. */
  64. stub(server.models()['Tag'], 'query').returns(mockReturn.tags)
  65. stub(server.models()['Profile'], 'query').returns({
  66. // Mocked for getProfile()
  67. where: () => ({
  68. first: () => ({
  69. withGraphFetched: () => ({
  70. withGraphFetched: () => ({
  71. withGraphFetched: () => mockReturn.profile,
  72. }),
  73. }),
  74. }),
  75. }),
  76. })
  77. /**
  78. * Test the server with registered models and services
  79. */
  80. const { payload } = await server.inject(pathToTest)
  81. const res = JSON.parse(payload)
  82. t.deepEqual(res.ok, true)
  83. t.deepEqual(res.data.profile_id, mockReturn.profile.profile_id)
  84. t.notDeepEqual(res.data, mockReturn.profile)
  85. t.true(Object.keys(res.data).includes('profile_description'))
  86. t.true(Object.keys(res.data).includes('profile_languages'))
  87. })