Skip to content

Commit

Permalink
test/purge: assert database state after purging (#1178)
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn authored Dec 4, 2024
1 parent 911739b commit 663e3b9
Showing 1 changed file with 93 additions and 35 deletions.
128 changes: 93 additions & 35 deletions test/integration/task/purge.js
Original file line number Diff line number Diff line change
@@ -1,150 +1,208 @@
const appRoot = require('app-root-path');
const assert = require('node:assert');
const { isEmpty } = require('ramda');
const { sql } = require('slonik');
const { testTask } = require('../setup');
const { purgeTask } = require(appRoot + '/lib/task/purge');

// The basics of this task are tested here, including returning the message
// of purged forms, but the full functionality is more thoroughly tested in
// test/integration/other/form-purging.js and test/integration/other/submission-purging.js.

const withDeleteChecks = container => {
const confirm = {
form: {},
forms: {},
};

const getDbFormRow = (projectId, xmlFormName) => container.maybeOne(sql`
SELECT * FROM forms WHERE "projectId"=${projectId} AND "xmlFormId"=${xmlFormName}
`);

confirm.forms.softDeleted = async expected => {
const actual = await container.oneFirst(sql`SELECT COUNT(*) FROM forms WHERE "deletedAt" IS NOT NULL`);
assert.equal(actual, expected);
};

confirm.form.softDeleted = async (projectId, xmlFormName) => {
const maybeForm = await getDbFormRow(projectId, xmlFormName);
assert.equal(isEmpty(maybeForm), false, 'Form has been hard-deleted or never existed.');
const { deletedAt } = maybeForm.get();
assert.ok(deletedAt, 'Form exists but has not been marked as deleted.');
};

confirm.form.hardDeleted = async (projectId, xmlFormName) => {
const maybeForm = await getDbFormRow(projectId, xmlFormName);
assert.ok(isEmpty(maybeForm), 'Form should have been deleted, but still exists in DB!');
};

return { ...container, confirm };
};

const testPurgeTask = fn => testTask(container => fn(withDeleteChecks(container)));

describe('task: purge deleted resources (forms and submissions)', () => {
describe('forms', () => {
describe('force flag', () => {
it('should not purge recently deleted forms by default', testTask(({ Forms }) =>
it('should not purge recently deleted forms by default', testPurgeTask(({ confirm, Forms }) =>
Forms.getByProjectAndXmlFormId(1, 'simple')
.then((form) => Forms.del(form.get())
.then(() => purgeTask({ mode: 'forms' }))
.then((message) => {
message.should.equal('Forms purged: 0');
}))));
.then(() => confirm.form.softDeleted(1, 'simple')))
.then(() => purgeTask({ mode: 'forms' }))
.then((message) => {
message.should.equal('Forms purged: 0');
})
.then(() => confirm.form.softDeleted(1, 'simple'))));

it('should purge recently deleted form if forced', testTask(({ Forms }) =>
it('should purge recently deleted form if forced', testPurgeTask(({ confirm, Forms }) =>
Forms.getByProjectAndXmlFormId(1, 'simple')
.then((form) => Forms.del(form.get())
.then(() => purgeTask({ mode: 'forms', force: true }))
.then((message) => {
message.should.equal('Forms purged: 1');
}))));
.then(() => confirm.form.softDeleted(1, 'simple')))
.then(() => purgeTask({ mode: 'forms', force: true }))
.then((message) => {
message.should.equal('Forms purged: 1');
})
.then(() => confirm.form.hardDeleted(1, 'simple'))));

it('should return message for multiple forms purged', testTask(({ Forms }) =>
it('should return message for multiple forms purged', testPurgeTask(({ confirm, Forms }) =>
Forms.getByProjectAndXmlFormId(1, 'simple')
.then((form) => Forms.del(form.get()))
.then(() => Forms.getByProjectAndXmlFormId(1, 'withrepeat')
.then((form) => Forms.del(form.get())))
.then(() => purgeTask({ mode: 'forms', force: true })
.then((message) => {
message.should.equal('Forms purged: 2');
}))));
})
.then(() => confirm.form.hardDeleted(1, 'simple'))
.then(() => confirm.form.hardDeleted(1, 'withrepeat')))));
});

