|
|
@@ -18,7 +18,8 @@ describe('InstitutionsAPI', function () {
|
|
|
ctx.settings = {
|
|
|
apis: { v1: { url: 'v1.url', user: '', pass: '', timeout: 5000 } },
|
|
|
}
|
|
|
-
|
|
|
+ ctx.request = sinon.stub()
|
|
|
+ ctx.fetchNothing = sinon.stub()
|
|
|
ctx.ipMatcherNotification = {
|
|
|
read: (ctx.markAsReadIpMatcher = sinon.stub().resolves()),
|
|
|
}
|
|
|
@@ -27,8 +28,12 @@ describe('InstitutionsAPI', function () {
|
|
|
default: ctx.settings,
|
|
|
}))
|
|
|
|
|
|
+ vi.doMock('requestretry', () => ({
|
|
|
+ default: ctx.request,
|
|
|
+ }))
|
|
|
+
|
|
|
vi.doMock('@overleaf/fetch-utils', () => ({
|
|
|
- fetchNothing: (ctx.fetchNothing = sinon.stub()),
|
|
|
+ fetchNothing: ctx.fetchNothing,
|
|
|
fetchJson: (ctx.fetchJson = sinon.stub()),
|
|
|
}))
|
|
|
|
|
|
@@ -69,18 +74,21 @@ describe('InstitutionsAPI', function () {
|
|
|
it('get affiliations', async function (ctx) {
|
|
|
ctx.institutionId = 123
|
|
|
const responseBody = ['123abc', '456def']
|
|
|
- ctx.fetchJson.resolves(responseBody)
|
|
|
+ ctx.request.yields(null, { statusCode: 200 }, responseBody)
|
|
|
const body =
|
|
|
await ctx.InstitutionsAPI.promises.getInstitutionAffiliations(
|
|
|
ctx.institutionId
|
|
|
)
|
|
|
|
|
|
- ctx.fetchJson.calledOnce.should.equal(true)
|
|
|
+ ctx.request.calledOnce.should.equal(true)
|
|
|
+ const requestOptions = ctx.request.lastCall.args[0]
|
|
|
const expectedUrl = `v1.url/api/v2/institutions/${ctx.institutionId}/affiliations`
|
|
|
- ctx.fetchJson.lastCall.args[0].should.equal(expectedUrl)
|
|
|
- const requestOptions = ctx.fetchJson.lastCall.args[1]
|
|
|
+ requestOptions.url.should.equal(expectedUrl)
|
|
|
requestOptions.method.should.equal('GET')
|
|
|
- expect(requestOptions.json).not.to.exist
|
|
|
+ requestOptions.maxAttempts.should.exist
|
|
|
+ requestOptions.maxAttempts.should.not.equal(0)
|
|
|
+ requestOptions.retryDelay.should.exist
|
|
|
+ expect(requestOptions.body).not.to.exist
|
|
|
body.should.equal(responseBody)
|
|
|
})
|
|
|
|
|
|
@@ -109,17 +117,15 @@ describe('InstitutionsAPI', function () {
|
|
|
max_confirmation_months: [],
|
|
|
},
|
|
|
}
|
|
|
- ctx.fetchJson.resolves(v1Result)
|
|
|
+ ctx.request.callsArgWith(1, null, { statusCode: 201 }, v1Result)
|
|
|
await ctx.InstitutionsAPI.promises.getLicencesForAnalytics(lag, queryDate)
|
|
|
- const expectedUrl = `v1.url/api/v2/institutions/institutions_licences`
|
|
|
- ctx.fetchJson.lastCall.args[0].should.equal(expectedUrl)
|
|
|
- const requestOptions = ctx.fetchJson.lastCall.args[1]
|
|
|
- expect(requestOptions.json.query_date).to.equal(queryDate)
|
|
|
- expect(requestOptions.json.lag).to.equal(lag)
|
|
|
+ const requestOptions = ctx.request.lastCall.args[0]
|
|
|
+ expect(requestOptions.body.query_date).to.equal(queryDate)
|
|
|
+ expect(requestOptions.body.lag).to.equal(lag)
|
|
|
requestOptions.method.should.equal('GET')
|
|
|
})
|
|
|
it('should handle errors', async function (ctx) {
|
|
|
- ctx.fetchJson.throws({ response: { status: 500 } })
|
|
|
+ ctx.request.callsArgWith(1, null, { statusCode: 500 })
|
|
|
let error
|
|
|
|
|
|
try {
|
|
|
@@ -146,17 +152,18 @@ describe('InstitutionsAPI', function () {
|
|
|
},
|
|
|
},
|
|
|
]
|
|
|
- ctx.fetchJson.resolves(responseBody)
|
|
|
+ ctx.request.callsArgWith(1, null, { statusCode: 201 }, responseBody)
|
|
|
const body = await ctx.InstitutionsAPI.promises.getUserAffiliations(
|
|
|
ctx.stubbedUser._id
|
|
|
)
|
|
|
- ctx.fetchJson.calledOnce.should.equal(true)
|
|
|
+ ctx.request.calledOnce.should.equal(true)
|
|
|
+ const requestOptions = ctx.request.lastCall.args[0]
|
|
|
const expectedUrl = `v1.url/api/v2/users/${ctx.stubbedUser._id}/affiliations`
|
|
|
- ctx.fetchJson.lastCall.args[0].should.equal(expectedUrl)
|
|
|
- const requestOptions = ctx.fetchJson.lastCall.args[1]
|
|
|
+ requestOptions.url.should.equal(expectedUrl)
|
|
|
requestOptions.method.should.equal('GET')
|
|
|
+ requestOptions.maxAttempts.should.equal(3)
|
|
|
ctx.Modules.promises.hooks.fire.should.have.been.called
|
|
|
- expect(requestOptions.json).not.to.exist
|
|
|
+ expect(requestOptions.body).not.to.exist
|
|
|
expect(body).to.deep.equal(responseBody)
|
|
|
})
|
|
|
|
|
|
@@ -166,13 +173,12 @@ describe('InstitutionsAPI', function () {
|
|
|
id: '123abc',
|
|
|
foo: 'bar',
|
|
|
institution: {
|
|
|
- id: 'test-institution-id',
|
|
|
commonsAccount: false,
|
|
|
confirmed: true,
|
|
|
},
|
|
|
},
|
|
|
]
|
|
|
- ctx.fetchJson.resolves(responseBody)
|
|
|
+ ctx.request.callsArgWith(1, null, { statusCode: 201 }, responseBody)
|
|
|
const groupResponse = {
|
|
|
_id: new ObjectId(),
|
|
|
managedUsersEnabled: false,
|
|
|
@@ -187,16 +193,17 @@ describe('InstitutionsAPI', function () {
|
|
|
const body = await ctx.InstitutionsAPI.promises.getUserAffiliations(
|
|
|
ctx.stubbedUser._id
|
|
|
)
|
|
|
- ctx.fetchJson.calledOnce.should.equal(true)
|
|
|
+ ctx.request.calledOnce.should.equal(true)
|
|
|
+ const requestOptions = ctx.request.lastCall.args[0]
|
|
|
const expectedUrl = `v1.url/api/v2/users/${ctx.stubbedUser._id}/affiliations`
|
|
|
- ctx.fetchJson.lastCall.args[0].should.equal(expectedUrl)
|
|
|
- const requestOptions = ctx.fetchJson.lastCall.args[1]
|
|
|
+ requestOptions.url.should.equal(expectedUrl)
|
|
|
requestOptions.method.should.equal('GET')
|
|
|
+ requestOptions.maxAttempts.should.equal(3)
|
|
|
ctx.Modules.promises.hooks.fire.should.have.been.calledWith(
|
|
|
'getGroupWithDomainCaptureByV1Id',
|
|
|
responseBody[0].institution.id
|
|
|
)
|
|
|
- expect(requestOptions.json).not.to.exist
|
|
|
+ expect(requestOptions.body).not.to.exist
|
|
|
expect(body).to.deep.equal([
|
|
|
{
|
|
|
...responseBody[0],
|
|
|
@@ -218,18 +225,19 @@ describe('InstitutionsAPI', function () {
|
|
|
},
|
|
|
},
|
|
|
]
|
|
|
- ctx.fetchJson.resolves(responseBody)
|
|
|
+ ctx.request.callsArgWith(1, null, { statusCode: 201 }, responseBody)
|
|
|
|
|
|
const body = await ctx.InstitutionsAPI.promises.getUserAffiliations(
|
|
|
ctx.stubbedUser._id
|
|
|
)
|
|
|
- ctx.fetchJson.calledOnce.should.equal(true)
|
|
|
+ ctx.request.calledOnce.should.equal(true)
|
|
|
+ const requestOptions = ctx.request.lastCall.args[0]
|
|
|
const expectedUrl = `v1.url/api/v2/users/${ctx.stubbedUser._id}/affiliations`
|
|
|
- ctx.fetchJson.lastCall.args[0].should.equal(expectedUrl)
|
|
|
- const requestOptions = ctx.fetchJson.lastCall.args[1]
|
|
|
+ requestOptions.url.should.equal(expectedUrl)
|
|
|
requestOptions.method.should.equal('GET')
|
|
|
+ requestOptions.maxAttempts.should.equal(3)
|
|
|
ctx.Modules.promises.hooks.fire.should.not.have.been.called
|
|
|
- expect(requestOptions.json).not.to.exist
|
|
|
+ expect(requestOptions.body).not.to.exist
|
|
|
expect(body).to.deep.equal([
|
|
|
{
|
|
|
...responseBody[0],
|
|
|
@@ -238,7 +246,8 @@ describe('InstitutionsAPI', function () {
|
|
|
})
|
|
|
|
|
|
it('handle error', async function (ctx) {
|
|
|
- ctx.fetchJson.throws({ response: { status: 503 } })
|
|
|
+ const body = { errors: 'affiliation error message' }
|
|
|
+ ctx.request.callsArgWith(1, null, { statusCode: 503 }, body)
|
|
|
let error
|
|
|
|
|
|
try {
|
|
|
@@ -264,17 +273,17 @@ describe('InstitutionsAPI', function () {
|
|
|
|
|
|
describe('getUsersNeedingReconfirmationsLapsedProcessed', function () {
|
|
|
it('get the list of users', async function (ctx) {
|
|
|
- ctx.fetchJson.resolves({})
|
|
|
+ ctx.fetchJson.resolves({ statusCode: 200 })
|
|
|
await ctx.InstitutionsAPI.promises.getUsersNeedingReconfirmationsLapsedProcessed()
|
|
|
ctx.fetchJson.calledOnce.should.equal(true)
|
|
|
+ const requestOptions = ctx.fetchJson.lastCall.args[1]
|
|
|
const expectedUrl = `v1.url/api/v2/institutions/need_reconfirmation_lapsed_processed`
|
|
|
ctx.fetchJson.lastCall.args[0].should.equal(expectedUrl)
|
|
|
- const requestOptions = ctx.fetchJson.lastCall.args[1]
|
|
|
requestOptions.method.should.equal('GET')
|
|
|
})
|
|
|
|
|
|
it('handle error', async function (ctx) {
|
|
|
- ctx.fetchJson.throws({ response: { status: 500 } })
|
|
|
+ ctx.fetchJson.throws({ info: { statusCode: 500 } })
|
|
|
await expect(
|
|
|
ctx.InstitutionsAPI.promises.getUsersNeedingReconfirmationsLapsedProcessed()
|
|
|
).to.be.rejected
|
|
|
@@ -283,7 +292,7 @@ describe('InstitutionsAPI', function () {
|
|
|
|
|
|
describe('addAffiliation', function () {
|
|
|
beforeEach(function (ctx) {
|
|
|
- ctx.fetchNothing.resolves()
|
|
|
+ ctx.fetchNothing.resolves({ status: 201 })
|
|
|
})
|
|
|
|
|
|
it('add affiliation', async function (ctx) {
|
|
|
@@ -300,9 +309,9 @@ describe('InstitutionsAPI', function () {
|
|
|
affiliationOptions
|
|
|
)
|
|
|
ctx.fetchNothing.calledOnce.should.equal(true)
|
|
|
+ const requestOptions = ctx.fetchNothing.lastCall.args[1]
|
|
|
const expectedUrl = `v1.url/api/v2/users/${ctx.stubbedUser._id}/affiliations`
|
|
|
expect(ctx.fetchNothing.lastCall.args[0]).to.equal(expectedUrl)
|
|
|
- const requestOptions = ctx.fetchNothing.lastCall.args[1]
|
|
|
requestOptions.method.should.equal('POST')
|
|
|
|
|
|
const { json } = requestOptions
|
|
|
@@ -406,9 +415,9 @@ describe('InstitutionsAPI', function () {
|
|
|
ctx.newEmail
|
|
|
)
|
|
|
ctx.fetchNothing.calledOnce.should.equal(true)
|
|
|
+ const requestOptions = ctx.fetchNothing.lastCall.args[1]
|
|
|
const expectedUrl = `v1.url/api/v2/users/${ctx.stubbedUser._id}/affiliations/remove`
|
|
|
ctx.fetchNothing.lastCall.args[0].should.equal(expectedUrl)
|
|
|
- const requestOptions = ctx.fetchNothing.lastCall.args[1]
|
|
|
requestOptions.method.should.equal('POST')
|
|
|
expect(requestOptions.json).to.deep.equal({ email: ctx.newEmail })
|
|
|
})
|
|
|
@@ -433,17 +442,18 @@ describe('InstitutionsAPI', function () {
|
|
|
|
|
|
describe('deleteAffiliations', function () {
|
|
|
it('delete affiliations', async function (ctx) {
|
|
|
- ctx.fetchNothing.resolves()
|
|
|
+ ctx.request.callsArgWith(1, null, { statusCode: 200 })
|
|
|
await ctx.InstitutionsAPI.promises.deleteAffiliations(ctx.stubbedUser._id)
|
|
|
- ctx.fetchNothing.calledOnce.should.equal(true)
|
|
|
+ ctx.request.calledOnce.should.equal(true)
|
|
|
+ const requestOptions = ctx.request.lastCall.args[0]
|
|
|
const expectedUrl = `v1.url/api/v2/users/${ctx.stubbedUser._id}/affiliations`
|
|
|
- ctx.fetchNothing.lastCall.args[0].should.equal(expectedUrl)
|
|
|
- const requestOptions = ctx.fetchNothing.lastCall.args[1]
|
|
|
+ requestOptions.url.should.equal(expectedUrl)
|
|
|
requestOptions.method.should.equal('DELETE')
|
|
|
})
|
|
|
|
|
|
it('handle error', async function (ctx) {
|
|
|
- ctx.fetchNothing.throws({ response: { status: 518 } })
|
|
|
+ const body = { errors: 'affiliation error message' }
|
|
|
+ ctx.request.callsArgWith(1, null, { statusCode: 518 }, body)
|
|
|
let error
|
|
|
|
|
|
try {
|
|
|
@@ -460,7 +470,7 @@ describe('InstitutionsAPI', function () {
|
|
|
|
|
|
describe('endorseAffiliation', function () {
|
|
|
beforeEach(function (ctx) {
|
|
|
- ctx.fetchNothing.resolves()
|
|
|
+ ctx.request.callsArgWith(1, null, { statusCode: 204 })
|
|
|
})
|
|
|
|
|
|
it('endorse affiliation', async function (ctx) {
|
|
|
@@ -470,17 +480,17 @@ describe('InstitutionsAPI', function () {
|
|
|
'Student',
|
|
|
'Physics'
|
|
|
)
|
|
|
- ctx.fetchNothing.calledOnce.should.equal(true)
|
|
|
+ ctx.request.calledOnce.should.equal(true)
|
|
|
+ const requestOptions = ctx.request.lastCall.args[0]
|
|
|
const expectedUrl = `v1.url/api/v2/users/${ctx.stubbedUser._id}/affiliations/endorse`
|
|
|
- ctx.fetchNothing.lastCall.args[0].should.equal(expectedUrl)
|
|
|
- const requestOptions = ctx.fetchNothing.lastCall.args[1]
|
|
|
+ requestOptions.url.should.equal(expectedUrl)
|
|
|
requestOptions.method.should.equal('POST')
|
|
|
|
|
|
- const { json } = requestOptions
|
|
|
- Object.keys(json).length.should.equal(3)
|
|
|
- json.email.should.equal(ctx.newEmail)
|
|
|
- json.role.should.equal('Student')
|
|
|
- json.department.should.equal('Physics')
|
|
|
+ const { body } = requestOptions
|
|
|
+ Object.keys(body).length.should.equal(3)
|
|
|
+ body.email.should.equal(ctx.newEmail)
|
|
|
+ body.role.should.equal('Student')
|
|
|
+ body.department.should.equal('Physics')
|
|
|
})
|
|
|
})
|
|
|
|
|
|
@@ -488,21 +498,21 @@ describe('InstitutionsAPI', function () {
|
|
|
const users = ['abc123', 'def456']
|
|
|
|
|
|
it('sends the list of users', async function (ctx) {
|
|
|
- ctx.fetchNothing.resolves()
|
|
|
+ ctx.request.callsArgWith(1, null, { statusCode: 200 })
|
|
|
await ctx.InstitutionsAPI.promises.sendUsersWithReconfirmationsLapsedProcessed(
|
|
|
users
|
|
|
)
|
|
|
- ctx.fetchNothing.calledOnce.should.equal(true)
|
|
|
+ ctx.request.calledOnce.should.equal(true)
|
|
|
+ const requestOptions = ctx.request.lastCall.args[0]
|
|
|
const expectedUrl =
|
|
|
'v1.url/api/v2/institutions/reconfirmation_lapsed_processed'
|
|
|
- ctx.fetchNothing.lastCall.args[0].should.equal(expectedUrl)
|
|
|
- const requestOptions = ctx.fetchNothing.lastCall.args[1]
|
|
|
+ requestOptions.url.should.equal(expectedUrl)
|
|
|
requestOptions.method.should.equal('POST')
|
|
|
- expect(requestOptions.json).to.deep.equal({ users })
|
|
|
+ expect(requestOptions.body).to.deep.equal({ users })
|
|
|
})
|
|
|
|
|
|
it('handle error', async function (ctx) {
|
|
|
- ctx.fetchNothing.throws({ response: { status: 500 } })
|
|
|
+ ctx.request.callsArgWith(1, null, { statusCode: 500 })
|
|
|
let error
|
|
|
|
|
|
try {
|