diff --git a/README.markdown b/README.markdown index 0e4c714..2d933fa 100644 --- a/README.markdown +++ b/README.markdown @@ -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 diff --git a/lib/database_cleaner/core.rb b/lib/database_cleaner/core.rb index cb919a4..d773dcb 100644 --- a/lib/database_cleaner/core.rb +++ b/lib/database_cleaner/core.rb @@ -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 diff --git a/lib/database_cleaner/safeguard.rb b/lib/database_cleaner/safeguard.rb index 900353d..5ca2703 100644 --- a/lib/database_cleaner/safeguard.rb +++ b/lib/database_cleaner/safeguard.rb @@ -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 diff --git a/spec/database_cleaner/safeguard_spec.rb b/spec/database_cleaner/safeguard_spec.rb index f71b133..573ae2c 100644 --- a/spec/database_cleaner/safeguard_spec.rb +++ b/spec/database_cleaner/safeguard_spec.rb @@ -72,43 +72,56 @@ 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 } + shared_examples 'allowlist assigned' do + describe 'A remote url is on the allowlist' do + let(:database_url) { 'postgres://foo.bar' } - describe 'A remote url is on the whitelist' do - let(:database_url) { 'postgres://foo.bar' } + it 'does not raise' do + expect { cleaner.start }.to_not raise_error + end + end - it 'does not raise' do - expect { cleaner.start }.to_not raise_error + describe 'A remote url is not on the allowlist' do + let(:database_url) { 'postgress://bar.baz' } + + it 'raises a allowlist error' do + expect { cleaner.start }.to raise_error(Safeguard::Error::UrlNotAllowed) + end + end + + describe 'A local url is on the allowlist' do + let(:database_url) { 'postgres://postgres@localhost' } + + it 'does not raise' do + expect { cleaner.start }.to_not raise_error + end + end + + describe 'A local url is not on the allowlist' do + let(:database_url) { 'postgres://localhost' } + + it 'raises a not allowed error' do + expect { cleaner.start }.to raise_error(Safeguard::Error::UrlNotAllowed) + end end end - describe 'A remote url is not on the whitelist' do - let(:database_url) { 'postgress://bar.baz' } + let(:url_allowlist) { ['postgres://postgres@localhost', 'postgres://foo.bar'] } - it 'raises a whitelist error' do - expect { cleaner.start }.to raise_error(Safeguard::Error::NotWhitelistedUrl) - end + describe 'url_allowlist' do + before { DatabaseCleaner.url_allowlist = url_allowlist } + after { DatabaseCleaner.url_allowlist = nil } + it_behaves_like 'allowlist assigned' end - describe 'A local url is on the whitelist' do - let(:database_url) { 'postgres://postgres@localhost' } - - it 'does not raise' do - expect { cleaner.start }.to_not raise_error - end + describe 'url_whitelist' do + before { DatabaseCleaner.url_whitelist = url_allowlist } + after { DatabaseCleaner.url_whitelist = nil } + it_behaves_like 'allowlist assigned' end - describe 'A local url is not on the whitelist' do - let(:database_url) { 'postgres://localhost' } - - it 'raises a whitelist error' do - expect { cleaner.start }.to raise_error(Safeguard::Error::NotWhitelistedUrl) - end - end end end