Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 Auth = require('../lib/models/authentication.js')
  8. const params = {
  9. user_email: 'test@testemail.com',
  10. }
  11. const mockReturn = {
  12. password: 'a;slkdfja;ldfjk;alkdsfja;lsdkfj',
  13. }
  14. const pathToTest = {
  15. method: 'GET',
  16. url: `/${params.user_email}/password`,
  17. }
  18. test(`path /${params.user_email}/password should return OK on GET`, async t => {
  19. /**
  20. * Create a new server and register services,
  21. * models and routes for testing
  22. * -
  23. * NOTE: We use a mocked registerModel() and register
  24. * models manually. Normally this is handled by
  25. * Schwifty at runtime.
  26. */
  27. const server = Hapi.server()
  28. /**
  29. * Overload so we don't register any models
  30. * using the plugin call (see plugins/profile.js)
  31. * and Manually load the model we need for the test
  32. */
  33. server.models = () => ({ Auth })
  34. // TODO: Apply server.registrations to other test specs
  35. server.registrations = {
  36. 'main-app-plugin': {
  37. options: {},
  38. },
  39. }
  40. /**
  41. * Register Routes and Services as usual
  42. */
  43. await plugin.register(server)
  44. server.services()['userService'] = new UserService(server)
  45. /**
  46. * Replace Objection model methods with our own mock functions
  47. * !: Janky - might be better to temp knex sqlite instance
  48. */
  49. stub(server.models()['Auth'], 'query').returns({
  50. where: () => ({
  51. first: () => ({
  52. token: 'a;slkdfja;ldfjk;alkdsfja;lsdkfj',
  53. }),
  54. }),
  55. })
  56. /**
  57. * Test the server with registered models and services
  58. */
  59. const { payload } = await server.inject(pathToTest)
  60. const res = JSON.parse(payload)
  61. t.deepEqual(res.ok, true)
  62. t.deepEqual(res.data, mockReturn)
  63. server.stop()
  64. })