describe('form specified by formId', () => {
it('should not purge specific recently deleted form', testTask(({ Forms }) =>
it('should not purge specific recently deleted form', testPurgeTask(({ confirm, Forms }) =>
Forms.getByProjectAndXmlFormId(1, 'simple')
.then((form) => Forms.del(form.get())
.then(() => purgeTask({ mode: 'forms', force: false, formId: 1 }))
.then((message) => {
message.should.equal('Forms purged: 0');
}))));
})
.then(() => confirm.form.softDeleted(1, 'simple')))));

it('should purge specific recently deleted form if forced', testTask(({ Forms }) =>
it('should purge specific recently deleted form if forced', testPurgeTask(({ confirm, Forms }) =>
Forms.getByProjectAndXmlFormId(1, 'simple')
.then((form) => Forms.del(form.get())
.then(() => purgeTask({ mode: 'forms', force: true, formId: 1 }))
.then((message) => {
message.should.equal('Forms purged: 1');
}))));
})
.then(() => confirm.form.hardDeleted(1, 'simple')))));

it('should force purge only specific form', testTask(({ Forms }) =>
it('should force purge only specific form', testPurgeTask(({ confirm, Forms }) =>
Forms.getByProjectAndXmlFormId(1, 'simple')
.then((form) => Forms.del(form.get()))
.then(() => Forms.getByProjectAndXmlFormId(1, 'withrepeat')
.then((form) => Forms.del(form.get())
.then(() => purgeTask({ mode: 'forms', force: true, formId: 1 }))
.then((message) => {
message.should.equal('Forms purged: 1');
})))));
})
.then(() => confirm.form.hardDeleted(1, 'simple'))
.then(() => confirm.form.softDeleted(1, 'withrepeat'))))));
});

describe('form specified with projectId', () => {
it('should not purge recently deleted forms even if projectId is matched (when not forced', testTask(({ Forms }) =>
it('should not purge recently deleted forms even if projectId is matched (when not forced', testPurgeTask(({ confirm, Forms }) =>
Forms.getByProjectAndXmlFormId(1, 'simple')
.then((form) => Forms.del(form.get())
.then(() => purgeTask({ mode: 'forms', projectId: 1 }))
.then((message) => {
message.should.equal('Forms purged: 0');
}))));
})
.then(() => confirm.form.softDeleted(1, 'simple')))));

it('should not purge recently deleted forms even if projectId AND formId is matched (when not forced)', testTask(({ Forms }) =>
it('should not purge recently deleted forms even if projectId AND formId is matched (when not forced)', testPurgeTask(({ confirm, Forms }) =>
Forms.getByProjectAndXmlFormId(1, 'simple')
.then((form) => Forms.del(form.get())
.then(() => purgeTask({ mode: 'forms', projectId: 1, formId: 1 }))
.then((message) => {
message.should.equal('Forms purged: 0');
}))));
})
.then(() => confirm.form.softDeleted(1, 'simple')))));

it('should purge specific form', testTask(({ Forms }) =>
it('should purge specific form', testPurgeTask(({ confirm, Forms }) =>
Forms.getByProjectAndXmlFormId(1, 'simple')
.then((form) => Forms.del(form.get()))
.then(() => Forms.getByProjectAndXmlFormId(1, 'withrepeat')
.then((form) => Forms.del(form.get())
.then(() => purgeTask({ mode: 'forms', force: true, projectId: 1, formId: 1 }))
.then((message) => {
message.should.equal('Forms purged: 1');
})))));
})
.then(() => confirm.form.hardDeleted(1, 'simple'))
.then(() => confirm.form.softDeleted(1, 'withrepeat'))))));

