AnalyticsControllerTests.js 2.8 KB

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