Skip to content

Public API: Create new project (fixes #1095) #1106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
999125f
Converts import script to use public API endpoints
andrewn Jun 11, 2019
e9a1bae
Exposes public API endpoint to fetch user's sketches
andrewn Jun 12, 2019
c45f24f
Implements public API delete endpoint
andrewn Jun 12, 2019
aa6cbe5
Adds helper to create custom ApplicationError classes
andrewn Jun 17, 2019
075f9b6
Adds create project endpoint that understand API's data structure
andrewn Jun 17, 2019
cef4dca
Returns '201 Created' to match API spec
andrewn Jun 19, 2019
8699c1b
Removes 'CustomError' variable assignment as it shows up in test output
andrewn Jun 19, 2019
dbeada5
transformFiles will return file validation errors
andrewn Jun 19, 2019
a38527d
Tests API project controller
andrewn Jun 19, 2019
728c427
Tests toModel()
andrewn Jun 19, 2019
ee6891d
Creates default files if no root-level .html file is provided
andrewn Jun 19, 2019
c2064e9
Do not auto-generate a slug if it is provided
andrewn Jun 19, 2019
0f393ec
Validates uniqueness of slugs for projects created by the public API
andrewn Jun 19, 2019
5ada625
Adds tests for slug uniqueness
andrewn Jun 27, 2019
1549fc8
Configures node's Promise implementation for mongoose (fixes warnings)
andrewn Jun 27, 2019
acdac37
Moves createProject tests to match controller location
andrewn Jun 27, 2019
db717b7
Adds support for code to ApplicationErrors
andrewn Jun 27, 2019
c75cdad
deleteProject controller tests
andrewn Jun 27, 2019
64e39aa
getProjectsForUser controller tests
andrewn Jul 1, 2019
3041b35
Ensure error objects have consistent property names
andrewn Jul 2, 2019
613f33a
Assert environment variables are provided at script start
andrewn Jul 4, 2019
57ced9c
Version public API
andrewn Jul 4, 2019
de85fea
Expect "files" property to always be provided
andrewn Jul 21, 2019
fdff64a
Fixes linting error
andrewn Jul 21, 2019
1bc2719
Converts import script to use public API endpoints
andrewn Jun 11, 2019
ae98c40
Exposes public API endpoint to fetch user's sketches
andrewn Jun 12, 2019
5673bf3
Implements public API delete endpoint
andrewn Jun 12, 2019
16427ad
Adds helper to create custom ApplicationError classes
andrewn Jun 17, 2019
3237160
Adds create project endpoint that understand API's data structure
andrewn Jun 17, 2019
dc1aad7
Returns '201 Created' to match API spec
andrewn Jun 19, 2019
c6a577f
Removes 'CustomError' variable assignment as it shows up in test output
andrewn Jun 19, 2019
d1dcb98
transformFiles will return file validation errors
andrewn Jun 19, 2019
f851d90
Tests API project controller
andrewn Jun 19, 2019
cb767af
Tests toModel()
andrewn Jun 19, 2019
d89be67
Creates default files if no root-level .html file is provided
andrewn Jun 19, 2019
558ad58
Do not auto-generate a slug if it is provided
andrewn Jun 19, 2019
475179c
Validates uniqueness of slugs for projects created by the public API
andrewn Jun 19, 2019
f80ba5c
Adds tests for slug uniqueness
andrewn Jun 27, 2019
ca66043
Configures node's Promise implementation for mongoose (fixes warnings)
andrewn Jun 27, 2019
5bd757d
Moves createProject tests to match controller location
andrewn Jun 27, 2019
a08603a
Adds support for code to ApplicationErrors
andrewn Jun 27, 2019
8798c6d
deleteProject controller tests
andrewn Jun 27, 2019
afb30c2
getProjectsForUser controller tests
andrewn Jul 1, 2019
d3a5621
Ensure error objects have consistent property names
andrewn Jul 2, 2019
e3c93e5
Assert environment variables are provided at script start
andrewn Jul 4, 2019
2b0a218
Version public API
andrewn Jul 4, 2019
218e54d
Expect "files" property to always be provided
andrewn Jul 21, 2019
9f06995
Fixes linting error
andrewn Jul 21, 2019
a925cb2
Merge branch 'feature/public-api-create-project' of github.com:andrew…
catarak Jul 22, 2019
24e9c55
Merge branch 'feature/public-api' into feature/public-api-create-project
catarak Jul 22, 2019
47e0ea8
Merge branch 'feature/public-api' into feature/public-api-create-project
catarak Jul 22, 2019
3c23fee
Checks that authenticated user has permission to create under this na…
andrewn Aug 30, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import '@babel/polyfill'
import mongoose from 'mongoose'

mongoose.Promise = global.Promise;

configure({ adapter: new Adapter() })
5 changes: 5 additions & 0 deletions server/controllers/__mocks__/aws.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const getObjectKey = jest.mock();
export const deleteObjectsFromS3 = jest.fn();
export const signS3 = jest.fn();
export const copyObjectInS3 = jest.fn();
export const listObjectsInS3ForUser = jest.fn();
155 changes: 0 additions & 155 deletions server/controllers/__test__/project.controller.test.js

This file was deleted.

59 changes: 3 additions & 56 deletions server/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import archiver from 'archiver';
import format from 'date-fns/format';
import isUrl from 'is-url';
import jsdom, { serializeDocument } from 'jsdom';
import isBefore from 'date-fns/is_before';
import isAfter from 'date-fns/is_after';
import request from 'request';
import slugify from 'slugify';
import Project from '../models/project';
import User from '../models/user';
import { resolvePathToFile } from '../utils/filePath';
import generateFileSystemSafeName from '../utils/generateFileSystemSafeName';
import { deleteObjectsFromS3, getObjectKey } from './aws.controller';

export { default as createProject } from './project.controller/createProject';
export { default as createProject, apiCreateProject } from './project.controller/createProject';
export { default as deleteProject } from './project.controller/deleteProject';
export { default as getProjectsForUser, apiGetProjectsForUser } from './project.controller/getProjectsForUser';

export function updateProject(req, res) {
Project.findById(req.params.project_id, (findProjectErr, project) => {
Expand Down Expand Up @@ -84,37 +84,6 @@ export function getProject(req, res) {
});
}

function deleteFilesFromS3(files) {
deleteObjectsFromS3(files.filter((file) => {
if (file.url) {
if (!process.env.S3_DATE || (
process.env.S3_DATE &&
isBefore(new Date(process.env.S3_DATE), new Date(file.createdAt)))) {
return true;
}
}
return false;
})
.map(file => getObjectKey(file.url)));
}

export function deleteProject(req, res) {
Project.findById(req.params.project_id, (findProjectErr, project) => {
if (!project.user.equals(req.user._id)) {
res.status(403).json({ success: false, message: 'Session does not match owner of project.' });
return;
}
deleteFilesFromS3(project.files);
Project.remove({ _id: req.params.project_id }, (removeProjectError) => {
if (removeProjectError) {
res.status(404).send({ message: 'Project with that id does not exist' });
return;
}
res.json({ success: true });
});
});
}

export function getProjectsForUserId(userId) {
return new Promise((resolve, reject) => {
Project.find({ user: userId })
Expand Down Expand Up @@ -157,10 +126,6 @@ export function getProjectAsset(req, res) {
});
}

export function getProjectsForUserName(username) {

}

export function getProjects(req, res) {
if (req.user) {
getProjectsForUserId(req.user._id)
Expand All @@ -173,24 +138,6 @@ export function getProjects(req, res) {
}
}

export function getProjectsForUser(req, res) {
if (req.params.username) {
User.findOne({ username: req.params.username }, (err, user) => {
if (!user) {
res.status(404).json({ message: 'User with that username does not exist.' });
return;
}
Project.find({ user: user._id })
.sort('-createdAt')
.select('name files id createdAt updatedAt')
.exec((innerErr, projects) => res.json(projects));
});
} else {
// could just move this to client side
res.json([]);
}
}

export function projectExists(projectId, callback) {
Project.findById(projectId, (err, project) => (
project ? callback(true) : callback(false)
Expand Down
Loading