AnalyticsManagerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const sinon = require('sinon')
  4. const MockRequest = require('../helpers/MockRequest')
  5. const MockResponse = require('../helpers/MockResponse')
  6. const { assert } = require('chai')
  7. const { ObjectID } = require('mongodb')
  8. const MODULE_PATH = path.join(
  9. __dirname,
  10. '../../../../app/src/Features/Analytics/AnalyticsManager'
  11. )
  12. describe('AnalyticsManager', function () {
  13. beforeEach(function () {
  14. this.fakeUserId = 'dbfc9438d14996f73dd172fb'
  15. this.analyticsId = 'ecdb935a-52f3-4f91-aebc-7a70d2ffbb55'
  16. this.Settings = {
  17. analytics: { enabled: true },
  18. }
  19. this.analyticsEventsQueue = {
  20. add: sinon.stub().resolves(),
  21. process: sinon.stub().resolves(),
  22. }
  23. this.analyticsEditingSessionQueue = {
  24. add: sinon.stub().resolves(),
  25. process: sinon.stub().resolves(),
  26. }
  27. this.onboardingEmailsQueue = {
  28. add: sinon.stub().resolves(),
  29. process: sinon.stub().resolves(),
  30. }
  31. this.analyticsUserPropertiesQueue = {
  32. add: sinon.stub().resolves(),
  33. process: sinon.stub().resolves(),
  34. }
  35. const self = this
  36. this.Queues = {
  37. getQueue: queueName => {
  38. switch (queueName) {
  39. case 'analytics-events':
  40. return self.analyticsEventsQueue
  41. case 'analytics-editing-sessions':
  42. return self.analyticsEditingSessionQueue
  43. case 'emails-onboarding':
  44. return self.onboardingEmailsQueue
  45. case 'analytics-user-properties':
  46. return self.analyticsUserPropertiesQueue
  47. default:
  48. throw new Error('Unexpected queue name')
  49. }
  50. },
  51. createScheduledJob: sinon.stub().resolves(),
  52. }
  53. this.backgroundRequest = sinon.stub().yields()
  54. this.request = sinon.stub().yields()
  55. this.AnalyticsManager = SandboxedModule.require(MODULE_PATH, {
  56. requires: {
  57. '@overleaf/settings': this.Settings,
  58. '../../infrastructure/Queues': this.Queues,
  59. './UserAnalyticsIdCache': (this.UserAnalyticsIdCache = {
  60. get: sinon.stub().resolves(this.analyticsId),
  61. }),
  62. },
  63. })
  64. })
  65. describe('ignores when', function () {
  66. it('user is smoke test user', function () {
  67. this.Settings.smokeTest = { userId: this.fakeUserId }
  68. this.AnalyticsManager.identifyUser(this.fakeUserId, '')
  69. sinon.assert.notCalled(this.Queues.createScheduledJob)
  70. })
  71. it('analytics service is disabled', function () {
  72. this.Settings.analytics.enabled = false
  73. this.AnalyticsManager.identifyUser(this.fakeUserId, '')
  74. sinon.assert.notCalled(this.Queues.createScheduledJob)
  75. })
  76. it('userId is missing', function () {
  77. this.AnalyticsManager.identifyUser(undefined, this.analyticsId)
  78. sinon.assert.notCalled(this.Queues.createScheduledJob)
  79. })
  80. it('analyticsId is missing', function () {
  81. this.AnalyticsManager.identifyUser(
  82. new ObjectID(this.fakeUserId),
  83. undefined
  84. )
  85. sinon.assert.notCalled(this.Queues.createScheduledJob)
  86. })
  87. it('analyticsId is not a valid UUID', function () {
  88. this.AnalyticsManager.identifyUser(
  89. new ObjectID(this.fakeUserId),
  90. this.fakeUserId
  91. )
  92. sinon.assert.notCalled(this.Queues.createScheduledJob)
  93. })
  94. it('userId and analyticsId are the same Mongo ID', function () {
  95. this.AnalyticsManager.identifyUser(
  96. new ObjectID(this.fakeUserId),
  97. new ObjectID(this.fakeUserId)
  98. )
  99. sinon.assert.notCalled(this.Queues.createScheduledJob)
  100. })
  101. it('editing session segmentation is not valid', function () {
  102. this.AnalyticsManager.updateEditingSession(
  103. this.fakeUserId,
  104. '789ghi',
  105. 'fr',
  106. { key: '<alert>' }
  107. )
  108. sinon.assert.notCalled(this.analyticsEditingSessionQueue.add)
  109. })
  110. it('event is not valid', async function () {
  111. await this.AnalyticsManager.recordEventForUser(
  112. this.fakeUserId,
  113. 'not an event!'
  114. )
  115. sinon.assert.notCalled(this.analyticsEventsQueue.add)
  116. })
  117. it('event segmentation is not valid', async function () {
  118. await this.AnalyticsManager.recordEventForUser(
  119. this.fakeUserId,
  120. 'an_event',
  121. { not_a: 'Valid Segmentation!' }
  122. )
  123. sinon.assert.notCalled(this.analyticsEventsQueue.add)
  124. })
  125. it('user property name is not valid', async function () {
  126. await this.AnalyticsManager.setUserPropertyForUser(
  127. this.fakeUserId,
  128. 'an invalid property',
  129. 'a_value'
  130. )
  131. sinon.assert.notCalled(this.analyticsUserPropertiesQueue.add)
  132. })
  133. it('user property value is not valid', async function () {
  134. await this.AnalyticsManager.setUserPropertyForUser(
  135. this.fakeUserId,
  136. 'a_property',
  137. 'an invalid value'
  138. )
  139. sinon.assert.notCalled(this.analyticsUserPropertiesQueue.add)
  140. })
  141. })
  142. describe('queues the appropriate message for', function () {
  143. it('identifyUser', function () {
  144. const analyticsId = 'bd101c4c-722f-4204-9e2d-8303e5d9c120'
  145. this.AnalyticsManager.identifyUser(this.fakeUserId, analyticsId, true)
  146. sinon.assert.calledWithMatch(
  147. this.Queues.createScheduledJob,
  148. 'analytics-events',
  149. {
  150. name: 'identify',
  151. data: {
  152. userId: this.fakeUserId,
  153. analyticsId,
  154. isNewUser: true,
  155. createdAt: sinon.match.date,
  156. },
  157. },
  158. 60000
  159. )
  160. })
  161. it('recordEventForUser', async function () {
  162. const event = 'fake-event'
  163. await this.AnalyticsManager.recordEventForUser(
  164. this.fakeUserId,
  165. event,
  166. null
  167. )
  168. sinon.assert.calledWithMatch(this.analyticsEventsQueue.add, 'event', {
  169. analyticsId: this.analyticsId,
  170. event,
  171. segmentation: null,
  172. isLoggedIn: true,
  173. })
  174. })
  175. it('updateEditingSession', function () {
  176. const projectId = '789ghi'
  177. const countryCode = 'fr'
  178. const segmentation = { editorType: 'abc' }
  179. this.AnalyticsManager.updateEditingSession(
  180. this.fakeUserId,
  181. projectId,
  182. countryCode,
  183. segmentation
  184. )
  185. sinon.assert.calledWithMatch(
  186. this.analyticsEditingSessionQueue.add,
  187. 'editing-session',
  188. {
  189. userId: this.fakeUserId,
  190. projectId,
  191. countryCode,
  192. segmentation,
  193. }
  194. )
  195. })
  196. it('empty field in event segmentation', async function () {
  197. const timings = null
  198. await this.AnalyticsManager.recordEventForUser(
  199. this.fakeUserId,
  200. 'an_event',
  201. { compileTime: timings?.compileE2E }
  202. )
  203. sinon.assert.calledWithMatch(this.analyticsEventsQueue.add, 'event', {
  204. analyticsId: this.analyticsId,
  205. event: 'an_event',
  206. segmentation: { compileTime: undefined },
  207. isLoggedIn: true,
  208. })
  209. })
  210. it('empty space in event segmentation value', async function () {
  211. await this.AnalyticsManager.recordEventForUser(
  212. this.fakeUserId,
  213. 'an_event',
  214. { segment: 'a value with spaces' }
  215. )
  216. sinon.assert.calledWithMatch(this.analyticsEventsQueue.add, 'event', {
  217. analyticsId: this.analyticsId,
  218. event: 'an_event',
  219. segmentation: { segment: 'a value with spaces' },
  220. isLoggedIn: true,
  221. })
  222. })
  223. it('boolean field in event segmentation', async function () {
  224. await this.AnalyticsManager.recordEventForUser(
  225. this.fakeUserId,
  226. 'an_event',
  227. { isAutoCompile: false }
  228. )
  229. sinon.assert.calledWithMatch(this.analyticsEventsQueue.add, 'event', {
  230. analyticsId: this.analyticsId,
  231. event: 'an_event',
  232. segmentation: { isAutoCompile: false },
  233. isLoggedIn: true,
  234. })
  235. })
  236. })
  237. describe('AnalyticsIdMiddleware', function () {
  238. beforeEach(function () {
  239. this.userId = '123abc'
  240. this.analyticsId = 'bccd308c-5d72-426e-a106-662e88557795'
  241. const self = this
  242. this.AnalyticsManager = SandboxedModule.require(MODULE_PATH, {
  243. requires: {
  244. '@overleaf/settings': {},
  245. '../../infrastructure/Queues': {
  246. getQueue: queueName => {
  247. switch (queueName) {
  248. case 'analytics-events':
  249. return self.analyticsEventsQueue
  250. case 'analytics-editing-sessions':
  251. return self.analyticsEditingSessionQueue
  252. case 'emails-onboarding':
  253. return self.onboardingEmailsQueue
  254. case 'analytics-user-properties':
  255. return self.analyticsUserPropertiesQueue
  256. default:
  257. throw new Error('Unexpected queue name')
  258. }
  259. },
  260. },
  261. './UserAnalyticsIdCache': {},
  262. uuid: {
  263. v4: () => this.analyticsId,
  264. },
  265. },
  266. })
  267. this.req = new MockRequest()
  268. this.req.session = {}
  269. this.res = new MockResponse()
  270. this.next = () => {}
  271. })
  272. it('sets session.analyticsId with no user in session', async function () {
  273. await this.AnalyticsManager.analyticsIdMiddleware(
  274. this.req,
  275. this.res,
  276. this.next
  277. )
  278. assert.equal(this.analyticsId, this.req.session.analyticsId)
  279. })
  280. it('does not update analyticsId when existing, with no user in session', async function () {
  281. this.req.session.analyticsId = 'foo'
  282. await this.AnalyticsManager.analyticsIdMiddleware(
  283. this.req,
  284. this.res,
  285. this.next
  286. )
  287. assert.equal('foo', this.req.session.analyticsId)
  288. })
  289. it('sets session.analyticsId with a logged in user in session having an analyticsId', async function () {
  290. this.req.session.user = {
  291. _id: this.userId,
  292. analyticsId: this.analyticsId,
  293. }
  294. await this.AnalyticsManager.analyticsIdMiddleware(
  295. this.req,
  296. this.res,
  297. this.next
  298. )
  299. assert.equal(this.analyticsId, this.req.session.analyticsId)
  300. })
  301. it('sets session.analyticsId with a legacy user session without an analyticsId', async function () {
  302. this.req.session.user = {
  303. _id: this.userId,
  304. analyticsId: undefined,
  305. }
  306. await this.AnalyticsManager.analyticsIdMiddleware(
  307. this.req,
  308. this.res,
  309. this.next
  310. )
  311. assert.equal(this.userId, this.req.session.analyticsId)
  312. })
  313. it('updates session.analyticsId with a legacy user session without an analyticsId if different', async function () {
  314. this.req.session.user = {
  315. _id: this.userId,
  316. analyticsId: undefined,
  317. }
  318. this.req.analyticsId = 'foo'
  319. await this.AnalyticsManager.analyticsIdMiddleware(
  320. this.req,
  321. this.res,
  322. this.next
  323. )
  324. assert.equal(this.userId, this.req.session.analyticsId)
  325. })
  326. it('does not update session.analyticsId with a legacy user session without an analyticsId if same', async function () {
  327. this.req.session.user = {
  328. _id: this.userId,
  329. analyticsId: undefined,
  330. }
  331. this.req.analyticsId = this.userId
  332. await this.AnalyticsManager.analyticsIdMiddleware(
  333. this.req,
  334. this.res,
  335. this.next
  336. )
  337. assert.equal(this.userId, this.req.session.analyticsId)
  338. await this.AnalyticsManager.analyticsIdMiddleware(
  339. this.req,
  340. this.res,
  341. this.next
  342. )
  343. assert.equal(this.userId, this.req.session.analyticsId)
  344. })
  345. })
  346. })