AccountMappingHelperTests.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. const SandboxedModule = require('sandboxed-module')
  2. const { expect } = require('chai')
  3. const { ObjectId } = require('mongodb-legacy')
  4. const path = require('node:path')
  5. const MODULE_PATH = path.join(
  6. __dirname,
  7. '../../../../app/src/Features/Analytics/AccountMappingHelper'
  8. )
  9. describe('AccountMappingHelper', function () {
  10. beforeEach(function () {
  11. this.AccountMappingHelper = SandboxedModule.require(MODULE_PATH)
  12. })
  13. describe('extractAccountMappingsFromSubscription', function () {
  14. describe('when the v1 id is the same in the updated subscription and the subscription', function () {
  15. describe('when the salesforce id is the same in the updated subscription and the subscription', function () {
  16. beforeEach(function () {
  17. this.subscription = {
  18. id: new ObjectId('abc123abc123abc123abc123'),
  19. salesforce_id: 'def456def456def456',
  20. }
  21. this.updatedSubscription = { salesforce_id: 'def456def456def456' }
  22. this.result =
  23. this.AccountMappingHelper.extractAccountMappingsFromSubscription(
  24. this.subscription,
  25. this.updatedSubscription
  26. )
  27. })
  28. it('returns an empty array', function () {
  29. expect(this.result).to.be.an('array')
  30. expect(this.result).to.have.length(0)
  31. })
  32. })
  33. describe('when the salesforce id has changed between the subscription and the updated subscription', function () {
  34. beforeEach(function () {
  35. this.subscription = {
  36. id: new ObjectId('abc123abc123abc123abc123'),
  37. salesforce_id: 'def456def456def456',
  38. }
  39. this.updatedSubscription = { salesforce_id: 'ghi789ghi789ghi789' }
  40. this.result =
  41. this.AccountMappingHelper.extractAccountMappingsFromSubscription(
  42. this.subscription,
  43. this.updatedSubscription
  44. )
  45. })
  46. it('returns an array with a single item', function () {
  47. expect(this.result).to.be.an('array')
  48. expect(this.result).to.have.length(1)
  49. })
  50. it('uses "account" as sourceEntity', function () {
  51. expect(this.result[0]).to.haveOwnProperty('sourceEntity', 'account')
  52. })
  53. it('uses the salesforceId from the updated subscription as sourceEntityId', function () {
  54. expect(this.result[0]).to.haveOwnProperty(
  55. 'sourceEntityId',
  56. this.updatedSubscription.salesforce_id
  57. )
  58. })
  59. it('uses "subscription" as targetEntity', function () {
  60. expect(this.result[0]).to.haveOwnProperty(
  61. 'targetEntity',
  62. 'subscription'
  63. )
  64. })
  65. it('uses the subscriptionId as targetEntityId', function () {
  66. expect(this.result[0]).to.haveOwnProperty(
  67. 'targetEntityId',
  68. this.subscription.id
  69. )
  70. })
  71. })
  72. describe('when the update subscription has a salesforce id and the subscription has no salesforce_id', function () {
  73. beforeEach(function () {
  74. this.subscription = { id: new ObjectId('abc123abc123abc123abc123') }
  75. this.updatedSubscription = { salesforce_id: 'def456def456def456' }
  76. this.result =
  77. this.AccountMappingHelper.extractAccountMappingsFromSubscription(
  78. this.subscription,
  79. this.updatedSubscription
  80. )
  81. })
  82. it('returns an array with a single item', function () {
  83. expect(this.result).to.be.an('array')
  84. expect(this.result).to.have.length(1)
  85. })
  86. it('uses "account" as sourceEntity', function () {
  87. expect(this.result[0]).to.haveOwnProperty('sourceEntity', 'account')
  88. })
  89. it('uses the salesforceId from the updated subscription as sourceEntityId', function () {
  90. expect(this.result[0]).to.haveOwnProperty(
  91. 'sourceEntityId',
  92. this.updatedSubscription.salesforce_id
  93. )
  94. })
  95. it('uses "subscription" as targetEntity', function () {
  96. expect(this.result[0]).to.haveOwnProperty(
  97. 'targetEntity',
  98. 'subscription'
  99. )
  100. })
  101. it('uses the subscriptionId as targetEntityId', function () {
  102. expect(this.result[0]).to.haveOwnProperty(
  103. 'targetEntityId',
  104. this.subscription.id
  105. )
  106. })
  107. })
  108. })
  109. describe('when the v1 id has changed between the subscription and the updated subscription', function () {
  110. describe('when the salesforce id has not changed between the subscription and the updated subscription', function () {
  111. beforeEach(function () {
  112. this.subscription = {
  113. id: new ObjectId('abc123abc123abc123abc123'),
  114. v1_id: '1',
  115. salesforce_id: '',
  116. }
  117. this.updatedSubscription = { v1_id: '2', salesforce_id: '' }
  118. this.result =
  119. this.AccountMappingHelper.extractAccountMappingsFromSubscription(
  120. this.subscription,
  121. this.updatedSubscription
  122. )
  123. })
  124. it('returns an array with a single item', function () {
  125. expect(this.result).to.be.an('array')
  126. expect(this.result).to.have.length(1)
  127. })
  128. it('uses "university" as the sourceEntity', function () {
  129. expect(this.result[0]).to.haveOwnProperty(
  130. 'sourceEntity',
  131. 'university'
  132. )
  133. })
  134. it('uses the v1_id from the updated subscription as the sourceEntityId', function () {
  135. expect(this.result[0]).to.haveOwnProperty(
  136. 'sourceEntityId',
  137. this.updatedSubscription.v1_id
  138. )
  139. })
  140. it('uses "subscription" as the targetEntity', function () {
  141. expect(this.result[0]).to.haveOwnProperty(
  142. 'targetEntity',
  143. 'subscription'
  144. )
  145. })
  146. it('uses the subscription id as the targetEntityId', function () {
  147. expect(this.result[0]).to.haveOwnProperty(
  148. 'targetEntityId',
  149. this.subscription.id
  150. )
  151. })
  152. })
  153. describe('when the salesforce id has changed between the subscription and the updated subscription', function () {
  154. beforeEach(function () {
  155. this.subscription = {
  156. id: new ObjectId('abc123abc123abc123abc123'),
  157. v1_id: '',
  158. salesforce_id: 'def456def456def456',
  159. }
  160. this.updatedSubscription = {
  161. v1_id: '2',
  162. salesforce_id: '',
  163. }
  164. this.result =
  165. this.AccountMappingHelper.extractAccountMappingsFromSubscription(
  166. this.subscription,
  167. this.updatedSubscription
  168. )
  169. })
  170. it('returns an array with two items', function () {
  171. expect(this.result).to.be.an('array')
  172. expect(this.result).to.have.length(2)
  173. })
  174. it('uses the salesforce_id from the updated subscription as the sourceEntityId for the first item', function () {
  175. expect(this.result[0]).to.haveOwnProperty(
  176. 'sourceEntityId',
  177. this.updatedSubscription.salesforce_id
  178. )
  179. })
  180. it('uses the subscription id as the targetEntityId for the first item', function () {
  181. expect(this.result[0]).to.haveOwnProperty(
  182. 'targetEntityId',
  183. this.subscription.id
  184. )
  185. })
  186. it('uses the v1_id from the updated subscription as the sourceEntityId for the second item', function () {
  187. expect(this.result[1]).to.haveOwnProperty(
  188. 'sourceEntityId',
  189. this.updatedSubscription.v1_id
  190. )
  191. })
  192. it('uses the subscription id as the targetEntityId for the second item', function () {
  193. expect(this.result[1]).to.haveOwnProperty(
  194. 'targetEntityId',
  195. this.subscription.id
  196. )
  197. })
  198. })
  199. })
  200. })
  201. describe('when the recurlySubscription_id has changed between the subscription and the updated subscription', function () {
  202. beforeEach(function () {
  203. this.subscription = {
  204. id: new ObjectId('abc123abc123abc123abc123'),
  205. recurlySubscription_id: '',
  206. }
  207. this.updatedSubscription = {
  208. recurlySubscription_id: '1234a5678b90123cd4567e8f901a2b34',
  209. }
  210. this.result =
  211. this.AccountMappingHelper.extractAccountMappingsFromSubscription(
  212. this.subscription,
  213. this.updatedSubscription
  214. )
  215. })
  216. it('returns an array with one item', function () {
  217. expect(this.result).to.be.an('array')
  218. expect(this.result).to.have.length(1)
  219. })
  220. it('uses "recurly" as the source', function () {
  221. expect(this.result[0]).to.haveOwnProperty('source', 'recurly')
  222. })
  223. it('uses "subscription" as the sourceEntity', function () {
  224. expect(this.result[0]).to.haveOwnProperty('sourceEntity', 'subscription')
  225. })
  226. it('uses the recurlySubscription_id as the sourceEntityId', function () {
  227. expect(this.result[0]).to.haveOwnProperty(
  228. 'sourceEntityId',
  229. this.updatedSubscription.recurlySubscription_id
  230. )
  231. })
  232. it('uses "v2" as the target', function () {
  233. expect(this.result[0]).to.haveOwnProperty('target', 'v2')
  234. })
  235. it('uses "subscription" as the targetEntity', function () {
  236. expect(this.result[0]).to.haveOwnProperty('targetEntity', 'subscription')
  237. })
  238. it('uses the subscription id as the targetEntityId', function () {
  239. expect(this.result[0]).to.haveOwnProperty(
  240. 'targetEntityId',
  241. this.subscription.id
  242. )
  243. })
  244. })
  245. })