Skip to content

Commit

Permalink
fix: throw error if feature requires database. fixes #358
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Aug 1, 2021
1 parent 19ed122 commit 9316eaa
Show file tree
Hide file tree
Showing 68 changed files with 206 additions and 126 deletions.
14 changes: 11 additions & 3 deletions helpers/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import {JekyllPreset} from '../../packages/preset-jekyll/index.js';
import {GithubStore} from '../../packages/store-github/index.js';
import {TwitterSyndicator} from '../../packages/syndicator-twitter/index.js';

export const indiekitConfig = (async () => {
const defaultOptions = {
hasDatabase: true
}

export const indiekitConfig = async options => {
options = {...defaultOptions, ...options};

// Configure MongoDb
const mongod = await MongoMemoryServer.create();
const mongodbUrl = mongod.getUri();
Expand Down Expand Up @@ -44,7 +50,9 @@ export const indiekitConfig = (async () => {
});

// Application settings
indiekit.set('application.mongodbUrl', mongodbUrl);
if (options && options.hasDatabase !== false) {
indiekit.set('application.mongodbUrl', mongodbUrl);
}

// Publication settings
indiekit.set('publication.me', process.env.TEST_PUBLICATION_URL);
Expand All @@ -57,4 +65,4 @@ export const indiekitConfig = (async () => {
const indiekitConfig = await indiekit.getConfig();

return indiekitConfig;
})();
};
6 changes: 3 additions & 3 deletions helpers/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import supertest from 'supertest';
import {indiekitConfig} from '@indiekit-test/config';
import {serverConfig} from '../../packages/indiekit/config/server.js';

export const server = (async () => {
const server = supertest(serverConfig(await indiekitConfig));
export const server = async options => {
const server = supertest(serverConfig(await indiekitConfig(options)));
return server;
})();
};
11 changes: 4 additions & 7 deletions packages/endpoint-media/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,10 @@ export const MediaEndpoint = class {
storage: multer.memoryStorage()
});

this._router.get('/', queryController(publication));
this._router.post('/', indieauth(publication), multipartParser.single('file'), uploadController(publication));

if (application.hasDatabase) {
this._router.get('/files', authenticate, filesController(publication).list);
this._router.get('/files/:id', authenticate, filesController(publication).view);
}
router.get('/', queryController(publication));
router.post('/', indieauth(publication), multipartParser.single('file'), uploadController(publication));
router.get('/files', authenticate, filesController(application, publication).list);
router.get('/files/:id', authenticate, filesController(application, publication).view);

return router;
}
Expand Down
22 changes: 15 additions & 7 deletions packages/endpoint-media/lib/controllers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@ import mongodb from 'mongodb';

const {ObjectId} = mongodb;

