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

449 lines
11 KiB
JavaScript
Raw Normal View History

import * as renderUtils from '../render';
2016-11-10 05:56:23 +00:00
import * as models from '../../models';
2018-06-25 17:42:50 +00:00
import { globalBeforeEach } from '../../__jest__/before-each';
jest.mock('electron');
describe('render()', () => {
2017-07-20 03:36:44 +00:00
beforeEach(globalBeforeEach);
it('renders hello world', async () => {
2018-06-25 17:42:50 +00:00
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 %}!');
2018-06-25 17:42:50 +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 {
2018-06-25 17:42:50 +00:00
await renderUtils.render('Hello {{ msg }!', { msg: 'World' });
fail('Render should have failed');
} catch (err) {
expect(err.message).toContain('expected variable end');
}
});
});
describe('buildRenderContext()', () => {
2017-07-20 03:36:44 +00:00
beforeEach(globalBeforeEach);
it('cascades properly', async () => {
const ancestors = [
{
type: models.requestGroup.type,
2018-06-25 17:42:50 +00:00
environment: { foo: 'parent', ancestor: true }
},
{
type: models.requestGroup.type,
2018-06-25 17:42:50 +00:00
environment: { foo: 'grandparent', ancestor: true }
}
];
const rootEnvironment = {
2016-11-10 01:15:27 +00:00
type: models.environment.type,
2018-06-25 17:42:50 +00:00
data: { foo: 'root', root: true }
};
const subEnvironment = {
2016-11-10 01:15:27 +00:00
type: models.environment.type,
2018-06-25 17:42:50 +00:00
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 () => {
2018-06-25 17:42:50 +00:00
const ancestors = [
{
// Sub Environment
type: models.requestGroup.type,
environment: { recursive: '{{ recursive }}/hello' }
}
];
2017-01-13 19:21:03 +00:00
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({
2018-06-25 17:42:50 +00:00
recursive:
'{{ recursive }}/hello/hello/hello/hello/hello/hello/hello/hello'
2017-02-07 16:09:12 +00:00
});
});
it('render up to 3 recursion levels', async () => {
2018-06-25 17:42:50 +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 }}'
}
2017-02-07 16:09:12 +00:00
}
2018-06-25 17:42:50 +00:00
];
2017-02-07 16:09:12 +00:00
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-02-07 16:09:12 +00:00
});
2017-01-20 17:51:18 +00:00
});
it('rendered sibling environment variables', async () => {
2018-06-25 17:42:50 +00:00
const ancestors = [
{
// Sub Environment
type: models.requestGroup.type,
environment: {
sibling: 'sibling',
test: '{{ sibling }}/hello'
}
2017-01-20 17:51:18 +00:00
}
2018-06-25 17:42:50 +00:00
];
2017-01-20 17:51:18 +00:00
const context = await renderUtils.buildRenderContext(ancestors);
2017-01-20 17:51:18 +00:00
2018-06-25 17:42:50 +00:00
expect(context).toEqual({ sibling: 'sibling', test: 'sibling/hello' });
2017-01-20 17:51:18 +00:00
});
it('rendered parent environment variables', async () => {
2018-06-25 17:42:50 +00:00
const ancestors = [
{
name: 'Parent',
type: models.requestGroup.type,
environment: {
test: '{{ grandparent }} parent'
}
},
{
name: 'Grandparent',
type: models.requestGroup.type,
environment: {
grandparent: 'grandparent'
}
2017-01-20 17:51:18 +00:00
}
2018-06-25 17:42:50 +00:00
];
2017-01-20 17:51:18 +00:00
const context = await renderUtils.buildRenderContext(ancestors);
2017-01-20 17:51:18 +00:00
2018-06-25 17:42:50 +00:00
expect(context).toEqual({
grandparent: 'grandparent',
test: 'grandparent parent'
});
2017-01-20 17:51:18 +00:00
});
it('rendered parent same name environment variables', async () => {
2018-06-25 17:42:50 +00:00
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'
}
}
2018-06-25 17:42:50 +00:00
];
const context = await renderUtils.buildRenderContext(ancestors);
2018-06-25 17:42:50 +00:00
expect(context).toEqual({ base_url: 'https://insomnia.rest/resource' });
});
it('rendered parent, ignoring sibling environment variables', async () => {
2018-06-25 17:42:50 +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 }}'
}
2017-01-24 18:09:19 +00:00
}
}
2018-06-25 17:42:50 +00:00
];
2017-01-24 18:09:19 +00:00
const context = await renderUtils.buildRenderContext(ancestors);
2018-06-25 17:42:50 +00:00
expect(await renderUtils.render('{{ urls.admin }}/foo', context)).toBe(
'https://parent.com/admin/foo'
);
expect(await renderUtils.render('{{ urls.test }}/foo', context)).toBe(
'https://parent.com/test/foo'
);
2017-01-24 18:09:19 +00:00
});
it('renders child environment variables', async () => {
2018-06-25 17:42:50 +00:00
const ancestors = [
{
name: 'Parent',
type: models.requestGroup.type,
environment: {
parent: 'parent'
}
},
{
name: 'Grandparent',
type: models.requestGroup.type,
environment: {
test: '{{ parent }} grandparent'
}
2017-01-20 17:51:18 +00:00
}
2018-06-25 17:42:50 +00:00
];
2017-01-20 17:51:18 +00:00
const context = await renderUtils.buildRenderContext(ancestors);
2017-01-20 17:51:18 +00:00
2018-06-25 17:42:50 +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,
2018-06-25 17:42:50 +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,
2018-06-25 17:42:50 +00:00
data: { winner: 'root', root: true, base_url: 'ignore this' }
2016-09-12 21:16:55 +00:00
};
2018-06-25 17:42:50 +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('handles variables being used in tags', async () => {
const rootEnvironment = {
type: models.environment.type,
data: {
hash_input: '{{ orderId }}{{ secret }}',
hash_input_expected: '123456789012345ThisIsATopSecretValue',
orderId: 123456789012345,
password: "{% hash 'sha512', 'hex', hash_input %}",
password_expected: "{% hash 'sha512', 'hex', hash_input_expected %}",
secret: 'ThisIsATopSecretValue'
}
};
const context = await renderUtils.buildRenderContext([], rootEnvironment);
expect(context).toEqual({
hash_input: '123456789012345ThisIsATopSecretValue',
hash_input_expected: '123456789012345ThisIsATopSecretValue',
orderId: 123456789012345,
2018-06-25 17:42:50 +00:00
password:
'ea84d15f33d3f9e9098fe01659b1ea0599d345770bba20ba98bf9056676a83ffe6b5528b2451ad04badbf690cf3009a94c510121cc6897045f8bb4ba0826134c',
password_expected:
'ea84d15f33d3f9e9098fe01659b1ea0599d345770bba20ba98bf9056676a83ffe6b5528b2451ad04badbf690cf3009a94c510121cc6897045f8bb4ba0826134c',
secret: 'ThisIsATopSecretValue'
});
});
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('render()', () => {
2017-07-20 03:36:44 +00:00
beforeEach(globalBeforeEach);
it('correctly renders simple Object', async () => {
2018-06-25 17:42:50 +00:00
const newObj = await renderUtils.render(
{
foo: '{{ foo }}',
bar: 'bar',
baz: '{{ bad }}'
},
{ foo: 'bar', bad: 'hi' }
);
expect(newObj).toEqual({
foo: 'bar',
bar: 'bar',
baz: 'hi'
});
});
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
};
2018-06-25 17:42:50 +00:00
const newObj = await renderUtils.render(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 {
2018-06-25 17:42:50 +00:00
await renderUtils.render(
{
foo: '{{ foo }',
bar: 'bar',
baz: '{{ bad }}'
},
{ foo: 'bar' }
);
fail('Render should have failed');
} catch (err) {
expect(err.message).toContain('expected variable end');
}
});
it('keep on error setting', async () => {
const template = '{{ foo }} {% invalid "hi" %}';
2018-06-25 17:42:50 +00:00
const context = { foo: 'bar' };
const resultOnlyVars = await renderUtils.render(
template,
context,
null,
renderUtils.KEEP_ON_ERROR
);
expect(resultOnlyVars).toBe('{{ foo }} {% invalid "hi" %}');
try {
await renderUtils.render(template, context, null);
fail('Render should not have succeeded');
} catch (err) {
expect(err.message).toBe('unknown block tag: invalid');
}
});
it('outputs correct error path', async () => {
const template = {
2018-06-25 17:42:50 +00:00
foo: [{ bar: '{% foo %}' }]
};
try {
await renderUtils.render(template);
fail('Should have failed to render');
} catch (err) {
expect(err.path).toBe('foo[0].bar');
}
});
it('outputs correct error path when private first node', async () => {
const template = {
2018-06-25 17:42:50 +00:00
_foo: { _bar: { baz: '{% foo %}' } }
};
try {
await renderUtils.render(template);
fail('Should have failed to render');
} catch (err) {
expect(err.path).toBe('_bar.baz');
}
});
});