RecurlyEventHandlerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. const SandboxedModule = require('sandboxed-module')
  2. const { ObjectId } = require('mongodb-legacy')
  3. const sinon = require('sinon')
  4. const modulePath =
  5. '../../../../app/src/Features/Subscription/RecurlyEventHandler'
  6. describe('RecurlyEventHandler', function () {
  7. beforeEach(function () {
  8. this.userId = '123abc234bcd456cde567def'
  9. this.planCode = 'collaborator-annual'
  10. this.eventData = {
  11. account: {
  12. account_code: this.userId,
  13. },
  14. subscription: {
  15. uuid: '8435ad98c1ce45da99b07f6a6a2e780f',
  16. plan: {
  17. plan_code: 'collaborator-annual',
  18. },
  19. quantity: 1,
  20. state: 'active',
  21. trial_started_at: new Date('2021-01-01 12:34:56'),
  22. trial_ends_at: new Date('2021-01-08 12:34:56'),
  23. current_period_started_at: new Date('2021-01-01 12:34:56'),
  24. current_period_ends_at: new Date('2021-01-08 12:34:56'),
  25. },
  26. }
  27. this.RecurlyEventHandler = SandboxedModule.require(modulePath, {
  28. requires: {
  29. 'mongodb-legacy': { ObjectId },
  30. './SubscriptionEmailHandler': (this.SubscriptionEmailHandler = {
  31. sendTrialOnboardingEmail: sinon.stub(),
  32. }),
  33. '../Analytics/AnalyticsManager': (this.AnalyticsManager = {
  34. recordEventForUserInBackground: sinon.stub(),
  35. setUserPropertyForUserInBackground: sinon.stub(),
  36. }),
  37. '../SplitTests/SplitTestHandler': (this.SplitTestHandler = {
  38. promises: {
  39. getAssignmentForUser: sinon.stub().resolves({
  40. variant: 'default',
  41. }),
  42. },
  43. }),
  44. },
  45. })
  46. })
  47. it('with new_subscription_notification - free trial', function () {
  48. this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
  49. 'new_subscription_notification',
  50. this.eventData
  51. )
  52. sinon.assert.calledWith(
  53. this.AnalyticsManager.recordEventForUserInBackground,
  54. this.userId,
  55. 'subscription-started',
  56. {
  57. plan_code: this.planCode,
  58. quantity: 1,
  59. is_trial: true,
  60. has_ai_add_on: false,
  61. subscriptionId: this.eventData.subscription.uuid,
  62. payment_provider: 'recurly',
  63. }
  64. )
  65. sinon.assert.calledWith(
  66. this.AnalyticsManager.setUserPropertyForUserInBackground,
  67. this.userId,
  68. 'subscription-plan-code',
  69. this.planCode
  70. )
  71. sinon.assert.calledWith(
  72. this.AnalyticsManager.setUserPropertyForUserInBackground,
  73. this.userId,
  74. 'subscription-state',
  75. 'active'
  76. )
  77. sinon.assert.calledWith(
  78. this.AnalyticsManager.setUserPropertyForUserInBackground,
  79. this.userId,
  80. 'subscription-is-trial',
  81. true
  82. )
  83. })
  84. it('sends free trial onboarding email if user starting a trial', async function () {
  85. await this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
  86. 'new_subscription_notification',
  87. this.eventData
  88. )
  89. sinon.assert.called(this.SubscriptionEmailHandler.sendTrialOnboardingEmail)
  90. })
  91. it('with new_subscription_notification - no free trial', function () {
  92. this.eventData.subscription.current_period_started_at = new Date(
  93. '2021-02-10 12:34:56'
  94. )
  95. this.eventData.subscription.current_period_ends_at = new Date(
  96. '2021-02-17 12:34:56'
  97. )
  98. this.eventData.subscription.quantity = 3
  99. this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
  100. 'new_subscription_notification',
  101. this.eventData
  102. )
  103. sinon.assert.calledWith(
  104. this.AnalyticsManager.recordEventForUserInBackground,
  105. this.userId,
  106. 'subscription-started',
  107. {
  108. plan_code: this.planCode,
  109. quantity: 3,
  110. is_trial: false,
  111. has_ai_add_on: false,
  112. subscriptionId: this.eventData.subscription.uuid,
  113. payment_provider: 'recurly',
  114. }
  115. )
  116. sinon.assert.calledWith(
  117. this.AnalyticsManager.setUserPropertyForUserInBackground,
  118. this.userId,
  119. 'subscription-state',
  120. 'active'
  121. )
  122. sinon.assert.calledWith(
  123. this.AnalyticsManager.setUserPropertyForUserInBackground,
  124. this.userId,
  125. 'subscription-is-trial',
  126. false
  127. )
  128. })
  129. it('with updated_subscription_notification', function () {
  130. this.planCode = 'new-plan-code'
  131. this.eventData.subscription.plan.plan_code = this.planCode
  132. this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
  133. 'updated_subscription_notification',
  134. this.eventData
  135. )
  136. sinon.assert.calledWith(
  137. this.AnalyticsManager.recordEventForUserInBackground,
  138. this.userId,
  139. 'subscription-updated',
  140. {
  141. plan_code: this.planCode,
  142. quantity: 1,
  143. is_trial: true,
  144. has_ai_add_on: false,
  145. subscriptionId: this.eventData.subscription.uuid,
  146. payment_provider: 'recurly',
  147. }
  148. )
  149. sinon.assert.calledWith(
  150. this.AnalyticsManager.setUserPropertyForUserInBackground,
  151. this.userId,
  152. 'subscription-plan-code',
  153. this.planCode
  154. )
  155. sinon.assert.calledWith(
  156. this.AnalyticsManager.setUserPropertyForUserInBackground,
  157. this.userId,
  158. 'subscription-state',
  159. 'active'
  160. )
  161. sinon.assert.calledWith(
  162. this.AnalyticsManager.setUserPropertyForUserInBackground,
  163. this.userId,
  164. 'subscription-is-trial',
  165. true
  166. )
  167. })
  168. it('with canceled_subscription_notification', async function () {
  169. this.eventData.subscription.state = 'cancelled'
  170. await this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
  171. 'canceled_subscription_notification',
  172. this.eventData
  173. )
  174. sinon.assert.calledWith(
  175. this.AnalyticsManager.recordEventForUserInBackground,
  176. this.userId,
  177. 'subscription-cancelled',
  178. {
  179. plan_code: this.planCode,
  180. quantity: 1,
  181. is_trial: true,
  182. has_ai_add_on: false,
  183. subscriptionId: this.eventData.subscription.uuid,
  184. payment_provider: 'recurly',
  185. }
  186. )
  187. sinon.assert.calledWith(
  188. this.AnalyticsManager.setUserPropertyForUserInBackground,
  189. this.userId,
  190. 'subscription-state',
  191. 'cancelled'
  192. )
  193. sinon.assert.calledWith(
  194. this.AnalyticsManager.setUserPropertyForUserInBackground,
  195. this.userId,
  196. 'subscription-is-trial',
  197. true
  198. )
  199. })
  200. it('with expired_subscription_notification', function () {
  201. this.eventData.subscription.state = 'expired'
  202. this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
  203. 'expired_subscription_notification',
  204. this.eventData
  205. )
  206. sinon.assert.calledWith(
  207. this.AnalyticsManager.recordEventForUserInBackground,
  208. this.userId,
  209. 'subscription-expired',
  210. {
  211. plan_code: this.planCode,
  212. quantity: 1,
  213. is_trial: true,
  214. has_ai_add_on: false,
  215. subscriptionId: this.eventData.subscription.uuid,
  216. payment_provider: 'recurly',
  217. }
  218. )
  219. sinon.assert.calledWith(
  220. this.AnalyticsManager.setUserPropertyForUserInBackground,
  221. this.userId,
  222. 'subscription-plan-code',
  223. this.planCode
  224. )
  225. sinon.assert.calledWith(
  226. this.AnalyticsManager.setUserPropertyForUserInBackground,
  227. this.userId,
  228. 'subscription-state',
  229. 'expired'
  230. )
  231. sinon.assert.calledWith(
  232. this.AnalyticsManager.setUserPropertyForUserInBackground,
  233. this.userId,
  234. 'subscription-is-trial',
  235. true
  236. )
  237. })
  238. it('with renewed_subscription_notification', function () {
  239. this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
  240. 'renewed_subscription_notification',
  241. this.eventData
  242. )
  243. sinon.assert.calledWith(
  244. this.AnalyticsManager.recordEventForUserInBackground,
  245. this.userId,
  246. 'subscription-renewed',
  247. {
  248. plan_code: this.planCode,
  249. quantity: 1,
  250. is_trial: true,
  251. has_ai_add_on: false,
  252. subscriptionId: this.eventData.subscription.uuid,
  253. payment_provider: 'recurly',
  254. }
  255. )
  256. })
  257. it('with reactivated_account_notification', function () {
  258. this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
  259. 'reactivated_account_notification',
  260. this.eventData
  261. )
  262. sinon.assert.calledWith(
  263. this.AnalyticsManager.recordEventForUserInBackground,
  264. this.userId,
  265. 'subscription-reactivated',
  266. {
  267. plan_code: this.planCode,
  268. quantity: 1,
  269. has_ai_add_on: false,
  270. subscriptionId: this.eventData.subscription.uuid,
  271. payment_provider: 'recurly',
  272. }
  273. )
  274. })
  275. it('with paid_charge_invoice_notification', function () {
  276. const invoice = {
  277. invoice_number: 1234,
  278. currency: 'USD',
  279. state: 'paid',
  280. total_in_cents: 720,
  281. tax_in_cents: 12,
  282. address: {
  283. country: 'Liurnia',
  284. },
  285. collection_method: 'automatic',
  286. subscription_ids: ['abcd1234', 'defa3214'],
  287. }
  288. this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
  289. 'paid_charge_invoice_notification',
  290. {
  291. account: {
  292. account_code: this.userId,
  293. },
  294. invoice,
  295. }
  296. )
  297. sinon.assert.calledWith(
  298. this.AnalyticsManager.recordEventForUserInBackground,
  299. this.userId,
  300. 'subscription-invoice-collected',
  301. {
  302. invoiceNumber: invoice.invoice_number,
  303. currency: invoice.currency,
  304. totalInCents: invoice.total_in_cents,
  305. taxInCents: invoice.tax_in_cents,
  306. country: invoice.address.country,
  307. collectionMethod: invoice.collection_method,
  308. subscriptionId1: invoice.subscription_ids[0],
  309. subscriptionId2: invoice.subscription_ids[1],
  310. payment_provider: 'recurly',
  311. }
  312. )
  313. })
  314. it('with paid_charge_invoice_notification and total_in_cents 0', function () {
  315. this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
  316. 'paid_charge_invoice_notification',
  317. {
  318. account: {
  319. account_code: this.userId,
  320. },
  321. invoice: {
  322. state: 'paid',
  323. total_in_cents: 0,
  324. },
  325. }
  326. )
  327. sinon.assert.notCalled(this.AnalyticsManager.recordEventForUserInBackground)
  328. })
  329. it('with closed_invoice_notification', function () {
  330. this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
  331. 'closed_invoice_notification',
  332. {
  333. account: {
  334. account_code: this.userId,
  335. },
  336. invoice: {
  337. state: 'collected',
  338. total_in_cents: 720,
  339. },
  340. }
  341. )
  342. sinon.assert.calledWith(
  343. this.AnalyticsManager.recordEventForUserInBackground,
  344. this.userId,
  345. 'subscription-invoice-collected'
  346. )
  347. })
  348. it('with closed_invoice_notification and total_in_cents 0', function () {
  349. this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
  350. 'closed_invoice_notification',
  351. {
  352. account: {
  353. account_code: this.userId,
  354. },
  355. invoice: {
  356. state: 'collected',
  357. total_in_cents: 0,
  358. },
  359. }
  360. )
  361. sinon.assert.notCalled(this.AnalyticsManager.recordEventForUserInBackground)
  362. })
  363. it('nothing is called with invalid account code', function () {
  364. this.eventData.account.account_code = 'foo_bar'
  365. this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
  366. 'new_subscription_notification',
  367. this.eventData
  368. )
  369. sinon.assert.notCalled(this.AnalyticsManager.recordEventForUserInBackground)
  370. sinon.assert.notCalled(
  371. this.AnalyticsManager.setUserPropertyForUserInBackground
  372. )
  373. sinon.assert.notCalled(
  374. this.AnalyticsManager.setUserPropertyForUserInBackground
  375. )
  376. sinon.assert.notCalled(
  377. this.AnalyticsManager.setUserPropertyForUserInBackground
  378. )
  379. })
  380. })