Skip to content

Commit

Permalink
Merge pull request #393 from danactive/fix-unit-test
Browse files Browse the repository at this point in the history
Enhance resize unit test to catch missing GraphicsMagick
  • Loading branch information
danactive authored Feb 24, 2020
2 parents 3599c2a + e143147 commit cebf03f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 27 deletions.
7 changes: 5 additions & 2 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
FROM node:10
FROM node:10-alpine

RUN apk add --no-cache --virtual build-dependencies make gcc g++ python && \
apk add --no-cache krb5-dev imagemagick graphicsmagick

WORKDIR /app/api
COPY package.json /app/api
COPY package-lock.json /app/api
RUN npm ci
COPY . /app/api
CMD npm start
EXPOSE 8000

26 changes: 15 additions & 11 deletions api/server/plugins/resize/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
const dotProp = require('dot-prop');

const resizeMod = require('./resize');
const validation = require('../../../lib/validation');

const handler = request => new Promise((reply) => {
async function handler(request, h) {
const sourcePath = request.payload.source_path;

resizeMod.resize(sourcePath)
.then(() => reply({ resize: true }))
.catch(reply);
});
try {
const response = await resizeMod.resize(sourcePath);
if (dotProp.get(response, 'meta.error.count', 0) > 0) {
return h.response(response.meta.error).code(500);
}

return { resize: true };
} catch (error) {
return error;
}
}

const register = (server) => {
server.route({
Expand All @@ -21,19 +30,14 @@ const register = (server) => {
source_path: validation.sourceFolder,
},
},
response: {
schema: {
resize: validation.resize,
},
},
},
});
};

const plugin = {
register,
name: 'resize',
version: '0.3.0',
version: '1.0.0',
};

