p5.js-web-editor/server/controllers/project.controller/__test__/deleteProject.test.js

120 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

Public API: Create new project (fixes #1095) (#1106) * Converts import script to use public API endpoints The endpoints don't exist yet, but this is a good way to see how the implementation of the data structures differ. * Exposes public API endpoint to fetch user's sketches * Implements public API delete endpoint * Adds helper to create custom ApplicationError classes * Adds create project endpoint that understand API's data structure This transforms the nested tree of file data into a mongoose Project model * Returns '201 Created' to match API spec * Removes 'CustomError' variable assignment as it shows up in test output * transformFiles will return file validation errors * Tests API project controller * Tests toModel() * Creates default files if no root-level .html file is provided * Do not auto-generate a slug if it is provided Fixes a bug where the slug was auto-generated using the sketch name, even if a slug property had been provided. * Validates uniqueness of slugs for projects created by the public API * Adds tests for slug uniqueness * Configures node's Promise implementation for mongoose (fixes warnings) * Moves createProject tests to match controller location * Adds support for code to ApplicationErrors * deleteProject controller tests * getProjectsForUser controller tests - implements tests - update apiKey tests to use new User mocks * Ensure error objects have consistent property names `message` is used as a high-level description of the errors `detail` is optional and has an plain language explanation of the individual errors `errors` is an array of each individual problem from `detail` in a machine-readable format * Assert environment variables are provided at script start * Version public API * Expect "files" property to always be provided * Fixes linting error * Converts import script to use public API endpoints The endpoints don't exist yet, but this is a good way to see how the implementation of the data structures differ. * Exposes public API endpoint to fetch user's sketches * Implements public API delete endpoint * Adds helper to create custom ApplicationError classes * Adds create project endpoint that understand API's data structure This transforms the nested tree of file data into a mongoose Project model * Returns '201 Created' to match API spec * Removes 'CustomError' variable assignment as it shows up in test output * transformFiles will return file validation errors * Tests API project controller * Tests toModel() * Creates default files if no root-level .html file is provided * Do not auto-generate a slug if it is provided Fixes a bug where the slug was auto-generated using the sketch name, even if a slug property had been provided. * Validates uniqueness of slugs for projects created by the public API * Adds tests for slug uniqueness * Configures node's Promise implementation for mongoose (fixes warnings) * Moves createProject tests to match controller location * deleteProject controller tests * Adds support for code to ApplicationErrors * getProjectsForUser controller tests - implements tests - update apiKey tests to use new User mocks * Ensure error objects have consistent property names `message` is used as a high-level description of the errors `detail` is optional and has an plain language explanation of the individual errors `errors` is an array of each individual problem from `detail` in a machine-readable format * Assert environment variables are provided at script start * Version public API * Expect "files" property to always be provided * Fixes linting error * Checks that authenticated user has permission to create under this namespace Previously, the project was always created under the authenticated user's namespace, but this not obvious behaviour.
2019-08-30 20:26:57 +02:00
/**
* @jest-environment node
*/
import { Request, Response } from 'jest-express';
import Project, { createMock, createInstanceMock } from '../../../models/project';
import User from '../../../models/user';
import deleteProject from '../../project.controller/deleteProject';
import { deleteObjectsFromS3 } from '../../aws.controller';
jest.mock('../../../models/project');
jest.mock('../../aws.controller');
describe('project.controller', () => {
describe('deleteProject()', () => {
let ProjectMock;
let ProjectInstanceMock;
beforeEach(() => {
ProjectMock = createMock();
ProjectInstanceMock = createInstanceMock();
});
afterEach(() => {
ProjectMock.restore();
ProjectInstanceMock.restore();
});
it('returns 403 if project is not owned by authenticated user', (done) => {
const user = new User();
const project = new Project();
project.user = user;
const request = new Request();
request.setParams({ project_id: project._id });
request.user = { _id: 'abc123' };
const response = new Response();
ProjectMock
.expects('findById')
.resolves(project);
const promise = deleteProject(request, response);
function expectations() {
expect(response.status).toHaveBeenCalledWith(403);
expect(response.json).toHaveBeenCalledWith({
message: 'Authenticated user does not match owner of project'
});
done();
}
promise.then(expectations, expectations).catch(expectations);
});
it('returns 404 if project does not exist', (done) => {
const user = new User();
const project = new Project();
project.user = user;
const request = new Request();
request.setParams({ project_id: project._id });
request.user = { _id: 'abc123' };
const response = new Response();
ProjectMock
.expects('findById')
.resolves(null);
const promise = deleteProject(request, response);
function expectations() {
expect(response.status).toHaveBeenCalledWith(404);
expect(response.json).toHaveBeenCalledWith({
message: 'Project with that id does not exist'
});
done();
}
promise.then(expectations, expectations).catch(expectations);
});
it('deletes project and dependent files from S3 ', (done) => {
const user = new User();
const project = new Project();
project.user = user;
const request = new Request();
request.setParams({ project_id: project._id });
request.user = { _id: user._id };
const response = new Response();
ProjectMock
.expects('findById')
.resolves(project);
ProjectInstanceMock.expects('remove')
.yields();
const promise = deleteProject(request, response);
function expectations() {
expect(response.status).toHaveBeenCalledWith(200);
expect(response.json).not.toHaveBeenCalled();
expect(deleteObjectsFromS3).toHaveBeenCalled();
done();
}
promise.then(expectations, expectations).catch(expectations);
});
});
});