ProjectCRUDTests.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const { expect } = require('chai')
  2. const User = require('./helpers/User').promises
  3. const { Project } = require('../../../app/src/models/Project')
  4. const { ObjectId } = require('mongodb')
  5. describe('Project CRUD', function() {
  6. beforeEach(async function() {
  7. this.user = new User()
  8. await this.user.login()
  9. this.projectId = await this.user.createProject('example-project')
  10. })
  11. describe("when project doesn't exist", function() {
  12. it('should return 404', async function() {
  13. const { response } = await this.user.doRequest(
  14. 'GET',
  15. '/project/aaaaaaaaaaaaaaaaaaaaaaaa'
  16. )
  17. expect(response.statusCode).to.equal(404)
  18. })
  19. })
  20. describe('when project has malformed id', function() {
  21. it('should return 404', async function() {
  22. const { response } = await this.user.doRequest('GET', '/project/blah')
  23. expect(response.statusCode).to.equal(404)
  24. })
  25. })
  26. describe('when trashing a project', function() {
  27. it('should mark the project as trashed for the user', async function() {
  28. const { response } = await this.user.doRequest(
  29. 'POST',
  30. `/project/${this.projectId}/trash`
  31. )
  32. expect(response.statusCode).to.equal(200)
  33. const trashedProject = await Project.findById(this.projectId).exec()
  34. expectObjectIdArrayEqual(trashedProject.trashed, [this.user._id])
  35. })
  36. it('does nothing if the user has already trashed the project', async function() {
  37. // Mark as trashed the first time
  38. await this.user.doRequest('POST', `/project/${this.projectId}/trash`)
  39. // And then a second time
  40. await this.user.doRequest('POST', `/project/${this.projectId}/trash`)
  41. const trashedProject = await Project.findById(this.projectId).exec()
  42. expectObjectIdArrayEqual(trashedProject.trashed, [this.user._id])
  43. })
  44. describe('with an array archived state', function() {
  45. it('should mark the project as not archived for the user', async function() {
  46. await Project.updateOne(
  47. { _id: this.projectId },
  48. { $set: { archived: [ObjectId(this.user._id)] } }
  49. ).exec()
  50. const { response } = await this.user.doRequest(
  51. 'POST',
  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.archived, [])
  57. })
  58. })
  59. describe('with a legacy boolean state', function() {
  60. it('should mark the project as not archived for the user', async function() {
  61. await Project.updateOne(
  62. { _id: this.projectId },
  63. { $set: { archived: true } }
  64. ).exec()
  65. const { response } = await this.user.doRequest(
  66. 'POST',
  67. `/project/${this.projectId}/trash`
  68. )
  69. expect(response.statusCode).to.equal(200)
  70. const trashedProject = await Project.findById(this.projectId).exec()
  71. expectObjectIdArrayEqual(trashedProject.archived, [])
  72. })
  73. })
  74. })
  75. describe('when untrashing a project', function() {
  76. it('should mark the project as untrashed for the user', async function() {
  77. await Project.updateOne(
  78. { _id: this.projectId },
  79. { trashed: [ObjectId(this.user._id)] }
  80. ).exec()
  81. const { response } = await this.user.doRequest(
  82. 'DELETE',
  83. `/project/${this.projectId}/trash`
  84. )
  85. expect(response.statusCode).to.equal(200)
  86. const trashedProject = await Project.findById(this.projectId).exec()
  87. expectObjectIdArrayEqual(trashedProject.trashed, [])
  88. })
  89. it('does nothing if the user has already untrashed the project', async function() {
  90. await Project.updateOne(
  91. { _id: this.projectId },
  92. { trashed: [ObjectId(this.user._id)] }
  93. ).exec()
  94. // Mark as untrashed the first time
  95. await this.user.doRequest('DELETE', `/project/${this.projectId}/trash`)
  96. // And then a second time
  97. await this.user.doRequest('DELETE', `/project/${this.projectId}/trash`)
  98. const trashedProject = await Project.findById(this.projectId).exec()
  99. expectObjectIdArrayEqual(trashedProject.trashed, [])
  100. })
  101. it('sets trashed to an empty array if not set', async function() {
  102. await this.user.doRequest('DELETE', `/project/${this.projectId}/trash`)
  103. const trashedProject = await Project.findById(this.projectId).exec()
  104. expectObjectIdArrayEqual(trashedProject.trashed, [])
  105. })
  106. })
  107. })
  108. function expectObjectIdArrayEqual(objectIdArray, stringArray) {
  109. const stringifiedArray = objectIdArray.map(id => id.toString())
  110. expect(stringifiedArray).to.deep.equal(stringArray)
  111. }