diff --git a/History.rdoc b/History.rdoc index b90652c..80da1ba 100644 --- a/History.rdoc +++ b/History.rdoc @@ -2,6 +2,7 @@ === Changes * Rename `url_whitelist` to `url_allowlist` + * Allowlist now supports regular expressions === Breaking changes * Failed checks against the allowlist now raise `UrlNotAllowed` rather than `NotWhitelistedUrl` diff --git a/README.markdown b/README.markdown index 290566e..3e18fc3 100644 --- a/README.markdown +++ b/README.markdown @@ -349,10 +349,13 @@ to one of the values specified in the url allowlist like so: DatabaseCleaner.url_allowlist = ['postgres://postgres@localhost', 'postgres://foo@bar'] ``` -Allowlist elements may be specified as regular expressions if extra flexibility is required:: +Allowlist elements are matched with case equality (`===`), so regular expressions or procs may be used: ```ruby -DatabaseCleaner.url_allowlist = [%r{^postgres://postgres@localhost}] +DatabaseCleaner.url_allowlist = [ + %r{^postgres://postgres@localhost}, # match any db with this prefix + proc {|uri| URI.parse(uri).user == "test" } # match any db authenticating with the 'test' user +] ``` ## COPYRIGHT diff --git a/spec/database_cleaner/safeguard_spec.rb b/spec/database_cleaner/safeguard_spec.rb index 20b7993..89d8656 100644 --- a/spec/database_cleaner/safeguard_spec.rb +++ b/spec/database_cleaner/safeguard_spec.rb @@ -122,9 +122,24 @@ module DatabaseCleaner expect { cleaner.start }.to raise_error(Safeguard::Error::UrlNotAllowed) end end + + describe 'A url that matches a proc' do + let(:database_url) { 'redis://test:test@foo.bar' } + + it 'does not raise' do + expect { cleaner.start }.to_not raise_error + end + end end - let(:url_allowlist) { ['postgres://postgres@localhost', 'postgres://foo.bar', %r{^postgres://bar.baz}] } + let(:url_allowlist) do + [ + 'postgres://postgres@localhost', + 'postgres://foo.bar', + %r{^postgres://bar.baz}, + proc { |x| URI.parse(x).user == 'test' } + ] + end describe 'url_allowlist' do before { DatabaseCleaner.url_allowlist = url_allowlist }