ContactControllerTests.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* eslint-disable
  2. max-len,
  3. no-dupe-keys,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const chai = require('chai')
  16. const should = chai.should()
  17. const { assert } = chai
  18. const { expect } = chai
  19. const modulePath = '../../../../app/src/Features/Contacts/ContactController.js'
  20. const SandboxedModule = require('sandboxed-module')
  21. describe('ContactController', function() {
  22. beforeEach(function() {
  23. this.AuthenticationController = { getLoggedInUserId: sinon.stub() }
  24. this.ContactController = SandboxedModule.require(modulePath, {
  25. globals: {
  26. console: console
  27. },
  28. requires: {
  29. 'logger-sharelatex': (this.logger = {
  30. log: sinon.stub(),
  31. error: sinon.stub()
  32. }),
  33. '../User/UserGetter': (this.UserGetter = {}),
  34. './ContactManager': (this.ContactManager = {}),
  35. '../Authentication/AuthenticationController': (this.AuthenticationController = {}),
  36. '../../infrastructure/Modules': (this.Modules = { hooks: {} }),
  37. '../Authentication/AuthenticationController': this
  38. .AuthenticationController
  39. }
  40. })
  41. this.next = sinon.stub()
  42. this.req = {}
  43. this.res = {}
  44. this.res.status = sinon.stub().returns(this.req)
  45. return (this.res.send = sinon.stub())
  46. })
  47. describe('getContacts', function() {
  48. beforeEach(function() {
  49. this.user_id = 'mock-user-id'
  50. this.contact_ids = ['contact-1', 'contact-2', 'contact-3']
  51. this.contacts = [
  52. {
  53. _id: 'contact-1',
  54. email: 'joe@example.com',
  55. first_name: 'Joe',
  56. last_name: 'Example',
  57. unsued: 'foo'
  58. },
  59. {
  60. _id: 'contact-2',
  61. email: 'jane@example.com',
  62. first_name: 'Jane',
  63. last_name: 'Example',
  64. unsued: 'foo',
  65. holdingAccount: true
  66. },
  67. {
  68. _id: 'contact-3',
  69. email: 'jim@example.com',
  70. first_name: 'Jim',
  71. last_name: 'Example',
  72. unsued: 'foo'
  73. }
  74. ]
  75. this.AuthenticationController.getLoggedInUserId = sinon
  76. .stub()
  77. .returns(this.user_id)
  78. this.ContactManager.getContactIds = sinon
  79. .stub()
  80. .callsArgWith(2, null, this.contact_ids)
  81. this.UserGetter.getUsers = sinon
  82. .stub()
  83. .callsArgWith(2, null, this.contacts)
  84. this.Modules.hooks.fire = sinon.stub().callsArg(3)
  85. return this.ContactController.getContacts(this.req, this.res, this.next)
  86. })
  87. it('should look up the logged in user id', function() {
  88. return this.AuthenticationController.getLoggedInUserId
  89. .calledWith(this.req)
  90. .should.equal(true)
  91. })
  92. it('should get the users contact ids', function() {
  93. return this.ContactManager.getContactIds
  94. .calledWith(this.user_id, { limit: 50 })
  95. .should.equal(true)
  96. })
  97. it('should populate the users contacts ids', function() {
  98. return this.UserGetter.getUsers
  99. .calledWith(this.contact_ids, {
  100. email: 1,
  101. first_name: 1,
  102. last_name: 1,
  103. holdingAccount: 1
  104. })
  105. .should.equal(true)
  106. })
  107. it('should fire the getContact module hook', function() {
  108. return this.Modules.hooks.fire
  109. .calledWith('getContacts', this.user_id)
  110. .should.equal(true)
  111. })
  112. it('should return a formatted list of contacts in contact list order, without holding accounts', function() {
  113. return this.res.send.args[0][0].contacts.should.deep.equal([
  114. {
  115. id: 'contact-1',
  116. email: 'joe@example.com',
  117. first_name: 'Joe',
  118. last_name: 'Example',
  119. type: 'user'
  120. },
  121. {
  122. id: 'contact-3',
  123. email: 'jim@example.com',
  124. first_name: 'Jim',
  125. last_name: 'Example',
  126. type: 'user'
  127. }
  128. ])
  129. })
  130. })
  131. })