2019-06-21 05:57:43 -04:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const path = require('path')
|
2020-11-04 08:35:37 -05:00
|
|
|
const sinon = require('sinon')
|
|
|
|
|
|
|
|
const MODULE_PATH = path.join(
|
2019-06-21 05:57:43 -04:00
|
|
|
__dirname,
|
|
|
|
'../../../../app/src/Features/Analytics/AnalyticsManager'
|
|
|
|
)
|
|
|
|
|
|
|
|
describe('AnalyticsManager', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.fakeUserId = '123abc'
|
2020-11-04 08:35:37 -05:00
|
|
|
this.Settings = {
|
|
|
|
analytics: { enabled: true }
|
|
|
|
}
|
|
|
|
|
|
|
|
this.Queues = {
|
|
|
|
analytics: {
|
|
|
|
events: {
|
|
|
|
add: sinon.stub().resolves()
|
|
|
|
},
|
|
|
|
editingSessions: {
|
|
|
|
add: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
}
|
2019-06-21 05:57:43 -04:00
|
|
|
}
|
|
|
|
this.backgroundRequest = sinon.stub().yields()
|
|
|
|
this.request = sinon.stub().yields()
|
2020-11-04 08:35:37 -05:00
|
|
|
this.AnalyticsManager = SandboxedModule.require(MODULE_PATH, {
|
2019-07-15 06:33:47 -04:00
|
|
|
globals: {
|
|
|
|
console: console
|
|
|
|
},
|
2019-06-21 05:57:43 -04:00
|
|
|
requires: {
|
2020-11-04 08:35:37 -05:00
|
|
|
'settings-sharelatex': this.Settings,
|
2019-06-21 05:57:43 -04:00
|
|
|
'logger-sharelatex': {
|
2020-11-04 08:35:37 -05:00
|
|
|
warn() {}
|
|
|
|
},
|
|
|
|
'../../infrastructure/Queues': this.Queues
|
2019-06-21 05:57:43 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-11-04 08:35:37 -05:00
|
|
|
describe('ignores when', function() {
|
|
|
|
it('user is smoke test user', function() {
|
|
|
|
this.Settings.smokeTest = { userId: this.fakeUserId }
|
|
|
|
this.AnalyticsManager.identifyUser(this.fakeUserId, '')
|
|
|
|
sinon.assert.notCalled(this.Queues.analytics.events.add)
|
2019-06-21 05:57:43 -04:00
|
|
|
})
|
|
|
|
|
2020-11-04 08:35:37 -05:00
|
|
|
it('analytics service is disabled', function() {
|
|
|
|
this.Settings.analytics.enabled = false
|
|
|
|
this.AnalyticsManager.identifyUser(this.fakeUserId, '')
|
|
|
|
sinon.assert.notCalled(this.Queues.analytics.events.add)
|
2019-06-21 05:57:43 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-11-04 08:35:37 -05:00
|
|
|
describe('queues the appropriate message for', function() {
|
|
|
|
it('identifyUser', function() {
|
2019-06-21 05:57:43 -04:00
|
|
|
const oldUserId = '456def'
|
2020-11-04 08:35:37 -05:00
|
|
|
this.AnalyticsManager.identifyUser(this.fakeUserId, oldUserId)
|
|
|
|
sinon.assert.calledWithMatch(
|
|
|
|
this.Queues.analytics.events.add,
|
|
|
|
'identify',
|
|
|
|
{
|
|
|
|
userId: this.fakeUserId,
|
|
|
|
oldUserId
|
|
|
|
}
|
|
|
|
)
|
2019-06-21 05:57:43 -04:00
|
|
|
})
|
|
|
|
|
2020-11-04 08:35:37 -05:00
|
|
|
it('recordEvent', function() {
|
2019-06-21 05:57:43 -04:00
|
|
|
const event = 'fake-event'
|
2020-11-04 08:35:37 -05:00
|
|
|
this.AnalyticsManager.recordEvent(this.fakeUserId, event, null)
|
|
|
|
sinon.assert.calledWithMatch(this.Queues.analytics.events.add, 'event', {
|
|
|
|
event,
|
|
|
|
userId: this.fakeUserId,
|
|
|
|
segmentation: null
|
2019-06-21 05:57:43 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-11-04 08:35:37 -05:00
|
|
|
it('updateEditingSession', function() {
|
2019-06-21 05:57:43 -04:00
|
|
|
const projectId = '789ghi'
|
|
|
|
const countryCode = 'fr'
|
|
|
|
this.AnalyticsManager.updateEditingSession(
|
|
|
|
this.fakeUserId,
|
|
|
|
projectId,
|
2020-11-04 08:35:37 -05:00
|
|
|
countryCode
|
2019-06-21 05:57:43 -04:00
|
|
|
)
|
2020-11-04 08:35:37 -05:00
|
|
|
sinon.assert.calledWithMatch(this.Queues.analytics.editingSessions.add, {
|
|
|
|
userId: this.fakeUserId,
|
|
|
|
projectId,
|
|
|
|
countryCode
|
|
|
|
})
|
2019-06-21 05:57:43 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|