-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A simple, configurable solution to blacklist usernames
- Loading branch information
Showing
8 changed files
with
179 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"useTheBigUsernameBlacklist": true, | ||
"customBlacklistedUsernames": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const blacklistConfig = require('../../config/usernames-blacklist.json') | ||
const blacklist = require('the-big-username-blacklist').list | ||
|
||
class BlacklistService { | ||
constructor () { | ||
this.reset() | ||
} | ||
|
||
addWord (word) { | ||
this.list.push(BlacklistService._prepareWord(word)) | ||
} | ||
|
||
reset (config) { | ||
this.list = BlacklistService._initList(config) | ||
} | ||
|
||
validate (word) { | ||
return this.list.indexOf(BlacklistService._prepareWord(word)) === -1 | ||
} | ||
|
||
static _initList (config = blacklistConfig) { | ||
return [ | ||
...(config.useTheBigUsernameBlacklist ? blacklist : []), | ||
...config.customBlacklistedUsernames | ||
] | ||
} | ||
|
||
static _prepareWord (word) { | ||
return word.trim().toLocaleLowerCase() | ||
} | ||
} | ||
|
||
module.exports = new BlacklistService() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict' | ||
|
||
const chai = require('chai') | ||
const expect = chai.expect | ||
|
||
const blacklist = require('the-big-username-blacklist').list | ||
const blacklistService = require('../../lib/common/blacklist-service') | ||
|
||
describe('BlacklistService', () => { | ||
afterEach(() => blacklistService.reset()) | ||
|
||
describe('addWord', () => { | ||
it('allows adding words', () => { | ||
const numberOfBlacklistedWords = blacklistService.list.length | ||
blacklistService.addWord('foo') | ||
expect(blacklistService.list.length).to.equal(numberOfBlacklistedWords + 1) | ||
}) | ||
}) | ||
|
||
describe('reset', () => { | ||
it('will reset list of blacklisted words', () => { | ||
blacklistService.addWord('foo') | ||
blacklistService.reset() | ||
expect(blacklistService.list.length).to.equal(blacklist.length) | ||
}) | ||
|
||
it('can configure service via reset', () => { | ||
blacklistService.reset({ | ||
useTheBigUsernameBlacklist: false, | ||
customBlacklistedUsernames: ['foo'] | ||
}) | ||
expect(blacklistService.list.length).to.equal(1) | ||
}) | ||
|
||
it('is a singleton', () => { | ||
const instanceA = blacklistService | ||
blacklistService.reset({ customBlacklistedUsernames: ['foo'] }) | ||
expect(instanceA.validate('foo')).to.equal(blacklistService.validate('foo')) | ||
}) | ||
}) | ||
|
||
describe('validate', () => { | ||
it('validates given a default list of blacklisted usernames', () => { | ||
const validWords = blacklist.reduce((memo, word) => memo + (blacklistService.validate(word) ? 1 : 0), 0) | ||
expect(validWords).to.equal(0) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters