ModelTests.mjs 2.6 KB

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