WebApiManagerTests.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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({ message: 'non-success status code from web: 500' })
  104. )
  105. .should.equal(true)
  106. })
  107. })
  108. describe('with no data from web', function () {
  109. beforeEach(function () {
  110. this.request.post = sinon
  111. .stub()
  112. .callsArgWith(1, null, { statusCode: 200 }, null)
  113. return this.WebApiManager.joinProject(
  114. this.project_id,
  115. this.user_id,
  116. this.callback
  117. )
  118. })
  119. return it('should call the callback with an error', function () {
  120. return this.callback
  121. .calledWith(
  122. sinon.match({
  123. message: 'no data returned from joinProject request'
  124. })
  125. )
  126. .should.equal(true)
  127. })
  128. })
  129. return describe('when the project is over its rate limit', function () {
  130. beforeEach(function () {
  131. this.request.post = sinon
  132. .stub()
  133. .callsArgWith(1, null, { statusCode: 429 }, null)
  134. return this.WebApiManager.joinProject(
  135. this.project_id,
  136. this.user_id,
  137. this.callback
  138. )
  139. })
  140. return it('should call the callback with a TooManyRequests error code', function () {
  141. return this.callback
  142. .calledWith(
  143. sinon.match({
  144. message: 'rate-limit hit when joining project',
  145. code: 'TooManyRequests'
  146. })
  147. )
  148. .should.equal(true)
  149. })
  150. })
  151. })
  152. })