| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- const { expect } = require('chai')
- const SandboxedModule = require('sandboxed-module')
- const path = require('path')
- const sinon = require('sinon')
- const modulePath = path.join(
- __dirname,
- '../../../../app/src/Features/Institutions/InstitutionsAPI'
- )
- const Errors = require('../../../../app/src/Features/Errors/Errors')
- describe('InstitutionsAPI', function () {
- beforeEach(function () {
- this.settings = { apis: { v1: { url: 'v1.url', user: '', pass: '' } } }
- this.request = sinon.stub()
- this.ipMatcherNotification = {
- read: (this.markAsReadIpMatcher = sinon.stub().callsArgWith(1, null)),
- }
- this.InstitutionsAPI = SandboxedModule.require(modulePath, {
- requires: {
- '@overleaf/metrics': {
- timeAsyncMethod: sinon.stub(),
- },
- '@overleaf/settings': this.settings,
- requestretry: this.request,
- '../Notifications/NotificationsBuilder': {
- ipMatcherAffiliation: sinon
- .stub()
- .returns(this.ipMatcherNotification),
- },
- },
- })
- this.stubbedUser = {
- _id: '3131231',
- name: 'bob',
- email: 'hello@world.com',
- }
- this.newEmail = 'bob@bob.com'
- })
- describe('getInstitutionAffiliations', function () {
- it('get affiliations', function (done) {
- this.institutionId = 123
- const responseBody = ['123abc', '456def']
- this.request.yields(null, { statusCode: 200 }, responseBody)
- this.InstitutionsAPI.getInstitutionAffiliations(
- this.institutionId,
- (err, body) => {
- expect(err).not.to.exist
- this.request.calledOnce.should.equal(true)
- const requestOptions = this.request.lastCall.args[0]
- const expectedUrl = `v1.url/api/v2/institutions/${this.institutionId}/affiliations`
- requestOptions.url.should.equal(expectedUrl)
- requestOptions.method.should.equal('GET')
- requestOptions.maxAttempts.should.exist
- requestOptions.maxAttempts.should.not.equal(0)
- requestOptions.retryDelay.should.exist
- expect(requestOptions.body).not.to.exist
- body.should.equal(responseBody)
- done()
- }
- )
- })
- it('handle empty response', function (done) {
- this.settings.apis.v1.url = ''
- this.InstitutionsAPI.getInstitutionAffiliations(
- this.institutionId,
- (err, body) => {
- expect(err).not.to.exist
- expect(body).to.be.a('Array')
- body.length.should.equal(0)
- done()
- }
- )
- })
- })
- describe('getLicencesForAnalytics', function () {
- const lag = 'daily'
- const queryDate = '2017-01-07:00:00.000Z'
- it('should send the request to v1', function (done) {
- const v1Result = {
- lag: 'daily',
- date: queryDate,
- data: {
- user_counts: { total: [], new: [] },
- max_confirmation_months: [],
- },
- }
- this.request.callsArgWith(1, null, { statusCode: 201 }, v1Result)
- this.InstitutionsAPI.getLicencesForAnalytics(
- lag,
- queryDate,
- (error, result) => {
- expect(error).not.to.exist
- const requestOptions = this.request.lastCall.args[0]
- expect(requestOptions.body.query_date).to.equal(queryDate)
- expect(requestOptions.body.lag).to.equal(lag)
- requestOptions.method.should.equal('GET')
- done()
- }
- )
- })
- it('should handle errors', function (done) {
- this.request.callsArgWith(1, null, { statusCode: 500 })
- this.InstitutionsAPI.getLicencesForAnalytics(
- lag,
- queryDate,
- (error, result) => {
- expect(error).to.be.instanceof(Errors.V1ConnectionError)
- done()
- }
- )
- })
- })
- describe('getUserAffiliations', function () {
- it('get affiliations', function (done) {
- const responseBody = [{ foo: 'bar' }]
- this.request.callsArgWith(1, null, { statusCode: 201 }, responseBody)
- this.InstitutionsAPI.getUserAffiliations(
- this.stubbedUser._id,
- (err, body) => {
- expect(err).not.to.exist
- this.request.calledOnce.should.equal(true)
- const requestOptions = this.request.lastCall.args[0]
- const expectedUrl = `v1.url/api/v2/users/${this.stubbedUser._id}/affiliations`
- requestOptions.url.should.equal(expectedUrl)
- requestOptions.method.should.equal('GET')
- requestOptions.maxAttempts.should.equal(3)
- expect(requestOptions.body).not.to.exist
- body.should.equal(responseBody)
- done()
- }
- )
- })
- it('handle error', function (done) {
- const body = { errors: 'affiliation error message' }
- this.request.callsArgWith(1, null, { statusCode: 503 }, body)
- this.InstitutionsAPI.getUserAffiliations(this.stubbedUser._id, err => {
- expect(err).to.be.instanceof(Errors.V1ConnectionError)
- done()
- })
- })
- it('handle empty response', function (done) {
- this.settings.apis.v1.url = ''
- this.InstitutionsAPI.getUserAffiliations(
- this.stubbedUser._id,
- (err, body) => {
- expect(err).not.to.exist
- expect(body).to.be.a('Array')
- body.length.should.equal(0)
- done()
- }
- )
- })
- })
- describe('getUsersNeedingReconfirmationsLapsedProcessed', function () {
- it('get the list of users', function (done) {
- this.request.callsArgWith(1, null, { statusCode: 200 })
- this.InstitutionsAPI.getUsersNeedingReconfirmationsLapsedProcessed(
- error => {
- expect(error).not.to.exist
- this.request.calledOnce.should.equal(true)
- const requestOptions = this.request.lastCall.args[0]
- const expectedUrl = `v1.url/api/v2/institutions/need_reconfirmation_lapsed_processed`
- requestOptions.url.should.equal(expectedUrl)
- requestOptions.method.should.equal('GET')
- done()
- }
- )
- })
- it('handle error', function (done) {
- this.request.callsArgWith(1, null, { statusCode: 500 })
- this.InstitutionsAPI.getUsersNeedingReconfirmationsLapsedProcessed(
- error => {
- expect(error).to.exist
- done()
- }
- )
- })
- })
- describe('addAffiliation', function () {
- beforeEach(function () {
- this.request.callsArgWith(1, null, { statusCode: 201 })
- })
- it('add affiliation', function (done) {
- const affiliationOptions = {
- university: { id: 1 },
- role: 'Prof',
- department: 'Math',
- confirmedAt: new Date(),
- entitlement: true,
- }
- this.InstitutionsAPI.addAffiliation(
- this.stubbedUser._id,
- this.newEmail,
- affiliationOptions,
- err => {
- expect(err).not.to.exist
- this.request.calledOnce.should.equal(true)
- const requestOptions = this.request.lastCall.args[0]
- const expectedUrl = `v1.url/api/v2/users/${this.stubbedUser._id}/affiliations`
- requestOptions.url.should.equal(expectedUrl)
- requestOptions.method.should.equal('POST')
- requestOptions.maxAttempts.should.equal(0)
- const { body } = requestOptions
- Object.keys(body).length.should.equal(7)
- body.email.should.equal(this.newEmail)
- body.university.should.equal(affiliationOptions.university)
- body.department.should.equal(affiliationOptions.department)
- body.role.should.equal(affiliationOptions.role)
- body.confirmedAt.should.equal(affiliationOptions.confirmedAt)
- body.entitlement.should.equal(affiliationOptions.entitlement)
- this.markAsReadIpMatcher.calledOnce.should.equal(true)
- done()
- }
- )
- })
- it('handle error', function (done) {
- const body = { errors: 'affiliation error message' }
- this.request.callsArgWith(1, null, { statusCode: 422 }, body)
- this.InstitutionsAPI.addAffiliation(
- this.stubbedUser._id,
- this.newEmail,
- {},
- err => {
- expect(err).to.exist
- err.message.should.have.string(422)
- err.message.should.have.string(body.errors)
- done()
- }
- )
- })
- })
- describe('removeAffiliation', function () {
- beforeEach(function () {
- this.request.callsArgWith(1, null, { statusCode: 404 })
- })
- it('remove affiliation', function (done) {
- this.InstitutionsAPI.removeAffiliation(
- this.stubbedUser._id,
- this.newEmail,
- err => {
- expect(err).not.to.exist
- this.request.calledOnce.should.equal(true)
- const requestOptions = this.request.lastCall.args[0]
- const expectedUrl = `v1.url/api/v2/users/${this.stubbedUser._id}/affiliations/remove`
- requestOptions.url.should.equal(expectedUrl)
- requestOptions.method.should.equal('POST')
- expect(requestOptions.body).to.deep.equal({ email: this.newEmail })
- done()
- }
- )
- })
- it('handle error', function (done) {
- this.request.callsArgWith(1, null, { statusCode: 500 })
- this.InstitutionsAPI.removeAffiliation(
- this.stubbedUser._id,
- this.newEmail,
- err => {
- expect(err).to.exist
- err.message.should.exist
- done()
- }
- )
- })
- })
- describe('deleteAffiliations', function () {
- it('delete affiliations', function (done) {
- this.request.callsArgWith(1, null, { statusCode: 200 })
- this.InstitutionsAPI.deleteAffiliations(this.stubbedUser._id, err => {
- expect(err).not.to.exist
- this.request.calledOnce.should.equal(true)
- const requestOptions = this.request.lastCall.args[0]
- const expectedUrl = `v1.url/api/v2/users/${this.stubbedUser._id}/affiliations`
- requestOptions.url.should.equal(expectedUrl)
- requestOptions.method.should.equal('DELETE')
- done()
- })
- })
- it('handle error', function (done) {
- const body = { errors: 'affiliation error message' }
- this.request.callsArgWith(1, null, { statusCode: 518 }, body)
- this.InstitutionsAPI.deleteAffiliations(this.stubbedUser._id, err => {
- expect(err).to.be.instanceof(Errors.V1ConnectionError)
- done()
- })
- })
- })
- describe('endorseAffiliation', function () {
- beforeEach(function () {
- this.request.callsArgWith(1, null, { statusCode: 204 })
- })
- it('endorse affiliation', function (done) {
- this.InstitutionsAPI.endorseAffiliation(
- this.stubbedUser._id,
- this.newEmail,
- 'Student',
- 'Physics',
- err => {
- expect(err).not.to.exist
- this.request.calledOnce.should.equal(true)
- const requestOptions = this.request.lastCall.args[0]
- const expectedUrl = `v1.url/api/v2/users/${this.stubbedUser._id}/affiliations/endorse`
- requestOptions.url.should.equal(expectedUrl)
- requestOptions.method.should.equal('POST')
- const { body } = requestOptions
- Object.keys(body).length.should.equal(3)
- body.email.should.equal(this.newEmail)
- body.role.should.equal('Student')
- body.department.should.equal('Physics')
- done()
- }
- )
- })
- })
- describe('sendUsersWithReconfirmationsLapsedProcessed', function () {
- const users = ['abc123', 'def456']
- it('sends the list of users', function (done) {
- this.request.callsArgWith(1, null, { statusCode: 200 })
- this.InstitutionsAPI.sendUsersWithReconfirmationsLapsedProcessed(
- users,
- error => {
- expect(error).not.to.exist
- this.request.calledOnce.should.equal(true)
- const requestOptions = this.request.lastCall.args[0]
- const expectedUrl =
- 'v1.url/api/v2/institutions/reconfirmation_lapsed_processed'
- requestOptions.url.should.equal(expectedUrl)
- requestOptions.method.should.equal('POST')
- expect(requestOptions.body).to.deep.equal({ users })
- done()
- }
- )
- })
- it('handle error', function (done) {
- this.request.callsArgWith(1, null, { statusCode: 500 })
- this.InstitutionsAPI.sendUsersWithReconfirmationsLapsedProcessed(
- users,
- error => {
- expect(error).to.exist
- done()
- }
- )
- })
- })
- })
|