WebApiManagerTests.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  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. const should = chai.should()
  14. const sinon = require('sinon')
  15. const modulePath = '../../../app/js/WebApiManager.js'
  16. const SandboxedModule = require('sandboxed-module')
  17. const { CodedError } = require('../../../app/js/Errors')
  18. describe('WebApiManager', function () {
  19. beforeEach(function () {
  20. this.project_id = 'project-id-123'
  21. this.user_id = 'user-id-123'
  22. this.user = { _id: this.user_id }
  23. this.callback = sinon.stub()
  24. return (this.WebApiManager = SandboxedModule.require(modulePath, {
  25. requires: {
  26. request: (this.request = {}),
  27. 'settings-sharelatex': (this.settings = {
  28. apis: {
  29. web: {
  30. url: 'http://web.example.com',
  31. user: 'username',
  32. pass: 'password'
  33. }
  34. }
  35. }),
  36. 'logger-sharelatex': (this.logger = {
  37. log: sinon.stub(),
  38. error: sinon.stub()
  39. })
  40. }
  41. }))
  42. })
  43. return describe('joinProject', function () {
  44. describe('successfully', function () {
  45. beforeEach(function () {
  46. this.response = {
  47. project: { name: 'Test project' },
  48. privilegeLevel: 'owner',
  49. isRestrictedUser: true
  50. }
  51. this.request.post = sinon
  52. .stub()
  53. .callsArgWith(1, null, { statusCode: 200 }, this.response)
  54. return this.WebApiManager.joinProject(
  55. this.project_id,
  56. this.user,
  57. this.callback
  58. )
  59. })
  60. it('should send a request to web to join the project', function () {
  61. return this.request.post
  62. .calledWith({
  63. url: `${this.settings.apis.web.url}/project/${this.project_id}/join`,
  64. qs: {
  65. user_id: this.user_id
  66. },
  67. auth: {
  68. user: this.settings.apis.web.user,
  69. pass: this.settings.apis.web.pass,
  70. sendImmediately: true
  71. },
  72. json: true,
  73. jar: false,
  74. headers: {}
  75. })
  76. .should.equal(true)
  77. })
  78. return it('should return the project, privilegeLevel, and restricted flag', function () {
  79. return this.callback
  80. .calledWith(
  81. null,
  82. this.response.project,
  83. this.response.privilegeLevel,
  84. this.response.isRestrictedUser
  85. )
  86. .should.equal(true)
  87. })
  88. })
  89. describe('with an error from web', function () {
  90. beforeEach(function () {
  91. this.request.post = sinon
  92. .stub()
  93. .callsArgWith(1, null, { statusCode: 500 }, null)
  94. return this.WebApiManager.joinProject(
  95. this.project_id,
  96. this.user_id,
  97. this.callback
  98. )
  99. })
  100. return it('should call the callback with an error', function () {
  101. return this.callback
  102. .calledWith(
  103. sinon.match({
  104. message: 'non-success status code from web',
  105. info: { statusCode: 500 }
  106. })
  107. )
  108. .should.equal(true)
  109. })
  110. })
  111. describe('with no data from web', function () {
  112. beforeEach(function () {
  113. this.request.post = sinon
  114. .stub()
  115. .callsArgWith(1, null, { statusCode: 200 }, null)
  116. return this.WebApiManager.joinProject(
  117. this.project_id,
  118. this.user_id,
  119. this.callback
  120. )
  121. })
  122. return it('should call the callback with an error', function () {
  123. return this.callback
  124. .calledWith(
  125. sinon.match({
  126. message: 'no data returned from joinProject request'
  127. })
  128. )
  129. .should.equal(true)
  130. })
  131. })
  132. return describe('when the project is over its rate limit', function () {
  133. beforeEach(function () {
  134. this.request.post = sinon
  135. .stub()
  136. .callsArgWith(1, null, { statusCode: 429 }, null)
  137. return this.WebApiManager.joinProject(
  138. this.project_id,
  139. this.user_id,
  140. this.callback
  141. )
  142. })
  143. return it('should call the callback with a TooManyRequests error code', function () {
  144. return this.callback
  145. .calledWith(
  146. sinon.match({
  147. message: 'rate-limit hit when joining project',
  148. info: {
  149. code: 'TooManyRequests'
  150. }
  151. })
  152. )
  153. .should.equal(true)
  154. })
  155. })
  156. })
  157. })