Refactor uploads

This commit is contained in:
Jonathan Cremin 2016-05-25 21:03:07 +01:00
parent 75a46212da
commit f59b9a5d22
14 changed files with 297 additions and 269 deletions

View file

@ -1,3 +1,4 @@
import path from 'path';
import assert from 'assert';
import { agent } from 'supertest';
import app from '../../app';
@ -25,11 +26,11 @@ describe('hostr-api file', function file() {
this.timeout(30000);
request
.post('/api/file')
.attach('file', './test/fixtures/utah-arches.jpg')
.attach('file', path.join(__dirname, '..', 'fixtures', 'tall.jpg'))
.auth('test@hostr.co', 'test-password')
.expect(201)
.expect((response) => {
assert(response.body.name === 'utah-arches.jpg');
assert(response.body.name === 'tall.jpg');
id = response.body.id;
})
.end(done);
@ -42,7 +43,7 @@ describe('hostr-api file', function file() {
.get('/api/file/' + id)
.expect(200)
.expect((response) => {
assert(response.body.name === 'utah-arches.jpg');
assert(response.body.name === 'tall.jpg');
})
.end(done);
});

View file

@ -1,35 +1,35 @@
import fs from 'fs';
import path from 'path';
import fs from 'mz/fs';
import { join } from 'path';
import assert from 'assert';
import tmp from 'tmp';
import resize from '../../lib/resize';
import imageType from 'image-type';
import sizeOf from 'image-size';
function testResize(path, done) {
const size = sizeOf(path);
resize(path, size.type, size, {width: 100, height: 100}).then((image) => {
const tmpFile = tmp.tmpNameSync() + '.' + size.type;
fs.writeFile(tmpFile, image).then(() => {
const newSize = sizeOf(fs.readFileSync(tmpFile));
assert(newSize.type === size.type);
done();
});
});
}
describe('Image resizing', () => {
it('should resize a jpg', function* resizeImage() {
const file = fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'utah-arches.jpg'));
const imageBuffer = yield resize(file, {height: 100, width: 100});
const tmpFile = tmp.tmpNameSync() + '.jpg';
fs.writeFileSync(tmpFile, imageBuffer);
const type = imageType(fs.readFileSync(tmpFile));
assert(type.ext === 'jpg');
it('should resize a jpg', (done) => {
const path = join(__dirname, '..', 'fixtures', 'utah-arches.jpg');
testResize(path, done);
});
it('should resize a png', function* resizeImage() {
const file = fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'app-icon.png'));
const imageBuffer = yield resize(file, {height: 100, width: 100});
const tmpFile = tmp.tmpNameSync() + '.png';
fs.writeFileSync(tmpFile, imageBuffer);
const type = imageType(fs.readFileSync(tmpFile));
assert(type.ext === 'png');
it('should resize a png', (done) => {
const path = join(__dirname, '..', 'fixtures', 'app-icon.png');
testResize(path, done);
});
it('should resize a gif', function* resizeImage() {
const file = fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'kim.gif'));
const imageBuffer = yield resize(file, {height: 100, width: 100});
const tmpFile = tmp.tmpNameSync() + '.gif';
fs.writeFileSync(tmpFile, imageBuffer);
const type = imageType(fs.readFileSync(tmpFile));
assert(type.ext === 'gif');
it('should resize a gif', (done) => {
const path = join(__dirname, '..', 'fixtures', 'kim.gif');
testResize(path, done);
});
});

View file

@ -1,3 +1,4 @@
import path from 'path';
import assert from 'assert';
import sizeOf from 'image-size';
import { agent } from 'supertest';
@ -12,7 +13,7 @@ describe('setup hostr-web file', function() {
this.timeout(30000);
request
.post('/api/file')
.attach('file', 'test/fixtures/utah-arches.jpg')
.attach('file', path.join(__dirname, '..', 'fixtures', 'utah-arches.jpg'))
.auth('test@hostr.co', 'test-password')
.expect(201)
.expect(function(response) {
@ -25,7 +26,6 @@ describe('setup hostr-web file', function() {
});
describe('hostr-web file', function() {
describe('when GET /file/:id/:name', function() {
it('should receive an image', function(done) {
request
@ -40,41 +40,40 @@ describe('hostr-web file', function() {
});
describe('when GET /file/150/:id/:name', function() {
it('should receive a 150px wide thumbnail of the image', function(done) {
it('should receive a 150px wide thumbnail of the image', function() {
request
.get('/file/150/' + file.id + '/' + file.name)
.expect(200)
.expect('Content-type', 'image/jpeg')
.expect(function(response) {
assert(sizeOf(response.body).width === 150);
})
.end(done);
const width = sizeOf(response.body).width;
assert(width === 150);
});
});
});
describe('when GET /file/970/:id/:name', function() {
it('should receive a 970px wide thumbnail of the image', function(done) {
it('should receive a 970px wide thumbnail of the image', function() {
request
.get('/file/970/' + file.id + '/' + file.name)
.expect(200)
.expect('Content-type', 'image/jpeg')
.expect(function(response) {
assert(sizeOf(response.body).width === 970);
})
.end(done);
const width = sizeOf(response.body).width;
assert(width === 970);
});
});
});
describe('when GET /:id', function() {
it('should receive some HTML', function(done) {
it('should receive some HTML', function() {
request
.get('/' + file.id)
.expect(200)
.expect('Content-type', /text\/html/) // Could include charset
.expect(function(response) {
assert(response.text.indexOf('src="/file/970/' + file.id + '/' + file.name + '"') > -1);
})
.end(done);
});
});
});

View file

@ -5,20 +5,20 @@ const request = agent(app.listen());
describe('hostr-web user', function() {
describe('when POST /signin with invalid credentials', function() {
it('should not redirect to /', function(done) {
it('should not redirect to /', function() {
request.get('/signin').end(function(err, response) {
const match = response.text.match(/name="_csrf" value="([^"]+)"/);
const csrf = match[1];
request
.post('/signin')
.send({'email': 'test@hostr.co', 'password': 'test-passworddeded', '_csrf': csrf})
.expect(200, done);
.expect(200);
});
});
});
describe('when POST /signin with valid credentials', function() {
it('should redirect to /', function(done) {
it('should redirect to /', function() {
request.get('/signin').end(function(err, response) {
const match = response.text.match(/name="_csrf" value="([^"]+)"/);
const csrf = match[1];
@ -27,7 +27,7 @@ describe('hostr-web user', function() {
.send({'email': 'test@hostr.co', 'password': 'test-password', '_csrf': csrf})
.expect(302)
.expect('Location', '/')
.end(done);
.end();
});
});
});