AnalyticsControllerTests.js 3.0 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. describe('AnalyticsController', function () {
  9. beforeEach(function () {
  10. this.SessionManager = { 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/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. }
  40. this.GeoIpLookup.getDetails = sinon
  41. .stub()
  42. .callsArgWith(1, null, { country_code: 'XY' })
  43. })
  44. it('delegates to the AnalyticsManager', function (done) {
  45. this.SessionManager.getLoggedInUserId.returns('1234')
  46. this.controller.updateEditingSession(this.req, this.res)
  47. this.AnalyticsManager.updateEditingSession
  48. .calledWith('1234', 'a project id', 'XY')
  49. .should.equal(true)
  50. done()
  51. })
  52. })
  53. describe('recordEvent', function () {
  54. beforeEach(function () {
  55. const body = {
  56. foo: 'stuff',
  57. _csrf: 'atoken123',
  58. }
  59. this.req = {
  60. params: {
  61. event: 'i_did_something',
  62. },
  63. body,
  64. sessionID: 'sessionIDHere',
  65. session: {},
  66. }
  67. this.expectedData = Object.assign({}, body)
  68. delete this.expectedData._csrf
  69. })
  70. it('should use the user_id', function (done) {
  71. this.SessionManager.getLoggedInUserId.returns('1234')
  72. this.controller.recordEvent(this.req, this.res)
  73. this.AnalyticsManager.recordEvent
  74. .calledWith('1234', this.req.params.event, this.expectedData)
  75. .should.equal(true)
  76. done()
  77. })
  78. it('should use the session id', function (done) {
  79. this.controller.recordEvent(this.req, this.res)
  80. this.AnalyticsManager.recordEvent
  81. .calledWith(
  82. this.req.sessionID,
  83. this.req.params.event,
  84. this.expectedData
  85. )
  86. .should.equal(true)
  87. done()
  88. })
  89. it('should remove the CSRF token before sending to the manager', function (done) {
  90. this.controller.recordEvent(this.req, this.res)
  91. this.AnalyticsManager.recordEvent
  92. .calledWith(
  93. this.req.sessionID,
  94. this.req.params.event,
  95. this.expectedData
  96. )
  97. .should.equal(true)
  98. done()
  99. })
  100. })
  101. })