WebApiManagerTests.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 sinon = require('sinon')
  13. const modulePath = '../../../app/js/WebApiManager.js'
  14. const SandboxedModule = require('sandboxed-module')
  15. const { CodedError } = require('../../../app/js/Errors')
  16. describe('WebApiManager', function () {
  17. beforeEach(function () {
  18. this.project_id = 'project-id-123'
  19. this.user_id = 'user-id-123'
  20. this.user = { _id: this.user_id }
  21. this.callback = sinon.stub()
  22. return (this.WebApiManager = SandboxedModule.require(modulePath, {
  23. requires: {
  24. request: (this.request = {}),
  25. '@overleaf/settings': (this.settings = {
  26. apis: {
  27. web: {
  28. url: 'http://web.example.com',
  29. user: 'username',
  30. pass: 'password',
  31. },
  32. },
  33. }),
  34. },
  35. }))
  36. })
  37. return describe('joinProject', function () {
  38. describe('successfully', function () {
  39. beforeEach(function () {
  40. this.response = {
  41. project: { name: 'Test project' },
  42. privilegeLevel: 'owner',
  43. isRestrictedUser: true,
  44. isTokenMember: true,
  45. isInvitedMember: true,
  46. }
  47. this.request.post = sinon
  48. .stub()
  49. .callsArgWith(1, null, { statusCode: 200 }, this.response)
  50. return this.WebApiManager.joinProject(
  51. this.project_id,
  52. this.user,
  53. this.callback
  54. )
  55. })
  56. it('should send a request to web to join the project', function () {
  57. return this.request.post
  58. .calledWith({
  59. url: `${this.settings.apis.web.url}/project/${this.project_id}/join`,
  60. qs: {
  61. user_id: this.user_id,
  62. },
  63. auth: {
  64. user: this.settings.apis.web.user,
  65. pass: this.settings.apis.web.pass,
  66. sendImmediately: true,
  67. },
  68. json: {
  69. userId: this.user_id,
  70. anonymousAccessToken: undefined,
  71. },
  72. jar: false,
  73. headers: {},
  74. })
  75. .should.equal(true)
  76. })
  77. return it('should return the project, privilegeLevel, and restricted flag', function () {
  78. return this.callback
  79. .calledWith(
  80. null,
  81. this.response.project,
  82. this.response.privilegeLevel,
  83. {
  84. isRestrictedUser: this.response.isRestrictedUser,
  85. isTokenMember: this.response.isTokenMember,
  86. isInvitedMember: this.response.isInvitedMember,
  87. }
  88. )
  89. .should.equal(true)
  90. })
  91. })
  92. describe('with anon user', function () {
  93. beforeEach(function () {
  94. this.user_id = 'anonymous-user'
  95. this.token = 'a-ro-token'
  96. this.user = {
  97. _id: this.user_id,
  98. anonymousAccessToken: this.token,
  99. }
  100. this.response = {
  101. project: { name: 'Test project' },
  102. privilegeLevel: 'readOnly',
  103. isRestrictedUser: true,
  104. isTokenMember: false,
  105. isInvitedMember: false,
  106. }
  107. this.request.post = sinon
  108. .stub()
  109. .yields(null, { statusCode: 200 }, this.response)
  110. this.WebApiManager.joinProject(
  111. this.project_id,
  112. this.user,
  113. this.callback
  114. )
  115. })
  116. it('should send a request to web to join the project', function () {
  117. this.request.post.should.have.been.calledWith({
  118. url: `${this.settings.apis.web.url}/project/${this.project_id}/join`,
  119. qs: {
  120. user_id: this.user_id,
  121. },
  122. auth: {
  123. user: this.settings.apis.web.user,
  124. pass: this.settings.apis.web.pass,
  125. sendImmediately: true,
  126. },
  127. json: {
  128. userId: this.user_id,
  129. anonymousAccessToken: this.token,
  130. },
  131. jar: false,
  132. headers: { 'x-sl-anonymous-access-token': this.token },
  133. })
  134. })
  135. it('should return the project, privilegeLevel, and restricted flag', function () {
  136. this.callback.should.have.been.calledWith(
  137. null,
  138. this.response.project,
  139. this.response.privilegeLevel,
  140. {
  141. isRestrictedUser: this.response.isRestrictedUser,
  142. isTokenMember: this.response.isTokenMember,
  143. isInvitedMember: this.response.isInvitedMember,
  144. }
  145. )
  146. })
  147. })
  148. describe('when web replies with a 403', function () {
  149. beforeEach(function () {
  150. this.request.post = sinon
  151. .stub()
  152. .callsArgWith(1, null, { statusCode: 403 }, null)
  153. this.WebApiManager.joinProject(
  154. this.project_id,
  155. this.user_id,
  156. this.callback
  157. )
  158. })
  159. it('should call the callback with an error', function () {
  160. this.callback
  161. .calledWith(
  162. sinon.match({
  163. message: 'not authorized',
  164. })
  165. )
  166. .should.equal(true)
  167. })
  168. })
  169. describe('when web replies with a 404', function () {
  170. beforeEach(function () {
  171. this.request.post = sinon
  172. .stub()
  173. .callsArgWith(1, null, { statusCode: 404 }, null)
  174. this.WebApiManager.joinProject(
  175. this.project_id,
  176. this.user_id,
  177. this.callback
  178. )
  179. })
  180. it('should call the callback with an error', function () {
  181. this.callback
  182. .calledWith(
  183. sinon.match({
  184. message: 'project not found',
  185. info: { code: 'ProjectNotFound' },
  186. })
  187. )
  188. .should.equal(true)
  189. })
  190. })
  191. describe('with an error from web', function () {
  192. beforeEach(function () {
  193. this.request.post = sinon
  194. .stub()
  195. .callsArgWith(1, null, { statusCode: 500 }, null)
  196. return this.WebApiManager.joinProject(
  197. this.project_id,
  198. this.user_id,
  199. this.callback
  200. )
  201. })
  202. return it('should call the callback with an error', function () {
  203. return this.callback
  204. .calledWith(
  205. sinon.match({
  206. message: 'non-success status code from web',
  207. info: { statusCode: 500 },
  208. })
  209. )
  210. .should.equal(true)
  211. })
  212. })
  213. describe('with no data from web', function () {
  214. beforeEach(function () {
  215. this.request.post = sinon
  216. .stub()
  217. .callsArgWith(1, null, { statusCode: 200 }, null)
  218. return this.WebApiManager.joinProject(
  219. this.project_id,
  220. this.user_id,
  221. this.callback
  222. )
  223. })
  224. return it('should call the callback with an error', function () {
  225. return this.callback
  226. .calledWith(
  227. sinon.match({
  228. message: 'no data returned from joinProject request',
  229. })
  230. )
  231. .should.equal(true)
  232. })
  233. })
  234. return describe('when the project is over its rate limit', function () {
  235. beforeEach(function () {
  236. this.request.post = sinon
  237. .stub()
  238. .callsArgWith(1, null, { statusCode: 429 }, null)
  239. return this.WebApiManager.joinProject(
  240. this.project_id,
  241. this.user_id,
  242. this.callback
  243. )
  244. })
  245. return it('should call the callback with a TooManyRequests error code', function () {
  246. return this.callback
  247. .calledWith(
  248. sinon.match({
  249. message: 'rate-limit hit when joining project',
  250. info: {
  251. code: 'TooManyRequests',
  252. },
  253. })
  254. )
  255. .should.equal(true)
  256. })
  257. })
  258. })
  259. })