module.exports = { plugin };
8 changes: 4 additions & 4 deletions api/server/plugins/resize/lib/resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function resize(sourcePath) {
const thumbPath = originalPath.replace('originals', 'thumbs');

if (errOrient) {
errors.push(`Original orientation write error: ${errOrient}`);
errors.push(`Resize original: ${errOrient.message}`);
}

function possibleCompletion() {
Expand All @@ -38,7 +38,7 @@ function resize(sourcePath) {
meta: {
error: {
count: errors.length,
message: errors.join('; '),
message: errors,
},
},
payload: {
Expand All @@ -56,7 +56,7 @@ function resize(sourcePath) {
.resize(photoDims.width, photoDims.height)
.write(photoPath, (errResize) => {
if (errResize) {
errors.push(`Photo resize write error: ${errResize}`);
errors.push(`Resize photo: ${errResize.message}`);
}
possibleCompletion();
});
Expand All @@ -66,7 +66,7 @@ function resize(sourcePath) {
.noProfile()
.write(thumbPath, (errResize) => {
if (errResize) {
errors.push(`Thumbnail resize write error: ${errResize}`);
errors.push(`Resize thumbnail: ${errResize.message}`);
}
possibleCompletion();
});
Expand Down
23 changes: 20 additions & 3 deletions api/server/plugins/resize/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const tape = require('tape-catch');

const existsChecker = require('../../exists/lib/exists');

tape('Verify /resize route', { skip: false }, (describe) => {
const calipers = require('calipers')('jpeg');
const hapi = require('@hapi/hapi');
Expand Down Expand Up @@ -78,16 +80,31 @@ tape('Verify /resize route', { skip: false }, (describe) => {
try {
await server.register(plugins);
const response = await server.inject(request);

assert.ok(response, 'Has response');

if (response.statusCode !== 200) {
assert.equal(response.statusCode, 200, JSON.stringify(response.result));
}

await existsChecker.pathExists(originalRelativeFile);
assert.ok(originalRelativeFile, `Original image file found at relative ${originalRelativeFile}`);

const originalAbsoluteFile = utils.file.safePublicPath(originalRelativeFile);
await existsChecker.pathExists(originalAbsoluteFile);
assert.ok(originalAbsoluteFile, `Original image file found at ${originalAbsoluteFile}`);

const photoPath = originalAbsoluteFile.replace(ORIGINAL_FOLDER_NAME, PHOTO_FOLDER_NAME);
await existsChecker.pathExists(photoPath);
assert.ok(originalRelativeFile, `Photo image file found at ${photoPath}`);

const resultsPhoto = await calipers.measure(photoPath);
assert.equal(resultsPhoto.pages[0].width, photoDims.width, 'Photo width measured');
assert.ok(resultsPhoto.pages[0].height <= photoDims.height, 'Photo height measured');
assert.equal(resultsPhoto.pages[0].width, photoDims.width, `Photo width measured at ${photoDims.width}`);
assert.ok(resultsPhoto.pages[0].height <= photoDims.height, `Photo height measured at ${photoDims.height}`);

const thumbPath = originalAbsoluteFile.replace(ORIGINAL_FOLDER_NAME, THUMB_FOLDER_NAME);
await existsChecker.pathExists(thumbPath);
assert.ok(originalRelativeFile, `Thumb image file found at ${thumbPath}`);

const resultsThumb = await calipers.measure(thumbPath);
assert.equal(resultsThumb.pages[0].width, thumbDims.width, 'Thumb width measured');
assert.ok(resultsThumb.pages[0].height <= thumbDims.height, 'Thumb height measured');
Expand Down
35 changes: 30 additions & 5 deletions api/server/plugins/resize/test/resize.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global JSON, require */
const tape = require('tape-catch');
const existsChecker = require('../../exists/lib/exists');

tape('Verify resize library', { skip: false }, (describe) => {
const calipers = require('calipers')('jpeg');
Expand Down Expand Up @@ -38,24 +39,48 @@ tape('Verify resize library', { skip: false }, (describe) => {

describe.test('* Resize JPEG file to photo and thumb to parent folder', { skip: false }, async (assert) => {
const originalRelativeFile = path.join(fixturesPath, 'originals/2016-07-12.jpg');
await plugin.resize(originalRelativeFile);
const originalAbsoluteFile = utils.file.safePublicPath(originalRelativeFile);

try {
await existsChecker.pathExists(originalRelativeFile);

assert.ok(originalRelativeFile, `Original image file found at relative ${originalRelativeFile}`);

await plugin.resize(originalRelativeFile);

await existsChecker.pathExists(originalAbsoluteFile);
assert.ok(originalAbsoluteFile, `Original image file found at ${originalAbsoluteFile}`);
} catch (error) {
assert.fail(`Original resize failed ${error}`);
}

try {
const originalAbsoluteFile = utils.file.safePublicPath(originalRelativeFile);
const photoPath = originalAbsoluteFile.replace(ORIGINAL_FOLDER_NAME, PHOTO_FOLDER_NAME);

await existsChecker.pathExists(photoPath);
assert.ok(originalRelativeFile, `Photo image file found at ${photoPath}`);

const resultPhoto = await calipers.measure(photoPath);
assert.equal(resultPhoto.pages[0].width, photoDims.width, 'Photo width measured');
assert.ok(resultPhoto.pages[0].height <= photoDims.height, 'Photo height measured');
assert.equal(resultPhoto.pages[0].width, photoDims.width, `Photo width measured at ${photoDims.width}`);
assert.ok(resultPhoto.pages[0].height <= photoDims.height, `Photo height measured at ${photoDims.height}`);

fs.unlink(photoPath, () => assert.pass('Clean up photo'));
} catch (error) {
assert.fail(`Photo resize failed ${error}`);
}

try {
const thumbPath = originalAbsoluteFile.replace(ORIGINAL_FOLDER_NAME, THUMB_FOLDER_NAME);

await existsChecker.pathExists(thumbPath);
assert.ok(originalRelativeFile, `Thumb image file found at ${thumbPath}`);

const resultThumb = await calipers.measure(thumbPath);
assert.equal(resultThumb.pages[0].width, thumbDims.width, 'Thumb width measured');
assert.ok(resultThumb.pages[0].height <= thumbDims.height, 'Thumb height measured');
fs.unlink(thumbPath, () => assert.pass('Clean up thumb'));
} catch (error) {
assert.fail(`Resize failed ${error}`);
assert.fail(`Thumbnail resize failed ${error}`);
}

assert.end();
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
version: '3'
services:
web:
api:
build: api
ports:
- "8000:8000"
volumes:
- ./config.json:/app/config.json:ro
- ./public:/app/public:ro
- ./public:/app/public:ro # Unit tests need no readonly mode so remove `:ro`
ui:
build: ui
ports:
Expand Down

0 comments on commit cebf03f

Please sign in to comment.