RecurlyClientTests.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const recurly = require('recurly')
  4. const modulePath = '../../../../app/src/Features/Subscription/RecurlyClient'
  5. const SandboxedModule = require('sandboxed-module')
  6. describe('RecurlyClient', function () {
  7. beforeEach(function () {
  8. this.settings = {
  9. apis: {
  10. recurly: {
  11. apiKey: 'nonsense',
  12. privateKey: 'private_nonsense',
  13. },
  14. },
  15. }
  16. this.user = { _id: '123456', email: 'joe@example.com', first_name: 'Joe' }
  17. this.subscription = {
  18. id: 'subscription-123',
  19. uuid: 'subscription-uuid-123',
  20. }
  21. this.subscriptionChange = { id: 'subscription-change-123' }
  22. this.recurlyAccount = new recurly.Account()
  23. Object.assign(this.recurlyAccount, { code: this.user._id })
  24. this.recurlySubscription = new recurly.Subscription()
  25. Object.assign(this.recurlySubscription, this.subscription)
  26. this.recurlySubscriptionChange = new recurly.SubscriptionChange()
  27. Object.assign(this.recurlySubscriptionChange, this.subscriptionChange)
  28. this.UserGetter = {
  29. promises: {
  30. getUser: sinon.stub().callsFake(userId => {
  31. if (userId === this.user._id) {
  32. return this.user
  33. }
  34. }),
  35. },
  36. }
  37. let client
  38. this.client = client = {
  39. getAccount: sinon.stub(),
  40. }
  41. this.recurly = {
  42. errors: recurly.errors,
  43. Client: function () {
  44. return client
  45. },
  46. }
  47. return (this.RecurlyClient = SandboxedModule.require(modulePath, {
  48. globals: {
  49. console,
  50. },
  51. requires: {
  52. '@overleaf/settings': this.settings,
  53. recurly: this.recurly,
  54. '@overleaf/logger': {
  55. err: sinon.stub(),
  56. error: sinon.stub(),
  57. warn: sinon.stub(),
  58. log: sinon.stub(),
  59. debug: sinon.stub(),
  60. },
  61. '../User/UserGetter': this.UserGetter,
  62. },
  63. }))
  64. })
  65. describe('initalizing recurly client with undefined API key parameter', function () {
  66. it('should create a client without error', function () {
  67. let testClient
  68. expect(() => {
  69. testClient = new recurly.Client(undefined)
  70. }).to.not.throw()
  71. expect(testClient).to.be.instanceOf(recurly.Client)
  72. })
  73. })
  74. describe('getAccountForUserId', function () {
  75. it('should return an Account if one exists', async function () {
  76. this.client.getAccount = sinon.stub().resolves(this.recurlyAccount)
  77. await expect(
  78. this.RecurlyClient.promises.getAccountForUserId(this.user._id)
  79. )
  80. .to.eventually.be.an.instanceOf(recurly.Account)
  81. .that.has.property('code', this.user._id)
  82. })
  83. it('should return nothing if no account found', async function () {
  84. this.client.getAccount = sinon
  85. .stub()
  86. .throws(new recurly.errors.NotFoundError())
  87. expect(
  88. this.RecurlyClient.promises.getAccountForUserId('nonsense')
  89. ).to.eventually.equal(undefined)
  90. })
  91. it('should re-throw caught errors', async function () {
  92. this.client.getAccount = sinon.stub().throws()
  93. await expect(
  94. this.RecurlyClient.promises.getAccountForUserId(this.user._id)
  95. ).to.eventually.be.rejectedWith(Error)
  96. })
  97. })
  98. describe('createAccountForUserId', function () {
  99. it('should return the Account as created by recurly', async function () {
  100. this.client.createAccount = sinon.stub().resolves(this.recurlyAccount)
  101. await expect(
  102. this.RecurlyClient.promises.createAccountForUserId(this.user._id)
  103. )
  104. .to.eventually.be.an.instanceOf(recurly.Account)
  105. .that.has.property('code', this.user._id)
  106. })
  107. it('should throw any API errors', async function () {
  108. this.client.createAccount = sinon.stub().throws()
  109. await expect(
  110. this.RecurlyClient.promises.createAccountForUserId(this.user._id)
  111. ).to.eventually.be.rejectedWith(Error)
  112. })
  113. })
  114. describe('getSubscription', function () {
  115. it('should return the subscription found by recurly', async function () {
  116. this.client.getSubscription = sinon
  117. .stub()
  118. .resolves(this.recurlySubscription)
  119. await expect(
  120. this.RecurlyClient.promises.getSubscription(this.subscription.id)
  121. )
  122. .to.eventually.be.an.instanceOf(recurly.Subscription)
  123. .that.has.property('id', this.subscription.id)
  124. })
  125. it('should throw any API errors', async function () {
  126. this.client.getSubscription = sinon.stub().throws()
  127. await expect(
  128. this.RecurlyClient.promises.getSubscription(this.user._id)
  129. ).to.eventually.be.rejectedWith(Error)
  130. })
  131. })
  132. describe('changeSubscription', function () {
  133. beforeEach(function () {
  134. this.client.createSubscriptionChange = sinon
  135. .stub()
  136. .resolves(this.recurlySubscriptionChange)
  137. })
  138. it('should attempt to create a subscription change', async function () {
  139. this.RecurlyClient.promises.changeSubscription(this.subscription.id, {})
  140. expect(this.client.createSubscriptionChange).to.be.calledWith(
  141. this.subscription.id
  142. )
  143. })
  144. it('should return the subscription change event', async function () {
  145. await expect(
  146. this.RecurlyClient.promises.changeSubscription(
  147. this.subscriptionChange.id,
  148. {}
  149. )
  150. )
  151. .to.eventually.be.an.instanceOf(recurly.SubscriptionChange)
  152. .that.has.property('id', this.subscriptionChange.id)
  153. })
  154. it('should throw any API errors', async function () {
  155. this.client.createSubscriptionChange = sinon.stub().throws()
  156. await expect(
  157. this.RecurlyClient.promises.changeSubscription(this.subscription.id, {})
  158. ).to.eventually.be.rejectedWith(Error)
  159. })
  160. describe('changeSubscriptionByUuid', function () {
  161. it('should attempt to create a subscription change', async function () {
  162. this.RecurlyClient.promises.changeSubscriptionByUuid(
  163. this.subscription.uuid,
  164. {}
  165. )
  166. expect(this.client.createSubscriptionChange).to.be.calledWith(
  167. 'uuid-' + this.subscription.uuid
  168. )
  169. })
  170. it('should return the subscription change event', async function () {
  171. await expect(
  172. this.RecurlyClient.promises.changeSubscriptionByUuid(
  173. this.subscriptionChange.id,
  174. {}
  175. )
  176. )
  177. .to.eventually.be.an.instanceOf(recurly.SubscriptionChange)
  178. .that.has.property('id', this.subscriptionChange.id)
  179. })
  180. it('should throw any API errors', async function () {
  181. this.client.createSubscriptionChange = sinon.stub().throws()
  182. await expect(
  183. this.RecurlyClient.promises.changeSubscriptionByUuid(
  184. this.subscription.id,
  185. {}
  186. )
  187. ).to.eventually.be.rejectedWith(Error)
  188. })
  189. })
  190. })
  191. describe('removeSubscriptionChange', function () {
  192. beforeEach(function () {
  193. this.client.removeSubscriptionChange = sinon.stub().resolves()
  194. })
  195. it('should attempt to remove a pending subscription change', async function () {
  196. this.RecurlyClient.promises.removeSubscriptionChange(
  197. this.subscription.id,
  198. {}
  199. )
  200. expect(this.client.removeSubscriptionChange).to.be.calledWith(
  201. this.subscription.id
  202. )
  203. })
  204. it('should throw any API errors', async function () {
  205. this.client.removeSubscriptionChange = sinon.stub().throws()
  206. await expect(
  207. this.RecurlyClient.promises.removeSubscriptionChange(
  208. this.subscription.id,
  209. {}
  210. )
  211. ).to.eventually.be.rejectedWith(Error)
  212. })
  213. describe('removeSubscriptionChangeByUuid', function () {
  214. it('should attempt to remove a pending subscription change', async function () {
  215. this.RecurlyClient.promises.removeSubscriptionChangeByUuid(
  216. this.subscription.uuid,
  217. {}
  218. )
  219. expect(this.client.removeSubscriptionChange).to.be.calledWith(
  220. 'uuid-' + this.subscription.uuid
  221. )
  222. })
  223. it('should throw any API errors', async function () {
  224. this.client.removeSubscriptionChange = sinon.stub().throws()
  225. await expect(
  226. this.RecurlyClient.promises.removeSubscriptionChangeByUuid(
  227. this.subscription.id,
  228. {}
  229. )
  230. ).to.eventually.be.rejectedWith(Error)
  231. })
  232. })
  233. })
  234. describe('reactivateSubscriptionByUuid', function () {
  235. it('should attempt to reactivate the subscription', async function () {
  236. this.client.reactivateSubscription = sinon
  237. .stub()
  238. .resolves(this.recurlySubscription)
  239. await expect(
  240. this.RecurlyClient.promises.reactivateSubscriptionByUuid(
  241. this.subscription.uuid
  242. )
  243. ).to.eventually.be.an.instanceOf(recurly.Subscription)
  244. expect(this.client.reactivateSubscription).to.be.calledWith(
  245. 'uuid-' + this.subscription.uuid
  246. )
  247. })
  248. })
  249. describe('cancelSubscriptionByUuid', function () {
  250. it('should attempt to cancel the subscription', async function () {
  251. this.client.cancelSubscription = sinon
  252. .stub()
  253. .resolves(this.recurlySubscription)
  254. await expect(
  255. this.RecurlyClient.promises.cancelSubscriptionByUuid(
  256. this.subscription.uuid
  257. )
  258. ).to.eventually.be.an.instanceOf(recurly.Subscription)
  259. expect(this.client.cancelSubscription).to.be.calledWith(
  260. 'uuid-' + this.subscription.uuid
  261. )
  262. })
  263. })
  264. })