First pass at API

This commit is contained in:
Tom Moor
2018-05-12 23:14:06 -07:00
parent dded458582
commit 9000aa3aac
10 changed files with 256 additions and 19 deletions

View File

@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`#shares.create should require authentication 1`] = `
Object {
"error": "authentication_required",
"message": "Authentication required",
"ok": false,
"status": 401,
}
`;
exports[`#shares.list should require authentication 1`] = `
Object {
"error": "authentication_required",
"message": "Authentication required",
"ok": false,
"status": 401,
}
`;

View File

@@ -12,6 +12,7 @@ import documents from './documents';
import views from './views';
import hooks from './hooks';
import apiKeys from './apiKeys';
import shares from './shares';
import team from './team';
import integrations from './integrations';
@@ -74,6 +75,7 @@ router.use('/', documents.routes());
router.use('/', views.routes());
router.use('/', hooks.routes());
router.use('/', apiKeys.routes());
router.use('/', shares.routes());
router.use('/', team.routes());
router.use('/', integrations.routes());

64
server/api/shares.js Normal file
View File

@@ -0,0 +1,64 @@
// @flow
import Router from 'koa-router';
import auth from './middlewares/authentication';
import pagination from './middlewares/pagination';
import { presentShare } from '../presenters';
import { Document, User, Share } from '../models';
import policy from '../policies';
const { authorize } = policy;
const router = new Router();
router.post('shares.list', auth(), pagination(), async ctx => {
let { sort = 'updatedAt', direction } = ctx.body;
if (direction !== 'ASC') direction = 'DESC';
const user = ctx.state.user;
const shares = await Share.findAll({
where: { teamId: user.teamId },
order: [[sort, direction]],
include: [
{
model: Document,
required: true,
as: 'document',
},
{
model: User,
required: true,
as: 'user',
},
],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
const data = await Promise.all(shares.map(share => presentShare(ctx, share)));
ctx.body = {
data,
};
});
router.post('shares.create', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const user = ctx.state.user;
const document = await Document.findById(id);
authorize(user, 'share', document);
const share = await Share.create({
documentId: document.id,
userId: user.id,
teamId: user.teamId,
});
share.user = user;
share.document = document;
ctx.body = {
data: presentShare(ctx, share),
};
});
export default router;

61
server/api/shares.test.js Normal file
View File

@@ -0,0 +1,61 @@
/* eslint-disable flowtype/require-valid-file-annotation */
import TestServer from 'fetch-test-server';
import app from '..';
import { flushdb, seed } from '../test/support';
import { buildUser } from '../test/factories';
const server = new TestServer(app.callback());
beforeEach(flushdb);
afterAll(server.close);
describe('#shares.list', async () => {
it('should return a list of shares', async () => {
const { user } = await seed();
const res = await server.post('/api/shares.list', {
body: { token: user.getJwtToken() },
});
expect(res.status).toEqual(200);
});
it('should require authentication', async () => {
const res = await server.post('/api/shares.list');
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
});
describe('#shares.create', async () => {
it('should allow creating a share record for document', async () => {
const { user, document } = await seed();
const res = await server.post('/api/shares.create', {
body: { token: user.getJwtToken(), id: document.id },
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.documentTitle).toBe(document.title);
});
it('should require authentication', async () => {
const { document } = await seed();
const res = await server.post('/api/shares.create', {
body: { id: document.id },
});
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
it('should require authorization', async () => {
const { document } = await seed();
const user = await buildUser();
const res = await server.post('/api/shares.create', {
body: { token: user.getJwtToken(), id: document.id },
});
expect(res.status).toEqual(403);
});
});

View File

@@ -0,0 +1,44 @@
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('shares', {
id: {
type: Sequelize.UUID,
allowNull: false,
primaryKey: true,
},
userId: {
type: Sequelize.UUID,
allowNull: false,
references: {
model: 'users',
},
},
teamId: {
type: Sequelize.UUID,
allowNull: false,
references: {
model: 'teams',
},
},
documentId: {
type: Sequelize.UUID,
allowNull: false,
references: {
model: 'documents',
},
},
createdAt: {
type: Sequelize.DATE,
allowNull: false,
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
},
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('shares');
},
};

27
server/models/Share.js Normal file
View File

@@ -0,0 +1,27 @@
// @flow
import { DataTypes, sequelize } from '../sequelize';
const Share = sequelize.define('share', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
});
Share.associate = models => {
Share.belongsTo(models.User, {
as: 'user',
foreignKey: 'userId',
});
Share.belongsTo(models.Team, {
as: 'team',
foreignKey: 'teamId',
});
Share.belongsTo(models.Document, {
as: 'document',
foreignKey: 'documentId',
});
};
export default Share;

View File

@@ -1,28 +1,30 @@
// @flow
import ApiKey from './ApiKey';
import Authentication from './Authentication';
import Integration from './Integration';
import Event from './Event';
import User from './User';
import Team from './Team';
import Collection from './Collection';
import Document from './Document';
import Event from './Event';
import Integration from './Integration';
import Revision from './Revision';
import ApiKey from './ApiKey';
import View from './View';
import Share from './Share';
import Star from './Star';
import Team from './Team';
import User from './User';
import View from './View';
const models = {
ApiKey,
Authentication,
Integration,
Event,
User,
Team,
Collection,
Document,
Event,
Integration,
Revision,
ApiKey,
View,
Share,
Star,
Team,
User,
View,
};
// based on https://github.com/sequelize/express-example/blob/master/models/index.js
@@ -33,15 +35,16 @@ Object.keys(models).forEach(modelName => {
});
export {
ApiKey,
Authentication,
Integration,
Event,
User,
Team,
Collection,
Document,
Event,
Integration,
Revision,
ApiKey,
View,
Share,
Star,
Team,
User,
View,
};

View File

@@ -8,7 +8,7 @@ allow(User, 'create', Document);
allow(
User,
['read', 'update', 'delete'],
['read', 'update', 'delete', 'share'],
Document,
(user, document) => user.teamId === document.teamId
);

View File

@@ -5,6 +5,7 @@ import presentDocument from './document';
import presentRevision from './revision';
import presentCollection from './collection';
import presentApiKey from './apiKey';
import presentShare from './share';
import presentTeam from './team';
import presentIntegration from './integration';
import presentSlackAttachment from './slackAttachment';
@@ -16,6 +17,7 @@ export {
presentRevision,
presentCollection,
presentApiKey,
presentShare,
presentTeam,
presentIntegration,
presentSlackAttachment,

View File

@@ -0,0 +1,15 @@
// @flow
import { Share } from '../models';
import { presentUser } from '.';
function present(ctx: Object, share: Share) {
return {
id: share.id,
user: presentUser(ctx, share.user),
documentTitle: share.document.title,
createdAt: share.createdAt,
updatedAt: share.updatedAt,
};
}
export default present;