| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- const sinon = require('sinon')
- const { expect } = require('chai')
- const recurly = require('recurly')
- const modulePath = '../../../../app/src/Features/Subscription/RecurlyClient'
- const SandboxedModule = require('sandboxed-module')
- describe('RecurlyClient', function () {
- beforeEach(function () {
- this.settings = {
- apis: {
- recurly: {
- apiKey: 'nonsense',
- privateKey: 'private_nonsense',
- },
- },
- }
- this.user = { _id: '123456', email: 'joe@example.com', first_name: 'Joe' }
- this.subscription = {
- id: 'subscription-123',
- uuid: 'subscription-uuid-123',
- }
- this.subscriptionChange = { id: 'subscription-change-123' }
- this.recurlyAccount = new recurly.Account()
- Object.assign(this.recurlyAccount, { code: this.user._id })
- this.recurlySubscription = new recurly.Subscription()
- Object.assign(this.recurlySubscription, this.subscription)
- this.recurlySubscriptionChange = new recurly.SubscriptionChange()
- Object.assign(this.recurlySubscriptionChange, this.subscriptionChange)
- this.UserGetter = {
- promises: {
- getUser: sinon.stub().callsFake(userId => {
- if (userId === this.user._id) {
- return this.user
- }
- }),
- },
- }
- let client
- this.client = client = {
- getAccount: sinon.stub(),
- }
- this.recurly = {
- errors: recurly.errors,
- Client: function () {
- return client
- },
- }
- return (this.RecurlyClient = SandboxedModule.require(modulePath, {
- globals: {
- console,
- },
- requires: {
- '@overleaf/settings': this.settings,
- recurly: this.recurly,
- '@overleaf/logger': {
- err: sinon.stub(),
- error: sinon.stub(),
- warn: sinon.stub(),
- log: sinon.stub(),
- debug: sinon.stub(),
- },
- '../User/UserGetter': this.UserGetter,
- },
- }))
- })
- describe('initalizing recurly client with undefined API key parameter', function () {
- it('should create a client without error', function () {
- let testClient
- expect(() => {
- testClient = new recurly.Client(undefined)
- }).to.not.throw()
- expect(testClient).to.be.instanceOf(recurly.Client)
- })
- })
- describe('getAccountForUserId', function () {
- it('should return an Account if one exists', async function () {
- this.client.getAccount = sinon.stub().resolves(this.recurlyAccount)
- await expect(
- this.RecurlyClient.promises.getAccountForUserId(this.user._id)
- )
- .to.eventually.be.an.instanceOf(recurly.Account)
- .that.has.property('code', this.user._id)
- })
- it('should return nothing if no account found', async function () {
- this.client.getAccount = sinon
- .stub()
- .throws(new recurly.errors.NotFoundError())
- expect(
- this.RecurlyClient.promises.getAccountForUserId('nonsense')
- ).to.eventually.equal(undefined)
- })
- it('should re-throw caught errors', async function () {
- this.client.getAccount = sinon.stub().throws()
- await expect(
- this.RecurlyClient.promises.getAccountForUserId(this.user._id)
- ).to.eventually.be.rejectedWith(Error)
- })
- })
- describe('createAccountForUserId', function () {
- it('should return the Account as created by recurly', async function () {
- this.client.createAccount = sinon.stub().resolves(this.recurlyAccount)
- await expect(
- this.RecurlyClient.promises.createAccountForUserId(this.user._id)
- )
- .to.eventually.be.an.instanceOf(recurly.Account)
- .that.has.property('code', this.user._id)
- })
- it('should throw any API errors', async function () {
- this.client.createAccount = sinon.stub().throws()
- await expect(
- this.RecurlyClient.promises.createAccountForUserId(this.user._id)
- ).to.eventually.be.rejectedWith(Error)
- })
- })
- describe('getSubscription', function () {
- it('should return the subscription found by recurly', async function () {
- this.client.getSubscription = sinon
- .stub()
- .resolves(this.recurlySubscription)
- await expect(
- this.RecurlyClient.promises.getSubscription(this.subscription.id)
- )
- .to.eventually.be.an.instanceOf(recurly.Subscription)
- .that.has.property('id', this.subscription.id)
- })
- it('should throw any API errors', async function () {
- this.client.getSubscription = sinon.stub().throws()
- await expect(
- this.RecurlyClient.promises.getSubscription(this.user._id)
- ).to.eventually.be.rejectedWith(Error)
- })
- })
- describe('changeSubscription', function () {
- beforeEach(function () {
- this.client.createSubscriptionChange = sinon
- .stub()
- .resolves(this.recurlySubscriptionChange)
- })
- it('should attempt to create a subscription change', async function () {
- this.RecurlyClient.promises.changeSubscription(this.subscription.id, {})
- expect(this.client.createSubscriptionChange).to.be.calledWith(
- this.subscription.id
- )
- })
- it('should return the subscription change event', async function () {
- await expect(
- this.RecurlyClient.promises.changeSubscription(
- this.subscriptionChange.id,
- {}
- )
- )
- .to.eventually.be.an.instanceOf(recurly.SubscriptionChange)
- .that.has.property('id', this.subscriptionChange.id)
- })
- it('should throw any API errors', async function () {
- this.client.createSubscriptionChange = sinon.stub().throws()
- await expect(
- this.RecurlyClient.promises.changeSubscription(this.subscription.id, {})
- ).to.eventually.be.rejectedWith(Error)
- })
- describe('changeSubscriptionByUuid', function () {
- it('should attempt to create a subscription change', async function () {
- this.RecurlyClient.promises.changeSubscriptionByUuid(
- this.subscription.uuid,
- {}
- )
- expect(this.client.createSubscriptionChange).to.be.calledWith(
- 'uuid-' + this.subscription.uuid
- )
- })
- it('should return the subscription change event', async function () {
- await expect(
- this.RecurlyClient.promises.changeSubscriptionByUuid(
- this.subscriptionChange.id,
- {}
- )
- )
- .to.eventually.be.an.instanceOf(recurly.SubscriptionChange)
- .that.has.property('id', this.subscriptionChange.id)
- })
- it('should throw any API errors', async function () {
- this.client.createSubscriptionChange = sinon.stub().throws()
- await expect(
- this.RecurlyClient.promises.changeSubscriptionByUuid(
- this.subscription.id,
- {}
- )
- ).to.eventually.be.rejectedWith(Error)
- })
- })
- })
- describe('removeSubscriptionChange', function () {
- beforeEach(function () {
- this.client.removeSubscriptionChange = sinon.stub().resolves()
- })
- it('should attempt to remove a pending subscription change', async function () {
- this.RecurlyClient.promises.removeSubscriptionChange(
- this.subscription.id,
- {}
- )
- expect(this.client.removeSubscriptionChange).to.be.calledWith(
- this.subscription.id
- )
- })
- it('should throw any API errors', async function () {
- this.client.removeSubscriptionChange = sinon.stub().throws()
- await expect(
- this.RecurlyClient.promises.removeSubscriptionChange(
- this.subscription.id,
- {}
- )
- ).to.eventually.be.rejectedWith(Error)
- })
- describe('removeSubscriptionChangeByUuid', function () {
- it('should attempt to remove a pending subscription change', async function () {
- this.RecurlyClient.promises.removeSubscriptionChangeByUuid(
- this.subscription.uuid,
- {}
- )
- expect(this.client.removeSubscriptionChange).to.be.calledWith(
- 'uuid-' + this.subscription.uuid
- )
- })
- it('should throw any API errors', async function () {
- this.client.removeSubscriptionChange = sinon.stub().throws()
- await expect(
- this.RecurlyClient.promises.removeSubscriptionChangeByUuid(
- this.subscription.id,
- {}
- )
- ).to.eventually.be.rejectedWith(Error)
- })
- })
- })
- describe('reactivateSubscriptionByUuid', function () {
- it('should attempt to reactivate the subscription', async function () {
- this.client.reactivateSubscription = sinon
- .stub()
- .resolves(this.recurlySubscription)
- await expect(
- this.RecurlyClient.promises.reactivateSubscriptionByUuid(
- this.subscription.uuid
- )
- ).to.eventually.be.an.instanceOf(recurly.Subscription)
- expect(this.client.reactivateSubscription).to.be.calledWith(
- 'uuid-' + this.subscription.uuid
- )
- })
- })
- describe('cancelSubscriptionByUuid', function () {
- it('should attempt to cancel the subscription', async function () {
- this.client.cancelSubscription = sinon
- .stub()
- .resolves(this.recurlySubscription)
- await expect(
- this.RecurlyClient.promises.cancelSubscriptionByUuid(
- this.subscription.uuid
- )
- ).to.eventually.be.an.instanceOf(recurly.Subscription)
- expect(this.client.cancelSubscription).to.be.calledWith(
- 'uuid-' + this.subscription.uuid
- )
- })
- })
- })
|