ModelTests.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.count({ email: email })).to.eventually.equal(1)
  18. })
  19. })
  20. describe('Subsription', function() {
  21. let user, otherUser
  22. beforeEach(async function() {
  23. user = await User.create({ email: 'wombat@potato.net' })
  24. otherUser = await User.create({ email: 'giraffe@turnip.org' })
  25. })
  26. it('allows the creation of a subscription', async function() {
  27. await expect(
  28. Subscription.create({ admin_id: user._id, manager_ids: [user._id] })
  29. ).to.be.fulfilled
  30. await expect(Subscription.findOne({ admin_id: user._id })).to.eventually
  31. .exist
  32. })
  33. it('does not allow the creation of a subscription without a manager', async function() {
  34. await expect(Subscription.create({ admin_id: user._id })).to.be.rejected
  35. })
  36. it('does not allow a user to manage more than one group', async function() {
  37. await expect(
  38. Subscription.create({ admin_id: user._id, manager_ids: [user._id] })
  39. ).to.be.fulfilled
  40. await expect(
  41. Subscription.create({
  42. admin_id: otherUser._id,
  43. manager_ids: [otherUser._id]
  44. })
  45. ).to.be.fulfilled
  46. await expect(
  47. Subscription.update(
  48. { admin_id: user._id },
  49. { $push: { manager_ids: otherUser._id } }
  50. )
  51. ).to.be.rejected
  52. })
  53. })
  54. })