diff options
author | Yuchen Pei <hi@ypei.me> | 2022-10-13 14:20:07 +1100 |
---|---|---|
committer | Yuchen Pei <hi@ypei.me> | 2022-10-13 14:27:08 +1100 |
commit | 388b1b9218a5f8971b6c1cdeb9cfa67940e1d33e (patch) | |
tree | c2856936c7582bd1aaf0c720320f56850263470a /test | |
parent | fcd816e95e73a78fd24a7e6baef709d7657d265f (diff) |
let -> const in tests
Diffstat (limited to 'test')
-rw-r--r-- | test/spec/LibreJSSpec.js | 122 |
1 files changed, 61 insertions, 61 deletions
diff --git a/test/spec/LibreJSSpec.js b/test/spec/LibreJSSpec.js index d7a49f5..7e21c24 100644 --- a/test/spec/LibreJSSpec.js +++ b/test/spec/LibreJSSpec.js @@ -22,45 +22,45 @@ 'use strict'; describe('LibreJS\' components', () => { - let LibreJS = browser.extension.getBackgroundPage().LibreJS; - let license = { + const LibreJS = browser.extension.getBackgroundPage().LibreJS; + const license = { id: 'GPL-3.0', url: 'http://www.gnu.org/licenses/gpl-3.0.html', magnet: 'magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt', }; - let unknownLicense = { + const unknownLicense = { id: 'Acme-proprietary-1.5', url: 'http://www.acme.com/license-1.5.html', magnet: 'magnet:?xt=urn:btih:2f739d935676111cfff4b4693e3816e664797050&dn=acme-1.5.txt' }; - let trivial = '1+1'; - let nontrivial = 'function nt() { document.documentElement.innerHTML=""; nt(); }'; + const trivial = '1+1'; + const nontrivial = 'function nt() { document.documentElement.innerHTML=""; nt(); }'; // code calling reserved object is nontrivial const nontrivialCall = 'eval();'; // code calling anything else is trivial const trivialCall = 'foo();'; - let licensed = `// @license ${license.magnet} ${license.id}\n${nontrivial}\n// @license-end`; - let unknownLicensed = `// @license ${unknownLicense.magnet} ${unknownLicense.id}\n${nontrivial}\n// @license-end`; - let commentedOutUnknownLicensed = + const licensed = `// @license ${license.magnet} ${license.id}\n${nontrivial}\n// @license-end`; + const unknownLicensed = `// @license ${unknownLicense.magnet} ${unknownLicense.id}\n${nontrivial}\n// @license-end`; + const commentedOutUnknownLicensed = unknownLicensed.split('\n').map(y => '// ' + y).join('\n'); - let malformedLicensed = `// @license\n${nontrivial}`; - let commentedOutMalformedLicensed = + const malformedLicensed = `// @license\n${nontrivial}`; + const commentedOutMalformedLicensed = malformedLicensed.split('\n').map(y => '// ' + y).join('\n'); let tab, documentUrl; const enableContactFinderTests = false; beforeAll(async () => { - let url = browser.extension.getURL('/test/resources/index.html'); + const url = browser.extension.getURL('/test/resources/index.html'); tab = (await browser.tabs.query({ url }))[0] || (await browser.tabs.create({ url })); documentUrl = url; }); describe('The whitelist/blacklist manager', () => { - let { ListManager, ListStore, Storage } = LibreJS; - let lm = new ListManager(new ListStore('_test.whitelist', Storage.CSV), new ListStore('_test.blacklist', Storage.CSV), new Set()); - let forgot = ['http://formerly.whitelist.ed/', 'http://formerly.blacklist.ed/']; + const { ListManager, ListStore, Storage } = LibreJS; + const lm = new ListManager(new ListStore('_test.whitelist', Storage.CSV), new ListStore('_test.blacklist', Storage.CSV), new Set()); + const forgot = ['http://formerly.whitelist.ed/', 'http://formerly.blacklist.ed/']; beforeAll(async () => { await lm.whitelist('https://fsf.org/*', 'https://*.gnu.org/*', forgot[0]); @@ -73,7 +73,7 @@ describe('LibreJS\' components', () => { await lm.forget(...forgot); - for (let url of forgot) { + for (const url of forgot) { expect(lm.getStatus(url)).toBe('unknown'); } }); @@ -96,9 +96,9 @@ describe('LibreJS\' components', () => { }) describe('The external script source processor', () => { - let url = 'https://www.gnu.org/mock-script.js'; + const url = 'https://www.gnu.org/mock-script.js'; - let processScript = async (source, whitelisted = false) => + const processScript = async (source, whitelisted = false) => await LibreJS.handleScript({ text: source, request: { url, tabId: tab.id, documentUrl, frameId: 0 }, @@ -109,55 +109,55 @@ describe('LibreJS\' components', () => { }); it('should block trivial scripts too', async () => { - let processed = await processScript(trivial); + const processed = await processScript(trivial); expect(processed || trivial).not.toContain(trivial); }); it('should block non-trivial scripts', async () => { - let processed = await processScript(nontrivial); + const processed = await processScript(nontrivial); expect(processed || nontrivial).not.toContain(nontrivial); }); it('should accept scripts with known free license tags', async () => { - let processed = await processScript(licensed); + const processed = await processScript(licensed); expect(processed || licensed).toContain(nontrivial); }); it('should block scripts with unknown license tags', async () => { - let processed = await processScript(unknownLicensed); + const processed = await processScript(unknownLicensed); expect(processed).not.toContain(nontrivial); }); it('should leave alone scripts with commented out unknown license tags', async () => { - let processed = await processScript(commentedOutUnknownLicensed); + const processed = await processScript(commentedOutUnknownLicensed); expect(processed).toContain(nontrivial); }); it('should block scripts with malformed license tags', async () => { - let processed = await processScript(malformedLicensed); + const processed = await processScript(malformedLicensed); expect(processed).not.toContain(nontrivial); }); it('should leave alone scripts with commented out malformed license tags', async () => { - let processed = await processScript(commentedOutMalformedLicensed); + const processed = await processScript(commentedOutMalformedLicensed); expect(processed).toContain(nontrivial); }); }); describe('The HTML processor', () => { - let processHtml = + const processHtml = async (html, whitelisted = false) => LibreJS.editHtml(html, tab.url, tab.id, 0, whitelisted); - let addScript = (html, script, before = '</head>') => + const addScript = (html, script, before = '</head>') => html.replace(before, `<script>${script}</script>${before}`); - let addToBody = (html, fragment) => html.replace('</body>', `${fragment}</body>`); + const addToBody = (html, fragment) => html.replace('</body>', `${fragment}</body>`); - let jsUrl = js => `javascript:${encodeURIComponent(js)}`; + const jsUrl = js => `javascript:${encodeURIComponent(js)}`; function extractScripts(html, def = '') { - let matches = html && html.match(/<script>[^]*?<\/script>/g); + const matches = html && html.match(/<script>[^]*?<\/script>/g); return matches && matches.join('') || def; } @@ -193,78 +193,78 @@ describe('LibreJS\' components', () => { }); it('should accept scripts with known free license tags', async () => { - let licensedInHtml = addScript(html, licensed); - let processed = await processHtml(licensedInHtml); + const licensedInHtml = addScript(html, licensed); + const processed = await processHtml(licensedInHtml); expect(extractScripts(processed, licensed)).toContain(nontrivial); }); it('should block scripts with unknown license tags', async () => { - let unknownInHtml = addScript(html, unknownLicensed); - let processed = await processHtml(unknownInHtml); + const unknownInHtml = addScript(html, unknownLicensed); + const processed = await processHtml(unknownInHtml); expect(extractScripts(processed, nontrivial)).not.toContain(nontrivial); }); it('should block scripts with malformed license tags', async () => { - let malformedInHtml = addScript(html, malformedLicensed); - let processed = await processHtml(malformedInHtml); + const malformedInHtml = addScript(html, malformedLicensed); + const processed = await processHtml(malformedInHtml); expect(extractScripts(processed, nontrivial)).not.toContain(nontrivial); }); it('should accept scripts on globally licensed pages', async () => { - let globalLicense = `/* @licstart The following is the entire license notice + const globalLicense = `/* @licstart The following is the entire license notice for the JavaScript code in this page. -- Some free license -- @licend The above is the entire license notice for the JavaScript code in this page. */`; - let licensed = addScript(nontrivialInHtml, globalLicense, '<script>'); - let processed = await processHtml(html); + const licensed = addScript(nontrivialInHtml, globalLicense, '<script>'); + const processed = await processHtml(html); expect(extractScripts(processed, licensed)).toContain(nontrivial); }); it('should discriminate trivial, non-trivial and licensed mixed on the same page', async () => { let mixedPage = addScript(addScript(nontrivialInHtml, trivial), licensed); - let processed = await processHtml(mixedPage); + const processed = await processHtml(mixedPage); expect(processed).not.toBeNull(); - let scripts = extractScripts(processed, nontrivial); + const scripts = extractScripts(processed, nontrivial); expect(scripts).toContain(trivial); expect(scripts).toContain(licensed); expect(scripts.replace(licensed, '')).not.toContain(nontrivial); }); it('should correctly process (de)duplicated inline scripts', async () => { - let trivialAsUrl = jsUrl(trivial); - let nontrivialAsUrl = jsUrl(nontrivial); - let a = (url, label) => `<a href="${url}">${label}</a>`; + const trivialAsUrl = jsUrl(trivial); + const nontrivialAsUrl = jsUrl(nontrivial); + const a = (url, label) => `<a href="${url}">${label}</a>`; let mixedPage = '<body></body>'; for (let dup = 0; dup < 3; dup++) { mixedPage = addToBody(mixedPage, a(trivialAsUrl, `Trivial #${dup}`)); mixedPage = addToBody(mixedPage, a(nontrivialAsUrl, `Nontrivial #${dup}`)); } - let processed = await processHtml(mixedPage); + const processed = await processHtml(mixedPage); expect(processed).not.toBeNull(); expect(processed).toContain(trivialAsUrl); expect(processed).not.toContain(nontrivialAsUrl); }); it('should force displaying NOSCRIPT elements (except those with @data-librejs-nodisplay) where scripts have been blocked', async () => { - let noscriptContent = 'I\'m NOSCRIPT content'; - let asNoscript = `<noscript>${noscriptContent}</noscript>`; - let asNodisplay = `<noscript data-librejs-nodisplay>${noscriptContent}</noscript>`; - let asSpan = `<span>${noscriptContent}</span>`; - let page = addToBody(addToBody(nontrivialInHtml, asNoscript), asNodisplay); - let processed = await processHtml(page); + const noscriptContent = 'I\'m NOSCRIPT content'; + const asNoscript = `<noscript>${noscriptContent}</noscript>`; + const asNodisplay = `<noscript data-librejs-nodisplay>${noscriptContent}</noscript>`; + const asSpan = `<span>${noscriptContent}</span>`; + const page = addToBody(addToBody(nontrivialInHtml, asNoscript), asNodisplay); + const processed = await processHtml(page); expect(processed).not.toContain(asNoscript); expect(processed).toContain(asSpan); expect(processed).not.toContain(asNodisplay); }); it('should always force displaying @data-librejs-display elements', async () => { - let content = 'I\'m FORCED content'; - let asDisplay = `<span data-librejs-display>${content}</span>`; - let asSpan = `<span>${content}</span>`; + const content = 'I\'m FORCED content'; + const asDisplay = `<span data-librejs-display>${content}</span>`; + const asSpan = `<span>${content}</span>`; for (let page of [nontrivialInHtml, '<body></body>']) { page = addToBody(page, asDisplay); - let processed = await processHtml(page); + const processed = await processHtml(page); expect(processed).not.toContain(asDisplay); expect(processed).not.toContain(asSpan); } @@ -272,12 +272,12 @@ describe('LibreJS\' components', () => { }); describe('The external (Web Labels) license checker', () => { - let { ExternalLicenses } = LibreJS; + const { ExternalLicenses } = LibreJS; let check; beforeAll(async () => { - let args = { tabId: tab.id, frameId: 0, documentUrl }; - let resolve = url => new URL(url, documentUrl).href; + const args = { tabId: tab.id, frameId: 0, documentUrl }; + const resolve = url => new URL(url, documentUrl).href; check = async url => await ExternalLicenses.check(Object.assign({ url: resolve(url) }, args)); await browser.tabs.executeScript(tab.id, { file: '/content/externalLicenseChecker.js' @@ -285,22 +285,22 @@ describe('LibreJS\' components', () => { }); it('should recognize free licenses', async () => { - let scriptInfo = await check('jquery.js'); + const scriptInfo = await check('jquery.js'); console.debug(scriptInfo); expect(scriptInfo.free).toBeTruthy(); }); it('should accept scripts if any of multiple licenses is free', async () => { - let scriptInfo = await check('app-trilicensed.js'); + const scriptInfo = await check('app-trilicensed.js'); console.debug(scriptInfo); expect(scriptInfo.free).toBeTruthy(); }); it('should block scripts declaring only proprietary license(s)', async () => { - let scriptInfo = await check('proprietary.js'); + const scriptInfo = await check('proprietary.js'); console.debug(scriptInfo); expect(scriptInfo.free).toBeFalsy(); }); it('should block scripts not declaring any license', async () => { - let scriptInfo = await check('tracker.js'); + const scriptInfo = await check('tracker.js'); console.debug(scriptInfo); expect(scriptInfo).toBeNull(); }); |