2018-06-27 12:29:56 -04:00
|
|
|
should = require('chai').should()
|
2018-06-27 03:33:06 -04:00
|
|
|
expect = require('chai').expect
|
2018-06-27 12:29:56 -04:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
assert = require('assert')
|
|
|
|
path = require('path')
|
|
|
|
sinon = require('sinon')
|
2018-07-10 15:49:24 -04:00
|
|
|
modulePath = path.join __dirname, "../../../../app/js/Features/Institutions/InstitutionsAPI"
|
2018-06-27 12:29:56 -04:00
|
|
|
expect = require("chai").expect
|
|
|
|
|
2018-07-10 15:49:24 -04:00
|
|
|
describe "InstitutionsAPI", ->
|
2018-06-27 12:29:56 -04:00
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@logger = err: sinon.stub(), log: ->
|
2018-08-20 10:29:44 -04:00
|
|
|
@settings = apis: { v1: { url: 'v1.url', user: '', pass: '' } }
|
2018-06-27 12:29:56 -04:00
|
|
|
@request = sinon.stub()
|
2018-07-10 15:49:24 -04:00
|
|
|
@InstitutionsAPI = SandboxedModule.require modulePath, requires:
|
2018-06-27 12:29:56 -04:00
|
|
|
"logger-sharelatex": @logger
|
|
|
|
"metrics-sharelatex": timeAsyncMethod: sinon.stub()
|
2018-08-20 10:29:44 -04:00
|
|
|
'settings-sharelatex': @settings
|
2018-06-27 12:29:56 -04:00
|
|
|
'request': @request
|
|
|
|
|
|
|
|
@stubbedUser =
|
|
|
|
_id: "3131231"
|
|
|
|
name:"bob"
|
|
|
|
email:"hello@world.com"
|
|
|
|
@newEmail = "bob@bob.com"
|
|
|
|
|
2018-07-10 17:53:06 -04:00
|
|
|
describe 'getInstitutionAffiliations', ->
|
|
|
|
it 'get affiliations', (done)->
|
|
|
|
@institutionId = 123
|
|
|
|
responseBody = ['123abc', '456def']
|
|
|
|
@request.yields(null, { statusCode: 200 }, responseBody)
|
|
|
|
@InstitutionsAPI.getInstitutionAffiliations @institutionId, (err, body) =>
|
|
|
|
should.not.exist(err)
|
|
|
|
@request.calledOnce.should.equal true
|
|
|
|
requestOptions = @request.lastCall.args[0]
|
|
|
|
expectedUrl = "v1.url/api/v2/institutions/#{@institutionId}/affiliations"
|
|
|
|
requestOptions.url.should.equal expectedUrl
|
|
|
|
requestOptions.method.should.equal 'GET'
|
|
|
|
should.not.exist(requestOptions.body)
|
|
|
|
body.should.equal responseBody
|
|
|
|
done()
|
2018-08-23 11:08:10 -04:00
|
|
|
|
2018-08-20 10:29:44 -04:00
|
|
|
it 'handle empty response', (done)->
|
|
|
|
@settings.apis = null
|
|
|
|
@InstitutionsAPI.getInstitutionAffiliations @institutionId, (err, body) =>
|
|
|
|
should.not.exist(err)
|
|
|
|
expect(body).to.be.a 'Array'
|
|
|
|
body.length.should.equal 0
|
|
|
|
done()
|
2018-07-10 17:53:06 -04:00
|
|
|
|
2018-08-22 13:31:29 -04:00
|
|
|
describe 'getInstitutionLicences', ->
|
|
|
|
it 'get licences', (done)->
|
|
|
|
@institutionId = 123
|
|
|
|
responseBody = {"lag":"monthly","data":[{"key":"users","values":[{"x":"2018-01-01","y":1}]}]}
|
|
|
|
@request.yields(null, { statusCode: 200 }, responseBody)
|
|
|
|
startDate = '1417392000'
|
|
|
|
endDate = '1420848000'
|
|
|
|
@InstitutionsAPI.getInstitutionLicences @institutionId, startDate, endDate, 'monthly', (err, body) =>
|
|
|
|
should.not.exist(err)
|
|
|
|
@request.calledOnce.should.equal true
|
|
|
|
requestOptions = @request.lastCall.args[0]
|
|
|
|
expectedUrl = "v1.url/api/v2/institutions/#{@institutionId}/institution_licences"
|
|
|
|
requestOptions.url.should.equal expectedUrl
|
|
|
|
requestOptions.method.should.equal 'GET'
|
|
|
|
requestOptions.body['start_date'].should.equal startDate
|
|
|
|
requestOptions.body['end_date'].should.equal endDate
|
|
|
|
requestOptions.body.lag.should.equal 'monthly'
|
|
|
|
body.should.equal responseBody
|
|
|
|
done()
|
|
|
|
|
2018-07-10 15:49:24 -04:00
|
|
|
describe 'getUserAffiliations', ->
|
2018-06-27 12:29:56 -04:00
|
|
|
it 'get affiliations', (done)->
|
|
|
|
responseBody = [{ foo: 'bar' }]
|
|
|
|
@request.callsArgWith(1, null, { statusCode: 201 }, responseBody)
|
2018-07-10 15:49:24 -04:00
|
|
|
@InstitutionsAPI.getUserAffiliations @stubbedUser._id, (err, body) =>
|
2018-06-27 12:29:56 -04:00
|
|
|
should.not.exist(err)
|
|
|
|
@request.calledOnce.should.equal true
|
|
|
|
requestOptions = @request.lastCall.args[0]
|
|
|
|
expectedUrl = "v1.url/api/v2/users/#{@stubbedUser._id}/affiliations"
|
|
|
|
requestOptions.url.should.equal expectedUrl
|
|
|
|
requestOptions.method.should.equal 'GET'
|
|
|
|
should.not.exist(requestOptions.body)
|
|
|
|
body.should.equal responseBody
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'handle error', (done)->
|
|
|
|
body = errors: 'affiliation error message'
|
|
|
|
@request.callsArgWith(1, null, { statusCode: 503 }, body)
|
2018-07-10 15:49:24 -04:00
|
|
|
@InstitutionsAPI.getUserAffiliations @stubbedUser._id, (err) =>
|
2018-06-27 12:29:56 -04:00
|
|
|
should.exist(err)
|
|
|
|
err.message.should.have.string 503
|
|
|
|
err.message.should.have.string body.errors
|
|
|
|
done()
|
|
|
|
|
2018-08-20 10:29:44 -04:00
|
|
|
it 'handle empty response', (done)->
|
|
|
|
@settings.apis = null
|
|
|
|
@InstitutionsAPI.getUserAffiliations @stubbedUser._id, (err, body) =>
|
|
|
|
should.not.exist(err)
|
|
|
|
expect(body).to.be.a 'Array'
|
|
|
|
body.length.should.equal 0
|
|
|
|
done()
|
|
|
|
|
2018-06-27 12:29:56 -04:00
|
|
|
describe 'addAffiliation', ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.callsArgWith(1, null, { statusCode: 201 })
|
|
|
|
|
|
|
|
it 'add affiliation', (done)->
|
|
|
|
affiliationOptions =
|
|
|
|
university: { id: 1 }
|
|
|
|
role: 'Prof'
|
|
|
|
department: 'Math'
|
2018-08-09 03:20:34 -04:00
|
|
|
confirmedAt: new Date()
|
2018-07-10 15:49:24 -04:00
|
|
|
@InstitutionsAPI.addAffiliation @stubbedUser._id, @newEmail, affiliationOptions, (err)=>
|
2018-06-27 12:29:56 -04:00
|
|
|
should.not.exist(err)
|
|
|
|
@request.calledOnce.should.equal true
|
|
|
|
requestOptions = @request.lastCall.args[0]
|
|
|
|
expectedUrl = "v1.url/api/v2/users/#{@stubbedUser._id}/affiliations"
|
|
|
|
requestOptions.url.should.equal expectedUrl
|
|
|
|
requestOptions.method.should.equal 'POST'
|
|
|
|
|
|
|
|
body = requestOptions.body
|
2018-08-02 11:19:23 -04:00
|
|
|
Object.keys(body).length.should.equal 5
|
2018-06-27 12:29:56 -04:00
|
|
|
body.email.should.equal @newEmail
|
|
|
|
body.university.should.equal affiliationOptions.university
|
|
|
|
body.department.should.equal affiliationOptions.department
|
|
|
|
body.role.should.equal affiliationOptions.role
|
2018-08-09 03:20:34 -04:00
|
|
|
body.confirmedAt.should.equal affiliationOptions.confirmedAt
|
2018-06-27 12:29:56 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'handle error', (done)->
|
|
|
|
body = errors: 'affiliation error message'
|
|
|
|
@request.callsArgWith(1, null, { statusCode: 422 }, body)
|
2018-07-10 15:49:24 -04:00
|
|
|
@InstitutionsAPI.addAffiliation @stubbedUser._id, @newEmail, {}, (err)=>
|
2018-06-27 12:29:56 -04:00
|
|
|
should.exist(err)
|
|
|
|
err.message.should.have.string 422
|
|
|
|
err.message.should.have.string body.errors
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'removeAffiliation', ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.callsArgWith(1, null, { statusCode: 404 })
|
|
|
|
|
|
|
|
it 'remove affiliation', (done)->
|
2018-07-10 15:49:24 -04:00
|
|
|
@InstitutionsAPI.removeAffiliation @stubbedUser._id, @newEmail, (err)=>
|
2018-06-27 12:29:56 -04:00
|
|
|
should.not.exist(err)
|
|
|
|
@request.calledOnce.should.equal true
|
|
|
|
requestOptions = @request.lastCall.args[0]
|
2018-06-27 03:33:06 -04:00
|
|
|
expectedUrl = "v1.url/api/v2/users/#{@stubbedUser._id}/affiliations/remove"
|
2018-06-27 12:29:56 -04:00
|
|
|
requestOptions.url.should.equal expectedUrl
|
2018-06-27 03:33:06 -04:00
|
|
|
requestOptions.method.should.equal 'POST'
|
|
|
|
expect(requestOptions.body).to.deep.equal { email: @newEmail }
|
2018-06-27 12:29:56 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'handle error', (done)->
|
|
|
|
@request.callsArgWith(1, null, { statusCode: 500 })
|
2018-07-10 15:49:24 -04:00
|
|
|
@InstitutionsAPI.removeAffiliation @stubbedUser._id, @newEmail, (err)=>
|
2018-06-27 12:29:56 -04:00
|
|
|
should.exist(err)
|
|
|
|
err.message.should.exist
|
|
|
|
done()
|
2018-06-27 12:43:20 -04:00
|
|
|
|
|
|
|
describe 'deleteAffiliations', ->
|
|
|
|
it 'delete affiliations', (done)->
|
|
|
|
@request.callsArgWith(1, null, { statusCode: 200 })
|
2018-07-10 15:49:24 -04:00
|
|
|
@InstitutionsAPI.deleteAffiliations @stubbedUser._id, (err) =>
|
2018-06-27 12:43:20 -04:00
|
|
|
should.not.exist(err)
|
|
|
|
@request.calledOnce.should.equal true
|
|
|
|
requestOptions = @request.lastCall.args[0]
|
|
|
|
expectedUrl = "v1.url/api/v2/users/#{@stubbedUser._id}/affiliations"
|
|
|
|
requestOptions.url.should.equal expectedUrl
|
|
|
|
requestOptions.method.should.equal 'DELETE'
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'handle error', (done)->
|
|
|
|
body = errors: 'affiliation error message'
|
|
|
|
@request.callsArgWith(1, null, { statusCode: 518 }, body)
|
2018-07-10 15:49:24 -04:00
|
|
|
@InstitutionsAPI.deleteAffiliations @stubbedUser._id, (err) =>
|
2018-06-27 12:43:20 -04:00
|
|
|
should.exist(err)
|
|
|
|
err.message.should.have.string 518
|
|
|
|
err.message.should.have.string body.errors
|
|
|
|
done()
|
2018-07-05 04:46:06 -04:00
|
|
|
|
|
|
|
describe 'endorseAffiliation', ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.callsArgWith(1, null, { statusCode: 204 })
|
|
|
|
|
|
|
|
it 'endorse affiliation', (done)->
|
2018-07-10 15:49:24 -04:00
|
|
|
@InstitutionsAPI.endorseAffiliation @stubbedUser._id, @newEmail, 'Student','Physics', (err)=>
|
2018-07-05 04:46:06 -04:00
|
|
|
should.not.exist(err)
|
|
|
|
@request.calledOnce.should.equal true
|
|
|
|
requestOptions = @request.lastCall.args[0]
|
|
|
|
expectedUrl = "v1.url/api/v2/users/#{@stubbedUser._id}/affiliations/endorse"
|
|
|
|
requestOptions.url.should.equal expectedUrl
|
|
|
|
requestOptions.method.should.equal 'POST'
|
|
|
|
|
|
|
|
body = requestOptions.body
|
|
|
|
Object.keys(body).length.should.equal 3
|
|
|
|
body.email.should.equal @newEmail
|
|
|
|
body.role.should.equal 'Student'
|
|
|
|
body.department.should.equal 'Physics'
|
|
|
|
done()
|