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'
|
|
|
|
)
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('AnalyticsManager', function () {
|
|
|
|
beforeEach(function () {
|
2019-06-21 05:57:43 -04:00
|
|
|
this.fakeUserId = '123abc'
|
2021-09-10 04:30:01 -04:00
|
|
|
this.analyticsId = '123456'
|
2020-11-04 08:35:37 -05:00
|
|
|
this.Settings = {
|
2021-04-27 03:52:58 -04:00
|
|
|
analytics: { enabled: true },
|
2020-11-04 08:35:37 -05:00
|
|
|
}
|
2021-03-31 05:24:39 -04:00
|
|
|
this.analyticsEventsQueue = {
|
|
|
|
add: sinon.stub().resolves(),
|
2021-04-27 03:52:58 -04:00
|
|
|
process: sinon.stub().resolves(),
|
2021-03-31 05:24:39 -04:00
|
|
|
}
|
|
|
|
this.analyticsEditingSessionQueue = {
|
|
|
|
add: sinon.stub().resolves(),
|
2021-04-27 03:52:58 -04:00
|
|
|
process: sinon.stub().resolves(),
|
2021-03-31 05:24:39 -04:00
|
|
|
}
|
|
|
|
this.onboardingEmailsQueue = {
|
|
|
|
add: sinon.stub().resolves(),
|
2021-04-27 03:52:58 -04:00
|
|
|
process: sinon.stub().resolves(),
|
2021-03-31 05:24:39 -04:00
|
|
|
}
|
2021-05-19 04:29:39 -04:00
|
|
|
this.analyticsUserPropertiesQueue = {
|
|
|
|
add: sinon.stub().resolves(),
|
|
|
|
process: sinon.stub().resolves(),
|
|
|
|
}
|
2021-03-31 05:24:39 -04:00
|
|
|
const self = this
|
2020-11-04 08:35:37 -05:00
|
|
|
this.Queues = {
|
2021-03-31 05:24:39 -04:00
|
|
|
getAnalyticsEventsQueue: () => {
|
|
|
|
return self.analyticsEventsQueue
|
|
|
|
},
|
|
|
|
getAnalyticsEditingSessionsQueue: () => {
|
|
|
|
return self.analyticsEditingSessionQueue
|
|
|
|
},
|
|
|
|
getOnboardingEmailsQueue: () => {
|
|
|
|
return self.onboardingEmailsQueue
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2021-05-19 04:29:39 -04:00
|
|
|
getAnalyticsUserPropertiesQueue: () => {
|
|
|
|
return self.analyticsUserPropertiesQueue
|
|
|
|
},
|
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-06-21 05:57:43 -04:00
|
|
|
requires: {
|
2021-07-07 05:38:56 -04:00
|
|
|
'@overleaf/settings': this.Settings,
|
2021-04-27 03:52:58 -04:00
|
|
|
'../../infrastructure/Queues': this.Queues,
|
2021-09-10 04:30:01 -04:00
|
|
|
'./UserAnalyticsIdCache': (this.UserAnalyticsIdCache = {
|
|
|
|
get: sinon.stub().resolves(this.analyticsId),
|
|
|
|
}),
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-06-21 05:57:43 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('ignores when', function () {
|
|
|
|
it('user is smoke test user', function () {
|
2020-11-04 08:35:37 -05:00
|
|
|
this.Settings.smokeTest = { userId: this.fakeUserId }
|
|
|
|
this.AnalyticsManager.identifyUser(this.fakeUserId, '')
|
2021-03-31 05:24:39 -04:00
|
|
|
sinon.assert.notCalled(this.analyticsEventsQueue.add)
|
2019-06-21 05:57:43 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('analytics service is disabled', function () {
|
2020-11-04 08:35:37 -05:00
|
|
|
this.Settings.analytics.enabled = false
|
|
|
|
this.AnalyticsManager.identifyUser(this.fakeUserId, '')
|
2021-03-31 05:24:39 -04:00
|
|
|
sinon.assert.notCalled(this.analyticsEventsQueue.add)
|
2019-06-21 05:57:43 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('queues the appropriate message for', function () {
|
|
|
|
it('identifyUser', function () {
|
2021-09-10 04:30:01 -04:00
|
|
|
const analyticsId = '456def'
|
|
|
|
this.AnalyticsManager.identifyUser(this.fakeUserId, analyticsId)
|
2021-03-31 05:24:39 -04:00
|
|
|
sinon.assert.calledWithMatch(this.analyticsEventsQueue.add, 'identify', {
|
|
|
|
userId: this.fakeUserId,
|
2021-09-10 04:30:01 -04:00
|
|
|
analyticsId,
|
2021-03-31 05:24:39 -04:00
|
|
|
})
|
2019-06-21 05:57:43 -04:00
|
|
|
})
|
|
|
|
|
2021-09-10 04:30:01 -04:00
|
|
|
it('recordEventForUser', async function () {
|
2019-06-21 05:57:43 -04:00
|
|
|
const event = 'fake-event'
|
2021-09-10 04:30:01 -04:00
|
|
|
await this.AnalyticsManager.recordEventForUser(
|
|
|
|
this.fakeUserId,
|
|
|
|
event,
|
|
|
|
null
|
|
|
|
)
|
2021-03-31 05:24:39 -04:00
|
|
|
sinon.assert.calledWithMatch(this.analyticsEventsQueue.add, 'event', {
|
2021-09-10 04:30:01 -04:00
|
|
|
analyticsId: this.analyticsId,
|
2020-11-04 08:35:37 -05:00
|
|
|
event,
|
2021-04-27 03:52:58 -04:00
|
|
|
segmentation: null,
|
2021-09-10 04:30:01 -04:00
|
|
|
isLoggedIn: true,
|
2019-06-21 05:57:43 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04: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
|
|
|
)
|
2021-03-31 05:24:39 -04:00
|
|
|
sinon.assert.calledWithMatch(this.analyticsEditingSessionQueue.add, {
|
2020-11-04 08:35:37 -05:00
|
|
|
userId: this.fakeUserId,
|
|
|
|
projectId,
|
2021-04-27 03:52:58 -04:00
|
|
|
countryCode,
|
2020-11-04 08:35:37 -05:00
|
|
|
})
|
2019-06-21 05:57:43 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|