diff --git a/services/domainlist.js b/services/domainlist.js index 3d5568fda..29673e2b6 100644 --- a/services/domainlist.js +++ b/services/domainlist.js @@ -55,17 +55,15 @@ class Domainlist { // This will return true in the event that at least one blockword is found // in the phrase. - return list.every((domain) => { - - // Not considering wildcards for now. - if (domain === domainToMatch) { + for (let i = 0; i < list.length; i++) { + if (list[i] === domainToMatch) { return true; } + } - // We've walked over all the whitelisted domains, and haven't had a - // mismatch... It is not an allowed domain! - return false; - }); + // We've walked over all the whitelisted domains, and haven't had a + // mismatch... It is not an allowed domain! + return false; } /** diff --git a/test/services/domainlist.js b/test/services/domainlist.js new file mode 100644 index 000000000..db53b77b4 --- /dev/null +++ b/test/services/domainlist.js @@ -0,0 +1,52 @@ +const expect = require('chai').expect; +const Domainlist = require('../../services/domainlist'); +const SettingsService = require('../../services/settings'); + +describe('services.Domainlist', () => { + + const domainlists = { + whitelist: [ + 'nytimes.com', + 'wapo.com' + ] + }; + + let domainlist = new Domainlist(); + const settings = {id: '1', moderation: 'PRE', domainlist: {whitelist: ['nytimes.com', 'wapo.com']}}; + + beforeEach(() => SettingsService.init(settings)); + + describe('#init', () => { + + before(() => domainlist.upsert(domainlists)); + + it('has entries', () => { + expect(domainlist.lists.whitelist).to.not.be.empty; + }); + + }); + + describe('#match', () => { + + const whiteList = Domainlist.parseList(domainlists['whitelist']); + + it('does match on an included domain', () => { + [ + 'wapo.com', + 'nytimes.com' + ].forEach((domain) => { + expect(domainlist.match(whiteList, domain)).to.be.true; + }); + }); + + it('does not match on a not included domain', () => { + [ + 'badsite.com', + 'www.badsite.com', + 'otherexample.com' + ].forEach((domain) => { + expect(domainlist.match(whiteList, domain)).to.be.false; + }); + }); + }); +});