RecurlyClientTests.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const sinon = require('sinon')
  2. const sinonChai = require('sinon-chai')
  3. const chai = require('chai')
  4. const chaiAsPromised = require('chai-as-promised')
  5. chai.use(sinonChai)
  6. chai.use(chaiAsPromised)
  7. const { expect } = chai
  8. const recurly = require('recurly')
  9. const modulePath = '../../../../app/src/Features/Subscription/RecurlyClient'
  10. const SandboxedModule = require('sandboxed-module')
  11. describe('RecurlyClient', function() {
  12. beforeEach(function() {
  13. this.settings = {
  14. apis: {
  15. recurly: {
  16. apiKey: 'nonsense',
  17. privateKey: 'private_nonsense'
  18. }
  19. }
  20. }
  21. this.user = { _id: '123456', email: 'joe@example.com', first_name: 'Joe' }
  22. this.recurlyAccount = new recurly.Account()
  23. Object.assign(this.recurlyAccount, { code: this.user._id })
  24. this.UserGetter = {
  25. promises: {
  26. getUser: sinon.stub().callsFake(userId => {
  27. if (userId === this.user._id) {
  28. return this.user
  29. }
  30. })
  31. }
  32. }
  33. let client
  34. this.client = client = {
  35. getAccount: sinon.stub()
  36. }
  37. this.recurly = {
  38. errors: recurly.errors,
  39. Client: function() {
  40. return client
  41. }
  42. }
  43. return (this.RecurlyClient = SandboxedModule.require(modulePath, {
  44. globals: {
  45. console: console
  46. },
  47. requires: {
  48. 'settings-sharelatex': this.settings,
  49. recurly: this.recurly,
  50. 'logger-sharelatex': {
  51. err: sinon.stub(),
  52. error: sinon.stub(),
  53. warn: sinon.stub(),
  54. log: sinon.stub(),
  55. debug: sinon.stub()
  56. },
  57. '../User/UserGetter': this.UserGetter
  58. }
  59. }))
  60. })
  61. describe('initalizing recurly client with undefined API key parameter', function() {
  62. it('should create a client without error', function() {
  63. let testClient
  64. expect(() => {
  65. testClient = new recurly.Client(undefined)
  66. }).to.not.throw()
  67. expect(testClient).to.be.instanceOf(recurly.Client)
  68. })
  69. })
  70. describe('getAccountForUserId', function() {
  71. it('should return an Account if one exists', async function() {
  72. this.client.getAccount = sinon.stub().resolves(this.recurlyAccount)
  73. await expect(
  74. this.RecurlyClient.promises.getAccountForUserId(this.user._id)
  75. )
  76. .to.eventually.be.an.instanceOf(recurly.Account)
  77. .that.has.property('code', this.user._id)
  78. })
  79. it('should return nothing if no account found', async function() {
  80. this.client.getAccount = sinon
  81. .stub()
  82. .throws(new recurly.errors.NotFoundError())
  83. expect(
  84. this.RecurlyClient.promises.getAccountForUserId('nonsense')
  85. ).to.eventually.equal(undefined)
  86. })
  87. it('should re-throw caught errors', async function() {
  88. this.client.getAccount = sinon.stub().throws()
  89. await expect(
  90. this.RecurlyClient.promises.getAccountForUserId(this.user._id)
  91. ).to.eventually.be.rejectedWith(Error)
  92. })
  93. })
  94. describe('createAccountForUserId', function() {
  95. it('should return the Account as created by recurly', async function() {
  96. this.client.createAccount = sinon.stub().resolves(this.recurlyAccount)
  97. await expect(
  98. this.RecurlyClient.promises.createAccountForUserId(this.user._id)
  99. )
  100. .to.eventually.be.an.instanceOf(recurly.Account)
  101. .that.has.property('code', this.user._id)
  102. })
  103. it('should throw any API errors', async function() {
  104. this.client.createAccount = sinon.stub().throws()
  105. await expect(
  106. this.RecurlyClient.promises.createAccountForUserId(this.user._id)
  107. ).to.eventually.be.rejectedWith(Error)
  108. })
  109. })
  110. })