it('should not purge specific form if tied to a different project', testTask(({ Forms }) =>
it('should not purge specific form if tied to a different project', testPurgeTask(({ confirm, Forms }) =>
Forms.getByProjectAndXmlFormId(1, 'simple')
.then((form) => Forms.del(form.get()))
.then(() => Forms.getByProjectAndXmlFormId(1, 'withrepeat')
.then((form) => Forms.del(form.get())
.then(() => purgeTask({ mode: 'forms', force: true, projectId: 2, formId: 1 }))
.then((message) => {
message.should.equal('Forms purged: 0');
})))));
})
.then(() => confirm.form.softDeleted(1, 'simple'))))));

it('should purge all forms in project if no form ID supplied', testTask(({ Forms }) =>
it('should purge all forms in project if no form ID supplied', testPurgeTask(({ confirm, Forms }) =>
Forms.getByProjectAndXmlFormId(1, 'simple')
.then((form) => Forms.del(form.get()))
.then(() => Forms.getByProjectAndXmlFormId(1, 'withrepeat')
.then((form) => Forms.del(form.get())
.then(() => purgeTask({ mode: 'forms', force: true, projectId: 1 }))
.then((message) => {
message.should.equal('Forms purged: 2');
})))));
})
.then(() => confirm.form.hardDeleted(1, 'simple'))
.then(() => confirm.form.hardDeleted(1, 'withrepeat'))))));

it('should not purge multiple forms if tied to a different project', testTask(({ Forms }) =>
it('should not purge multiple forms if tied to a different project', testPurgeTask(({ confirm, Forms }) =>
Forms.getByProjectAndXmlFormId(1, 'simple')
.then((form) => Forms.del(form.get()))
.then(() => Forms.getByProjectAndXmlFormId(1, 'withrepeat')
.then((form) => Forms.del(form.get())
.then(() => purgeTask({ mode: 'forms', force: true, projectId: 2 }))
.then((message) => {
message.should.equal('Forms purged: 0');
})))));
})
.then(() => confirm.forms.softDeleted(2))
.then(() => confirm.form.softDeleted(1, 'simple'))
.then(() => confirm.form.softDeleted(1, 'withrepeat'))))));
});

describe('with xmlFormId', () => {
it('should throw error if xmlFormId specified without projectId', testTask(async ({ Forms }) => {
it('should throw error if xmlFormId specified without projectId', testPurgeTask(async ({ confirm, Forms }) => {
const form = await Forms.getByProjectAndXmlFormId(1, 'simple');
await Forms.del(form.get());
const message = await purgeTask({ mode: 'forms', force: true, xmlFormId: 'simple' });
message.should.equal('Must also specify projectId when using xmlFormId');
await confirm.form.softDeleted(1, 'simple');
}));

it('should force purge form by project and xmlFormId', testTask(({ Forms }) =>
it('should force purge form by project and xmlFormId', testPurgeTask(({ confirm, Forms }) =>
Forms.getByProjectAndXmlFormId(1, 'simple')
.then((form) => Forms.del(form.get())
.then(() => purgeTask({ mode: 'forms', force: true, projectId: 1, xmlFormId: 'simple' }))
.then((message) => {
message.should.equal('Forms purged: 1');
}))));
})
.then(() => confirm.form.hardDeleted(1, 'simple')))));

it('should not purge form by project and xmlFormId if form deleted recently and not forced', testTask(({ Forms }) =>
it('should not purge form by project and xmlFormId if form deleted recently and not forced', testPurgeTask(({ confirm, Forms }) =>
Forms.getByProjectAndXmlFormId(1, 'simple')
.then((form) => Forms.del(form.get())
.then(() => purgeTask({ mode: 'forms', force: false, projectId: 1, xmlFormId: 'simple' }))
.then((message) => {
message.should.equal('Forms purged: 0');
}))));
})
.then(() => confirm.form.softDeleted(1, 'simple')))));
});
});

Expand Down

0 comments on commit 663e3b9

Please sign in to comment.