InstitutionsAPITests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. const { expect } = require('chai')
  2. const SandboxedModule = require('sandboxed-module')
  3. const path = require('path')
  4. const sinon = require('sinon')
  5. const modulePath = path.join(
  6. __dirname,
  7. '../../../../app/src/Features/Institutions/InstitutionsAPI'
  8. )
  9. const Errors = require('../../../../app/src/Features/Errors/Errors')
  10. describe('InstitutionsAPI', function () {
  11. beforeEach(function () {
  12. this.settings = { apis: { v1: { url: 'v1.url', user: '', pass: '' } } }
  13. this.request = sinon.stub()
  14. this.ipMatcherNotification = {
  15. read: (this.markAsReadIpMatcher = sinon.stub().callsArgWith(1, null)),
  16. }
  17. this.InstitutionsAPI = SandboxedModule.require(modulePath, {
  18. requires: {
  19. '@overleaf/metrics': {
  20. timeAsyncMethod: sinon.stub(),
  21. },
  22. '@overleaf/settings': this.settings,
  23. requestretry: this.request,
  24. '../Notifications/NotificationsBuilder': {
  25. ipMatcherAffiliation: sinon
  26. .stub()
  27. .returns(this.ipMatcherNotification),
  28. },
  29. },
  30. })
  31. this.stubbedUser = {
  32. _id: '3131231',
  33. name: 'bob',
  34. email: 'hello@world.com',
  35. }
  36. this.newEmail = 'bob@bob.com'
  37. })
  38. describe('getInstitutionAffiliations', function () {
  39. it('get affiliations', function (done) {
  40. this.institutionId = 123
  41. const responseBody = ['123abc', '456def']
  42. this.request.yields(null, { statusCode: 200 }, responseBody)
  43. this.InstitutionsAPI.getInstitutionAffiliations(
  44. this.institutionId,
  45. (err, body) => {
  46. expect(err).not.to.exist
  47. this.request.calledOnce.should.equal(true)
  48. const requestOptions = this.request.lastCall.args[0]
  49. const expectedUrl = `v1.url/api/v2/institutions/${this.institutionId}/affiliations`
  50. requestOptions.url.should.equal(expectedUrl)
  51. requestOptions.method.should.equal('GET')
  52. requestOptions.maxAttempts.should.exist
  53. requestOptions.maxAttempts.should.not.equal(0)
  54. requestOptions.retryDelay.should.exist
  55. expect(requestOptions.body).not.to.exist
  56. body.should.equal(responseBody)
  57. done()
  58. }
  59. )
  60. })
  61. it('handle empty response', function (done) {
  62. this.settings.apis.v1.url = ''
  63. this.InstitutionsAPI.getInstitutionAffiliations(
  64. this.institutionId,
  65. (err, body) => {
  66. expect(err).not.to.exist
  67. expect(body).to.be.a('Array')
  68. body.length.should.equal(0)
  69. done()
  70. }
  71. )
  72. })
  73. })
  74. describe('getLicencesForAnalytics', function () {
  75. const lag = 'daily'
  76. const queryDate = '2017-01-07:00:00.000Z'
  77. it('should send the request to v1', function (done) {
  78. const v1Result = {
  79. lag: 'daily',
  80. date: queryDate,
  81. data: {
  82. user_counts: { total: [], new: [] },
  83. max_confirmation_months: [],
  84. },
  85. }
  86. this.request.callsArgWith(1, null, { statusCode: 201 }, v1Result)
  87. this.InstitutionsAPI.getLicencesForAnalytics(
  88. lag,
  89. queryDate,
  90. (error, result) => {
  91. expect(error).not.to.exist
  92. const requestOptions = this.request.lastCall.args[0]
  93. expect(requestOptions.body.query_date).to.equal(queryDate)
  94. expect(requestOptions.body.lag).to.equal(lag)
  95. requestOptions.method.should.equal('GET')
  96. done()
  97. }
  98. )
  99. })
  100. it('should handle errors', function (done) {
  101. this.request.callsArgWith(1, null, { statusCode: 500 })
  102. this.InstitutionsAPI.getLicencesForAnalytics(
  103. lag,
  104. queryDate,
  105. (error, result) => {
  106. expect(error).to.be.instanceof(Errors.V1ConnectionError)
  107. done()
  108. }
  109. )
  110. })
  111. })
  112. describe('getUserAffiliations', function () {
  113. it('get affiliations', function (done) {
  114. const responseBody = [{ foo: 'bar' }]
  115. this.request.callsArgWith(1, null, { statusCode: 201 }, responseBody)
  116. this.InstitutionsAPI.getUserAffiliations(
  117. this.stubbedUser._id,
  118. (err, body) => {
  119. expect(err).not.to.exist
  120. this.request.calledOnce.should.equal(true)
  121. const requestOptions = this.request.lastCall.args[0]
  122. const expectedUrl = `v1.url/api/v2/users/${this.stubbedUser._id}/affiliations`
  123. requestOptions.url.should.equal(expectedUrl)
  124. requestOptions.method.should.equal('GET')
  125. requestOptions.maxAttempts.should.equal(3)
  126. expect(requestOptions.body).not.to.exist
  127. body.should.equal(responseBody)
  128. done()
  129. }
  130. )
  131. })
  132. it('handle error', function (done) {
  133. const body = { errors: 'affiliation error message' }
  134. this.request.callsArgWith(1, null, { statusCode: 503 }, body)
  135. this.InstitutionsAPI.getUserAffiliations(this.stubbedUser._id, err => {
  136. expect(err).to.be.instanceof(Errors.V1ConnectionError)
  137. done()
  138. })
  139. })
  140. it('handle empty response', function (done) {
  141. this.settings.apis.v1.url = ''
  142. this.InstitutionsAPI.getUserAffiliations(
  143. this.stubbedUser._id,
  144. (err, body) => {
  145. expect(err).not.to.exist
  146. expect(body).to.be.a('Array')
  147. body.length.should.equal(0)
  148. done()
  149. }
  150. )
  151. })
  152. })
  153. describe('getUsersNeedingReconfirmationsLapsedProcessed', function () {
  154. it('get the list of users', function (done) {
  155. this.request.callsArgWith(1, null, { statusCode: 200 })
  156. this.InstitutionsAPI.getUsersNeedingReconfirmationsLapsedProcessed(
  157. error => {
  158. expect(error).not.to.exist
  159. this.request.calledOnce.should.equal(true)
  160. const requestOptions = this.request.lastCall.args[0]
  161. const expectedUrl = `v1.url/api/v2/institutions/need_reconfirmation_lapsed_processed`
  162. requestOptions.url.should.equal(expectedUrl)
  163. requestOptions.method.should.equal('GET')
  164. done()
  165. }
  166. )
  167. })
  168. it('handle error', function (done) {
  169. this.request.callsArgWith(1, null, { statusCode: 500 })
  170. this.InstitutionsAPI.getUsersNeedingReconfirmationsLapsedProcessed(
  171. error => {
  172. expect(error).to.exist
  173. done()
  174. }
  175. )
  176. })
  177. })
  178. describe('addAffiliation', function () {
  179. beforeEach(function () {
  180. this.request.callsArgWith(1, null, { statusCode: 201 })
  181. })
  182. it('add affiliation', function (done) {
  183. const affiliationOptions = {
  184. university: { id: 1 },
  185. role: 'Prof',
  186. department: 'Math',
  187. confirmedAt: new Date(),
  188. entitlement: true,
  189. }
  190. this.InstitutionsAPI.addAffiliation(
  191. this.stubbedUser._id,
  192. this.newEmail,
  193. affiliationOptions,
  194. err => {
  195. expect(err).not.to.exist
  196. this.request.calledOnce.should.equal(true)
  197. const requestOptions = this.request.lastCall.args[0]
  198. const expectedUrl = `v1.url/api/v2/users/${this.stubbedUser._id}/affiliations`
  199. requestOptions.url.should.equal(expectedUrl)
  200. requestOptions.method.should.equal('POST')
  201. requestOptions.maxAttempts.should.equal(0)
  202. const { body } = requestOptions
  203. Object.keys(body).length.should.equal(7)
  204. body.email.should.equal(this.newEmail)
  205. body.university.should.equal(affiliationOptions.university)
  206. body.department.should.equal(affiliationOptions.department)
  207. body.role.should.equal(affiliationOptions.role)
  208. body.confirmedAt.should.equal(affiliationOptions.confirmedAt)
  209. body.entitlement.should.equal(affiliationOptions.entitlement)
  210. this.markAsReadIpMatcher.calledOnce.should.equal(true)
  211. done()
  212. }
  213. )
  214. })
  215. it('handle error', function (done) {
  216. const body = { errors: 'affiliation error message' }
  217. this.request.callsArgWith(1, null, { statusCode: 422 }, body)
  218. this.InstitutionsAPI.addAffiliation(
  219. this.stubbedUser._id,
  220. this.newEmail,
  221. {},
  222. err => {
  223. expect(err).to.exist
  224. err.message.should.have.string(422)
  225. err.message.should.have.string(body.errors)
  226. done()
  227. }
  228. )
  229. })
  230. })
  231. describe('removeAffiliation', function () {
  232. beforeEach(function () {
  233. this.request.callsArgWith(1, null, { statusCode: 404 })
  234. })
  235. it('remove affiliation', function (done) {
  236. this.InstitutionsAPI.removeAffiliation(
  237. this.stubbedUser._id,
  238. this.newEmail,
  239. err => {
  240. expect(err).not.to.exist
  241. this.request.calledOnce.should.equal(true)
  242. const requestOptions = this.request.lastCall.args[0]
  243. const expectedUrl = `v1.url/api/v2/users/${this.stubbedUser._id}/affiliations/remove`
  244. requestOptions.url.should.equal(expectedUrl)
  245. requestOptions.method.should.equal('POST')
  246. expect(requestOptions.body).to.deep.equal({ email: this.newEmail })
  247. done()
  248. }
  249. )
  250. })
  251. it('handle error', function (done) {
  252. this.request.callsArgWith(1, null, { statusCode: 500 })
  253. this.InstitutionsAPI.removeAffiliation(
  254. this.stubbedUser._id,
  255. this.newEmail,
  256. err => {
  257. expect(err).to.exist
  258. err.message.should.exist
  259. done()
  260. }
  261. )
  262. })
  263. })
  264. describe('deleteAffiliations', function () {
  265. it('delete affiliations', function (done) {
  266. this.request.callsArgWith(1, null, { statusCode: 200 })
  267. this.InstitutionsAPI.deleteAffiliations(this.stubbedUser._id, err => {
  268. expect(err).not.to.exist
  269. this.request.calledOnce.should.equal(true)
  270. const requestOptions = this.request.lastCall.args[0]
  271. const expectedUrl = `v1.url/api/v2/users/${this.stubbedUser._id}/affiliations`
  272. requestOptions.url.should.equal(expectedUrl)
  273. requestOptions.method.should.equal('DELETE')
  274. done()
  275. })
  276. })
  277. it('handle error', function (done) {
  278. const body = { errors: 'affiliation error message' }
  279. this.request.callsArgWith(1, null, { statusCode: 518 }, body)
  280. this.InstitutionsAPI.deleteAffiliations(this.stubbedUser._id, err => {
  281. expect(err).to.be.instanceof(Errors.V1ConnectionError)
  282. done()
  283. })
  284. })
  285. })
  286. describe('endorseAffiliation', function () {
  287. beforeEach(function () {
  288. this.request.callsArgWith(1, null, { statusCode: 204 })
  289. })
  290. it('endorse affiliation', function (done) {
  291. this.InstitutionsAPI.endorseAffiliation(
  292. this.stubbedUser._id,
  293. this.newEmail,
  294. 'Student',
  295. 'Physics',
  296. err => {
  297. expect(err).not.to.exist
  298. this.request.calledOnce.should.equal(true)
  299. const requestOptions = this.request.lastCall.args[0]
  300. const expectedUrl = `v1.url/api/v2/users/${this.stubbedUser._id}/affiliations/endorse`
  301. requestOptions.url.should.equal(expectedUrl)
  302. requestOptions.method.should.equal('POST')
  303. const { body } = requestOptions
  304. Object.keys(body).length.should.equal(3)
  305. body.email.should.equal(this.newEmail)
  306. body.role.should.equal('Student')
  307. body.department.should.equal('Physics')
  308. done()
  309. }
  310. )
  311. })
  312. })
  313. describe('sendUsersWithReconfirmationsLapsedProcessed', function () {
  314. const users = ['abc123', 'def456']
  315. it('sends the list of users', function (done) {
  316. this.request.callsArgWith(1, null, { statusCode: 200 })
  317. this.InstitutionsAPI.sendUsersWithReconfirmationsLapsedProcessed(
  318. users,
  319. error => {
  320. expect(error).not.to.exist
  321. this.request.calledOnce.should.equal(true)
  322. const requestOptions = this.request.lastCall.args[0]
  323. const expectedUrl =
  324. 'v1.url/api/v2/institutions/reconfirmation_lapsed_processed'
  325. requestOptions.url.should.equal(expectedUrl)
  326. requestOptions.method.should.equal('POST')
  327. expect(requestOptions.body).to.deep.equal({ users })
  328. done()
  329. }
  330. )
  331. })
  332. it('handle error', function (done) {
  333. this.request.callsArgWith(1, null, { statusCode: 500 })
  334. this.InstitutionsAPI.sendUsersWithReconfirmationsLapsedProcessed(
  335. users,
  336. error => {
  337. expect(error).to.exist
  338. done()
  339. }
  340. )
  341. })
  342. })
  343. })