AnalyticsControllerTests.coffee 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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(4)
  14. recordEvent: sinon.stub().callsArgWith(3)
  15. @controller = SandboxedModule.require modulePath, requires:
  16. "./AnalyticsManager":@AnalyticsManager
  17. "../Authentication/AuthenticationController":@AuthenticationController
  18. "logger-sharelatex":
  19. log:->
  20. '../../infrastructure/GeoIpLookup': @GeoIpLookup =
  21. getDetails: sinon.stub()
  22. @res =
  23. send:->
  24. describe "updateEditingSession", ->
  25. beforeEach ->
  26. @req =
  27. params:
  28. projectId: "a project id"
  29. @GeoIpLookup.getDetails = sinon.stub()
  30. .callsArgWith(1, null, {country_code: 'XY'})
  31. it "delegates to the AnalyticsManager", (done) ->
  32. @AuthenticationController.getLoggedInUserId.returns("1234")
  33. @controller.updateEditingSession @req, @res
  34. @AnalyticsManager.updateEditingSession.calledWith(
  35. "1234",
  36. "a project id",
  37. 'XY',
  38. {}
  39. ).should.equal true
  40. done()
  41. describe "recordEvent", ->
  42. beforeEach ->
  43. @req =
  44. params:
  45. event:"i_did_something"
  46. body:"stuff"
  47. sessionID: "sessionIDHere"
  48. session: {}
  49. it "should use the user_id", (done)->
  50. @AuthenticationController.getLoggedInUserId.returns("1234")
  51. @controller.recordEvent @req, @res
  52. @AnalyticsManager.recordEvent.calledWith("1234", @req.params["event"], @req.body).should.equal true
  53. done()
  54. it "should use the session id", (done)->
  55. @controller.recordEvent @req, @res
  56. @AnalyticsManager.recordEvent.calledWith(@req.sessionID, @req.params["event"], @req.body).should.equal true
  57. done()