AnalyticsControllerTests.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. const MockResponse = require('../helpers/MockResponse')
  9. describe('AnalyticsController', function () {
  10. beforeEach(function () {
  11. this.SessionManager = { getLoggedInUserId: sinon.stub() }
  12. this.AnalyticsManager = {
  13. updateEditingSession: sinon.stub(),
  14. recordEventForSession: sinon.stub(),
  15. }
  16. this.Features = {
  17. hasFeature: sinon.stub().returns(true),
  18. }
  19. this.controller = SandboxedModule.require(modulePath, {
  20. requires: {
  21. './AnalyticsManager': this.AnalyticsManager,
  22. '../Authentication/SessionManager': this.SessionManager,
  23. '../../infrastructure/Features': this.Features,
  24. '../../infrastructure/GeoIpLookup': (this.GeoIpLookup = {
  25. promises: {
  26. getDetails: sinon.stub().resolves(),
  27. },
  28. }),
  29. },
  30. })
  31. this.res = new MockResponse()
  32. })
  33. describe('updateEditingSession', function () {
  34. beforeEach(function () {
  35. this.req = {
  36. params: {
  37. projectId: 'a project id',
  38. },
  39. session: {},
  40. body: {
  41. segmentation: {
  42. editorType: 'abc',
  43. },
  44. },
  45. }
  46. this.GeoIpLookup.promises.getDetails = sinon
  47. .stub()
  48. .resolves({ country_code: 'XY' })
  49. })
  50. it('delegates to the AnalyticsManager', function (done) {
  51. this.SessionManager.getLoggedInUserId.returns('1234')
  52. this.res.callback = () => {
  53. sinon.assert.calledWith(
  54. this.AnalyticsManager.updateEditingSession,
  55. '1234',
  56. 'a project id',
  57. 'XY',
  58. { editorType: 'abc' }
  59. )
  60. done()
  61. }
  62. this.controller.updateEditingSession(this.req, this.res)
  63. })
  64. })
  65. describe('recordEvent', function () {
  66. beforeEach(function () {
  67. const body = {
  68. foo: 'stuff',
  69. _csrf: 'atoken123',
  70. }
  71. this.req = {
  72. params: {
  73. event: 'i_did_something',
  74. },
  75. body,
  76. sessionID: 'sessionIDHere',
  77. session: {},
  78. }
  79. this.expectedData = Object.assign({}, body)
  80. delete this.expectedData._csrf
  81. })
  82. it('should use the session', function (done) {
  83. this.controller.recordEvent(this.req, this.res)
  84. sinon.assert.calledWith(
  85. this.AnalyticsManager.recordEventForSession,
  86. this.req.session,
  87. this.req.params.event,
  88. this.expectedData
  89. )
  90. done()
  91. })
  92. it('should remove the CSRF token before sending to the manager', function (done) {
  93. this.controller.recordEvent(this.req, this.res)
  94. sinon.assert.calledWith(
  95. this.AnalyticsManager.recordEventForSession,
  96. this.req.session,
  97. this.req.params.event,
  98. this.expectedData
  99. )
  100. done()
  101. })
  102. })
  103. })