Initial commit.

This commit is contained in:
Jonathan Cremin 2015-07-09 23:01:43 +01:00
commit b48a4e92e1
169 changed files with 7538 additions and 0 deletions

6
test/.eslintrc Normal file
View file

@ -0,0 +1,6 @@
{
"env": {
"mocha": true,
"es6": true
}
}

33
test/api/auth.spec.js Normal file
View file

@ -0,0 +1,33 @@
import { agent } from 'supertest';
import app from '../../api/app';
const request = agent(app.listen());
describe('hostr-api auth', function(){
describe('with no credentials', function(){
it('should `throw` 401', function(done){
request
.get('/user')
.expect(401, done);
});
});
describe('with invalid credentials', function(){
it('should `throw` 401', function(done){
request
.get('/user')
.auth('user', 'invalid password')
.expect(401, done);
});
});
describe('with valid credentials', function(){
it('should call the next middleware', function(done){
request
.get('/')
.auth('test@hostr.co', 'test-password')
.expect(200, done);
});
});
});

68
test/api/file.spec.js Normal file
View file

@ -0,0 +1,68 @@
import assert from 'assert';
import { agent } from 'supertest';
import app from '../../api/app';
const request = agent(app.listen());
describe('hostr-api file', function() {
let id;
describe('when GET /file', function() {
it('should receive a list of file objects', function(done) {
request
.get('/file')
.auth('test@hostr.co', 'test-password')
.expect(200)
.expect(function(response) {
assert(response.body instanceof Array);
})
.end(done);
});
});
describe('when POSTing a file to /file', function() {
it('should receive a new file object', function(done) {
this.timeout(30000);
request
.post('/file')
.attach('file', 'test/fixtures/utah-arches.jpg')
.auth('test@hostr.co', 'test-password')
.expect(201)
.expect(function(response) {
assert(response.body.name === 'utah-arches.jpg');
id = response.body.id;
})
.end(done);
});
});
describe('when GET /file/:id', function() {
it('should receive the file object', function(done) {
request
.get('/file/' + id)
.expect(200)
.expect(function(response) {
assert(response.body.name === 'utah-arches.jpg');
})
.end(done);
});
});
describe('when DELETE /file/:id', function() {
it('should delete the file object', function(done) {
request
.delete('/file/' + id)
.auth('test@hostr.co', 'test-password')
.expect(200, done);
});
});
describe('when GET deleted /file/:id', function() {
it('should not receive the file object', function(done) {
request
.get('/file/' + id)
.expect(404, done);
});
});
});

62
test/api/user.spec.js Normal file
View file

@ -0,0 +1,62 @@
import assert from 'assert';
import { agent } from 'supertest';
import app from '../../api/app';
const request = agent(app.listen());
describe('hostr-api user', function() {
describe('when GET /user', function() {
it('should receive a user object', function(done) {
request
.get('/user')
.auth('test@hostr.co', 'test-password')
.expect(function(response) {
assert(response.body.id === '54fd04a37675bcd06213eac8');
})
.expect(200)
.end(done);
});
});
describe('when GET /user/token', function() {
it('should receive a user token object', function(done) {
request
.get('/user/token')
.auth('test@hostr.co', 'test-password')
.expect(function(response) {
assert(response.body.token);
})
.expect(200)
.end(done);
});
});
describe('when GET /user/transaction', function() {
it('should receive a user transactions object', function(done) {
request
.get('/user/transaction')
.auth('test@hostr.co', 'test-password')
.expect(200)
.expect(function(response) {
assert(response.body instanceof Array);
})
.end(done);
});
});
describe('when GET /user/settings', function() {
it('should update user password', function(done) {
request
.post('/user/settings')
.send({'current_password': 'test-password', 'new_password': 'test-password' })
.auth('test@hostr.co', 'test-password')
.expect(200)
.expect(function(response) {
assert(response.body instanceof Object);
})
.end(done);
});
});
});

21
test/fixtures/mongo-file.js vendored Normal file
View file

