From d3078d14de1c06390caac86d52d4b9bf8039b8fb Mon Sep 17 00:00:00 2001 From: Brian P O'Rourke Date: Tue, 30 Jun 2020 11:39:22 -0700 Subject: [PATCH] Support regular expressions in allowlist --- lib/database_cleaner/safeguard.rb | 2 +- spec/database_cleaner/safeguard_spec.rb | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/database_cleaner/safeguard.rb b/lib/database_cleaner/safeguard.rb index 5ca2703..91197ca 100644 --- a/lib/database_cleaner/safeguard.rb +++ b/lib/database_cleaner/safeguard.rb @@ -29,7 +29,7 @@ module DatabaseCleaner private def database_url_not_allowed? - !DatabaseCleaner.url_allowlist.include?(ENV['DATABASE_URL']) + !DatabaseCleaner.url_allowlist.any? {|allowed| allowed === ENV['DATABASE_URL'] } end def skip? diff --git a/spec/database_cleaner/safeguard_spec.rb b/spec/database_cleaner/safeguard_spec.rb index 573ae2c..3a24721 100644 --- a/spec/database_cleaner/safeguard_spec.rb +++ b/spec/database_cleaner/safeguard_spec.rb @@ -91,6 +91,22 @@ module DatabaseCleaner 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 let(:database_url) { 'postgres://postgres@localhost' } @@ -108,7 +124,7 @@ module DatabaseCleaner 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 before { DatabaseCleaner.url_allowlist = url_allowlist }