export const filesController = publication => ({
export const filesController = (application, publication) => ({
/**
* List previously uploaded files
*
* @param {object} request HTTP request
* @param {object} response HTTP response
* @returns {object} HTTP response
*/
async list(request, response) {
response.render('files', {
title: response.__('media.files.title'),
files: await publication.media.find().toArray(),
parentUrl: `${publication.mediaEndpoint}/files/`
});
async list(request, response, next) {
try {
if (!application.hasDatabase) {
throw new Error(response.__('errors.noDatabase.content'));
}

response.render('files', {
title: response.__('media.files.title'),
files: await publication.media.find().toArray(),
parentUrl: `${publication.mediaEndpoint}/files/`
});
} catch (error) {
next(error);
}
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {JSDOM} from 'jsdom';
import {server} from '@indiekit-test/server';

test('Returns list of previously uploaded files', async t => {
const request = await server;
const request = await server();
const response = await request.get('/media/files')
.auth(process.env.TEST_BEARER_TOKEN, {type: 'bearer'});
const dom = new JSDOM(response.text);
Expand Down
28 changes: 12 additions & 16 deletions packages/endpoint-media/tests/integration/200-files-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {getFixture} from '@indiekit-test/get-fixture';
import {JSDOM} from 'jsdom';
import {server} from '@indiekit-test/server';

test.before(async t => {
test('Views previously uploaded file', async t => {
nock('https://tokens.indieauth.com')
.get('/token')
.reply(200, {
Expand All @@ -16,30 +16,26 @@ test.before(async t => {
.reply(200, {commit: {message: 'Message'}});

// Upload file
const request = await server;
const request = await server();
await request.post('/media')
.auth(process.env.TEST_BEARER_TOKEN, {type: 'bearer'})
.set('Accept', 'application/json')
.attach('file', getFixture('file-types/photo.jpg', false), 'photo.jpg');

// Get file data by parsing list of files and getting values from link
const response = await request.get('/media/files')
const filesResponse = await request.get('/media/files')
.auth(process.env.TEST_BEARER_TOKEN, {type: 'bearer'});
const dom = new JSDOM(response.text);
const link = dom.window.document.querySelector('.file a');

// Return test data
t.context.filename = link.textContent;
t.context.fileId = link.href.split('/').pop();
});
const filesDom = new JSDOM(filesResponse.text);
const fileLink = filesDom.window.document.querySelector('.file a');
const fileName = fileLink.textContent;
const fileId = fileLink.href.split('/').pop();

test('Views previously uploaded file', async t => {
const request = await server;
const response = await request.get(`/media/files/${t.context.fileId}`)
// Visit file page
const fileResponse = await request.get(`/media/files/${fileId}`)
.auth(process.env.TEST_BEARER_TOKEN, {type: 'bearer'});
const dom = new JSDOM(response.text);
const fileDom = new JSDOM(fileResponse.text);

const result = dom.window.document;
const result = fileDom.window.document;

t.is(result.querySelector('title').textContent, `${t.context.filename} - Indiekit`);
t.is(result.querySelector('title').textContent, `${fileName} - Indiekit`);
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import {server} from '@indiekit-test/server';

test('Returns list of previously uploaded files', async t => {
const request = await server;
const request = await server();

const result = await request.get('/media')
.set('Accept', 'application/json')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('Uploads file', async t => {
nock('https://api.github.com')
.put(uri => uri.includes('.jpg'))
.reply(200, {commit: {message: 'Message'}});
const request = await server;
const request = await server();

const result = await request.post('/media')
.auth(process.env.TEST_BEARER_TOKEN, {type: 'bearer'})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import {server} from '@indiekit-test/server';

test('Returns 400 if unsupported query provided', async t => {
const request = await server;
const request = await server();

const result = await request.get('/media')
.set('Accept', 'application/json')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import {server} from '@indiekit-test/server';

test('Returns 400 if unsupported parameter provided', async t => {
const request = await server;
const request = await server();

const result = await request.get('/media')
.set('Accept', 'application/json')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test.serial('Returns 400 if no file included in request', async t => {
me: process.env.TEST_PUBLICATION_URL,
scope: 'media'
});
const request = await server;
const request = await server();

const result = await request.post('/media')
.auth(process.env.TEST_BEARER_TOKEN, {type: 'bearer'})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test('Returns 401 if access token does not provide adequate scope', async t => {
me: process.env.TEST_PUBLICATION_URL,
scope: 'update'
});
const request = await server;
const request = await server();

const result = await request.post('/media')
.auth(process.env.TEST_BEARER_TOKEN_NOSCOPE, {type: 'bearer'})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import {server} from '@indiekit-test/server';

test('Returns 404 if can’t find previously uploaded file', async t => {
const request = await server;
const request = await server();

const result = await request.get('/media/files/5ffcc8025c561a7bf53bd6e8')
.auth(process.env.TEST_BEARER_TOKEN, {type: 'bearer'});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import test from 'ava';
import {JSDOM} from 'jsdom';
import {server} from '@indiekit-test/server';

test('Returns list of previously uploaded files', async t => {
const request = await server({hasDatabase: false});
const response = await request.get('/media/files')
.auth(process.env.TEST_BEARER_TOKEN, {type: 'bearer'});
const dom = new JSDOM(response.text);

const result = dom.window.document;

t.is(result.querySelector('title').textContent, 'Internal Server Error - Indiekit');
t.is(result.querySelector('.article__body p').textContent, 'This feature requires a database.');
});
11 changes: 4 additions & 7 deletions packages/endpoint-micropub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,10 @@ export const MicropubEndpoint = class {
storage: multer.memoryStorage()
});

this._router.get('/', queryController(publication));
this._router.post('/', indieauth(publication), multipartParser.any(), actionController(publication));

if (application.hasDatabase) {
this._router.get('/posts', authenticate, postsController(publication).list);
this._router.get('/posts/:id', authenticate, postsController(publication).view);
}
router.get('/', queryController(publication));
router.post('/', indieauth(publication), multipartParser.any(), actionController(publication));
router.get('/posts', authenticate, postsController(application, publication).list);
router.get('/posts/:id', authenticate, postsController(application, publication).view);

return router;
}
Expand Down
22 changes: 15 additions & 7 deletions packages/endpoint-micropub/lib/controllers/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@ import mongodb from 'mongodb';

const {ObjectId} = mongodb;

export const postsController = publication => ({
export const postsController = (application, publication) => ({
/**
* List previously published posts
*
* @param {object} request HTTP request
* @param {object} response HTTP response
* @returns {object} HTTP response
*/
async list(request, response) {
response.render('posts', {
title: response.__('micropub.posts.title'),
posts: await publication.posts.find().toArray(),
parentUrl: `${publication.micropubEndpoint}/posts/`
});
async list(request, response, next) {
try {
if (!application.hasDatabase) {
throw new Error(response.__('errors.noDatabase.content'));
}

response.render('posts', {
title: response.__('micropub.posts.title'),
posts: await publication.posts.find().toArray(),
parentUrl: `${publication.micropubEndpoint}/posts/`
});
} catch (error) {
next(error);
}
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {JSDOM} from 'jsdom';
import {server} from '@indiekit-test/server';

test('Returns list of previously uploaded files', async t => {
const request = await server;
const request = await server();
const response = await request.get('/micropub/posts')
.auth(process.env.TEST_BEARER_TOKEN, {type: 'bearer'});
const dom = new JSDOM(response.text);
Expand Down
28 changes: 12 additions & 16 deletions packages/endpoint-micropub/tests/integration/200-posts-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import nock from 'nock';
import {JSDOM} from 'jsdom';
import {server} from '@indiekit-test/server';

test.before(async t => {
test('Views previously uploaded file', async t => {
nock('https://tokens.indieauth.com')
.get('/token')
.reply(200, {
Expand All @@ -15,31 +15,27 @@ test.before(async t => {
.reply(200);

// Create post
const request = await server;
const request = await server();
await request.post('/micropub')
.auth(process.env.TEST_BEARER_TOKEN, {type: 'bearer'})
.set('Accept', 'application/json')
.send('h=entry')
.send('name=Foobar');

// Get post data by parsing list of posts and getting values from link
const response = await request.get('/micropub/posts')
const postsResponse = await request.get('/micropub/posts')
.auth(process.env.TEST_BEARER_TOKEN, {type: 'bearer'});
const dom = new JSDOM(response.text);
const link = dom.window.document.querySelector('.file a');

// Return test data
t.context.postName = link.textContent;
t.context.postId = link.href.split('/').pop();
});
const postsDom = new JSDOM(postsResponse.text);
const postLink = postsDom.window.document.querySelector('.file a');
const postName = postLink.textContent;
const postId = postLink.href.split('/').pop();

test('Views previously uploaded file', async t => {
const request = await server;
const response = await request.get(`/micropub/posts/${t.context.postId}`)
// Visit post page
const postResponse = await request.get(`/micropub/posts/${postId}`)
.auth(process.env.TEST_BEARER_TOKEN, {type: 'bearer'});
const dom = new JSDOM(response.text);
const postDom = new JSDOM(postResponse.text);

const result = dom.window.document;
const result = postDom.window.document;

t.is(result.querySelector('title').textContent, `${t.context.postName} - Indiekit`);
t.is(result.querySelector('title').textContent, `${postName} - Indiekit`);
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import {server} from '@indiekit-test/server';

test('Returns categories', async t => {
const request = await server;
const request = await server();

const response = await request.get('/micropub')
.set('Accept', 'application/json')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import {server} from '@indiekit-test/server';

test('Returns categories', async t => {
const request = await server;
const request = await server();

const response = await request.get('/micropub')
.set('Accept', 'application/json')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import {server} from '@indiekit-test/server';

test('Returns categories', async t => {
const request = await server;
const request = await server();

const response = await request.get('/micropub')
.set('Accept', 'application/json')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import {server} from '@indiekit-test/server';

test('Returns available post types', async t => {
const request = await server;
const request = await server();

const response = await request.get('/micropub')
.set('Accept', 'application/json')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test('Returns list of previously published posts', async t => {
nock('https://website.example')
.get('/post.html')
.reply(200, getFixture('html/post.html'));
const request = await server;
const request = await server();

const result = await request.get('/micropub')
.set('Accept', 'application/json')
Expand Down
Loading

0 comments on commit 9316eaa

Please sign in to comment.