InstitutionsAPITests.coffee 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. should = require('chai').should()
  2. expect = require('chai').expect
  3. SandboxedModule = require('sandboxed-module')
  4. assert = require('assert')
  5. path = require('path')
  6. sinon = require('sinon')
  7. modulePath = path.join __dirname, "../../../../app/js/Features/Institutions/InstitutionsAPI"
  8. expect = require("chai").expect
  9. describe "InstitutionsAPI", ->
  10. beforeEach ->
  11. @logger = err: sinon.stub(), log: ->
  12. @settings = apis: { v1: { url: 'v1.url', user: '', pass: '' } }
  13. @request = sinon.stub()
  14. @InstitutionsAPI = SandboxedModule.require modulePath, requires:
  15. "logger-sharelatex": @logger
  16. "metrics-sharelatex": timeAsyncMethod: sinon.stub()
  17. 'settings-sharelatex': @settings
  18. 'request': @request
  19. @stubbedUser =
  20. _id: "3131231"
  21. name:"bob"
  22. email:"hello@world.com"
  23. @newEmail = "bob@bob.com"
  24. describe 'getInstitutionAffiliations', ->
  25. it 'get affiliations', (done)->
  26. @institutionId = 123
  27. responseBody = ['123abc', '456def']
  28. @request.yields(null, { statusCode: 200 }, responseBody)
  29. @InstitutionsAPI.getInstitutionAffiliations @institutionId, (err, body) =>
  30. should.not.exist(err)
  31. @request.calledOnce.should.equal true
  32. requestOptions = @request.lastCall.args[0]
  33. expectedUrl = "v1.url/api/v2/institutions/#{@institutionId}/affiliations"
  34. requestOptions.url.should.equal expectedUrl
  35. requestOptions.method.should.equal 'GET'
  36. should.not.exist(requestOptions.body)
  37. body.should.equal responseBody
  38. done()
  39. it 'handle empty response', (done)->
  40. @settings.apis = null
  41. @InstitutionsAPI.getInstitutionAffiliations @institutionId, (err, body) =>
  42. should.not.exist(err)
  43. expect(body).to.be.a 'Array'
  44. body.length.should.equal 0
  45. done()
  46. describe 'getInstitutionLicences', ->
  47. it 'get licences', (done)->
  48. @institutionId = 123
  49. responseBody = {"lag":"monthly","data":[{"key":"users","values":[{"x":"2018-01-01","y":1}]}]}
  50. @request.yields(null, { statusCode: 200 }, responseBody)
  51. startDate = '1417392000'
  52. endDate = '1420848000'
  53. @InstitutionsAPI.getInstitutionLicences @institutionId, startDate, endDate, 'monthly', (err, body) =>
  54. should.not.exist(err)
  55. @request.calledOnce.should.equal true
  56. requestOptions = @request.lastCall.args[0]
  57. expectedUrl = "v1.url/api/v2/institutions/#{@institutionId}/institution_licences"
  58. requestOptions.url.should.equal expectedUrl
  59. requestOptions.method.should.equal 'GET'
  60. requestOptions.body['start_date'].should.equal startDate
  61. requestOptions.body['end_date'].should.equal endDate
  62. requestOptions.body.lag.should.equal 'monthly'
  63. body.should.equal responseBody
  64. done()
  65. describe 'getUserAffiliations', ->
  66. it 'get affiliations', (done)->
  67. responseBody = [{ foo: 'bar' }]
  68. @request.callsArgWith(1, null, { statusCode: 201 }, responseBody)
  69. @InstitutionsAPI.getUserAffiliations @stubbedUser._id, (err, body) =>
  70. should.not.exist(err)
  71. @request.calledOnce.should.equal true
  72. requestOptions = @request.lastCall.args[0]
  73. expectedUrl = "v1.url/api/v2/users/#{@stubbedUser._id}/affiliations"
  74. requestOptions.url.should.equal expectedUrl
  75. requestOptions.method.should.equal 'GET'
  76. should.not.exist(requestOptions.body)
  77. body.should.equal responseBody
  78. done()
  79. it 'handle error', (done)->
  80. body = errors: 'affiliation error message'
  81. @request.callsArgWith(1, null, { statusCode: 503 }, body)
  82. @InstitutionsAPI.getUserAffiliations @stubbedUser._id, (err) =>
  83. should.exist(err)
  84. err.message.should.have.string 503
  85. err.message.should.have.string body.errors
  86. done()
  87. it 'handle empty response', (done)->
  88. @settings.apis = null
  89. @InstitutionsAPI.getUserAffiliations @stubbedUser._id, (err, body) =>
  90. should.not.exist(err)
  91. expect(body).to.be.a 'Array'
  92. body.length.should.equal 0
  93. done()
  94. describe 'addAffiliation', ->
  95. beforeEach ->
  96. @request.callsArgWith(1, null, { statusCode: 201 })
  97. it 'add affiliation', (done)->
  98. affiliationOptions =
  99. university: { id: 1 }
  100. role: 'Prof'
  101. department: 'Math'
  102. confirmedAt: new Date()
  103. @InstitutionsAPI.addAffiliation @stubbedUser._id, @newEmail, affiliationOptions, (err)=>
  104. should.not.exist(err)
  105. @request.calledOnce.should.equal true
  106. requestOptions = @request.lastCall.args[0]
  107. expectedUrl = "v1.url/api/v2/users/#{@stubbedUser._id}/affiliations"
  108. requestOptions.url.should.equal expectedUrl
  109. requestOptions.method.should.equal 'POST'
  110. body = requestOptions.body
  111. Object.keys(body).length.should.equal 5
  112. body.email.should.equal @newEmail
  113. body.university.should.equal affiliationOptions.university
  114. body.department.should.equal affiliationOptions.department
  115. body.role.should.equal affiliationOptions.role
  116. body.confirmedAt.should.equal affiliationOptions.confirmedAt
  117. done()
  118. it 'handle error', (done)->
  119. body = errors: 'affiliation error message'
  120. @request.callsArgWith(1, null, { statusCode: 422 }, body)
  121. @InstitutionsAPI.addAffiliation @stubbedUser._id, @newEmail, {}, (err)=>
  122. should.exist(err)
  123. err.message.should.have.string 422
  124. err.message.should.have.string body.errors
  125. done()
  126. describe 'removeAffiliation', ->
  127. beforeEach ->
  128. @request.callsArgWith(1, null, { statusCode: 404 })
  129. it 'remove affiliation', (done)->
  130. @InstitutionsAPI.removeAffiliation @stubbedUser._id, @newEmail, (err)=>
  131. should.not.exist(err)
  132. @request.calledOnce.should.equal true
  133. requestOptions = @request.lastCall.args[0]
  134. expectedUrl = "v1.url/api/v2/users/#{@stubbedUser._id}/affiliations/remove"
  135. requestOptions.url.should.equal expectedUrl
  136. requestOptions.method.should.equal 'POST'
  137. expect(requestOptions.body).to.deep.equal { email: @newEmail }
  138. done()
  139. it 'handle error', (done)->
  140. @request.callsArgWith(1, null, { statusCode: 500 })
  141. @InstitutionsAPI.removeAffiliation @stubbedUser._id, @newEmail, (err)=>
  142. should.exist(err)
  143. err.message.should.exist
  144. done()
  145. describe 'deleteAffiliations', ->
  146. it 'delete affiliations', (done)->
  147. @request.callsArgWith(1, null, { statusCode: 200 })
  148. @InstitutionsAPI.deleteAffiliations @stubbedUser._id, (err) =>
  149. should.not.exist(err)
  150. @request.calledOnce.should.equal true
  151. requestOptions = @request.lastCall.args[0]
  152. expectedUrl = "v1.url/api/v2/users/#{@stubbedUser._id}/affiliations"
  153. requestOptions.url.should.equal expectedUrl
  154. requestOptions.method.should.equal 'DELETE'
  155. done()
  156. it 'handle error', (done)->
  157. body = errors: 'affiliation error message'
  158. @request.callsArgWith(1, null, { statusCode: 518 }, body)
  159. @InstitutionsAPI.deleteAffiliations @stubbedUser._id, (err) =>
  160. should.exist(err)
  161. err.message.should.have.string 518
  162. err.message.should.have.string body.errors
  163. done()
  164. describe 'endorseAffiliation', ->
  165. beforeEach ->
  166. @request.callsArgWith(1, null, { statusCode: 204 })
  167. it 'endorse affiliation', (done)->
  168. @InstitutionsAPI.endorseAffiliation @stubbedUser._id, @newEmail, 'Student','Physics', (err)=>
  169. should.not.exist(err)
  170. @request.calledOnce.should.equal true
  171. requestOptions = @request.lastCall.args[0]
  172. expectedUrl = "v1.url/api/v2/users/#{@stubbedUser._id}/affiliations/endorse"
  173. requestOptions.url.should.equal expectedUrl
  174. requestOptions.method.should.equal 'POST'
  175. body = requestOptions.body
  176. Object.keys(body).length.should.equal 3
  177. body.email.should.equal @newEmail
  178. body.role.should.equal 'Student'
  179. body.department.should.equal 'Physics'
  180. done()