Add more descriptive "allowlist" terminology

This runs s/url_whitelist/url_allowlist/ and adds backwards-compatible
aliases for the terminology which can be easily deprecated or dropped
in the future, should that be desired.
This commit is contained in:
Brian P O'Rourke 2020-06-17 20:45:14 -07:00 committed by Micah Geisel
parent 4da67e2d51
commit 52514b3c8a
4 changed files with 55 additions and 39 deletions

View file

@ -342,11 +342,11 @@ DatabaseCleaner.allow_production = true
DatabaseCleaner.allow_remote_database_url = true DatabaseCleaner.allow_remote_database_url = true
``` ```
In Ruby, a URL whitelist can be specified. When specified, DatabaseCleaner will only allow `DATABASE_URL` to be equal In Ruby, a URL allowlist can be specified. When specified, DatabaseCleaner will only allow `DATABASE_URL` to be equal
to one of the values specified in the url whitelist like so: to one of the values specified in the url allowlist like so:
```ruby ```ruby
DatabaseCleaner.url_whitelist = ['postgres://postgres@localhost', 'postgres://foo@bar'] DatabaseCleaner.url_allowlist = ['postgres://postgres@localhost', 'postgres://foo@bar']
``` ```
## COPYRIGHT ## COPYRIGHT

View file

@ -14,7 +14,10 @@ module DatabaseCleaner
:cleaning, :cleaning,
] => :cleaners ] => :cleaners
attr_accessor :allow_remote_database_url, :allow_production, :url_whitelist attr_accessor :allow_remote_database_url, :allow_production, :url_allowlist
alias :url_whitelist :url_allowlist
alias :url_whitelist= :url_allowlist=
def cleaners def cleaners
@cleaners ||= Cleaners.new @cleaners ||= Cleaners.new

View file

@ -13,27 +13,27 @@ module DatabaseCleaner
end end
end end
class NotWhitelistedUrl < Error class UrlNotAllowed < Error
def initialize def initialize
super("ENV['DATABASE_URL'] is set to a URL that is not on the whitelist. Please refer to https://github.com/DatabaseCleaner/database_cleaner#safeguards") super("ENV['DATABASE_URL'] is set to a URL that is not on the allowlist. Please refer to https://github.com/DatabaseCleaner/database_cleaner#safeguards")
end end
end end
end end
class WhitelistedUrl class AllowedUrl
def run def run
return if skip? return if skip?
raise Error::NotWhitelistedUrl if database_url_not_whitelisted? raise Error::UrlNotAllowed if database_url_not_allowed?
end end
private private
def database_url_not_whitelisted? def database_url_not_allowed?
!DatabaseCleaner.url_whitelist.include?(ENV['DATABASE_URL']) !DatabaseCleaner.url_allowlist.include?(ENV['DATABASE_URL'])
end end
def skip? def skip?
!DatabaseCleaner.url_whitelist !DatabaseCleaner.url_allowlist
end end
end end
@ -67,7 +67,7 @@ module DatabaseCleaner
def skip? def skip?
ENV['DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL'] || ENV['DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL'] ||
DatabaseCleaner.allow_remote_database_url || DatabaseCleaner.allow_remote_database_url ||
DatabaseCleaner.url_whitelist DatabaseCleaner.url_allowlist
end end
end end
@ -97,7 +97,7 @@ module DatabaseCleaner
CHECKS = [ CHECKS = [
RemoteDatabaseUrl, RemoteDatabaseUrl,
Production, Production,
WhitelistedUrl AllowedUrl
] ]
def run def run

View file

@ -72,13 +72,10 @@ module DatabaseCleaner
end end
end end
describe 'DatabaseCleaner.url_whitelist is set' do describe 'DatabaseCleaner.url_allowlist is set' do
let(:url_whitelist) { ['postgres://postgres@localhost', 'postgres://foo.bar'] }
before { DatabaseCleaner.url_whitelist = url_whitelist } shared_examples 'allowlist assigned' do
after { DatabaseCleaner.url_whitelist = nil } describe 'A remote url is on the allowlist' do
describe 'A remote url is on the whitelist' do
let(:database_url) { 'postgres://foo.bar' } let(:database_url) { 'postgres://foo.bar' }
it 'does not raise' do it 'does not raise' do
@ -86,15 +83,15 @@ module DatabaseCleaner
end end
end end
describe 'A remote url is not on the whitelist' do describe 'A remote url is not on the allowlist' do
let(:database_url) { 'postgress://bar.baz' } let(:database_url) { 'postgress://bar.baz' }
it 'raises a whitelist error' do it 'raises a allowlist error' do
expect { cleaner.start }.to raise_error(Safeguard::Error::NotWhitelistedUrl) expect { cleaner.start }.to raise_error(Safeguard::Error::UrlNotAllowed)
end end
end end
describe 'A local url is on the whitelist' do describe 'A local url is on the allowlist' do
let(:database_url) { 'postgres://postgres@localhost' } let(:database_url) { 'postgres://postgres@localhost' }
it 'does not raise' do it 'does not raise' do
@ -102,14 +99,30 @@ module DatabaseCleaner
end end
end end
describe 'A local url is not on the whitelist' do describe 'A local url is not on the allowlist' do
let(:database_url) { 'postgres://localhost' } let(:database_url) { 'postgres://localhost' }
it 'raises a whitelist error' do it 'raises a not allowed error' do
expect { cleaner.start }.to raise_error(Safeguard::Error::NotWhitelistedUrl) expect { cleaner.start }.to raise_error(Safeguard::Error::UrlNotAllowed)
end end
end end
end end
let(:url_allowlist) { ['postgres://postgres@localhost', 'postgres://foo.bar'] }
describe 'url_allowlist' do
before { DatabaseCleaner.url_allowlist = url_allowlist }
after { DatabaseCleaner.url_allowlist = nil }
it_behaves_like 'allowlist assigned'
end
describe 'url_whitelist' do
before { DatabaseCleaner.url_whitelist = url_allowlist }
after { DatabaseCleaner.url_whitelist = nil }
it_behaves_like 'allowlist assigned'
end
end
end end
describe 'ENV is set to production' do describe 'ENV is set to production' do