2017-06-30 03:30:22 +00:00
|
|
|
import path from 'path';
|
|
|
|
import * as electron from 'electron';
|
|
|
|
import * as models from '../../models';
|
2017-07-20 03:36:44 +00:00
|
|
|
import {globalBeforeEach} from '../../__jest__/before-each';
|
2017-06-30 03:30:22 +00:00
|
|
|
|
|
|
|
describe('migrate()', () => {
|
2017-07-20 01:55:40 +00:00
|
|
|
beforeEach(async () => {
|
2017-07-20 03:36:44 +00:00
|
|
|
await globalBeforeEach();
|
2017-06-30 03:30:22 +00:00
|
|
|
Date.now = jest.genMockFunction().mockReturnValue(1234567890);
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('migrates utf8 body correctly', async () => {
|
|
|
|
const initialModel = {body: 'hello world!', encoding: 'utf8'};
|
|
|
|
|
2017-09-13 06:11:49 +00:00
|
|
|
const newModel = await models.initModel(models.response.type, initialModel);
|
2017-06-30 03:30:22 +00:00
|
|
|
const expectedBodyPath = path.join(
|
|
|
|
electron.remote.app.getPath('userData'),
|
|
|
|
`responses/fc3ff98e8c6a0d3087d515c0473f8677.zip`
|
|
|
|
);
|
|
|
|
const storedBody = models.response.getBodyBuffer({bodyPath: expectedBodyPath});
|
|
|
|
|
|
|
|
// Should have set bodyPath and stored the body
|
|
|
|
expect(newModel.bodyPath).toBe(expectedBodyPath);
|
|
|
|
expect(storedBody + '').toBe('hello world!');
|
2017-08-23 00:06:56 +00:00
|
|
|
|
|
|
|
// Should have stripped these
|
|
|
|
expect(newModel.body).toBeUndefined();
|
|
|
|
expect(newModel.encoding).toBeUndefined();
|
2017-06-30 03:30:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('migrates base64 body correctly', async () => {
|
|
|
|
const initialModel = {body: 'aGVsbG8gd29ybGQh', encoding: 'base64'};
|
|
|
|
|
2017-09-13 06:11:49 +00:00
|
|
|
const newModel = await models.initModel(models.response.type, initialModel);
|
2017-06-30 03:30:22 +00:00
|
|
|
jest.runAllTimers();
|
|
|
|
const expectedBodyPath = path.join(
|
|
|
|
electron.remote.app.getPath('userData'),
|
|
|
|
`responses/fc3ff98e8c6a0d3087d515c0473f8677.zip`
|
|
|
|
);
|
|
|
|
const storedBody = models.response.getBodyBuffer({bodyPath: expectedBodyPath});
|
|
|
|
|
|
|
|
// Should have stripped these
|
|
|
|
expect(newModel.body).toBeUndefined();
|
|
|
|
expect(newModel.encoding).toBeUndefined();
|
|
|
|
|
|
|
|
// Should have set bodyPath and stored the body
|
|
|
|
expect(newModel.bodyPath).toBe(expectedBodyPath);
|
|
|
|
expect(storedBody + '').toBe('hello world!');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('migrates empty body', async () => {
|
|
|
|
const initialModel = {body: ''};
|
|
|
|
|
2017-09-13 06:11:49 +00:00
|
|
|
const newModel = await models.initModel(models.response.type, initialModel);
|
2017-06-30 03:30:22 +00:00
|
|
|
jest.runAllTimers();
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
const expectedBodyPath = path.join(
|
|
|
|
electron.remote.app.getPath('userData'),
|
|
|
|
'responses/d41d8cd98f00b204e9800998ecf8427e.zip'
|
|
|
|
);
|
|
|
|
const storedBody = models.response.getBodyBuffer({bodyPath: expectedBodyPath});
|
|
|
|
|
|
|
|
// Should have stripped these
|
|
|
|
expect(newModel.body).toBeUndefined();
|
|
|
|
expect(newModel.encoding).toBeUndefined();
|
|
|
|
|
|
|
|
// Should have set bodyPath and stored the body
|
|
|
|
expect(newModel.bodyPath).toBe(expectedBodyPath);
|
|
|
|
expect(storedBody + '').toBe('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not migrate body again', async () => {
|
|
|
|
const initialModel = {bodyPath: '/foo/bar'};
|
|
|
|
|
2017-09-13 06:11:49 +00:00
|
|
|
const newModel = await models.initModel(models.response.type, initialModel);
|
2017-06-30 03:30:22 +00:00
|
|
|
|
|
|
|
// Should have stripped these
|
|
|
|
expect(newModel.body).toBeUndefined();
|
|
|
|
expect(newModel.encoding).toBeUndefined();
|
|
|
|
|
|
|
|
// Should have set bodyPath and stored the body
|
|
|
|
expect(newModel.bodyPath).toBe('/foo/bar');
|
|
|
|
});
|
|
|
|
});
|