SubscriptionLocatorTests.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const modulePath =
  4. '../../../../app/src/Features/Subscription/SubscriptionLocator'
  5. const { expect } = require('chai')
  6. describe('Subscription Locator Tests', function () {
  7. beforeEach(function () {
  8. this.user = { _id: '5208dd34438842e2db333333' }
  9. this.subscription = { hello: 'world' }
  10. this.Subscription = {
  11. findOne: sinon.stub().returns({
  12. exec: sinon.stub().resolves(),
  13. }),
  14. find: sinon.stub().returns({
  15. exec: sinon.stub().resolves(),
  16. }),
  17. }
  18. this.DeletedSubscription = {
  19. findOne: sinon.stub().returns({
  20. exec: sinon.stub().resolves(),
  21. }),
  22. find: sinon.stub().returns({
  23. exec: sinon.stub().resolves(),
  24. }),
  25. }
  26. this.SubscriptionLocator = SandboxedModule.require(modulePath, {
  27. requires: {
  28. './GroupPlansData': {},
  29. '../../models/Subscription': {
  30. Subscription: this.Subscription,
  31. },
  32. '../../models/DeletedSubscription': {
  33. DeletedSubscription: this.DeletedSubscription,
  34. },
  35. '../../models/SSOConfig': {
  36. SSOConfig: this.SSOConfig,
  37. },
  38. },
  39. })
  40. })
  41. describe('finding users subscription', function () {
  42. it('should send the users features', async function () {
  43. this.Subscription.findOne.returns({
  44. exec: sinon.stub().resolves(this.subscription),
  45. })
  46. const subscription =
  47. await this.SubscriptionLocator.promises.getUsersSubscription(this.user)
  48. this.Subscription.findOne
  49. .calledWith({ admin_id: this.user._id })
  50. .should.equal(true)
  51. subscription.should.equal(this.subscription)
  52. })
  53. it('should error if not found', async function () {
  54. this.Subscription.findOne.returns({
  55. exec: sinon.stub().rejects('not found'),
  56. })
  57. await expect(
  58. this.SubscriptionLocator.promises.getUsersSubscription(this.user)
  59. ).to.be.rejected
  60. })
  61. it('should take a user id rather than the user object', async function () {
  62. this.Subscription.findOne.returns({
  63. exec: sinon.stub().resolves(this.subscription),
  64. })
  65. const subscription =
  66. await this.SubscriptionLocator.promises.getUsersSubscription(
  67. this.user._id
  68. )
  69. this.Subscription.findOne
  70. .calledWith({ admin_id: this.user._id })
  71. .should.equal(true)
  72. subscription.should.equal(this.subscription)
  73. })
  74. })
  75. })