backups: split config and policy
keeping them together makes the test/validation quite complicated. for example, when policy is changed, we test the storage backends part of #817
This commit is contained in:
+21
-3
@@ -72,7 +72,6 @@ async function setBackupConfig(req, res, next) {
|
||||
assert.strictEqual(typeof req.body, 'object');
|
||||
|
||||
if (typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
|
||||
if (typeof req.body.schedulePattern !== 'string') return next(new HttpError(400, 'schedulePattern is required'));
|
||||
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password must be a string'));
|
||||
if ('encryptedFilenames' in req.body && typeof req.body.encryptedFilenames !== 'boolean') return next(new HttpError(400, 'encryptedFilenames must be a boolean'));
|
||||
|
||||
@@ -101,8 +100,6 @@ async function setBackupConfig(req, res, next) {
|
||||
if (typeof req.body.format !== 'string') return next(new HttpError(400, 'format must be a string'));
|
||||
if ('acceptSelfSignedCerts' in req.body && typeof req.body.acceptSelfSignedCerts !== 'boolean') return next(new HttpError(400, 'format must be a boolean'));
|
||||
|
||||
if (!req.body.retentionPolicy || typeof req.body.retentionPolicy !== 'object') return next(new HttpError(400, 'retentionPolicy is required'));
|
||||
|
||||
if ('mountOptions' in req.body && typeof req.body.mountOptions !== 'object') return next(new HttpError(400, 'mountOptions must be a object'));
|
||||
|
||||
// testing the backup using put/del takes a bit of time at times
|
||||
@@ -177,6 +174,25 @@ async function setDynamicDnsConfig(req, res, next) {
|
||||
next(new HttpSuccess(200, {}));
|
||||
}
|
||||
|
||||
async function getBackupPolicy(req, res, next) {
|
||||
const [error, policy] = await safe(settings.getBackupPolicy());
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { policy }));
|
||||
}
|
||||
|
||||
async function setBackupPolicy(req, res, next) {
|
||||
assert.strictEqual(typeof req.body, 'object');
|
||||
|
||||
if (typeof req.body.schedule !== 'string') return next(new HttpError(400, 'schedule is required'));
|
||||
if (!req.body.retention || typeof req.body.retention !== 'object') return next(new HttpError(400, 'retention is required'));
|
||||
|
||||
const [error] = await safe(settings.setBackupPolicy(req.body));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, {}));
|
||||
}
|
||||
|
||||
async function getIPv6Config(req, res, next) {
|
||||
const [error, ipv6Config] = await safe(settings.getIPv6Config());
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
@@ -296,6 +312,7 @@ function get(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.setting, 'string');
|
||||
|
||||
switch (req.params.setting) {
|
||||
case settings.BACKUP_POLICY_KEY: return getBackupPolicy(req, res, next);
|
||||
case settings.DYNAMIC_DNS_KEY: return getDynamicDnsConfig(req, res, next);
|
||||
case settings.IPV6_CONFIG_KEY: return getIPv6Config(req, res, next);
|
||||
case settings.BACKUP_CONFIG_KEY: return getBackupConfig(req, res, next);
|
||||
@@ -320,6 +337,7 @@ function set(req, res, next) {
|
||||
assert.strictEqual(typeof req.body, 'object');
|
||||
|
||||
switch (req.params.setting) {
|
||||
case settings.BACKUP_POLICY_KEY: return setBackupPolicy(req, res, next);
|
||||
case settings.DYNAMIC_DNS_KEY: return setDynamicDnsConfig(req, res, next);
|
||||
case settings.IPV6_CONFIG_KEY: return setIPv6Config(req, res, next);
|
||||
case settings.EXTERNAL_LDAP_KEY: return setExternalLdapConfig(req, res, next);
|
||||
|
||||
@@ -23,8 +23,6 @@ describe('Backups API', function () {
|
||||
backupFolder: '/tmp/backups',
|
||||
format: 'tgz',
|
||||
encryption: null,
|
||||
retentionPolicy: { keepWithinSecs: 2 * 24 * 60 * 60 }, // 2 days
|
||||
schedulePattern: '00 00 23 * * *' // every day at 11pm
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -169,6 +169,97 @@ describe('Settings API', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('backup_policy', function () {
|
||||
const defaultPolicy = {
|
||||
retention: { keepWithinSecs: 2 * 24 * 60 * 60 }, // 2 days
|
||||
schedule: '00 00 23 * * *' // every day at 11pm
|
||||
};
|
||||
|
||||
it('cannot set backup_policy without schedule', async function () {
|
||||
const tmp = Object.assign({} , defaultPolicy);
|
||||
delete tmp.schedule;
|
||||
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/settings/backup_policy`)
|
||||
.query({ access_token: owner.token })
|
||||
.send(tmp)
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_policy with invalid schedule', async function () {
|
||||
const tmp = Object.assign({} , defaultPolicy);
|
||||
tmp.schedule = 'not a pattern';
|
||||
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/settings/backup_policy`)
|
||||
.query({ access_token: owner.token })
|
||||
.send(tmp)
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_policy without retention', async function () {
|
||||
const tmp = Object.assign({} , defaultPolicy);
|
||||
delete tmp.retention;
|
||||
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/settings/backup_policy`)
|
||||
.query({ access_token: owner.token })
|
||||
.send(tmp)
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_policy with invalid retention', async function () {
|
||||
const tmp = Object.assign({} , defaultPolicy);
|
||||
tmp.retention = 'not an object';
|
||||
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/settings/backup_policy`)
|
||||
.query({ access_token: owner.token })
|
||||
.send(tmp)
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_policy with empty retention', async function () {
|
||||
const tmp = Object.assign({} , defaultPolicy);
|
||||
tmp.retention = {};
|
||||
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/settings/backup_policy`)
|
||||
.query({ access_token: owner.token })
|
||||
.send(tmp)
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_policy with retention missing properties', async function () {
|
||||
const tmp = Object.assign({} , defaultPolicy);
|
||||
tmp.retention = { foo: 'bar' };
|
||||
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/settings/backup_policy`)
|
||||
.query({ access_token: owner.token })
|
||||
.send(tmp)
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_policy with retention with invalid keepWithinSecs', async function () {
|
||||
const tmp = Object.assign({} , defaultPolicy);
|
||||
tmp.retention = { keepWithinSecs: 'not a number' };
|
||||
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/settings/backup_policy`)
|
||||
.query({ access_token: owner.token })
|
||||
.send(tmp)
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe('backup_config', function () {
|
||||
// keep in sync with defaults in settings.js
|
||||
let defaultConfig = {
|
||||
@@ -176,8 +267,6 @@ describe('Settings API', function () {
|
||||
backupFolder: '/var/backups',
|
||||
format: 'tgz',
|
||||
encryption: null,
|
||||
retentionPolicy: { keepWithinSecs: 2 * 24 * 60 * 60 }, // 2 days
|
||||
schedulePattern: '00 00 23 * * *' // every day at 11pm
|
||||
};
|
||||
|
||||
it('can get backup_config (default)', async function () {
|
||||
@@ -212,30 +301,6 @@ describe('Settings API', function () {
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_config without schedulePattern', async function () {
|
||||
let tmp = JSON.parse(JSON.stringify(defaultConfig));
|
||||
delete tmp.schedulePattern;
|
||||
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/settings/backup_config`)
|
||||
.query({ access_token: owner.token })
|
||||
.send(tmp)
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_config with invalid schedulePattern', async function () {
|
||||
let tmp = JSON.parse(JSON.stringify(defaultConfig));
|
||||
tmp.schedulePattern = 'not a pattern';
|
||||
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/settings/backup_config`)
|
||||
.query({ access_token: owner.token })
|
||||
.send(tmp)
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_config without format', async function () {
|
||||
let tmp = JSON.parse(JSON.stringify(defaultConfig));
|
||||
delete tmp.format;
|
||||
@@ -260,66 +325,6 @@ describe('Settings API', function () {
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_config without retentionPolicy', async function () {
|
||||
let tmp = JSON.parse(JSON.stringify(defaultConfig));
|
||||
delete tmp.retentionPolicy;
|
||||
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/settings/backup_config`)
|
||||
.query({ access_token: owner.token })
|
||||
.send(tmp)
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_config with invalid retentionPolicy', async function () {
|
||||
let tmp = JSON.parse(JSON.stringify(defaultConfig));
|
||||
tmp.retentionPolicy = 'not an object';
|
||||
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/settings/backup_config`)
|
||||
.query({ access_token: owner.token })
|
||||
.send(tmp)
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_config with empty retentionPolicy', async function () {
|
||||
let tmp = JSON.parse(JSON.stringify(defaultConfig));
|
||||
tmp.retentionPolicy = {};
|
||||
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/settings/backup_config`)
|
||||
.query({ access_token: owner.token })
|
||||
.send(tmp)
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_config with retentionPolicy missing properties', async function () {
|
||||
let tmp = JSON.parse(JSON.stringify(defaultConfig));
|
||||
tmp.retentionPolicy = { foo: 'bar' };
|
||||
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/settings/backup_config`)
|
||||
.query({ access_token: owner.token })
|
||||
.send(tmp)
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_config with retentionPolicy with invalid keepWithinSecs', async function () {
|
||||
let tmp = JSON.parse(JSON.stringify(defaultConfig));
|
||||
tmp.retentionPolicy = { keepWithinSecs: 'not a number' };
|
||||
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/settings/backup_config`)
|
||||
.query({ access_token: owner.token })
|
||||
.send(tmp)
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(400);
|
||||
});
|
||||
|
||||
it('cannot set backup_config with invalid password', async function () {
|
||||
let tmp = JSON.parse(JSON.stringify(defaultConfig));
|
||||
tmp.password = 1234;
|
||||
|
||||
Reference in New Issue
Block a user