2019-05-29 05:21:06 -04:00
|
|
|
const { expect } = require('chai')
|
2019-09-26 08:42:15 -04:00
|
|
|
const User = require('./helpers/User').promises
|
|
|
|
const { Project } = require('../../../app/src/models/Project')
|
2020-09-23 04:49:26 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
describe('Project CRUD', function() {
|
2019-09-26 08:42:15 -04:00
|
|
|
beforeEach(async function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user = new User()
|
2019-09-26 08:42:15 -04:00
|
|
|
await this.user.login()
|
|
|
|
|
|
|
|
this.projectId = await this.user.createProject('example-project')
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe("when project doesn't exist", function() {
|
2019-09-26 08:42:15 -04:00
|
|
|
it('should return 404', async function() {
|
|
|
|
const { response } = await this.user.doRequest(
|
|
|
|
'GET',
|
|
|
|
'/project/aaaaaaaaaaaaaaaaaaaaaaaa'
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-09-26 08:42:15 -04:00
|
|
|
expect(response.statusCode).to.equal(404)
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('when project has malformed id', function() {
|
2019-09-26 08:42:15 -04:00
|
|
|
it('should return 404', async function() {
|
|
|
|
const { response } = await this.user.doRequest('GET', '/project/blah')
|
|
|
|
expect(response.statusCode).to.equal(404)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when trashing a project', function() {
|
|
|
|
it('should mark the project as trashed for the user', async function() {
|
|
|
|
const { response } = await this.user.doRequest(
|
|
|
|
'POST',
|
|
|
|
`/project/${this.projectId}/trash`
|
|
|
|
)
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
|
|
|
|
const trashedProject = await Project.findById(this.projectId).exec()
|
|
|
|
expectObjectIdArrayEqual(trashedProject.trashed, [this.user._id])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does nothing if the user has already trashed the project', async function() {
|
|
|
|
// Mark as trashed the first time
|
|
|
|
await this.user.doRequest('POST', `/project/${this.projectId}/trash`)
|
|
|
|
|
|
|
|
// And then a second time
|
|
|
|
await this.user.doRequest('POST', `/project/${this.projectId}/trash`)
|
|
|
|
|
|
|
|
const trashedProject = await Project.findById(this.projectId).exec()
|
|
|
|
expectObjectIdArrayEqual(trashedProject.trashed, [this.user._id])
|
|
|
|
})
|
2020-03-03 11:37:31 -05:00
|
|
|
|
|
|
|
describe('with an array archived state', function() {
|
|
|
|
it('should mark the project as not archived for the user', async function() {
|
2020-11-02 09:57:41 -05:00
|
|
|
await Project.update(
|
2020-03-03 11:37:31 -05:00
|
|
|
{ _id: this.projectId },
|
|
|
|
{ $set: { archived: [ObjectId(this.user._id)] } }
|
|
|
|
).exec()
|
|
|
|
|
|
|
|
const { response } = await this.user.doRequest(
|
|
|
|
'POST',
|
|
|
|
`/project/${this.projectId}/trash`
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
|
|
|
|
const trashedProject = await Project.findById(this.projectId).exec()
|
|
|
|
expectObjectIdArrayEqual(trashedProject.archived, [])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with a legacy boolean state', function() {
|
|
|
|
it('should mark the project as not archived for the user', async function() {
|
2020-11-02 09:57:41 -05:00
|
|
|
await Project.update(
|
2020-03-03 11:37:31 -05:00
|
|
|
{ _id: this.projectId },
|
|
|
|
{ $set: { archived: true } }
|
|
|
|
).exec()
|
|
|
|
|
|
|
|
const { response } = await this.user.doRequest(
|
|
|
|
'POST',
|
|
|
|
`/project/${this.projectId}/trash`
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
|
|
|
|
const trashedProject = await Project.findById(this.projectId).exec()
|
|
|
|
expectObjectIdArrayEqual(trashedProject.archived, [])
|
|
|
|
})
|
|
|
|
})
|
2019-09-26 08:42:15 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('when untrashing a project', function() {
|
|
|
|
it('should mark the project as untrashed for the user', async function() {
|
2020-11-02 09:57:41 -05:00
|
|
|
await Project.update(
|
2019-09-26 08:42:15 -04:00
|
|
|
{ _id: this.projectId },
|
2020-03-03 11:37:31 -05:00
|
|
|
{ trashed: [ObjectId(this.user._id)] }
|
|
|
|
).exec()
|
2019-09-26 08:42:15 -04:00
|
|
|
const { response } = await this.user.doRequest(
|
|
|
|
'DELETE',
|
|
|
|
`/project/${this.projectId}/trash`
|
|
|
|
)
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
|
|
|
|
const trashedProject = await Project.findById(this.projectId).exec()
|
|
|
|
expectObjectIdArrayEqual(trashedProject.trashed, [])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does nothing if the user has already untrashed the project', async function() {
|
2020-11-02 09:57:41 -05:00
|
|
|
await Project.update(
|
2019-09-26 08:42:15 -04:00
|
|
|
{ _id: this.projectId },
|
2020-03-03 11:37:31 -05:00
|
|
|
{ trashed: [ObjectId(this.user._id)] }
|
|
|
|
).exec()
|
2019-09-26 08:42:15 -04:00
|
|
|
// Mark as untrashed the first time
|
|
|
|
await this.user.doRequest('DELETE', `/project/${this.projectId}/trash`)
|
|
|
|
|
|
|
|
// And then a second time
|
|
|
|
await this.user.doRequest('DELETE', `/project/${this.projectId}/trash`)
|
|
|
|
|
|
|
|
const trashedProject = await Project.findById(this.projectId).exec()
|
|
|
|
expectObjectIdArrayEqual(trashedProject.trashed, [])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('sets trashed to an empty array if not set', async function() {
|
|
|
|
await this.user.doRequest('DELETE', `/project/${this.projectId}/trash`)
|
|
|
|
|
|
|
|
const trashedProject = await Project.findById(this.projectId).exec()
|
|
|
|
expectObjectIdArrayEqual(trashedProject.trashed, [])
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2019-09-26 08:42:15 -04:00
|
|
|
|
|
|
|
function expectObjectIdArrayEqual(objectIdArray, stringArray) {
|
|
|
|
const stringifiedArray = objectIdArray.map(id => id.toString())
|
|
|
|
expect(stringifiedArray).to.deep.equal(stringArray)
|
|
|
|
}
|