ContactManagerTests.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const chai = require('chai')
  13. chai.should()
  14. const sinon = require('sinon')
  15. const modulePath = '../../../../app/src/Features/Contacts/ContactManager'
  16. const SandboxedModule = require('sandboxed-module')
  17. describe('ContactManager', function() {
  18. beforeEach(function() {
  19. this.ContactManager = SandboxedModule.require(modulePath, {
  20. globals: {
  21. console: console
  22. },
  23. requires: {
  24. request: (this.request = sinon.stub()),
  25. 'settings-sharelatex': (this.settings = {
  26. apis: {
  27. contacts: {
  28. url: 'contacts.sharelatex.com'
  29. }
  30. }
  31. })
  32. }
  33. })
  34. this.user_id = 'user-id-123'
  35. this.contact_id = 'contact-id-123'
  36. return (this.callback = sinon.stub())
  37. })
  38. describe('getContacts', function() {
  39. describe('with a successful response code', function() {
  40. beforeEach(function() {
  41. this.request.get = sinon
  42. .stub()
  43. .callsArgWith(
  44. 1,
  45. null,
  46. { statusCode: 204 },
  47. { contact_ids: (this.contact_ids = ['mock', 'contact_ids']) }
  48. )
  49. return this.ContactManager.getContactIds(
  50. this.user_id,
  51. { limit: 42 },
  52. this.callback
  53. )
  54. })
  55. it('should get the contacts from the contacts api', function() {
  56. return this.request.get
  57. .calledWith({
  58. url: `${this.settings.apis.contacts.url}/user/${
  59. this.user_id
  60. }/contacts`,
  61. qs: { limit: 42 },
  62. json: true,
  63. jar: false
  64. })
  65. .should.equal(true)
  66. })
  67. it('should call the callback with the contatcs', function() {
  68. return this.callback
  69. .calledWith(null, this.contact_ids)
  70. .should.equal(true)
  71. })
  72. })
  73. describe('with a failed response code', function() {
  74. beforeEach(function() {
  75. this.request.get = sinon
  76. .stub()
  77. .callsArgWith(1, null, { statusCode: 500 }, null)
  78. return this.ContactManager.getContactIds(
  79. this.user_id,
  80. { limit: 42 },
  81. this.callback
  82. )
  83. })
  84. it('should call the callback with an error', function() {
  85. return this.callback
  86. .calledWith(
  87. sinon.match
  88. .instanceOf(Error)
  89. .and(
  90. sinon.match.has(
  91. 'message',
  92. 'contacts api responded with non-success code: 500'
  93. )
  94. )
  95. )
  96. .should.equal(true)
  97. })
  98. })
  99. })
  100. describe('addContact', function() {
  101. describe('with a successful response code', function() {
  102. beforeEach(function() {
  103. this.request.post = sinon
  104. .stub()
  105. .callsArgWith(1, null, { statusCode: 200 }, null)
  106. return this.ContactManager.addContact(
  107. this.user_id,
  108. this.contact_id,
  109. this.callback
  110. )
  111. })
  112. it('should add the contacts for the user in the contacts api', function() {
  113. return this.request.post
  114. .calledWith({
  115. url: `${this.settings.apis.contacts.url}/user/${
  116. this.user_id
  117. }/contacts`,
  118. json: {
  119. contact_id: this.contact_id
  120. },
  121. jar: false
  122. })
  123. .should.equal(true)
  124. })
  125. it('should call the callback', function() {
  126. return this.callback.called.should.equal(true)
  127. })
  128. })
  129. describe('with a failed response code', function() {
  130. beforeEach(function() {
  131. this.request.post = sinon
  132. .stub()
  133. .callsArgWith(1, null, { statusCode: 500 }, null)
  134. return this.ContactManager.addContact(
  135. this.user_id,
  136. this.contact_id,
  137. this.callback
  138. )
  139. })
  140. it('should call the callback with an error', function() {
  141. return this.callback
  142. .calledWith(
  143. sinon.match
  144. .instanceOf(Error)
  145. .and(
  146. sinon.match.has(
  147. 'message',
  148. 'contacts api responded with non-success code: 500'
  149. )
  150. )
  151. )
  152. .should.equal(true)
  153. })
  154. })
  155. })
  156. })