|
|
@@ -1,16 +1,6 @@
|
|
|
import { vi, describe, beforeEach, it } from 'vitest'
|
|
|
-/* eslint-disable
|
|
|
- no-return-assign,
|
|
|
- no-unused-vars,
|
|
|
-*/
|
|
|
-// TODO: This file was created by bulk-decaffeinate.
|
|
|
-// Fix any style issues and re-enable lint.
|
|
|
-/*
|
|
|
- * decaffeinate suggestions:
|
|
|
- * DS102: Remove unnecessary code created because of implicit returns
|
|
|
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
- */
|
|
|
import sinon from 'sinon'
|
|
|
+import { RequestFailedError } from '@overleaf/fetch-utils'
|
|
|
|
|
|
const modulePath = '../../../app/js/WebApiManager.js'
|
|
|
|
|
|
@@ -21,9 +11,12 @@ describe('WebApiManager', function () {
|
|
|
ctx.user = { _id: ctx.user_id }
|
|
|
ctx.callback = sinon.stub()
|
|
|
|
|
|
- vi.doMock('request', () => ({
|
|
|
- default: (ctx.request = {}),
|
|
|
- }))
|
|
|
+ ctx.fetchUtils = {
|
|
|
+ fetchJson: sinon.stub(),
|
|
|
+ RequestFailedError,
|
|
|
+ }
|
|
|
+
|
|
|
+ vi.doMock('@overleaf/fetch-utils', () => ctx.fetchUtils)
|
|
|
|
|
|
vi.doMock('@overleaf/settings', () => ({
|
|
|
default: (ctx.settings = {
|
|
|
@@ -37,10 +30,10 @@ describe('WebApiManager', function () {
|
|
|
}),
|
|
|
}))
|
|
|
|
|
|
- return (ctx.WebApiManager = (await import(modulePath)).default)
|
|
|
+ ctx.WebApiManager = (await import(modulePath)).default
|
|
|
})
|
|
|
|
|
|
- return describe('joinProject', function () {
|
|
|
+ describe('joinProject', function () {
|
|
|
describe('successfully', function () {
|
|
|
beforeEach(function (ctx) {
|
|
|
ctx.response = {
|
|
|
@@ -50,36 +43,29 @@ describe('WebApiManager', function () {
|
|
|
isTokenMember: true,
|
|
|
isInvitedMember: true,
|
|
|
}
|
|
|
- ctx.request.post = sinon
|
|
|
- .stub()
|
|
|
- .callsArgWith(1, null, { statusCode: 200 }, ctx.response)
|
|
|
- return ctx.WebApiManager.joinProject(
|
|
|
- ctx.project_id,
|
|
|
- ctx.user,
|
|
|
- ctx.callback
|
|
|
- )
|
|
|
+ ctx.fetchUtils.fetchJson.resolves(ctx.response)
|
|
|
+ ctx.WebApiManager.joinProject(ctx.project_id, ctx.user, ctx.callback)
|
|
|
})
|
|
|
|
|
|
it('should send a request to web to join the project', function (ctx) {
|
|
|
- return ctx.request.post
|
|
|
- .calledWith({
|
|
|
- url: `${ctx.settings.apis.web.url}/project/${ctx.project_id}/join`,
|
|
|
- auth: {
|
|
|
+ ctx.fetchUtils.fetchJson.should.have.been.calledWith(
|
|
|
+ new URL(`/project/${ctx.project_id}/join`, ctx.settings.apis.web.url),
|
|
|
+ {
|
|
|
+ method: 'POST',
|
|
|
+ basicAuth: {
|
|
|
user: ctx.settings.apis.web.user,
|
|
|
- pass: ctx.settings.apis.web.pass,
|
|
|
- sendImmediately: true,
|
|
|
+ password: ctx.settings.apis.web.pass,
|
|
|
},
|
|
|
json: {
|
|
|
userId: ctx.user_id,
|
|
|
anonymousAccessToken: undefined,
|
|
|
},
|
|
|
- jar: false,
|
|
|
- })
|
|
|
- .should.equal(true)
|
|
|
+ }
|
|
|
+ )
|
|
|
})
|
|
|
|
|
|
- return it('should return the project, privilegeLevel, and restricted flag', function (ctx) {
|
|
|
- return ctx.callback
|
|
|
+ it('should return the project, privilegeLevel, and restricted flag', function (ctx) {
|
|
|
+ ctx.callback
|
|
|
.calledWith(null, ctx.response.project, ctx.response.privilegeLevel, {
|
|
|
isRestrictedUser: ctx.response.isRestrictedUser,
|
|
|
isTokenMember: ctx.response.isTokenMember,
|
|
|
@@ -104,26 +90,25 @@ describe('WebApiManager', function () {
|
|
|
isTokenMember: false,
|
|
|
isInvitedMember: false,
|
|
|
}
|
|
|
- ctx.request.post = sinon
|
|
|
- .stub()
|
|
|
- .yields(null, { statusCode: 200 }, ctx.response)
|
|
|
+ ctx.fetchUtils.fetchJson.resolves(ctx.response)
|
|
|
ctx.WebApiManager.joinProject(ctx.project_id, ctx.user, ctx.callback)
|
|
|
})
|
|
|
|
|
|
it('should send a request to web to join the project', function (ctx) {
|
|
|
- ctx.request.post.should.have.been.calledWith({
|
|
|
- url: `${ctx.settings.apis.web.url}/project/${ctx.project_id}/join`,
|
|
|
- auth: {
|
|
|
- user: ctx.settings.apis.web.user,
|
|
|
- pass: ctx.settings.apis.web.pass,
|
|
|
- sendImmediately: true,
|
|
|
- },
|
|
|
- json: {
|
|
|
- userId: ctx.user_id,
|
|
|
- anonymousAccessToken: ctx.token,
|
|
|
- },
|
|
|
- jar: false,
|
|
|
- })
|
|
|
+ ctx.fetchUtils.fetchJson.should.have.been.calledWith(
|
|
|
+ new URL(`/project/${ctx.project_id}/join`, ctx.settings.apis.web.url),
|
|
|
+ {
|
|
|
+ method: 'POST',
|
|
|
+ basicAuth: {
|
|
|
+ user: ctx.settings.apis.web.user,
|
|
|
+ password: ctx.settings.apis.web.pass,
|
|
|
+ },
|
|
|
+ json: {
|
|
|
+ userId: ctx.user_id,
|
|
|
+ anonymousAccessToken: ctx.token,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ )
|
|
|
})
|
|
|
|
|
|
it('should return the project, privilegeLevel, and restricted flag', function (ctx) {
|
|
|
@@ -142,9 +127,13 @@ describe('WebApiManager', function () {
|
|
|
|
|
|
describe('when web replies with a 403', function () {
|
|
|
beforeEach(function (ctx) {
|
|
|
- ctx.request.post = sinon
|
|
|
- .stub()
|
|
|
- .callsArgWith(1, null, { statusCode: 403 }, null)
|
|
|
+ ctx.fetchUtils.fetchJson.rejects(
|
|
|
+ new RequestFailedError(
|
|
|
+ `/project/${ctx.project_id}/join`,
|
|
|
+ { method: 'POST' },
|
|
|
+ { status: 403 }
|
|
|
+ )
|
|
|
+ )
|
|
|
ctx.WebApiManager.joinProject(ctx.project_id, ctx.user_id, ctx.callback)
|
|
|
})
|
|
|
|
|
|
@@ -161,9 +150,13 @@ describe('WebApiManager', function () {
|
|
|
|
|
|
describe('when web replies with a 404', function () {
|
|
|
beforeEach(function (ctx) {
|
|
|
- ctx.request.post = sinon
|
|
|
- .stub()
|
|
|
- .callsArgWith(1, null, { statusCode: 404 }, null)
|
|
|
+ ctx.fetchUtils.fetchJson.rejects(
|
|
|
+ new RequestFailedError(
|
|
|
+ `/project/${ctx.project_id}/join`,
|
|
|
+ { method: 'POST' },
|
|
|
+ { status: 404 }
|
|
|
+ )
|
|
|
+ )
|
|
|
ctx.WebApiManager.joinProject(ctx.project_id, ctx.user_id, ctx.callback)
|
|
|
})
|
|
|
|
|
|
@@ -181,18 +174,18 @@ describe('WebApiManager', function () {
|
|
|
|
|
|
describe('with an error from web', function () {
|
|
|
beforeEach(function (ctx) {
|
|
|
- ctx.request.post = sinon
|
|
|
- .stub()
|
|
|
- .callsArgWith(1, null, { statusCode: 500 }, null)
|
|
|
- return ctx.WebApiManager.joinProject(
|
|
|
- ctx.project_id,
|
|
|
- ctx.user_id,
|
|
|
- ctx.callback
|
|
|
+ ctx.fetchUtils.fetchJson.rejects(
|
|
|
+ new RequestFailedError(
|
|
|
+ `/project/${ctx.project_id}/join`,
|
|
|
+ { method: 'POST' },
|
|
|
+ { status: 500 }
|
|
|
+ )
|
|
|
)
|
|
|
+ ctx.WebApiManager.joinProject(ctx.project_id, ctx.user_id, ctx.callback)
|
|
|
})
|
|
|
|
|
|
- return it('should call the callback with an error', function (ctx) {
|
|
|
- return ctx.callback
|
|
|
+ it('should call the callback with an error', function (ctx) {
|
|
|
+ ctx.callback
|
|
|
.calledWith(
|
|
|
sinon.match({
|
|
|
message: 'non-success status code from web',
|
|
|
@@ -205,18 +198,12 @@ describe('WebApiManager', function () {
|
|
|
|
|
|
describe('with no data from web', function () {
|
|
|
beforeEach(function (ctx) {
|
|
|
- ctx.request.post = sinon
|
|
|
- .stub()
|
|
|
- .callsArgWith(1, null, { statusCode: 200 }, null)
|
|
|
- return ctx.WebApiManager.joinProject(
|
|
|
- ctx.project_id,
|
|
|
- ctx.user_id,
|
|
|
- ctx.callback
|
|
|
- )
|
|
|
+ ctx.fetchUtils.fetchJson.resolves(null)
|
|
|
+ ctx.WebApiManager.joinProject(ctx.project_id, ctx.user_id, ctx.callback)
|
|
|
})
|
|
|
|
|
|
- return it('should call the callback with an error', function (ctx) {
|
|
|
- return ctx.callback
|
|
|
+ it('should call the callback with an error', function (ctx) {
|
|
|
+ ctx.callback
|
|
|
.calledWith(
|
|
|
sinon.match({
|
|
|
message: 'no data returned from joinProject request',
|
|
|
@@ -226,20 +213,20 @@ describe('WebApiManager', function () {
|
|
|
})
|
|
|
})
|
|
|
|
|
|
- return describe('when the project is over its rate limit', function () {
|
|
|
+ describe('when the project is over its rate limit', function () {
|
|
|
beforeEach(function (ctx) {
|
|
|
- ctx.request.post = sinon
|
|
|
- .stub()
|
|
|
- .callsArgWith(1, null, { statusCode: 429 }, null)
|
|
|
- return ctx.WebApiManager.joinProject(
|
|
|
- ctx.project_id,
|
|
|
- ctx.user_id,
|
|
|
- ctx.callback
|
|
|
+ ctx.fetchUtils.fetchJson.rejects(
|
|
|
+ new RequestFailedError(
|
|
|
+ `/project/${ctx.project_id}/join`,
|
|
|
+ { method: 'POST' },
|
|
|
+ { status: 429 }
|
|
|
+ )
|
|
|
)
|
|
|
+ ctx.WebApiManager.joinProject(ctx.project_id, ctx.user_id, ctx.callback)
|
|
|
})
|
|
|
|
|
|
- return it('should call the callback with a TooManyRequests error code', function (ctx) {
|
|
|
- return ctx.callback
|
|
|
+ it('should call the callback with a TooManyRequests error code', function (ctx) {
|
|
|
+ ctx.callback
|
|
|
.calledWith(
|
|
|
sinon.match({
|
|
|
message: 'rate-limit hit when joining project',
|