Support regular expressions in allowlist

This commit is contained in:
Brian P O'Rourke 2020-06-30 11:39:22 -07:00
parent 855abc968d
commit d3078d14de
No known key found for this signature in database
GPG key ID: 5B0C73433C16416F
2 changed files with 18 additions and 2 deletions

View file

@ -29,7 +29,7 @@ module DatabaseCleaner
private private
def database_url_not_allowed? def database_url_not_allowed?
!DatabaseCleaner.url_allowlist.include?(ENV['DATABASE_URL']) !DatabaseCleaner.url_allowlist.any? {|allowed| allowed === ENV['DATABASE_URL'] }
end end
def skip? def skip?

View file

@ -91,6 +91,22 @@ module DatabaseCleaner
end end
end end
describe 'A similar url not explicitly matched as a pattern' do
let(:database_url) { 'postgres://foo.bar?pool=8' }
it 'raises an allowlist error' do
expect { cleaner.start }.to raise_error(Safeguard::Error::UrlNotAllowed)
end
end
describe 'A remote url matches a pattern on the allowlist' do
let(:database_url) { 'postgres://bar.baz?pool=16' }
it 'does not raise' do
expect { cleaner.start }.to_not raise_error
end
end
describe 'A local url is on the allowlist' do describe 'A local url is on the allowlist' do
let(:database_url) { 'postgres://postgres@localhost' } let(:database_url) { 'postgres://postgres@localhost' }
@ -108,7 +124,7 @@ module DatabaseCleaner
end end
end end
let(:url_allowlist) { ['postgres://postgres@localhost', 'postgres://foo.bar'] } let(:url_allowlist) { ['postgres://postgres@localhost', 'postgres://foo.bar', %r{^postgres://bar.baz}] }
describe 'url_allowlist' do describe 'url_allowlist' do
before { DatabaseCleaner.url_allowlist = url_allowlist } before { DatabaseCleaner.url_allowlist = url_allowlist }