Skip to content

Commit

Permalink
replaced faraday with custom HTTP management classes, all specs passing
Browse files Browse the repository at this point in the history
  • Loading branch information
subvertallchris committed Dec 22, 2014
1 parent be1b7f1 commit 0d46714
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 59 deletions.
2 changes: 0 additions & 2 deletions lib/neo4j-server.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'json'
require 'faraday'
require 'faraday_middleware'
require 'typhoeus'
require 'neo4j-server/resource'
require 'neo4j-server/cypher_node'
Expand Down
12 changes: 4 additions & 8 deletions lib/neo4j-server/cypher_authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ def authenticate
nil
elsif auth_response.body.is_a?(String)
JSON.parse(auth_response.body)['errors'][0]['code'] == 'Neo.ClientError.Security.AuthorizationFailed' ? auth_attempt : nil
elsif auth_response.body.is_a?(Hash)
auth_response.body.has_key?('errors') ? auth_attempt : auth_response
else
auth_response
nil
end
return nil if auth_hash.nil?
add_auth_headers(token_or_error(auth_hash))
Expand Down Expand Up @@ -103,13 +105,7 @@ def auth_connection(url)
end

def self.new_connection
conn = Faraday.new do |b|
b.request :json
b.response :json, :content_type => "application/json"
b.use Faraday::Adapter::NetHttpPersistent
end
conn.headers = { 'Content-Type' => 'application/json' }
conn
Neo4j::Server::Connection.new
end

def new_connection
Expand Down
1 change: 0 additions & 1 deletion lib/neo4j-server/cypher_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ def first_data(id = nil)
data = @data.first['row'].first
#data.is_a?(Hash) ? {'data' => data, 'id' => id} : data
else
# binding.pry
data = @data[0][0]
data.is_a?(Hash) ? add_entity_id(data) : data
end
Expand Down
21 changes: 3 additions & 18 deletions lib/neo4j-server/cypher_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,9 @@ def initialize(data_url, connection, auth_obj = nil)
end

# @param [Hash] params could be empty or contain basic authentication user and password
# @return [Faraday]
# @see https://github.com/lostisland/faraday
# @return Neo4j::Session::Connection
def self.create_connection(params)
init_params = params[:initialize] and params.delete(:initialize)
# conn = Faraday.new(init_params) do |b|
# b.request :basic_auth, params[:basic_auth][:username], params[:basic_auth][:password] if params[:basic_auth]
# b.request :json
# #b.response :logger
# b.response :json, :content_type => "application/json"
# #b.use Faraday::Response::RaiseError
# b.use Faraday::Adapter::NetHttpPersistent
# # b.adapter Faraday.default_adapter
# end
# conn.headers = { 'Content-Type' => 'application/json', 'User-Agent' => ::Neo4j::Session.user_agent_string }
# conn
Neo4j::Server::Connection.new
Neo4j::Server::Connection.new(params)
end

# Opens a session to the database
Expand All @@ -49,7 +36,7 @@ def self.open(endpoint_url = nil, params = {})
connection = params[:connection] || create_connection(params)
url = endpoint_url || 'http://localhost:7474'
auth_obj = CypherAuthentication.new(url, connection, params)
# auth_obj.authenticate
auth_obj.authenticate
response = connection.get(url)
raise "Server not available on #{url} (response code #{response.response_code})" unless response.status == 200
establish_session(response.body, connection, auth_obj)
Expand Down Expand Up @@ -191,11 +178,9 @@ def _query_data(q)
Neo4j::Transaction.current ? r : r['data']
end

require 'pry'
def _query_or_fail(q, single_row = false, params=nil)
response = _query(q, params)
response.raise_error if response.error?
# binding.pry
single_row ? response.first_data : response
end

Expand Down
3 changes: 2 additions & 1 deletion lib/neo4j-server/neo4j_connection_response.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Neo4j::Server
class ConnectionResponse
attr_reader :response, :endpoint
EMPTY = ''

def initialize(response, endpoint)
@response = response
Expand All @@ -12,7 +13,7 @@ def status
end

def body
@body ||= JSON.parse!(response.body)
@body ||= response.body.empty? ? EMPTY : JSON.parse!(response.body)
end

def headers
Expand Down
51 changes: 41 additions & 10 deletions lib/neo4j-server/neo4j_server_connection.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,63 @@
module Neo4j::Server
class Connection
attr_reader :host, :hydra
attr_reader :host, :hydra, :headers, :global_params

GET = :get
POST = :post
JSON = { 'Content-Type' => 'application/json' }
GET = :get
POST = :post
JSON = { 'Content-Type' => 'application/json' }
SSL_VERIFY = { ssl_verifyhost: 2 }
SSL_NO_VERIFY = { ssl_verifyhost: 0 }
EMPTY_PARAMS = {}
Ethon.logger.level = 999

def initialize(host = 'http://localhost:7474')
def initialize(params = nil)
@hydra = Typhoeus::Hydra.new
@host = host
@global_params = params_hash(params)
@headers = JSON.dup
end

def basic_creds
global_params[:userpwd]
end

def verify_ssl?
global_params[:ssl_verifyhost] == 2
end

def get(endpoint)
response_for(Typhoeus.get(endpoint, headers: JSON), endpoint)
response_for(Typhoeus.get(endpoint, post_params(nil)), endpoint)
end

