ProjectCRUDTests.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. const cheerio = require('cheerio')
  6. describe('Project CRUD', function () {
  7. beforeEach(async function () {
  8. this.user = new User()
  9. await this.user.login()
  10. this.projectId = await this.user.createProject('example-project')
  11. })
  12. describe('project page', function () {
  13. it('should cast refProviders to booleans', async function () {
  14. await this.user.mongoUpdate({
  15. $set: {
  16. refProviders: {
  17. mendeley: { encrypted: 'aaa' },
  18. zotero: { encrypted: 'bbb' },
  19. },
  20. },
  21. })
  22. const { response, body } = await this.user.doRequest(
  23. 'GET',
  24. `/project/${this.projectId}`
  25. )
  26. expect(response.statusCode).to.equal(200)
  27. const dom = cheerio.load(body)
  28. const metaOlUser = dom('meta[name="ol-user"]')[0]
  29. const userData = JSON.parse(metaOlUser.attribs.content)
  30. expect(userData.refProviders.mendeley).to.equal(true)
  31. expect(userData.refProviders.zotero).to.equal(true)
  32. })
  33. })
  34. describe("when project doesn't exist", function () {
  35. it('should return 404', async function () {
  36. const { response } = await this.user.doRequest(
  37. 'GET',
  38. '/project/aaaaaaaaaaaaaaaaaaaaaaaa'
  39. )
  40. expect(response.statusCode).to.equal(404)
  41. })
  42. })
  43. describe('when project has malformed id', function () {
  44. it('should return 404', async function () {
  45. const { response } = await this.user.doRequest('GET', '/project/blah')
  46. expect(response.statusCode).to.equal(404)
  47. })
  48. })
  49. describe('when trashing a project', function () {
  50. it('should mark the project as trashed for the user', async function () {
  51. const { response } = await this.user.doRequest(
  52. 'POST',
  53. `/project/${this.projectId}/trash`
  54. )
  55. expect(response.statusCode).to.equal(200)
  56. const trashedProject = await Project.findById(this.projectId).exec()
  57. expectObjectIdArrayEqual(trashedProject.trashed, [this.user._id])
  58. })
  59. it('does nothing if the user has already trashed the project', async function () {
  60. // Mark as trashed the first time
  61. await this.user.doRequest('POST', `/project/${this.projectId}/trash`)
  62. // And then a second time
  63. await this.user.doRequest('POST', `/project/${this.projectId}/trash`)
  64. const trashedProject = await Project.findById(this.projectId).exec()
  65. expectObjectIdArrayEqual(trashedProject.trashed, [this.user._id])
  66. })
  67. describe('with an array archived state', function () {
  68. it('should mark the project as not archived for the user', async function () {
  69. await Project.updateOne(
  70. { _id: this.projectId },
  71. { $set: { archived: [new ObjectId(this.user._id)] } }
  72. ).exec()
  73. const { response } = await this.user.doRequest(
  74. 'POST',
  75. `/project/${this.projectId}/trash`
  76. )
  77. expect(response.statusCode).to.equal(200)
  78. const trashedProject = await Project.findById(this.projectId).exec()
  79. expectObjectIdArrayEqual(trashedProject.archived, [])
  80. })
  81. })
  82. describe('with a legacy boolean state', function () {
  83. it('should mark the project as not archived for the user', async function () {
  84. await Project.updateOne(
  85. { _id: this.projectId },
  86. { $set: { archived: true } }
  87. ).exec()
  88. const { response } = await this.user.doRequest(
  89. 'POST',
  90. `/project/${this.projectId}/trash`
  91. )
  92. expect(response.statusCode).to.equal(200)
  93. const trashedProject = await Project.findById(this.projectId).exec()
  94. expectObjectIdArrayEqual(trashedProject.archived, [])
  95. })
  96. })
  97. })
  98. describe('when untrashing a project', function () {
  99. it('should mark the project as untrashed for the user', async function () {
  100. await Project.updateOne(
  101. { _id: this.projectId },
  102. { trashed: [new ObjectId(this.user._id)] }
  103. ).exec()
  104. const { response } = await this.user.doRequest(
  105. 'DELETE',
  106. `/project/${this.projectId}/trash`
  107. )
  108. expect(response.statusCode).to.equal(200)
  109. const trashedProject = await Project.findById(this.projectId).exec()
  110. expectObjectIdArrayEqual(trashedProject.trashed, [])
  111. })
  112. it('does nothing if the user has already untrashed the project', async function () {
  113. await Project.updateOne(
  114. { _id: this.projectId },
  115. { trashed: [new ObjectId(this.user._id)] }
  116. ).exec()
  117. // Mark as untrashed the first time
  118. await this.user.doRequest('DELETE', `/project/${this.projectId}/trash`)
  119. // And then a second time
  120. await this.user.doRequest('DELETE', `/project/${this.projectId}/trash`)
  121. const trashedProject = await Project.findById(this.projectId).exec()
  122. expectObjectIdArrayEqual(trashedProject.trashed, [])
  123. })
  124. it('sets trashed to an empty array if not set', async function () {
  125. await this.user.doRequest('DELETE', `/project/${this.projectId}/trash`)
  126. const trashedProject = await Project.findById(this.projectId).exec()
  127. expectObjectIdArrayEqual(trashedProject.trashed, [])
  128. })
  129. })
  130. })
  131. function expectObjectIdArrayEqual(objectIdArray, stringArray) {
  132. const stringifiedArray = objectIdArray.map(id => id.toString())
  133. expect(stringifiedArray).to.deep.equal(stringArray)
  134. }