From c382715405ebc31d0c8c01f441f1c35816dc8f8e Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 1 Oct 2017 15:42:45 +0900 Subject: search engine validation --- src/shared/validators/setting.js | 28 +++++++++++++++++ test/shared/validators/setting.test.js | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/src/shared/validators/setting.js b/src/shared/validators/setting.js index 0b853a8..caba5cc 100644 --- a/src/shared/validators/setting.js +++ b/src/shared/validators/setting.js @@ -23,11 +23,39 @@ const validateKeymaps = (keymaps) => { } }; +const validateSearch = (search) => { + let engines = search.engines; + for (let key of Object.keys(engines)) { + if (/\s/.test(key)) { + throw new Error( + `While space in search engine name is not allowed: "${key}"` + ); + } + let url = engines[key]; + if (!url.match(/{}/)) { + throw new Error(`No {}-placeholders in URL of "${key}"`); + } + if (url.match(/{}/g).length > 1) { + throw new Error(`Multiple {}-placeholders in URL of "${key}"`); + } + } + + if (!search.default) { + throw new Error(`Default engine is not set`); + } + if (!Object.keys(engines).includes(search.default)) { + throw new Error(`Default engine "${search.default}" not found`); + } +}; + const validate = (settings) => { validateInvalidTopKeys(settings); if (settings.keymaps) { validateKeymaps(settings.keymaps); } + if (settings.search) { + validateSearch(settings.search); + } }; export { validate }; diff --git a/test/shared/validators/setting.test.js b/test/shared/validators/setting.test.js index 8bf0cbe..9baf858 100644 --- a/test/shared/validators/setting.test.js +++ b/test/shared/validators/setting.test.js @@ -22,4 +22,61 @@ describe("setting validator", () => { expect(fn).to.throw(Error, 'poison.dressing'); }); }); + + describe("search settings", () => { + it('throws an error for invalid search engine name', () => { + let settings = { + search: { + default: 'google', + engines: { + 'google': 'https://google.com/search?q={}', + 'cherry pie': 'https://cherypie.com/search?q={}', + } + } + }; + let fn = validate.bind(undefined, settings) + expect(fn).to.throw(Error, 'cherry pie'); + }); + + it('throws an error for no {}-placeholder', () => { + let settings = { + search: { + default: 'google', + engines: { + 'google': 'https://google.com/search?q={}', + 'yahoo': 'https://search.yahoo.com/search', + } + } + }; + let fn = validate.bind(undefined, settings) + expect(fn).to.throw(Error, 'yahoo'); + }); + + it('throws an error for no default engines', () => { + let settings = { + search: { + engines: { + 'google': 'https://google.com/search?q={}', + 'yahoo': 'https://search.yahoo.com/search?q={}', + } + } + }; + let fn = validate.bind(undefined, settings) + expect(fn).to.throw(Error, 'Default engine'); + }); + + it('throws an error for invalid default engine', () => { + let settings = { + search: { + default: 'twitter', + engines: { + 'google': 'https://google.com/search?q={}', + 'yahoo': 'https://search.yahoo.com/search?q={}', + } + } + }; + let fn = validate.bind(undefined, settings) + expect(fn).to.throw(Error, 'twitter'); + }); + }); }); -- cgit v1.2.3