def post(endpoint, body = nil)
post_params = body ? { body: body.to_json, headers: JSON } : { headers: JSON }
response_for(Typhoeus.post(endpoint, post_params), endpoint)
response_for(Typhoeus.post(endpoint, post_params(body)), endpoint)
end

def delete(endpoint, body = nil)
response_for(Typhoeus.delete(endpoint, body: body.to_json, headers: JSON), endpoint)
response_for(Typhoeus.delete(endpoint, post_params(body)), endpoint)
end

private

def params_hash(params)
return EMPTY_PARAMS unless params
init_params = params[:initialize] ? params[:initialize] : {}
basic_creds = params[:basic_auth] ? { userpwd: "#{params[:basic_auth][:username]}:#{params[:basic_auth][:password]}" } : {}
ssl = init_params[:ssl] ? verify_ssl(init_params[:ssl][:verify]) : {}
basic_creds.merge! ssl
end

def post_params(body)
if body
{ body: body.to_json, headers: headers }.merge! global_params
else
{ headers: headers }.merge! global_params
end
end

def verify_ssl(ssl_param)
ssl_param ? SSL_VERIFY : SSL_NO_VERIFY
end

def response_for(request, endpoint)
Neo4j::Server::ConnectionResponse.new request, endpoint
end
Expand Down
8 changes: 4 additions & 4 deletions spec/neo4j-server/e2e/cypher_authentication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def auth_breakdown
end

it 'informs of a required password change' do
response_double = double('A Faraday connection object')
expect_any_instance_of(Faraday::Connection).to receive(:post).and_return(response_double)
response_double = double('A Neo4j HTTP Connection')
expect_any_instance_of(Neo4j::Server::Connection).to receive(:post).and_return(response_double)
expect(response_double).to receive(:body).and_return({ 'password_change_required' => true })
expect { Neo4j::Session.open(:server_db, 'http://localhost:7474', basic_auth: { username: 'neo4j', password: @suite_default }) }
.to raise_error Neo4j::Server::CypherAuthentication::PasswordChangeRequiredError
Expand Down Expand Up @@ -150,7 +150,7 @@ def auth_breakdown
let(:auth_object) { Neo4j::Server::CypherAuthentication.new('http://localhost:7474') }

it 'can create a new, dedicated auth connection' do
expect(auth_object.connection).to be_a(Faraday::Connection)
expect(auth_object.connection).to be_a(Neo4j::Server::Connection)
end

it 'allows manual setting of basic auth params' do
Expand All @@ -165,7 +165,7 @@ def auth_breakdown
expect(auth_object.invalidate_token(:foo)['errors'][0]['code']).to eq 'Neo.ClientError.Security.AuthenticationFailed'
end

# Here, we're demonstrating that existing sessions with the server -- sessions with their own CypherAuthentication and Faraday::Connection objects --
# Here, we're demonstrating that existing sessions with the server -- sessions with their own CypherAuthentication and Neo4j::Server::Connection objects --
# are broken and required to reauthenticate when their tokens are invalidated.
# We establish a new session, create a node, prove that it is valid and can be loaded, invalidate the token, and then get an error when we try
# something that worked just a moment before.
Expand Down
10 changes: 2 additions & 8 deletions spec/neo4j-server/e2e/cypher_session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@ def open_named_session(name, default = nil)
Neo4j::Session.set_current(@before_session)
end

it 'can use a user supplied faraday connection for a new session' do
connection = Faraday.new do |b|
b.request :json
b.response :json, :content_type => "application/json"
b.adapter Faraday.default_adapter
end
connection.headers = {'Content-Type' => 'application/json'}

it 'can use a user supplied Neo4j::Server::Connection object for a new session' do
connection = Neo4j::Server::Connection.new
expect(connection).to receive(:get).at_least(:once).and_call_original
session = Neo4j::Session.open(:server_db, 'http://localhost:7474', { connection: connection })
end
Expand Down
12 changes: 5 additions & 7 deletions spec/neo4j-server/unit/cypher_session_unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,15 @@ def request
base_url = 'http://localhost:7474'
params = [base_url, auth]
session = Neo4j::Session.create_session(:server_db, params)
handlers = session.connection.builder.handlers.map(&:name)
expect(handlers).to include('Faraday::Request::BasicAuthentication')
expect(session.connection.basic_creds).to eq 'username:password'
end
end

describe 'with auth params inside URL' do
it 'creates session with basic auth params' do
url = 'http://username:password@localhost:7474'
url = 'http://username:password-foo@localhost:7474'
session = Neo4j::Session.create_session(:server_db, url)
handlers = session.connection.builder.handlers.map(&:name)
expect(handlers).to include('Faraday::Request::BasicAuthentication')
expect(session.connection.basic_creds).to eq 'username:password-foo'
end
end

Expand All @@ -133,11 +131,11 @@ def request

params = [base_url, init_params_false]
session_false = Neo4j::Session.create_session(:server_db, params)
expect(session_false.connection.ssl.verify).to be_falsey
expect(session_false.connection.verify_ssl?).to be_falsey

params = [base_url, init_params_true]
session_true = Neo4j::Session.create_session(:server_db, params)
expect(session_true.connection.ssl.verify).to be_truthy
expect(session_true.connection.verify_ssl?).to be_truthy
end
end

Expand Down

0 comments on commit 0d46714

Please sign in to comment.