AnalyticsControllerTests.coffee 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. modulePath = path.join __dirname, '../../../../app/js/Features/Analytics/AnalyticsController'
  6. sinon = require("sinon")
  7. expect = require("chai").expect
  8. describe 'AnalyticsController', ->
  9. beforeEach ->
  10. @AuthenticationController =
  11. getLoggedInUserId: sinon.stub()
  12. @AnalyticsManager =
  13. updateEditingSession: sinon.stub().callsArgWith(3)
  14. recordEvent: sinon.stub().callsArgWith(3)
  15. @InstitutionsAPI =
  16. getInstitutionLicences: sinon.stub().callsArgWith(4)
  17. @controller = SandboxedModule.require modulePath, requires:
  18. "./AnalyticsManager":@AnalyticsManager
  19. "../Authentication/AuthenticationController":@AuthenticationController
  20. "../Institutions/InstitutionsAPI":@InstitutionsAPI
  21. "logger-sharelatex":
  22. log:->
  23. '../../infrastructure/GeoIpLookup': @GeoIpLookup =
  24. getDetails: sinon.stub()
  25. @res =
  26. send:->
  27. describe "updateEditingSession", ->
  28. beforeEach ->
  29. @req =
  30. params:
  31. projectId: "a project id"
  32. @GeoIpLookup.getDetails = sinon.stub()
  33. .callsArgWith(1, null, {country_code: 'XY'})
  34. it "delegates to the AnalyticsManager", (done) ->
  35. @AuthenticationController.getLoggedInUserId.returns("1234")
  36. @controller.updateEditingSession @req, @res
  37. @AnalyticsManager.updateEditingSession.calledWith(
  38. "1234",
  39. "a project id",
  40. 'XY'
  41. ).should.equal true
  42. done()
  43. describe "recordEvent", ->
  44. beforeEach ->
  45. @req =
  46. params:
  47. event:"i_did_something"
  48. body:"stuff"
  49. sessionID: "sessionIDHere"
  50. session: {}
  51. it "should use the user_id", (done)->
  52. @AuthenticationController.getLoggedInUserId.returns("1234")
  53. @controller.recordEvent @req, @res
  54. @AnalyticsManager.recordEvent.calledWith("1234", @req.params["event"], @req.body).should.equal true
  55. done()
  56. it "should use the session id", (done)->
  57. @controller.recordEvent @req, @res
  58. @AnalyticsManager.recordEvent.calledWith(@req.sessionID, @req.params["event"], @req.body).should.equal true
  59. done()
  60. describe "licences", ->
  61. beforeEach ->
  62. @req =
  63. query:
  64. resource_id:1
  65. start_date:'1514764800'
  66. end_date:'1530662400'
  67. resource_type:'institution'
  68. sessionID: "sessionIDHere"
  69. session: {}
  70. it "should trigger institutions api to fetch licences graph data", (done)->
  71. @controller.licences @req, @res
  72. @InstitutionsAPI.getInstitutionLicences.calledWith(@req.query["resource_id"], @req.query["start_date"], @req.query["end_date"], @req.query["lag"]).should.equal true
  73. done()