2021-03-15 10:43:18 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const sinonChai = require('sinon-chai')
|
|
|
|
const chai = require('chai')
|
|
|
|
const chaiAsPromised = require('chai-as-promised')
|
|
|
|
chai.use(sinonChai)
|
|
|
|
chai.use(chaiAsPromised)
|
|
|
|
const { expect } = chai
|
|
|
|
const recurly = require('recurly')
|
|
|
|
const modulePath = '../../../../app/src/Features/Subscription/RecurlyClient'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('RecurlyClient', function () {
|
|
|
|
beforeEach(function () {
|
2021-03-15 10:43:18 -04:00
|
|
|
this.settings = {
|
|
|
|
apis: {
|
|
|
|
recurly: {
|
|
|
|
apiKey: 'nonsense',
|
2021-04-27 03:52:58 -04:00
|
|
|
privateKey: 'private_nonsense',
|
|
|
|
},
|
|
|
|
},
|
2021-03-15 10:43:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.user = { _id: '123456', email: 'joe@example.com', first_name: 'Joe' }
|
|
|
|
this.recurlyAccount = new recurly.Account()
|
|
|
|
Object.assign(this.recurlyAccount, { code: this.user._id })
|
|
|
|
|
|
|
|
this.UserGetter = {
|
|
|
|
promises: {
|
|
|
|
getUser: sinon.stub().callsFake(userId => {
|
|
|
|
if (userId === this.user._id) {
|
|
|
|
return this.user
|
|
|
|
}
|
2021-04-27 03:52:58 -04:00
|
|
|
}),
|
|
|
|
},
|
2021-03-15 10:43:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let client
|
|
|
|
this.client = client = {
|
2021-04-27 03:52:58 -04:00
|
|
|
getAccount: sinon.stub(),
|
2021-03-15 10:43:18 -04:00
|
|
|
}
|
|
|
|
this.recurly = {
|
|
|
|
errors: recurly.errors,
|
2021-04-14 09:17:21 -04:00
|
|
|
Client: function () {
|
2021-03-15 10:43:18 -04:00
|
|
|
return client
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2021-03-15 10:43:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (this.RecurlyClient = SandboxedModule.require(modulePath, {
|
|
|
|
globals: {
|
2021-04-27 03:52:58 -04:00
|
|
|
console: console,
|
2021-03-15 10:43:18 -04:00
|
|
|
},
|
|
|
|
requires: {
|
|
|
|
'settings-sharelatex': this.settings,
|
|
|
|
recurly: this.recurly,
|
|
|
|
'logger-sharelatex': {
|
|
|
|
err: sinon.stub(),
|
|
|
|
error: sinon.stub(),
|
|
|
|
warn: sinon.stub(),
|
|
|
|
log: sinon.stub(),
|
2021-04-27 03:52:58 -04:00
|
|
|
debug: sinon.stub(),
|
2021-03-15 10:43:18 -04:00
|
|
|
},
|
2021-04-27 03:52:58 -04:00
|
|
|
'../User/UserGetter': this.UserGetter,
|
|
|
|
},
|
2021-03-15 10:43:18 -04:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('initalizing recurly client with undefined API key parameter', function () {
|
|
|
|
it('should create a client without error', function () {
|
2021-03-15 10:43:18 -04:00
|
|
|
let testClient
|
|
|
|
expect(() => {
|
|
|
|
testClient = new recurly.Client(undefined)
|
|
|
|
}).to.not.throw()
|
|
|
|
expect(testClient).to.be.instanceOf(recurly.Client)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getAccountForUserId', function () {
|
|
|
|
it('should return an Account if one exists', async function () {
|
2021-03-15 10:43:18 -04:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return nothing if no account found', async function () {
|
2021-03-15 10:43:18 -04:00
|
|
|
this.client.getAccount = sinon
|
|
|
|
.stub()
|
|
|
|
.throws(new recurly.errors.NotFoundError())
|
|
|
|
expect(
|
|
|
|
this.RecurlyClient.promises.getAccountForUserId('nonsense')
|
|
|
|
).to.eventually.equal(undefined)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should re-throw caught errors', async function () {
|
2021-03-15 10:43:18 -04:00
|
|
|
this.client.getAccount = sinon.stub().throws()
|
|
|
|
await expect(
|
|
|
|
this.RecurlyClient.promises.getAccountForUserId(this.user._id)
|
|
|
|
).to.eventually.be.rejectedWith(Error)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('createAccountForUserId', function () {
|
|
|
|
it('should return the Account as created by recurly', async function () {
|
2021-03-15 10:43:18 -04:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should throw any API errors', async function () {
|
2021-03-15 10:43:18 -04:00
|
|
|
this.client.createAccount = sinon.stub().throws()
|
|
|
|
await expect(
|
|
|
|
this.RecurlyClient.promises.createAccountForUserId(this.user._id)
|
|
|
|
).to.eventually.be.rejectedWith(Error)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|