Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clean deleted responses task on startup, and add tests #1292

Merged
merged 5 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions packages/insomnia-app/app/models/__tests__/response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ describe('migrate()', () => {
jest.useFakeTimers();
});

afterEach(async () => {
// Reset to real timers so that other test suites don't fail.
jest.useRealTimers();
});

it('migrates utf8 body correctly', async () => {
const initialModel = { body: 'hello world!', encoding: 'utf8' };

Expand Down Expand Up @@ -134,3 +139,89 @@ describe('migrate()', () => {
).toBe('zip');
});
});

describe('cleanDeletedResponses()', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on these tests 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is my first time working with Jest and mocking functions seems to have many gotchas, such as affecting other test suites. I spent a few hours debugging them 😆.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, it can be tricky sometimes for sure. I'm quite the fan of Jest though as far as JS test frameworks go 💯

beforeEach(globalBeforeEach);
afterEach(function() {
jest.restoreAllMocks();
});

it('deletes nothing if there is no files in directory', async function() {
const mockReaddirSync = jest.spyOn(fs, 'readdirSync');
const mockUnlinkSync = jest.spyOn(fs, 'unlinkSync');
mockReaddirSync.mockReturnValueOnce([]);
mockUnlinkSync.mockImplementation();

await models.response.cleanDeletedResponses();

expect(fs.unlinkSync.mock.calls.length).toBe(0);
});

it('only deletes response files that are not in db', async function() {
const responsesDir = path.join(getDataDirectory(), 'responses');
let dbResponseIds = await createModels(responsesDir, 10);
let notDbResponseIds = [];
for (let index = 100; index < 110; index++) {
notDbResponseIds.push('res_' + index);
}

const mockReaddirSync = jest.spyOn(fs, 'readdirSync');
const mockUnlinkSync = jest.spyOn(fs, 'unlinkSync');
mockReaddirSync.mockReturnValueOnce([...dbResponseIds, ...notDbResponseIds]);
mockUnlinkSync.mockImplementation();

await models.response.cleanDeletedResponses();

expect(fs.unlinkSync.mock.calls.length).toBe(notDbResponseIds.length);
Object.keys(notDbResponseIds).map(index => {
const resId = notDbResponseIds[index];
const bodyPath = path.join(responsesDir, resId);
expect(fs.unlinkSync.mock.calls[index][0]).toBe(bodyPath);
});
});
});

/**
* Create mock workspaces, requests, and responses as many as {@code count}.
* @param responsesDir
* @param count
* @returns {Promise<string[]>} the created response ids
*/
async function createModels(responsesDir, count) {
if (count < 1) {
return [];
}

let responseIds = [];

for (let index = 0; index < count; index++) {
const workspaceId = 'wrk_' + index;
const requestId = 'req_' + index;
const responseId = 'res_' + index;

await models.workspace.create({
_id: workspaceId,
created: 111,
modified: 222
});
await models.request.create({
_id: requestId,
parentId: workspaceId,
created: 111,
modified: 222,
metaSortKey: 0,
url: 'https://insomnia.rest'
});

await models.response.create({
_id: responseId,
parentId: requestId,
statusCode: 200,
body: 'foo',
bodyPath: path.join(responsesDir, responseId)
});
responseIds.push(responseId);
}

return responseIds;
}
23 changes: 23 additions & 0 deletions packages/insomnia-app/app/models/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export async function migrate(doc: Object) {
}

export async function hookDatabaseInit() {
await models.response.cleanDeletedResponses();

console.log('Init responses DB');
}

Expand Down Expand Up @@ -236,3 +238,24 @@ function migrateBodyCompression(doc: Object) {

return doc;
}

export async function cleanDeletedResponses() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful. Super clean and simple now 👍 😄

const responsesDir = path.join(getDataDirectory(), 'responses');
mkdirp.sync(responsesDir);

let files = fs.readdirSync(responsesDir);
if (files.length === 0) {
return;
}

let whitelistFiles = (await db.all(type)).map(res => {
return res.bodyPath.slice(responsesDir.length + 1);
});

for (let index = 0; index < files.length; index++) {
if (whitelistFiles.indexOf(files[index]) === -1) {
const bodyPath = path.join(responsesDir, files[index]);
fs.unlinkSync(bodyPath);
}
}
}