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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 ZipCode = require('../lib/models/zip-code')
  8. const MatchQueue = require('../lib/models/matchqueue')
  9. // !: Must match the key set in servives/profile.js
  10. const zipcodeKey = 7
  11. /**
  12. * Route parameters
  13. */
  14. const params = {
  15. profile_id: 1,
  16. max_distance: 1000,
  17. }
  18. const mockReturn = {
  19. user: [
  20. {
  21. profile_id: 1,
  22. user: { is_poster: 1 },
  23. responses: [
  24. { val: '120' },
  25. { val: '200' },
  26. { response_key_id: zipcodeKey, val: '90065' },
  27. ],
  28. },
  29. {
  30. profile_id: 2,
  31. user: { is_poster: 0 },
  32. responses: [
  33. { val: '120' },
  34. { val: '200' },
  35. { response_key_id: zipcodeKey, val: '96741' },
  36. ],
  37. },
  38. {
  39. profile_id: 3,
  40. user: { is_poster: 0 },
  41. responses: [
  42. { val: '180' },
  43. { val: '140' },
  44. { response_key_id: zipcodeKey, val: '97002' },
  45. ],
  46. },
  47. ],
  48. }
  49. const pathToTest = {
  50. method: 'GET',
  51. url: `/${params.profile_id}/score?max_distance=${params.max_distance}`,
  52. }
  53. test(`path ${pathToTest.url} should return ok on GET`, async t => {
  54. /**
  55. * Create a new server and register services,
  56. * models and routes for testing
  57. * -
  58. * NOTE: We use a mocked registerModel() and register
  59. * models manually. Normally this is handled by
  60. * Schwifty at runtime.
  61. */
  62. const server = Hapi.server()
  63. /**
  64. * Overload so we don't register any models
  65. * using the plugin call (see plugins/profile.js)
  66. * and Manually load the model we need for the test
  67. */
  68. server.registerModel = () => {}
  69. server.models = () => ({ MatchQueue, Profile, ZipCode })
  70. /**
  71. * Register Routes and Services as usual
  72. */
  73. await plugin.register(server)
  74. /**
  75. * Replace Objection model methods with our own mock functions
  76. * !: Janky - might be better to temp knex sqlite instance
  77. */
  78. stub(server.models()['Profile'], 'query').returns({
  79. // Mocked for pathToTest(), scoreProfilesFor()
  80. findOne: () => ({
  81. withGraphFetched: () => ({
  82. withGraphFetched: () => mockReturn.user[0],
  83. }),
  84. }),
  85. // Mocked for scoreProfilesFor()
  86. withGraphFetched: () => ({
  87. withGraphFetched: () => [mockReturn.user[1], mockReturn.user[2]],
  88. }),
  89. // Mocked for _getProfileIdsForUserId()
  90. where: () => ({}),
  91. // Mocked for getCompleteProfilesFor(), getProfilesFor()
  92. whereIn: () => ({
  93. withGraphFetched: () => ({}),
  94. }),
  95. // Mocked for deleteProfile()
  96. delete: () => ({
  97. where: () => ({}),
  98. }),
  99. })
  100. stub(server.models()['ZipCode'], 'query').returns({
  101. // Mocked for _latLonForZip()
  102. findOne: () => ({
  103. latitude: 38.0 + Math.random(),
  104. longitude: -121.0 + Math.random(),
  105. }),
  106. })
  107. stub(server.models()['MatchQueue'], 'query').returns({
  108. patch: () => ({
  109. where: () => ({}),
  110. }),
  111. insert: () => ({}),
  112. where: () => ({
  113. andWhere: () => ({}),
  114. }),
  115. })
  116. /**
  117. * Test the server with registered models and services
  118. */
  119. const { payload } = await server.inject(pathToTest)
  120. const res = JSON.parse(payload)
  121. t.deepEqual(res.ok, true)
  122. t.is(res.data.length, 2)
  123. t.is(res.data[0].profile_id, 3)
  124. })