insomnia/app/common/__tests__/render.test.js

336 lines
8.2 KiB
JavaScript
Raw Normal View History

import * as renderUtils from '../render';
2016-11-10 05:56:23 +00:00
import * as models from '../../models';
jest.mock('electron');
describe('render()', () => {
it('renders hello world', async () => {
const rendered = await renderUtils.render('Hello {{ msg }}!', {msg: 'World'});
expect(rendered).toBe('Hello World!');
});
it('renders custom tag: uuid', async () => {
const rendered = await renderUtils.render('Hello {% uuid %}!');
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
expect(rendered).toMatch(/Hello [a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}!/);
});
it('renders custom tag: timestamp', async () => {
const rendered = await renderUtils.render('Hello {% timestamp %}!');
expect(rendered).toMatch(/Hello \d{13}!/);
});
it('fails on invalid template', async () => {
try {
await renderUtils.render('Hello {{ msg }!', {msg: 'World'});
fail('Render should have failed');
} catch (err) {
expect(err.message).toContain('expected variable end');
}
});
});
describe('buildRenderContext()', () => {
it('cascades properly', async () => {
const ancestors = [
{
type: models.requestGroup.type,
environment: {foo: 'parent', ancestor: true}
},
{
type: models.requestGroup.type,
environment: {foo: 'grandparent', ancestor: true}
},
];
const rootEnvironment = {
2016-11-10 01:15:27 +00:00
type: models.environment.type,
data: {foo: 'root', root: true}
};
const subEnvironment = {
2016-11-10 01:15:27 +00:00
type: models.environment.type,
data: {foo: 'sub', sub: true}
};
const context = await renderUtils.buildRenderContext(
ancestors,
rootEnvironment,
subEnvironment
);
expect(context).toEqual({
foo: 'parent',
ancestor: true,
root: true,
sub: true
});
});
it('rendered recursive should not infinite loop', async () => {
2017-01-13 19:21:03 +00:00
const ancestors = [{
// Sub Environment
type: models.requestGroup.type,
environment: {recursive: '{{ recursive }}/hello'}
}];
const context = await renderUtils.buildRenderContext(ancestors);
2017-01-13 19:21:03 +00:00
2017-02-07 16:09:12 +00:00
// This is longer than 3 because it multiplies every time (1 -> 2 -> 4 -> 8)
expect(context).toEqual({
recursive: '{{ recursive }}/hello/hello/hello/hello/hello/hello/hello/hello'
});
});
it('render up to 3 recursion levels', async () => {
2017-02-07 16:09:12 +00:00
const ancestors = [{
// Sub Environment
type: models.requestGroup.type,
environment: {
d: '/d',
c: '/c{{ d }}',
b: '/b{{ c }}',
a: '/a{{ b }}',
test: 'http://insomnia.rest{{ a }}'
}
}];
const context = await renderUtils.buildRenderContext(ancestors);
2017-02-07 16:09:12 +00:00
expect(context).toEqual({
d: '/d',
c: '/c/d',
b: '/b/c/d',
a: '/a/b/c/d',
test: 'http://insomnia.rest/a/b/c/d',
});
2017-01-20 17:51:18 +00:00
});
it('rendered sibling environment variables', async () => {
2017-01-20 17:51:18 +00:00
const ancestors = [{
// Sub Environment
type: models.requestGroup.type,
environment: {
sibling: 'sibling',
test: '{{ sibling }}/hello'
}
}];
const context = await renderUtils.buildRenderContext(ancestors);
2017-01-20 17:51:18 +00:00
expect(context).toEqual({sibling: 'sibling', test: 'sibling/hello'});
});
it('rendered parent environment variables', async () => {
2017-01-20 17:51:18 +00:00
const ancestors = [{
name: 'Parent',
type: models.requestGroup.type,
environment: {
test: '{{ grandparent }} parent'
}
}, {
name: 'Grandparent',
type: models.requestGroup.type,
environment: {
grandparent: 'grandparent'
}
}];
const context = await renderUtils.buildRenderContext(ancestors);
2017-01-20 17:51:18 +00:00
expect(context).toEqual({grandparent: 'grandparent', test: 'grandparent parent'});
});
it('rendered parent same name environment variables', async () => {
const ancestors = [{
name: 'Parent',
type: models.requestGroup.type,
environment: {
base_url: '{{ base_url }}/resource'
}
}, {
name: 'Grandparent',
type: models.requestGroup.type,
environment: {
base_url: 'https://insomnia.rest'
}
}];
const context = await renderUtils.buildRenderContext(ancestors);
expect(context).toEqual({base_url: 'https://insomnia.rest/resource'});
});
it('rendered parent, ignoring sibling environment variables', async () => {
2017-01-24 18:09:19 +00:00
const ancestors = [{
name: 'Parent',
type: models.requestGroup.type,
environment: {
host: 'parent.com',
}
}, {
name: 'Grandparent',
type: models.requestGroup.type,
environment: {
host: 'grandparent.com',
node: {
admin: 'admin',
test: 'test',
port: 8080,
},
urls: {
admin: 'https://{{ host }}/{{ node.admin }}',
test: 'https://{{ host }}/{{ node.test }}',
}
}
}];
const context = await renderUtils.buildRenderContext(ancestors);
const result = await renderUtils.render('{{ urls.admin }}/foo', context);
2017-01-24 18:09:19 +00:00
expect(result).toEqual('https://parent.com/admin/foo');
});
it('renders child environment variables', async () => {
2017-01-20 17:51:18 +00:00
const ancestors = [{
name: 'Parent',
type: models.requestGroup.type,
environment: {
parent: 'parent',
}
}, {
name: 'Grandparent',
type: models.requestGroup.type,
environment: {
test: '{{ parent }} grandparent'
}
}];
const context = await renderUtils.buildRenderContext(ancestors);
2017-01-20 17:51:18 +00:00
2017-01-24 18:09:19 +00:00
expect(context).toEqual({parent: 'parent', test: 'parent grandparent'});
2017-01-13 19:21:03 +00:00
});
it('cascades properly and renders', async () => {
const ancestors = [
{
type: models.requestGroup.type,
2017-01-13 19:21:03 +00:00
environment: {
2017-01-24 18:09:19 +00:00
url: '{{ base_url }}/resource',
2017-01-13 19:21:03 +00:00
ancestor: true,
winner: 'folder parent'
}
},
{
type: models.requestGroup.type,
2017-01-13 19:21:03 +00:00
environment: {
ancestor: true,
winner: 'folder grandparent'
}
}
];
2016-09-12 21:16:55 +00:00
2017-01-24 18:09:19 +00:00
const subEnvironment = {
2016-11-10 01:15:27 +00:00
type: models.environment.type,
2017-01-24 18:09:19 +00:00
data: {winner: 'sub', sub: true, base_url: 'https://insomnia.rest'}
2016-09-12 21:16:55 +00:00
};
2017-01-24 18:09:19 +00:00
const rootEnvironment = {
2016-11-10 01:15:27 +00:00
type: models.environment.type,
2017-01-24 18:09:19 +00:00
data: {winner: 'root', root: true, base_url: 'ignore this'}
2016-09-12 21:16:55 +00:00
};
const context = await renderUtils.buildRenderContext(ancestors,
2016-09-12 21:16:55 +00:00
rootEnvironment,
subEnvironment
);
expect(context).toEqual({
2017-01-24 18:09:19 +00:00
base_url: 'https://insomnia.rest',
url: 'https://insomnia.rest/resource',
2016-09-12 21:16:55 +00:00
ancestor: true,
2017-01-13 19:21:03 +00:00
winner: 'folder parent',
2016-09-12 21:16:55 +00:00
root: true,
sub: true
});
});
it('works with minimal parameters', async () => {
const ancestors = null;
const rootEnvironment = null;
const subEnvironment = null;
const context = await renderUtils.buildRenderContext(
ancestors,
rootEnvironment,
subEnvironment
);
expect(context).toEqual({});
});
});
describe('recursiveRender()', () => {
it('correctly renders simple Object', async () => {
const newObj = await renderUtils.recursiveRender({
foo: '{{ foo }}',
bar: 'bar',
baz: '{{ bad }}'
}, {foo: 'bar'});
expect(newObj).toEqual({
foo: 'bar',
bar: 'bar',
baz: ''
})
});
it('correctly renders complex Object', async () => {
const d = new Date();
2016-09-06 16:14:48 +00:00
const obj = {
foo: '{{ foo }}',
null: null,
bool: true,
date: d,
undef: undefined,
num: 1234,
nested: {
foo: '{{ foo }}',
arr: [1, 2, '{{ foo }}']
}
2016-09-06 16:14:48 +00:00
};
const newObj = await renderUtils.recursiveRender(obj, {foo: 'bar'});
expect(newObj).toEqual({
foo: 'bar',
null: null,
bool: true,
date: d,
undef: undefined,
num: 1234,
nested: {
foo: 'bar',
arr: [1, 2, 'bar']
}
2016-09-06 16:14:48 +00:00
});
// Make sure original request isn't changed
expect(obj.foo).toBe('{{ foo }}');
expect(obj.nested.foo).toBe('{{ foo }}');
expect(obj.nested.arr[2]).toBe('{{ foo }}');
});
it('fails on bad template', async () => {
try {
await renderUtils.recursiveRender({
foo: '{{ foo }',
bar: 'bar',
baz: '{{ bad }}'
}, {foo: 'bar'});
fail('Render should have failed');
} catch (err) {
expect(err.message).toContain('expected variable end');
}
})
});