import { describe, it, before, after } from 'mocha'; import archives from '../archives.js'; import backups from '../backups.js'; import backupSites from '../backupsites.js'; import BoxError from '../boxerror.js'; import common from './common.js'; import constants from '../constants.js'; import assert from 'node:assert/strict'; import safe from 'safetydance'; describe('backups', function () { const { setup, cleanup, auditSource, getDefaultBackupSite } = common; before(async function () { await setup(); }); after(cleanup); const backupSite = { provider: 'filesystem', name: 'Another', config: { backupDir: '/tmp/boxtest2' }, format: 'rsync', retention: { keepWithinSecs: 2 * 24 * 60 * 60 }, schedule: '00 00 23 * * *', contents: null, enableForUpdates: true }; const appBackup = { id: null, remotePath: 'backup-box', encryptionVersion: 2, packageVersion: '1.0.0', type: backups.BACKUP_TYPE_APP, state: backups.BACKUP_STATE_NORMAL, identifier: 'box', dependsOn: [ 'dep1' ], manifest: null, preserveSecs: 0, label: '', appConfig: { loc: 'loc1' }, siteId: null }; let defaultBackupSite = null; before(async function () { defaultBackupSite = await getDefaultBackupSite(); appBackup.siteId = defaultBackupSite.id; appBackup.id = await backups.add(appBackup); await archives.add(appBackup.id, {}, auditSource); }); it('can add another site', async function () { const id = await backupSites.add(backupSite, auditSource); assert.ok(id); backupSite.id = id; }); it('can list backup sites', async function () { const result = await backupSites.list(); assert.equal(result.length, 2); }); it('can get backup site', async function () { const result = await backupSites.get(defaultBackupSite.id); assert.equal(result.provider, 'filesystem'); assert.ok(result.config.backupDir); // the test sets this to some tmp location assert.equal(result.format, 'tgz'); assert.equal(result.encryption, null); assert.equal(result.contents, null); assert.equal(result.enableForUpdates, true); }); it('cannot get random backup site', async function () { const result = await backupSites.get('random'); assert.equal(result, null); }); it('can set backup config', async function () { const newConfig = Object.assign({}, defaultBackupSite.config, { backupDir: '/tmp/backups' }); await backupSites.setConfig(defaultBackupSite, newConfig, auditSource); const result = await backupSites.get(defaultBackupSite.id); assert.equal(result.config.backupDir, '/tmp/backups'); }); it('cannot set invalid schedule', async function () { const [error] = await safe(backupSites.setSchedule(defaultBackupSite, '', auditSource)); assert.equal(error.reason, BoxError.BAD_FIELD); }); it('can set valid schedule', async function () { for (const pattern of [ '00 * * * * *', constants.CRON_PATTERN_NEVER ]) { await backupSites.setSchedule(defaultBackupSite, pattern, auditSource); const result = await backupSites.get(defaultBackupSite.id); assert.equal(result.schedule, pattern); } }); it('cannot set invalid retention', async function () { const [error] = await safe(backupSites.setRetention(defaultBackupSite, { keepWhenever: 4 }, auditSource)); assert.equal(error.reason, BoxError.BAD_FIELD); }); it('can set valid retention', async function () { for (const retention of [ { keepWithinSecs: 1 }, { keepYearly: 3 }, { keepMonthly: 14 } ]) { await backupSites.setRetention(defaultBackupSite, retention, auditSource); const result = await backupSites.get(defaultBackupSite.id); assert.deepEqual(result.retention, retention); } }); it('cannot disable for update', async function () { await backupSites.setEnabledForUpdates(defaultBackupSite, false, auditSource); const result = await backupSites.get(defaultBackupSite.id); assert.deepEqual(result.enableForUpdates, false); }); it('can delete all the backup sites', async function () { await backupSites.del(defaultBackupSite, auditSource); backupSite.integrityKeyPair = {}; // keep removePrivateFields happy await backupSites.del(backupSite, auditSource); assert.deepEqual(await backups.listByTypePaged(backups.BACKUP_TYPE_BOX, defaultBackupSite.id, 1, 10), []); assert.deepEqual(await archives.list(1, 10), []); assert.deepEqual(await backupSites.list(), []); }); });