WebApiManagerTests.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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('when web replies with a 403', function () {
  90. beforeEach(function () {
  91. this.request.post = sinon
  92. .stub()
  93. .callsArgWith(1, null, { statusCode: 403 }, null)
  94. this.WebApiManager.joinProject(
  95. this.project_id,
  96. this.user_id,
  97. this.callback
  98. )
  99. })
  100. it('should call the callback with an error', function () {
  101. this.callback
  102. .calledWith(
  103. sinon.match({
  104. message: 'not authorized'
  105. })
  106. )
  107. .should.equal(true)
  108. })
  109. })
  110. describe('when web replies with a 404', function () {
  111. beforeEach(function () {
  112. this.request.post = sinon
  113. .stub()
  114. .callsArgWith(1, null, { statusCode: 404 }, null)
  115. this.WebApiManager.joinProject(
  116. this.project_id,
  117. this.user_id,
  118. this.callback
  119. )
  120. })
  121. it('should call the callback with an error', function () {
  122. this.callback
  123. .calledWith(
  124. sinon.match({
  125. message: 'project not found',
  126. info: { code: 'ProjectNotFound' }
  127. })
  128. )
  129. .should.equal(true)
  130. })
  131. })
  132. describe('with an error from web', function () {
  133. beforeEach(function () {
  134. this.request.post = sinon
  135. .stub()
  136. .callsArgWith(1, null, { statusCode: 500 }, 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 an error', function () {
  144. return this.callback
  145. .calledWith(
  146. sinon.match({
  147. message: 'non-success status code from web',
  148. info: { statusCode: 500 }
  149. })
  150. )
  151. .should.equal(true)
  152. })
  153. })
  154. describe('with no data from web', function () {
  155. beforeEach(function () {
  156. this.request.post = sinon
  157. .stub()
  158. .callsArgWith(1, null, { statusCode: 200 }, null)
  159. return this.WebApiManager.joinProject(
  160. this.project_id,
  161. this.user_id,
  162. this.callback
  163. )
  164. })
  165. return it('should call the callback with an error', function () {
  166. return this.callback
  167. .calledWith(
  168. sinon.match({
  169. message: 'no data returned from joinProject request'
  170. })
  171. )
  172. .should.equal(true)
  173. })
  174. })
  175. return describe('when the project is over its rate limit', function () {
  176. beforeEach(function () {
  177. this.request.post = sinon
  178. .stub()
  179. .callsArgWith(1, null, { statusCode: 429 }, null)
  180. return this.WebApiManager.joinProject(
  181. this.project_id,
  182. this.user_id,
  183. this.callback
  184. )
  185. })
  186. return it('should call the callback with a TooManyRequests error code', function () {
  187. return this.callback
  188. .calledWith(
  189. sinon.match({
  190. message: 'rate-limit hit when joining project',
  191. info: {
  192. code: 'TooManyRequests'
  193. }
  194. })
  195. )
  196. .should.equal(true)
  197. })
  198. })
  199. })
  200. })