AnalyticsControllerTests.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. getDetails: sinon.stub(),
  25. }),
  26. },
  27. })
  28. this.res = {
  29. send() {},
  30. sendStatus() {},
  31. }
  32. })
  33. describe('updateEditingSession', function () {
  34. beforeEach(function () {
  35. this.req = {
  36. params: {
  37. projectId: 'a project id',
  38. },
  39. session: {},
  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.SessionManager.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. const body = {
  57. foo: 'stuff',
  58. _csrf: 'atoken123',
  59. }
  60. this.req = {
  61. params: {
  62. event: 'i_did_something',
  63. },
  64. body,
  65. sessionID: 'sessionIDHere',
  66. session: {},
  67. }
  68. this.expectedData = Object.assign({}, body)
  69. delete this.expectedData._csrf
  70. })
  71. it('should use the session', function (done) {
  72. this.controller.recordEvent(this.req, this.res)
  73. this.AnalyticsManager.recordEventForSession
  74. .calledWith(this.req.session, this.req.params.event, this.expectedData)
  75. .should.equal(true)
  76. done()
  77. })
  78. it('should remove the CSRF token before sending to the manager', function (done) {
  79. this.controller.recordEvent(this.req, this.res)
  80. this.AnalyticsManager.recordEventForSession
  81. .calledWith(this.req.session, this.req.params.event, this.expectedData)
  82. .should.equal(true)
  83. done()
  84. })
  85. })
  86. })