Files
cloudron-box/src/routes/test/backups-test.js
T

143 lines
4.6 KiB
JavaScript
Raw Normal View History

/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict';
var appdb = require('../../appdb.js'),
async = require('async'),
2019-07-25 15:43:51 -07:00
constants = require('../../constants.js'),
database = require('../../database.js'),
2018-01-26 21:31:04 +01:00
domains = require('../../domains.js'),
expect = require('expect.js'),
2017-04-24 15:37:03 -07:00
nock = require('nock'),
2015-12-15 09:12:52 -08:00
superagent = require('superagent'),
server = require('../../server.js'),
2018-01-24 14:58:37 -08:00
settings = require('../../settings.js');
2019-07-25 15:43:51 -07:00
const SERVER_URL = 'http://localhost:' + constants.PORT;
2018-01-26 21:31:04 +01:00
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',
2018-01-31 18:09:38 +01:00
fallbackCertificate: null,
tlsConfig: { provider: 'fallback' }
2018-01-26 21:31:04 +01:00
};
2018-11-10 00:43:46 -08:00
let AUDIT_SOURCE = { ip: '1.2.3.4' };
2019-07-02 20:22:17 -07:00
var token = null;
function setup(done) {
2017-04-14 01:35:04 -07:00
nock.cleanAll();
2016-03-19 10:23:12 -07:00
async.series([
2018-01-26 21:31:04 +01:00
server.start,
2016-02-09 12:21:36 -08:00
database._clear,
2018-11-10 00:43:46 -08:00
domains.add.bind(null, DOMAIN_0.domain, DOMAIN_0, AUDIT_SOURCE),
function createAdmin(callback) {
2015-12-15 09:12:52 -08:00
superagent.post(SERVER_URL + '/api/v1/cloudron/activate')
2017-09-17 18:50:26 -07:00
.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) {
2015-07-24 06:59:34 -07:00
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);
2015-11-07 22:18:36 -08:00
},
function createSettings(callback) {
2020-05-28 21:05:21 +02:00
settings.setBackupConfig({ provider: 'filesystem', backupFolder: '/tmp', format: 'tgz', retentionPolicy: { keepWithinSecs: 2 * 24 * 60 * 60 } }, callback);
}
], done);
}
function cleanup(done) {
database._clear(function (error) {
expect(!error).to.be.ok();
server.stop(done);
});
}
describe('Backups API', function () {
before(setup);
2017-04-24 15:37:03 -07:00
after(cleanup);
describe('create', function () {
it('fails due to mising token', function (done) {
2019-04-13 18:09:04 -07:00
superagent.post(SERVER_URL + '/api/v1/backups/create')
2017-09-17 18:50:26 -07:00
.end(function (error, result) {
expect(result.statusCode).to.equal(401);
done();
});
});
it('fails due to wrong token', function (done) {
2019-04-13 18:09:04 -07:00
superagent.post(SERVER_URL + '/api/v1/backups/create')
2017-09-17 18:50:26 -07:00
.query({ access_token: token.toUpperCase() })
.end(function (error, result) {
expect(result.statusCode).to.equal(401);
done();
});
});
it('succeeds', function (done) {
2019-04-13 18:09:04 -07:00
superagent.post(SERVER_URL + '/api/v1/backups/create')
2017-09-17 18:50:26 -07:00
.query({ access_token: token })
.end(function (error, result) {
expect(result.statusCode).to.equal(202);
2018-01-18 13:56:41 -08:00
done();
2017-09-17 18:50:26 -07:00
});
});
});
2020-07-28 17:23:21 +02:00
describe('check', function () {
it('fails due to mising token', function (done) {
superagent.get(SERVER_URL + '/api/v1/backups/check')
.end(function (error, result) {
expect(result.statusCode).to.equal(401);
done();
});
});
it('fails due to wrong token', function (done) {
superagent.get(SERVER_URL + '/api/v1/backups/check')
.query({ access_token: token.toUpperCase() })
.end(function (error, result) {
expect(result.statusCode).to.equal(401);
done();
});
});
it('succeeds', function (done) {
superagent.get(SERVER_URL + '/api/v1/backups/check')
.query({ access_token: token })
.end(function (error, result) {
expect(result.statusCode).to.equal(200);
expect(result.body.ok).to.equal(false);
expect(result.body.message).to.not.be.empty();
done();
});
});
});
});