Added more structure and tests to our authorization code

This commit is contained in:
Tom Moor
2018-02-18 00:11:48 -08:00
parent b53749c95c
commit 2f81eb5e87
9 changed files with 138 additions and 19 deletions

View File

@ -3,6 +3,7 @@ import TestServer from 'fetch-test-server';
import app from '..';
import { View, Star } from '../models';
import { flushdb, seed } from '../test/support';
import { buildUser } from '../test/factories';
import Document from '../models/Document';
const server = new TestServer(app.callback());
@ -55,7 +56,7 @@ describe('#documents.list', async () => {
});
describe('#documents.revision', async () => {
it("should return document's revisions", async () => {
it("should return a document's revisions", async () => {
const { user, document } = await seed();
const res = await server.post('/api/documents.revisions', {
body: {
@ -70,6 +71,18 @@ describe('#documents.revision', async () => {
expect(body.data[0].id).not.toEqual(document.id);
expect(body.data[0].title).toEqual(document.title);
});
it('should require authorization', async () => {
const { document } = await seed();
const user = await buildUser();
const res = await server.post('/api/documents.revisions', {
body: {
token: user.getJwtToken(),
id: document.id,
},
});
expect(res.status).toEqual(404);
});
});
describe('#documents.search', async () => {
@ -199,6 +212,15 @@ describe('#documents.star', async () => {
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/documents.star', {
body: { token: user.getJwtToken(), id: document.id },
});
expect(res.status).toEqual(404);
});
});
describe('#documents.unstar', async () => {
@ -222,6 +244,15 @@ describe('#documents.unstar', async () => {
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/documents.unstar', {
body: { token: user.getJwtToken(), id: document.id },
});
expect(res.status).toEqual(404);
});
});
describe('#documents.create', async () => {
@ -393,4 +424,24 @@ describe('#documents.update', async () => {
'Updated title'
);
});
it('should require authentication', async () => {
const { document } = await seed();
const res = await server.post('/api/documents.update', {
body: { id: document.id, text: 'Updated' },
});
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/documents.update', {
body: { token: user.getJwtToken(), id: document.id, text: 'Updated' },
});
expect(res.status).toEqual(404);
});
});