@ -0,0 +1,21 @@
db.files.createIndex({
"owner": 1,
"status": 1,
"time_added": -1
});
db.files.save({"_id": "94U1ruo7anyQ",
"owner": ObjectId("54fd04a37675bcd06213eac8"),
"system_name": "94U1ruo7anyQ",
"file_name": "utah-arches.jpg",
"original_name": "utah-arches.jpg",
"file_size": 194544,
"time_added": 1436223854,
"status": "active",
"last_accessed": null,
"s3": true,
"type": "image",
"ip": "::1",
"md5": "1f4185751b4db05494cbc0aad68d7d77",
"width": 1024,
"height": 683
});

7
test/fixtures/mongo-user.js vendored Normal file
View file

@ -0,0 +1,7 @@
db.users.save({
"_id": ObjectId("54fd04a37675bcd06213eac8"),
"email": "test@hostr.co",
"salted_password": "$pbkdf2-256-1$2$kBhIDRqFwnF/1ms6ZHfME2o2$a48e8c350d26397fcc88bf0a7a2817b1cdcd1ffffe0521a5",
"joined": 1425867940,
"signup_ip": "127.0.0.1"
});

BIN
test/fixtures/utah-arches.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 KiB

View file

@ -0,0 +1,16 @@
import fs from 'fs';
import path from 'path';
import assert from 'assert';
import tmp from 'tmp';
import resize from '../../lib/resize';
const file = fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'utah-arches.jpg'));
describe('Image resizing', function() {
it('should resize an image', function*() {
const imageBuffer = yield resize(file, {height: 100, width: 100});
const tmpFile = tmp.tmpNameSync();
fs.writeFileSync(tmpFile + '.jpg', imageBuffer);
assert(tmpFile);
});
});

102
test/web/file.spec.js Normal file
View file

@ -0,0 +1,102 @@
import web from '../../web/app';
import api from '../../api/app';
import assert from 'assert';
import { agent } from 'supertest';
const request = agent(web.listen());
const apiRequest = agent(api.listen());
let file = {};
describe('setup hostr-web file', function() {
describe('when POSTing a file to /file', function() {
it('should receive a new file object', function(done) {
this.timeout(30000);
apiRequest
.post('/file')
.attach('file', 'test/fixtures/utah-arches.jpg')
.auth('test@hostr.co', 'test-password')
.expect(201)
.expect(function(response) {
assert(response.body.name === 'utah-arches.jpg');
file = response.body;
})
.end(done);
});
});
});
describe('hostr-web file', function() {
describe('when GET /file/:id/:name', function() {
it('should receive an image', function(done) {
request
.get('/file/' + file.id + '/' + file.name)
.expect(200)
.expect('Content-type', 'image/jpeg')
.expect(function(response) {
assert(response.body.length === 194544);
})
.end(done);
});
});
describe('when GET /file/150/:id/:name', function() {
it('should receive a 150px wide thumbnail of the image', function(done) {
request
.get('/file/150/' + file.id + '/' + file.name)
.expect(200)
.expect('Content-type', 'image/jpeg')
.expect(function(response) {
assert(response.body.length === 3658);
})
.end(done);
});
});
describe('when GET /file/970/:id/:name', function() {
it('should receive a 970px wide thumbnail of the image', function(done) {
request
.get('/file/970/' + file.id + '/' + file.name)
.expect(200)
.expect('Content-type', 'image/jpeg')
.expect(function(response) {
assert(response.body.length === 79091);
})
.end(done);
});
});
describe('when GET /:id', function() {
it('should receive some HTML', function(done) {
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);
});
});
describe('when GET /file/:badid/:name', function() {
it('should receive 404 and some HTML', function(done) {
request
.get('/notarealid')
.expect(404)
.expect('Content-type', /text\/html/) // Could include charset
.end(done);
});
});
describe('when GET /:bad-id', function() {
it('should receive 404 and some HTML', function(done) {
request
.get('/file/notarealid/orname')
.expect(404)
.expect('Content-type', /text\/html/) // Could include charset
.end(done);
});
});
});

26
test/web/user.spec.js Normal file
View file

@ -0,0 +1,26 @@
import app from '../../web/app';
import { agent } from 'supertest';
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) {
request
.post('/signin')
.send({'email': 'test@hostr.co', 'password': 'test-passworddeded'})
.expect(200, done);
});
});
describe('when POST /signin with valid credentials', function() {
it('should redirect to /', function(done) {
request
.post('/signin')
.send({'email': 'test@hostr.co', 'password': 'test-password'})
.expect(302)
.expect('Location', '/')
.end(done);
});
});
});