Permalink
Browse files

Username and Password detection (#382)

* Create tests for a non jdbc connection and fix the arising issues
* Substitutes the `raw` calls to `parsed_uri` so the connection is still lazily initialized
* Remove redundant assertions from tests
  • Loading branch information...
mereghost committed Feb 23, 2017
1 parent 384c5a1 commit 387f78753fb02925bb62f9bb54b77944abdb802a
Showing with 70 additions and 23 deletions.
  1. +2 −2 lib/hanami/model/migrator/connection.rb
  2. +68 −21 test/migrator/connection_test.rb
@@ -83,7 +83,7 @@ def database_type
# @since 0.5.0
# @api private
def user
@user ||= parsed_opt('user')
@user ||= parsed_opt('user') || parsed_uri.user
end

# Returns user from DB connection
@@ -93,7 +93,7 @@ def user
# @since 0.5.0
# @api private
def password
@password ||= parsed_opt('password')
@password ||= parsed_opt('password') || parsed_uri.password
end

# Returns DB connection URI directly from adapter
@@ -4,27 +4,79 @@
describe Hanami::Model::Migrator::Connection do
let(:connection) { Hanami::Model::Migrator::Connection.new(hanami_model_configuration) }

let(:hanami_model_configuration) do
OpenStruct.new(
url: 'postgresql://postgres:s3cr3T@127.0.0.1:5432/database'
)
end
describe 'when not a jdbc connection' do
let(:hanami_model_configuration) do
OpenStruct.new(
url: 'postgresql://postgres:s3cr3T@127.0.0.1:5432/database'
)
end

describe '#jdbc?' do
it 'returns false' do
connection.jdbc?.must_equal false
describe '#jdbc?' do
it 'returns false' do
connection.jdbc?.must_equal false
end
end
end

describe '#global_uri' do
it 'returns connection URI without database' do
connection.global_uri.scan('database').empty?.must_equal true
describe '#global_uri' do
it 'returns connection URI without database' do
connection.global_uri.scan('database').empty?.must_equal true
end
end

describe '#parsed_uri?' do
it 'returns an URI instance' do
connection.parsed_uri.must_be_kind_of URI
end
end

describe '#host' do
it 'returns configured host' do
connection.host.must_equal '127.0.0.1'
end
end
end

describe '#parsed_uri?' do
it 'returns an URI instance' do
connection.parsed_uri.must_be_kind_of URI
describe '#port' do
it 'returns configured port' do
connection.port.must_equal 5432
end
end

describe '#database' do
it 'returns configured database' do
connection.database.must_equal 'database'
end
end

describe '#user' do
it 'returns configured user' do
connection.user.must_equal 'postgres'
end

describe 'when there is no user option' do
let(:hanami_model_configuration) do
OpenStruct.new(url: 'postgresql://127.0.0.1:5432/database')
end

it 'returns nil' do
connection.user.must_equal nil
end
end
end

describe '#password' do
it 'returns configured password' do
connection.password.must_equal 's3cr3T'
end

describe 'when there is no password option' do
let(:hanami_model_configuration) do
OpenStruct.new(url: 'postgresql://127.0.0.1/database')
end

it 'returns nil' do
connection.password.must_equal nil
end
end
end
end

@@ -43,21 +95,18 @@

describe '#host' do
it 'returns configured host' do
connection.host.wont_equal nil
connection.host.must_equal '127.0.0.1'
end
end

describe '#port' do
it 'returns configured port' do
connection.port.wont_equal nil
connection.port.must_equal 5432
end
end

describe '#user' do
it 'returns configured user' do
connection.user.wont_equal nil
connection.user.must_equal 'postgres'
end

@@ -74,7 +123,6 @@

describe '#password' do
it 'returns configured password' do
connection.password.wont_equal nil
connection.password.must_equal 's3cr3T'
end

@@ -91,7 +139,6 @@

describe '#database' do
it 'returns configured database' do
connection.database.wont_equal nil
connection.database.must_equal 'database'
end
end

0 comments on commit 387f787

Please sign in to comment.