AnalyticsControllerTests.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import esmock from 'esmock'
  2. import sinon from 'sinon'
  3. import MockResponse from '../helpers/MockResponse.js'
  4. const modulePath = new URL(
  5. '../../../../app/src/Features/Analytics/AnalyticsController.mjs',
  6. import.meta.url
  7. ).pathname
  8. describe('AnalyticsController', function () {
  9. beforeEach(async 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 = await esmock.strict(modulePath, {
  19. '../../../../app/src/Features/Analytics/AnalyticsManager.js':
  20. this.AnalyticsManager,
  21. '../../../../app/src/Features/Authentication/SessionManager.js':
  22. this.SessionManager,
  23. '../../../../app/src/infrastructure/Features.js': this.Features,
  24. '../../../../app/src/infrastructure/GeoIpLookup.js': (this.GeoIpLookup = {
  25. promises: {
  26. getDetails: sinon.stub().resolves(),
  27. },
  28. }),
  29. })
  30. this.res = new MockResponse()
  31. })
  32. describe('updateEditingSession', function () {
  33. beforeEach(function () {
  34. this.req = {
  35. params: {
  36. projectId: 'a project id',
  37. },
  38. session: {},
  39. body: {
  40. segmentation: {
  41. editorType: 'abc',
  42. },
  43. },
  44. }
  45. this.GeoIpLookup.promises.getDetails = sinon
  46. .stub()
  47. .resolves({ country_code: 'XY' })
  48. })
  49. it('delegates to the AnalyticsManager', function (done) {
  50. this.SessionManager.getLoggedInUserId.returns('1234')
  51. this.res.callback = () => {
  52. sinon.assert.calledWith(
  53. this.AnalyticsManager.updateEditingSession,
  54. '1234',
  55. 'a project id',
  56. 'XY',
  57. { editorType: 'abc' }
  58. )
  59. done()
  60. }
  61. this.controller.updateEditingSession(this.req, this.res)
  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. })