Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

matchqueue.spec.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 Aspect = require('../lib/models/aspect')
  7. const AspectLabel = require('../lib/models/aspect_label')
  8. const Profile = require('../lib/models/profile')
  9. const MatchQueue = require('../lib/models/matchqueue')
  10. /**
  11. * Route parameters
  12. */
  13. const params = {
  14. profile_id: 1,
  15. target_id: 2,
  16. reinsert: true,
  17. include_profile: false,
  18. }
  19. const mockReturn = {
  20. queue: [
  21. { mocked_profile_id: 1, target_id: 2, is_deleted: false },
  22. { mocked_profile_id: 2, target_id: 3, is_deleted: false },
  23. { mocked_profile_id: 3, target_id: 1, is_deleted: false },
  24. ],
  25. labels: [
  26. { aspect_id: 1, a: 100, b: 100 },
  27. { aspect_id: 2, a: 100, b: 200 },
  28. { aspect_id: 3, a: 200, b: 200 },
  29. { aspect_id: 4, a: 200, b: 100 },
  30. ],
  31. aspects: [
  32. { aspect_id: 1, 1: 111, 2: 112, 3: 113, 4: 114 },
  33. { aspect_id: 2, 1: 222, 2: 222, 3: 333, 4: 222 },
  34. { aspect_id: 3, 1: 111, 2: 112, 3: 113, 4: 114 },
  35. { aspect_id: 4, 1: 111, 2: 111, 3: 111, 4: 111 },
  36. ],
  37. }
  38. const pathToTest = {
  39. method: 'PATCH',
  40. url: `/${params.profile_id}/queue/${params.target_id}/delete?include_profile=${params.include_profile}&reinsert=${params.reinsert}`,
  41. }
  42. test('path /<profile_id>/queue/<target_id> should return ok on PATCH', async t => {
  43. /**
  44. * Create a new server and register services,
  45. * models and routes for testing
  46. * -
  47. * NOTE: We use a mocked registerModel() and register
  48. * models manually. Normally this is handled by
  49. * Schwifty at runtime.
  50. */
  51. const server = Hapi.server()
  52. /**
  53. * Overload so we don't register any models
  54. * using the plugin call (see plugins/profile.js)
  55. * and Manually load the model we need for the test
  56. */
  57. server.registerModel = () => {}
  58. server.models = () => ({ Aspect, AspectLabel, MatchQueue, Profile })
  59. /**
  60. * Register mock authentication just for testing
  61. * -
  62. * Profile plugin does not do this
  63. */
  64. server.auth.scheme('jwt', () => ({
  65. authenticate: (req, h) => h.authenticated({ credentials: {} }),
  66. }))
  67. server.auth.strategy('default_jwt', 'jwt')
  68. /**
  69. * Register Routes and Services as usual
  70. */
  71. await plugin.register(server)
  72. /**
  73. * Replace Objection model methods with our own mock functions
  74. * !: Janky - might be better to temp knex sqlite instance
  75. */
  76. stub(server.models()['AspectLabel'], 'query').returns(mockReturn.labels)
  77. stub(server.models()['Aspect'], 'query').returns(mockReturn.aspects)
  78. stub(server.models()['MatchQueue'], 'query').returns({
  79. insert: queueRecord => {
  80. t.is(queueRecord.profile_id, params.profile_id)
  81. t.is(queueRecord.target_id, params.target_id)
  82. t.is(queueRecord.is_deleted, !params.reinsert)
  83. },
  84. // Mocked for getQueue()
  85. where: () => ({
  86. where: (cmd, val) => {
  87. return mockReturn.queue
  88. },
  89. // Mocked for markAsDeleted()
  90. andWhere: () => ({
  91. patch: () => ({
  92. first: () => ({
  93. patch: 'bop',
  94. }),
  95. }),
  96. }),
  97. }),
  98. })
  99. /**
  100. * Test the server with registered models and services
  101. */
  102. const { payload } = await server.inject(pathToTest)
  103. const res = JSON.parse(payload)
  104. t.deepEqual(res.ok, true)
  105. t.deepEqual(
  106. res.data,
  107. mockReturn.queue.map(entry => entry.target_id),
  108. )
  109. t.deepEqual(res.data[0], 2)
  110. t.deepEqual(res.data[1], 3)
  111. })