ModelTests.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const chai = require('chai')
  2. const expect = chai.expect
  3. const chaiAsPromised = require('chai-as-promised')
  4. chai.use(chaiAsPromised)
  5. const { User } = require('../../../app/src/models/User')
  6. const { Subscription } = require('../../../app/src/models/Subscription')
  7. describe('mongoose', function() {
  8. describe('User', function() {
  9. const email = 'wombat@potato.net'
  10. it('allows the creation of a user', async function() {
  11. await expect(User.create({ email: email })).to.be.fulfilled
  12. await expect(User.findOne({ email: email })).to.eventually.exist
  13. })
  14. it('does not allow the creation of multiple users with the same email', async function() {
  15. await expect(User.create({ email: email })).to.be.fulfilled
  16. await expect(User.create({ email: email })).to.be.rejected
  17. await expect(User.countDocuments({ email: email })).to.eventually.equal(1)
  18. })
  19. })
  20. describe('Subsription', function() {
  21. let user
  22. beforeEach(async function() {
  23. user = await User.create({ email: 'wombat@potato.net' })
  24. })
  25. it('allows the creation of a subscription', async function() {
  26. await expect(
  27. Subscription.create({ admin_id: user._id, manager_ids: [user._id] })
  28. ).to.be.fulfilled
  29. await expect(Subscription.findOne({ admin_id: user._id })).to.eventually
  30. .exist
  31. })
  32. it('does not allow the creation of a subscription without a manager', async function() {
  33. await expect(Subscription.create({ admin_id: user._id })).to.be.rejected
  34. })
  35. })
  36. })