2017-11-26 20:45:40 +00:00
|
|
|
|
const tag = require('..').templateTags[0];
|
2017-11-01 11:24:00 +00:00
|
|
|
|
|
2017-11-26 20:45:40 +00:00
|
|
|
|
function assertTemplate (args, expected) {
|
2017-11-01 11:24:00 +00:00
|
|
|
|
return async function () {
|
2017-11-26 20:45:40 +00:00
|
|
|
|
const result = await tag.run(null, ...args);
|
2017-11-01 11:24:00 +00:00
|
|
|
|
expect(result).toBe(expected);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-26 20:45:40 +00:00
|
|
|
|
function assertTemplateFails (args, expected) {
|
2017-11-01 11:24:00 +00:00
|
|
|
|
return async function () {
|
|
|
|
|
try {
|
2017-11-26 20:45:40 +00:00
|
|
|
|
await tag.run(null, ...args);
|
2017-11-01 11:24:00 +00:00
|
|
|
|
fail(`Render should have thrown ${expected}`);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
expect(err.message).toContain(expected);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-26 20:45:40 +00:00
|
|
|
|
describe('Plugin', () => {
|
2017-11-01 11:24:00 +00:00
|
|
|
|
// Algorithms
|
|
|
|
|
it('hashes sha1', assertTemplate(
|
2017-11-26 20:45:40 +00:00
|
|
|
|
['sha1', 'hex', 'foo'],
|
2017-11-01 11:24:00 +00:00
|
|
|
|
'0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33'
|
|
|
|
|
));
|
|
|
|
|
it('hashes sha256', assertTemplate(
|
2017-11-26 20:45:40 +00:00
|
|
|
|
['sha256', 'hex', 'foo'],
|
2017-11-01 11:24:00 +00:00
|
|
|
|
'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
|
|
|
|
|
));
|
|
|
|
|
it('hashes md5', assertTemplate(
|
2017-11-26 20:45:40 +00:00
|
|
|
|
['md5', 'hex', 'foo'],
|
2017-11-01 11:24:00 +00:00
|
|
|
|
'acbd18db4cc2f85cedef654fccc4a4d8'
|
|
|
|
|
));
|
|
|
|
|
it('fails to hash invalid algorithm', assertTemplateFails(
|
2017-11-26 20:45:40 +00:00
|
|
|
|
['bad', 'hex', 'foo'],
|
2017-11-01 11:24:00 +00:00
|
|
|
|
'Digest method not supported'
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
// Digests
|
|
|
|
|
it('hashes to latin1', assertTemplate(
|
2017-11-26 20:45:40 +00:00
|
|
|
|
['md5', 'latin1', 'foo'],
|
2017-11-01 11:24:00 +00:00
|
|
|
|
'¬½ÛLÂø\\íïeOÌĤØ'
|
|
|
|
|
));
|
|
|
|
|
it('hashes to hex', assertTemplate(
|
2017-11-26 20:45:40 +00:00
|
|
|
|
['md5', 'hex', 'foo'],
|
2017-11-01 11:24:00 +00:00
|
|
|
|
'acbd18db4cc2f85cedef654fccc4a4d8'
|
|
|
|
|
));
|
|
|
|
|
it('hashes to base64', assertTemplate(
|
2017-11-26 20:45:40 +00:00
|
|
|
|
['md5', 'base64', 'foo'],
|
2017-11-01 11:24:00 +00:00
|
|
|
|
'rL0Y20zC+Fzt72VPzMSk2A=='
|
|
|
|
|
));
|
|
|
|
|
it('fails to hash to invalid', assertTemplateFails(
|
2017-11-26 20:45:40 +00:00
|
|
|
|
['md5', 'bad', 'foo'],
|
|
|
|
|
'Invalid encoding bad. Choices are hex, latin1, base64'
|
2017-11-01 11:24:00 +00:00
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
// Values
|
|
|
|
|
it('hashes empty string', assertTemplate(
|
2017-11-26 20:45:40 +00:00
|
|
|
|
['md5', 'hex', ''],
|
2017-11-01 11:24:00 +00:00
|
|
|
|
'd41d8cd98f00b204e9800998ecf8427e'
|
|
|
|
|
));
|
|
|
|
|
it('hashes no string', assertTemplate(
|
2017-11-26 20:45:40 +00:00
|
|
|
|
['md5', 'hex'],
|
2017-11-01 11:24:00 +00:00
|
|
|
|
'd41d8cd98f00b204e9800998ecf8427e'
|
|
|
|
|
));
|
|
|
|
|
it('fails to hash non string', assertTemplateFails(
|
2017-11-26 20:45:40 +00:00
|
|
|
|
['md5', 'hex', true],
|
2017-11-01 11:24:00 +00:00
|
|
|
|
'Cannot hash value of type "boolean"'
|
|
|
|
|
));
|
|
|
|
|
});
|