AnalyticsControllerTests.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const modulePath = path.join(
  4. __dirname,
  5. '../../../../app/src/Features/Analytics/AnalyticsController'
  6. )
  7. const sinon = require('sinon')
  8. describe('AnalyticsController', function () {
  9. beforeEach(function () {
  10. this.AuthenticationController = { getLoggedInUserId: sinon.stub() }
  11. this.AnalyticsManager = {
  12. updateEditingSession: sinon.stub(),
  13. recordEvent: sinon.stub()
  14. }
  15. this.Features = {
  16. hasFeature: sinon.stub().returns(true)
  17. }
  18. this.controller = SandboxedModule.require(modulePath, {
  19. requires: {
  20. './AnalyticsManager': this.AnalyticsManager,
  21. '../Authentication/AuthenticationController': this
  22. .AuthenticationController,
  23. '../../infrastructure/Features': this.Features,
  24. '../../infrastructure/GeoIpLookup': (this.GeoIpLookup = {
  25. getDetails: sinon.stub()
  26. })
  27. }
  28. })
  29. this.res = {
  30. send() {},
  31. sendStatus() {}
  32. }
  33. })
  34. describe('updateEditingSession', function () {
  35. beforeEach(function () {
  36. this.req = {
  37. params: {
  38. projectId: 'a project id'
  39. }
  40. }
  41. this.GeoIpLookup.getDetails = sinon
  42. .stub()
  43. .callsArgWith(1, null, { country_code: 'XY' })
  44. })
  45. it('delegates to the AnalyticsManager', function (done) {
  46. this.AuthenticationController.getLoggedInUserId.returns('1234')
  47. this.controller.updateEditingSession(this.req, this.res)
  48. this.AnalyticsManager.updateEditingSession
  49. .calledWith('1234', 'a project id', 'XY')
  50. .should.equal(true)
  51. done()
  52. })
  53. })
  54. describe('recordEvent', function () {
  55. beforeEach(function () {
  56. this.req = {
  57. params: {
  58. event: 'i_did_something'
  59. },
  60. body: 'stuff',
  61. sessionID: 'sessionIDHere',
  62. session: {}
  63. }
  64. })
  65. it('should use the user_id', function (done) {
  66. this.AuthenticationController.getLoggedInUserId.returns('1234')
  67. this.controller.recordEvent(this.req, this.res)
  68. this.AnalyticsManager.recordEvent
  69. .calledWith('1234', this.req.params.event, this.req.body)
  70. .should.equal(true)
  71. done()
  72. })
  73. it('should use the session id', function (done) {
  74. this.controller.recordEvent(this.req, this.res)
  75. this.AnalyticsManager.recordEvent
  76. .calledWith(this.req.sessionID, this.req.params.event, this.req.body)
  77. .should.equal(true)
  78. done()
  79. })
  80. })
  81. })