AnalyticsManagerTests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const modulePath = path.join(
  4. __dirname,
  5. '../../../../app/src/Features/Analytics/AnalyticsManager'
  6. )
  7. const sinon = require('sinon')
  8. const { expect } = require('chai')
  9. const Errors = require('../../../../app/src/Features/Errors/Errors')
  10. describe('AnalyticsManager', function() {
  11. beforeEach(function() {
  12. this.fakeUserId = '123abc'
  13. this.settings = {
  14. overleaf: true,
  15. apis: { analytics: { url: 'analytics.test' } }
  16. }
  17. this.backgroundRequest = sinon.stub().yields()
  18. this.request = sinon.stub().yields()
  19. this.AnalyticsManager = SandboxedModule.require(modulePath, {
  20. requires: {
  21. 'settings-sharelatex': this.settings,
  22. '../../infrastructure/FaultTolerantRequest': {
  23. backgroundRequest: this.backgroundRequest
  24. },
  25. '../Errors/Errors': Errors,
  26. request: this.request,
  27. 'logger-sharelatex': {
  28. log() {}
  29. }
  30. }
  31. })
  32. })
  33. describe('checkAnalyticsRequest', function() {
  34. it('ignores smoke test user', function(done) {
  35. this.settings.smokeTest = { userId: this.fakeUserId }
  36. this.AnalyticsManager.identifyUser(this.fakeUserId, '', error => {
  37. expect(error).to.not.exist
  38. sinon.assert.notCalled(this.request)
  39. done()
  40. })
  41. })
  42. it('return error if analytics service is not configured', function(done) {
  43. this.settings.apis.analytics = null
  44. this.AnalyticsManager.identifyUser(this.fakeUserId, '', error => {
  45. expect(error).to.be.instanceof(Errors.ServiceNotConfiguredError)
  46. sinon.assert.notCalled(this.request)
  47. done()
  48. })
  49. })
  50. })
  51. describe('makes correct request to analytics', function() {
  52. it('identifyUser', function(done) {
  53. const oldUserId = '456def'
  54. this.AnalyticsManager.identifyUser(this.fakeUserId, oldUserId, error => {
  55. expect(error).to.not.exist
  56. sinon.assert.calledWithMatch(this.backgroundRequest, {
  57. body: { old_user_id: oldUserId },
  58. url: 'analytics.test/user/123abc/identify'
  59. })
  60. done()
  61. })
  62. })
  63. it('recordEvent', function(done) {
  64. const event = 'fake-event'
  65. this.AnalyticsManager.recordEvent(this.fakeUserId, event, null, error => {
  66. expect(error).to.not.exist
  67. sinon.assert.calledWithMatch(this.backgroundRequest, {
  68. body: { event },
  69. qs: { fromV2: 1 },
  70. url: 'analytics.test/user/123abc/event',
  71. timeout: 30000,
  72. maxAttempts: 9,
  73. backoffBase: 3000,
  74. backoffMultiplier: 3
  75. })
  76. done()
  77. })
  78. })
  79. it('updateEditingSession', function(done) {
  80. const projectId = '789ghi'
  81. const countryCode = 'fr'
  82. this.AnalyticsManager.updateEditingSession(
  83. this.fakeUserId,
  84. projectId,
  85. countryCode,
  86. error => {
  87. expect(error).to.not.exist
  88. sinon.assert.calledWithMatch(this.backgroundRequest, {
  89. qs: { userId: this.fakeUserId, projectId, countryCode, fromV2: 1 },
  90. url: 'analytics.test/editingSession'
  91. })
  92. done()
  93. }
  94. )
  95. })
  96. it('getLastOccurrence', function(done) {
  97. const event = 'fake-event'
  98. this.AnalyticsManager.getLastOccurrence(this.fakeUserId, event, error => {
  99. expect(error).to.not.exist
  100. sinon.assert.calledWithMatch(this.request, {
  101. body: { event },
  102. url: 'analytics.test/user/123abc/event/last_occurrence'
  103. })
  104. done()
  105. })
  106. })
  107. })
  108. })