AnalyticsControllerTests.coffee 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. @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. ).should.equal true
  39. done()
  40. describe "recordEvent", ->
  41. beforeEach ->
  42. @req =
  43. params:
  44. event:"i_did_something"
  45. body:"stuff"
  46. sessionID: "sessionIDHere"
  47. session: {}
  48. it "should use the user_id", (done)->
  49. @AuthenticationController.getLoggedInUserId.returns("1234")
  50. @controller.recordEvent @req, @res
  51. @AnalyticsManager.recordEvent.calledWith("1234", @req.params["event"], @req.body).should.equal true
  52. done()
  53. it "should use the session id", (done)->
  54. @controller.recordEvent @req, @res
  55. @AnalyticsManager.recordEvent.calledWith(@req.sessionID, @req.params["event"], @req.body).should.equal true
  56. done()