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
```
In Ruby, a URL whitelist 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:
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 allowlist like so:
```ruby
DatabaseCleaner.url_whitelist = ['postgres://postgres@localhost', 'postgres://foo@bar']
DatabaseCleaner.url_allowlist = ['postgres://postgres@localhost', 'postgres://foo@bar']
```
## COPYRIGHT

View file

@ -14,7 +14,10 @@ module DatabaseCleaner
:cleaning,
] => :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
@cleaners ||= Cleaners.new

View file

@ -13,27 +13,27 @@ module DatabaseCleaner
end
end
class NotWhitelistedUrl < Error
class UrlNotAllowed < Error
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
class WhitelistedUrl
class AllowedUrl
def run
return if skip?
raise Error::NotWhitelistedUrl if database_url_not_whitelisted?
raise Error::UrlNotAllowed if database_url_not_allowed?
end
private
def database_url_not_whitelisted?
!DatabaseCleaner.url_whitelist.include?(ENV['DATABASE_URL'])
def database_url_not_allowed?
!DatabaseCleaner.url_allowlist.include?(ENV['DATABASE_URL'])
end
def skip?
!DatabaseCleaner.url_whitelist
!DatabaseCleaner.url_allowlist
end
end
@ -67,7 +67,7 @@ module DatabaseCleaner
def skip?
ENV['DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL'] ||
DatabaseCleaner.allow_remote_database_url ||
DatabaseCleaner.url_whitelist
DatabaseCleaner.url_allowlist
end
end
@ -97,7 +97,7 @@ module DatabaseCleaner
CHECKS = [
RemoteDatabaseUrl,
Production,
WhitelistedUrl
AllowedUrl
]
def run

View file

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