ProjectCRUDTests.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const { expect } = require('chai')
  2. const User = require('./helpers/User').promises
  3. const { Project } = require('../../../app/src/models/Project')
  4. describe('Project CRUD', function() {
  5. beforeEach(async function() {
  6. this.user = new User()
  7. await this.user.login()
  8. this.projectId = await this.user.createProject('example-project')
  9. })
  10. describe("when project doesn't exist", function() {
  11. it('should return 404', async function() {
  12. const { response } = await this.user.doRequest(
  13. 'GET',
  14. '/project/aaaaaaaaaaaaaaaaaaaaaaaa'
  15. )
  16. expect(response.statusCode).to.equal(404)
  17. })
  18. })
  19. describe('when project has malformed id', function() {
  20. it('should return 404', async function() {
  21. const { response } = await this.user.doRequest('GET', '/project/blah')
  22. expect(response.statusCode).to.equal(404)
  23. })
  24. })
  25. describe('when trashing a project', function() {
  26. it('should mark the project as trashed for the user', async function() {
  27. const { response } = await this.user.doRequest(
  28. 'POST',
  29. `/project/${this.projectId}/trash`
  30. )
  31. expect(response.statusCode).to.equal(200)
  32. const trashedProject = await Project.findById(this.projectId).exec()
  33. expectObjectIdArrayEqual(trashedProject.trashed, [this.user._id])
  34. })
  35. it('does nothing if the user has already trashed the project', async function() {
  36. // Mark as trashed the first time
  37. await this.user.doRequest('POST', `/project/${this.projectId}/trash`)
  38. // And then a second time
  39. await this.user.doRequest('POST', `/project/${this.projectId}/trash`)
  40. const trashedProject = await Project.findById(this.projectId).exec()
  41. expectObjectIdArrayEqual(trashedProject.trashed, [this.user._id])
  42. })
  43. })
  44. describe('when untrashing a project', function() {
  45. it('should mark the project as untrashed for the user', async function() {
  46. await Project.update(
  47. { _id: this.projectId },
  48. { trashed: [this.user._id] }
  49. )
  50. const { response } = await this.user.doRequest(
  51. 'DELETE',
  52. `/project/${this.projectId}/trash`
  53. )
  54. expect(response.statusCode).to.equal(200)
  55. const trashedProject = await Project.findById(this.projectId).exec()
  56. expectObjectIdArrayEqual(trashedProject.trashed, [])
  57. })
  58. it('does nothing if the user has already untrashed the project', async function() {
  59. await Project.update(
  60. { _id: this.projectId },
  61. { trashed: [this.user._id] }
  62. )
  63. // Mark as untrashed the first time
  64. await this.user.doRequest('DELETE', `/project/${this.projectId}/trash`)
  65. // And then a second time
  66. await this.user.doRequest('DELETE', `/project/${this.projectId}/trash`)
  67. const trashedProject = await Project.findById(this.projectId).exec()
  68. expectObjectIdArrayEqual(trashedProject.trashed, [])
  69. })
  70. it('sets trashed to an empty array if not set', async function() {
  71. await this.user.doRequest('DELETE', `/project/${this.projectId}/trash`)
  72. const trashedProject = await Project.findById(this.projectId).exec()
  73. expectObjectIdArrayEqual(trashedProject.trashed, [])
  74. })
  75. })
  76. })
  77. function expectObjectIdArrayEqual(objectIdArray, stringArray) {
  78. const stringifiedArray = objectIdArray.map(id => id.toString())
  79. expect(stringifiedArray).to.deep.equal(stringArray)
  80. }