ModelTests.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { expect } from 'chai'
  2. import { User } from '../../../app/src/models/User.mjs'
  3. import { Subscription } from '../../../app/src/models/Subscription.mjs'
  4. import Features from '../../../app/src/infrastructure/Features.mjs'
  5. describe('mongoose', function () {
  6. describe('User', function () {
  7. const email = 'wombat@potato.net'
  8. it('allows the creation of a user', async function () {
  9. await expect(User.create({ email })).to.be.fulfilled
  10. await expect(User.findOne({ email }, { _id: 1 })).to.eventually.exist
  11. })
  12. it('does not allow the creation of multiple users with the same email', async function () {
  13. await expect(User.create({ email })).to.be.fulfilled
  14. await expect(User.create({ email })).to.be.rejected
  15. await expect(User.countDocuments({ email })).to.eventually.equal(1)
  16. })
  17. it('formats assignedAt as Date', async function () {
  18. await expect(
  19. User.create({
  20. email,
  21. splitTests: {
  22. 'some-test': [
  23. {
  24. variantName: 'control',
  25. versionNumber: 1,
  26. phase: 'release',
  27. assignedAt: '2021-09-24T11:53:18.313Z',
  28. },
  29. {
  30. variantName: 'control',
  31. versionNumber: 2,
  32. phase: 'release',
  33. assignedAt: new Date(),
  34. },
  35. ],
  36. },
  37. })
  38. ).to.be.fulfilled
  39. const user = await User.findOne({ email }, { splitTests: 1 })
  40. expect(user.splitTests['some-test'][0].assignedAt).to.be.a('date')
  41. expect(user.splitTests['some-test'][1].assignedAt).to.be.a('date')
  42. })
  43. })
  44. describe('Subscription', function () {
  45. let user
  46. beforeEach(async function () {
  47. if (!Features.hasFeature('saas')) {
  48. this.skip()
  49. }
  50. user = await User.create({ email: 'wombat@potato.net' })
  51. })
  52. it('allows the creation of a subscription', async function () {
  53. await expect(
  54. Subscription.create({
  55. admin_id: user._id,
  56. manager_ids: [user._id],
  57. salesforce_id: 'a0a0a00000AAA0AAAA',
  58. })
  59. ).to.be.fulfilled
  60. await expect(Subscription.findOne({ admin_id: user._id })).to.eventually
  61. .exist
  62. })
  63. it('does not allow the creation of a subscription without a manager', async function () {
  64. await expect(Subscription.create({ admin_id: user._id })).to.be.rejected
  65. })
  66. it('does not allow the creation of a subscription with an invalid salesforce_id', async function () {
  67. await expect(
  68. Subscription.create({
  69. admin_id: user._id,
  70. manager_ids: [user._id],
  71. salesforce_id: 'a00aaaAAa0000a',
  72. })
  73. ).to.be.rejected
  74. })
  75. })
  76. })