hostr/test/api/file.spec.js

69 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2016-05-25 21:03:07 +01:00
import path from 'path';
2015-07-09 23:01:43 +01:00
import assert from 'assert';
import { agent } from 'supertest';
2015-08-22 16:16:15 +01:00
import app from '../../app';
2015-07-09 23:01:43 +01:00
const request = agent(app.listen());
2016-05-24 19:30:22 +01:00
describe('hostr-api file', function file() {
2015-07-09 23:01:43 +01:00
let id;
2016-05-24 19:30:22 +01:00
describe('when GET /file', function getFile() {
it('should receive a list of file objects', function listFiles(done) {
2015-07-09 23:01:43 +01:00
request
2015-08-22 16:16:15 +01:00
.get('/api/file')
2015-07-09 23:01:43 +01:00
.auth('test@hostr.co', 'test-password')
.expect(200)
2016-05-24 19:30:22 +01:00
.expect((response) => {
2015-07-09 23:01:43 +01:00
assert(response.body instanceof Array);
})
.end(done);
});
});
2016-05-24 19:30:22 +01:00
describe('when POSTing a file to /file', function postFile() {
it('should receive a new file object', function receiveFile(done) {
2015-07-09 23:01:43 +01:00
this.timeout(30000);
request
2015-08-22 16:16:15 +01:00
.post('/api/file')
2016-06-02 19:23:39 +01:00
.attach('file', path.join(__dirname, '..', 'fixtures', 'utah-arches.jpg'))
2015-07-09 23:01:43 +01:00
.auth('test@hostr.co', 'test-password')
.expect(201)
2016-05-24 19:30:22 +01:00
.expect((response) => {
2016-06-02 19:23:39 +01:00
assert(response.body.name === 'utah-arches.jpg');
2015-07-09 23:01:43 +01:00
id = response.body.id;
})
.end(done);
});
});
2016-05-24 19:30:22 +01:00
describe('when GET /file/:id', function getFileById() {
it('should receive the file object', function receiveFile(done) {
2015-07-09 23:01:43 +01:00
request
2015-08-22 16:16:15 +01:00
.get('/api/file/' + id)
2015-07-09 23:01:43 +01:00
.expect(200)
2016-05-24 19:30:22 +01:00
.expect((response) => {
2016-06-02 19:23:39 +01:00
assert(response.body.name === 'utah-arches.jpg');
2015-07-09 23:01:43 +01:00
})
.end(done);
});
});
2016-05-24 19:30:22 +01:00
describe('when DELETE /file/:id', function deleteFile() {
it('should delete the file object', function deleted(done) {
2015-07-09 23:01:43 +01:00
request
2015-08-22 16:16:15 +01:00
.delete('/api/file/' + id)
2015-07-09 23:01:43 +01:00
.auth('test@hostr.co', 'test-password')
2015-08-22 16:16:15 +01:00
.expect(204, done);
2015-07-09 23:01:43 +01:00
});
});
2016-05-24 19:30:22 +01:00
describe('when GET deleted /file/:id', function getDeletedFileById() {
it('should not receive the file object', function receiveFile(done) {
2015-07-09 23:01:43 +01:00
request
2015-08-22 16:16:15 +01:00
.get('/api/file/' + id)
2015-07-09 23:01:43 +01:00
.expect(404, done);
});
});
});