insomnia/plugins/insomnia-plugin-hash/__tests__/index.test.js

60 lines
1.8 KiB
JavaScript
Raw Normal View History

const tag = require('..').templateTags[0];
2017-11-01 11:24:00 +00:00
2018-06-25 17:42:50 +00:00
function assertTemplate(args, expected) {
return async function() {
const result = await tag.run(null, ...args);
2017-11-01 11:24:00 +00:00
expect(result).toBe(expected);
};
}
2018-06-25 17:42:50 +00:00
function assertTemplateFails(args, expected) {
return async function() {
2017-11-01 11:24:00 +00:00
try {
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);
}
};
}
describe('Plugin', () => {
2017-11-01 11:24:00 +00:00
// Algorithms
2018-06-25 17:42:50 +00:00
it(
'hashes sha1',
2018-10-17 16:42:33 +00:00
assertTemplate(['sha1', 'hex', 'foo'], '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33')
2018-06-25 17:42:50 +00:00
);
it(
'hashes sha256',
assertTemplate(
['sha256', 'hex', 'foo'],
'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
)
);
2018-10-17 16:42:33 +00:00
it('hashes md5', assertTemplate(['md5', 'hex', 'foo'], 'acbd18db4cc2f85cedef654fccc4a4d8'));
2018-06-25 17:42:50 +00:00
it(
'fails to hash invalid algorithm',
assertTemplateFails(['bad', 'hex', 'foo'], 'Digest method not supported')
);
2017-11-01 11:24:00 +00:00
// Digests
2018-10-17 16:42:33 +00:00
it('hashes to latin1', assertTemplate(['md5', 'latin1', 'foo'], '¬½ÛLÂø\\íïeOÌĤØ'));
it('hashes to hex', assertTemplate(['md5', 'hex', 'foo'], 'acbd18db4cc2f85cedef654fccc4a4d8'));
it('hashes to base64', assertTemplate(['md5', 'base64', 'foo'], 'rL0Y20zC+Fzt72VPzMSk2A=='));
2018-06-25 17:42:50 +00:00
it(
'fails to hash to invalid',
assertTemplateFails(
['md5', 'bad', 'foo'],
'Invalid encoding bad. Choices are hex, latin1, base64'
)
);
2017-11-01 11:24:00 +00:00
// Values
2018-10-17 16:42:33 +00:00
it('hashes empty string', assertTemplate(['md5', 'hex', ''], 'd41d8cd98f00b204e9800998ecf8427e'));
it('hashes no string', assertTemplate(['md5', 'hex'], 'd41d8cd98f00b204e9800998ecf8427e'));
2018-06-25 17:42:50 +00:00
it(
'fails to hash non string',
2018-10-17 16:42:33 +00:00
assertTemplateFails(['md5', 'hex', true], 'Cannot hash value of type "boolean"')
2018-06-25 17:42:50 +00:00
);
2017-11-01 11:24:00 +00:00
});