AnalyticsControllerTests.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.AuthenticationController = { getLoggedInUserId: sinon.stub() }
  11. this.AnalyticsManager = {
  12. updateEditingSession: sinon.stub(),
  13. recordEvent: sinon.stub()
  14. }
  15. this.InstitutionsAPI = {
  16. getInstitutionLicences: sinon.stub().callsArgWith(4)
  17. }
  18. this.Features = {
  19. hasFeature: sinon.stub().returns(true)
  20. }
  21. this.controller = SandboxedModule.require(modulePath, {
  22. globals: {
  23. console: console
  24. },
  25. requires: {
  26. './AnalyticsManager': this.AnalyticsManager,
  27. '../Authentication/AuthenticationController': this
  28. .AuthenticationController,
  29. '../Institutions/InstitutionsAPI': this.InstitutionsAPI,
  30. '../../infrastructure/Features': this.Features,
  31. 'logger-sharelatex': {
  32. log() {}
  33. },
  34. '../../infrastructure/GeoIpLookup': (this.GeoIpLookup = {
  35. getDetails: sinon.stub()
  36. })
  37. }
  38. })
  39. this.res = {
  40. send() {},
  41. sendStatus() {}
  42. }
  43. })
  44. describe('updateEditingSession', function() {
  45. beforeEach(function() {
  46. this.req = {
  47. params: {
  48. projectId: 'a project id'
  49. }
  50. }
  51. this.GeoIpLookup.getDetails = sinon
  52. .stub()
  53. .callsArgWith(1, null, { country_code: 'XY' })
  54. })
  55. it('delegates to the AnalyticsManager', function(done) {
  56. this.AuthenticationController.getLoggedInUserId.returns('1234')
  57. this.controller.updateEditingSession(this.req, this.res)
  58. this.AnalyticsManager.updateEditingSession
  59. .calledWith('1234', 'a project id', 'XY')
  60. .should.equal(true)
  61. done()
  62. })
  63. })
  64. describe('recordEvent', function() {
  65. beforeEach(function() {
  66. this.req = {
  67. params: {
  68. event: 'i_did_something'
  69. },
  70. body: 'stuff',
  71. sessionID: 'sessionIDHere',
  72. session: {}
  73. }
  74. })
  75. it('should use the user_id', function(done) {
  76. this.AuthenticationController.getLoggedInUserId.returns('1234')
  77. this.controller.recordEvent(this.req, this.res)
  78. this.AnalyticsManager.recordEvent
  79. .calledWith('1234', this.req.params.event, this.req.body)
  80. .should.equal(true)
  81. done()
  82. })
  83. it('should use the session id', function(done) {
  84. this.controller.recordEvent(this.req, this.res)
  85. this.AnalyticsManager.recordEvent
  86. .calledWith(this.req.sessionID, this.req.params.event, this.req.body)
  87. .should.equal(true)
  88. done()
  89. })
  90. })
  91. describe('licences', function() {
  92. beforeEach(function() {
  93. this.req = {
  94. query: {
  95. resource_id: 1,
  96. start_date: '1514764800',
  97. end_date: '1530662400',
  98. resource_type: 'institution'
  99. },
  100. sessionID: 'sessionIDHere',
  101. session: {}
  102. }
  103. })
  104. it('should trigger institutions api to fetch licences graph data', function(done) {
  105. this.controller.licences(this.req, this.res)
  106. this.InstitutionsAPI.getInstitutionLicences
  107. .calledWith(
  108. this.req.query.resource_id,
  109. this.req.query.start_date,
  110. this.req.query.end_date,
  111. this.req.query.lag
  112. )
  113. .should.equal(true)
  114. done()
  115. })
  116. })
  117. })