tests: complete common'ification of routes tests
This commit is contained in:
@@ -5,107 +5,44 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const appdb = require('../../appdb.js'),
|
||||
async = require('async'),
|
||||
constants = require('../../constants.js'),
|
||||
database = require('../../database.js'),
|
||||
domains = require('../../domains.js'),
|
||||
const common = require('./common.js'),
|
||||
expect = require('expect.js'),
|
||||
nock = require('nock'),
|
||||
superagent = require('superagent'),
|
||||
server = require('../../server.js'),
|
||||
settings = require('../../settings.js');
|
||||
|
||||
const SERVER_URL = 'http://localhost:' + constants.PORT;
|
||||
|
||||
const USERNAME = 'superadmin', PASSWORD = 'Foobar?1337', EMAIL ='silly@me.com';
|
||||
|
||||
const DOMAIN_0 = {
|
||||
domain: 'example-backups-test.com',
|
||||
zoneName: 'example-backups-test.com',
|
||||
config: {},
|
||||
provider: 'noop',
|
||||
fallbackCertificate: null,
|
||||
tlsConfig: { provider: 'fallback' }
|
||||
};
|
||||
|
||||
let AUDIT_SOURCE = { ip: '1.2.3.4' };
|
||||
|
||||
var token = null;
|
||||
|
||||
function setup(done) {
|
||||
nock.cleanAll();
|
||||
|
||||
async.series([
|
||||
server.start,
|
||||
database._clear,
|
||||
domains.add.bind(null, DOMAIN_0.domain, DOMAIN_0, AUDIT_SOURCE),
|
||||
|
||||
function createAdmin(callback) {
|
||||
superagent.post(SERVER_URL + '/api/v1/cloudron/activate')
|
||||
.query({ setupToken: 'somesetuptoken' })
|
||||
.send({ username: USERNAME, password: PASSWORD, email: EMAIL })
|
||||
.end(function (error, result) {
|
||||
expect(result).to.be.ok();
|
||||
expect(result.statusCode).to.eql(201);
|
||||
|
||||
// stash token for further use
|
||||
token = result.body.token;
|
||||
|
||||
callback();
|
||||
});
|
||||
},
|
||||
|
||||
function addApp(callback) {
|
||||
var manifest = { version: '0.0.1', manifestVersion: 1, dockerImage: 'foo', healthCheckPath: '/', httpPort: 3, title: 'ok', addons: { } };
|
||||
appdb.add('appid', 'appStoreId', manifest, 'location', DOMAIN_0.domain, [ ] /* portBindings */, { installationState: 'installed', runState: 'running', mailboxDomain: DOMAIN_0.domain }, callback);
|
||||
},
|
||||
|
||||
function createSettings(callback) {
|
||||
settings.setBackupConfig({ provider: 'filesystem', backupFolder: '/tmp', format: 'tgz', retentionPolicy: { keepWithinSecs: 2 * 24 * 60 * 60 }, schedulePattern: '00 00 23 * * *' }, callback);
|
||||
}
|
||||
], done);
|
||||
}
|
||||
|
||||
function cleanup(done) {
|
||||
database._clear(function (error) {
|
||||
expect(!error).to.be.ok();
|
||||
|
||||
server.stop(done);
|
||||
});
|
||||
}
|
||||
superagent = require('superagent');
|
||||
|
||||
describe('Backups API', function () {
|
||||
before(setup);
|
||||
const { setup, cleanup, serverUrl, owner } = common;
|
||||
|
||||
before(setup);
|
||||
after(cleanup);
|
||||
|
||||
describe('create', function () {
|
||||
it('fails due to mising token', function (done) {
|
||||
superagent.post(SERVER_URL + '/api/v1/backups/create')
|
||||
.end(function (error, result) {
|
||||
expect(result.statusCode).to.equal(401);
|
||||
done();
|
||||
});
|
||||
it('fails due to mising token', async function () {
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/backups/create`)
|
||||
.ok(() => true);
|
||||
expect(response.statusCode).to.equal(401);
|
||||
});
|
||||
|
||||
it('fails due to wrong token', function (done) {
|
||||
superagent.post(SERVER_URL + '/api/v1/backups/create')
|
||||
.query({ access_token: token.toUpperCase() })
|
||||
.end(function (error, result) {
|
||||
expect(result.statusCode).to.equal(401);
|
||||
done();
|
||||
});
|
||||
it('fails due to wrong token', async function () {
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/backups/create`)
|
||||
.query({ access_token: 'randomtoken' })
|
||||
.ok(() => true);
|
||||
expect(response.statusCode).to.equal(401);
|
||||
});
|
||||
|
||||
it('succeeds', function (done) {
|
||||
superagent.post(SERVER_URL + '/api/v1/backups/create')
|
||||
.query({ access_token: token })
|
||||
.end(function (error, result) {
|
||||
expect(result.statusCode).to.equal(202);
|
||||
it('succeeds', async function () {
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/backups/create`)
|
||||
.query({ access_token: owner.token });
|
||||
expect(response.statusCode).to.equal(202);
|
||||
expect(response.body.taskId).to.be.a('string');
|
||||
});
|
||||
});
|
||||
|
||||
done();
|
||||
});
|
||||
describe('list', function () {
|
||||
it('succeeds', async function () {
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/backups`)
|
||||
.query({ access_token: owner.token });
|
||||
expect(response.statusCode).to.equal(200);
|
||||
expect(response.body.backups).to.be.an('array');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user