ContactManagerTests.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 sinon = require('sinon')
  13. const modulePath = '../../../../app/src/Features/Contacts/ContactManager'
  14. const SandboxedModule = require('sandboxed-module')
  15. describe('ContactManager', function () {
  16. beforeEach(function () {
  17. this.ContactManager = SandboxedModule.require(modulePath, {
  18. requires: {
  19. request: (this.request = sinon.stub()),
  20. '@overleaf/settings': (this.settings = {
  21. apis: {
  22. contacts: {
  23. url: 'contacts.sharelatex.com',
  24. },
  25. },
  26. }),
  27. },
  28. })
  29. this.user_id = 'user-id-123'
  30. this.contact_id = 'contact-id-123'
  31. return (this.callback = sinon.stub())
  32. })
  33. describe('getContacts', function () {
  34. describe('with a successful response code', function () {
  35. beforeEach(function () {
  36. this.request.get = sinon
  37. .stub()
  38. .callsArgWith(
  39. 1,
  40. null,
  41. { statusCode: 204 },
  42. { contact_ids: (this.contact_ids = ['mock', 'contact_ids']) }
  43. )
  44. return this.ContactManager.getContactIds(
  45. this.user_id,
  46. { limit: 42 },
  47. this.callback
  48. )
  49. })
  50. it('should get the contacts from the contacts api', function () {
  51. return this.request.get
  52. .calledWith({
  53. url: `${this.settings.apis.contacts.url}/user/${this.user_id}/contacts`,
  54. qs: { limit: 42 },
  55. json: true,
  56. jar: false,
  57. })
  58. .should.equal(true)
  59. })
  60. it('should call the callback with the contatcs', function () {
  61. return this.callback
  62. .calledWith(null, this.contact_ids)
  63. .should.equal(true)
  64. })
  65. })
  66. describe('with a failed response code', function () {
  67. beforeEach(function () {
  68. this.request.get = sinon
  69. .stub()
  70. .callsArgWith(1, null, { statusCode: 500 }, null)
  71. return this.ContactManager.getContactIds(
  72. this.user_id,
  73. { limit: 42 },
  74. this.callback
  75. )
  76. })
  77. it('should call the callback with an error', function () {
  78. return this.callback
  79. .calledWith(
  80. sinon.match
  81. .instanceOf(Error)
  82. .and(
  83. sinon.match.has(
  84. 'message',
  85. 'contacts api responded with non-success code: 500'
  86. )
  87. )
  88. )
  89. .should.equal(true)
  90. })
  91. })
  92. })
  93. describe('addContact', function () {
  94. describe('with a successful response code', function () {
  95. beforeEach(function () {
  96. this.request.post = sinon
  97. .stub()
  98. .callsArgWith(1, null, { statusCode: 200 }, null)
  99. return this.ContactManager.addContact(
  100. this.user_id,
  101. this.contact_id,
  102. this.callback
  103. )
  104. })
  105. it('should add the contacts for the user in the contacts api', function () {
  106. return this.request.post
  107. .calledWith({
  108. url: `${this.settings.apis.contacts.url}/user/${this.user_id}/contacts`,
  109. json: {
  110. contact_id: this.contact_id,
  111. },
  112. jar: false,
  113. })
  114. .should.equal(true)
  115. })
  116. it('should call the callback', function () {
  117. return this.callback.called.should.equal(true)
  118. })
  119. })
  120. describe('with a failed response code', function () {
  121. beforeEach(function () {
  122. this.request.post = sinon
  123. .stub()
  124. .callsArgWith(1, null, { statusCode: 500 }, null)
  125. return this.ContactManager.addContact(
  126. this.user_id,
  127. this.contact_id,
  128. this.callback
  129. )
  130. })
  131. it('should call the callback with an error', function () {
  132. return this.callback
  133. .calledWith(
  134. sinon.match
  135. .instanceOf(Error)
  136. .and(
  137. sinon.match.has(
  138. 'message',
  139. 'contacts api responded with non-success code: 500'
  140. )
  141. )
  142. )
  143. .should.equal(true)
  144. })
  145. })
  146. })
  147. })