Skip to content

Commit

Permalink
removed url validation from activegraph. Delegating to neo4j-ruby-dri…
Browse files Browse the repository at this point in the history
…ver.
  • Loading branch information
klobuczek committed Mar 4, 2020
1 parent b1b3ae6 commit 20f8ea8
Show file tree
Hide file tree
Showing 25 changed files with 33 additions and 175 deletions.
4 changes: 2 additions & 2 deletions lib/active_graph/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def establish_driver
@establish_driver_block.call if @establish_driver_block
end

def new_driver(url, options = {})
def new_driver(url, auth_token, options = {})
verbose_query_logs = ActiveGraph::Config.fetch(:verbose_query_logs, false)
ActiveGraph::Core::Driver
.new(url, options, verbose_query_logs: verbose_query_logs)
.new(url, auth_token, options, verbose_query_logs: verbose_query_logs)
end

def transaction
Expand Down
29 changes: 6 additions & 23 deletions lib/active_graph/core/driver.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
require 'active_support/core_ext/module/attribute_accessors'
require 'active_graph/core/logging'
require 'active_graph/core/has_uri'
require 'active_graph/version'

module ActiveGraph
module Core
class Driver
include HasUri

USER_AGENT_STRING = "neo4j-gem/#{::ActiveGraph::VERSION} (https://github.com/neo4jrb/neo4j)"
USER_AGENT_STRING = "activegraph-gem/#{::ActiveGraph::VERSION} (https://github.com/neo4jrb/activegraph)"

attr_accessor :wrap_level
attr_reader :options, :driver
attr_reader :options, :driver, :url
delegate :close, to: :driver

default_url('bolt://neo4:neo4j@localhost:7687')

validate_uri do |uri|
uri.scheme == 'bolt'
end

class << self
def new_instance(url, options = {})
uri = URI(url)
user = uri.user
password = uri.password
auth_token = if user
Neo4j::Driver::AuthTokens.basic(user, password)
else
Neo4j::Driver::AuthTokens.none
end
def new_instance(url, auth_token, options = {})
Neo4j::Driver::GraphDatabase.driver(url, auth_token, options)
end
end

def initialize(url, options = {}, extended_options = {})
self.url = url
@driver = self.class.new_instance(url, options)
def initialize(url, auth_token = Neo4j::Driver::AuthTokens.none, options = {}, extended_options = {})
@url = url
@driver = self.class.new_instance(url, auth_token, options)
@options = extended_options
end

Expand Down
63 changes: 0 additions & 63 deletions lib/active_graph/core/has_uri.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/active_graph/core/instrumentable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Instrumentable
def subscribe_to_request
ActiveSupport::Notifications.subscribe('neo4j.core.bolt.request') do |_, start, finish, _id, _payload|
ms = (finish - start) * 1000
yield " #{ANSI::BLUE}BOLT:#{ANSI::CLEAR} #{ANSI::YELLOW}#{ms.round}ms#{ANSI::CLEAR} #{Base.current_driver.url_without_password}"
yield " #{ANSI::BLUE}BOLT:#{ANSI::CLEAR} #{ANSI::YELLOW}#{ms.round}ms#{ANSI::CLEAR} #{Base.current_driver.url}"
end
end

Expand Down
8 changes: 5 additions & 3 deletions lib/active_graph/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ def empty_config
end

def setup!(neo4j_config = empty_config)
url, path, options = final_driver_config!(neo4j_config).values_at(:url, :path, :options)
options ||= {}
url, path, auth_token, username, password, config, options =
final_driver_config!(neo4j_config).values_at(:url, :path, :auth_token, :username, :password, :config, :options)
auth_token ||= username ? Neo4j::Driver::AuthTokens.basic(username, password) : Neo4j::Driver::AuthTokens.none
options ||= config || {}
register_neo4j_cypher_logging

ActiveGraph::Base.new_driver( url || path || default_driver_path_or_url, options)
ActiveGraph::Base.new_driver(url || path || default_driver_path_or_url, auth_token, options)
end

def final_driver_config!(neo4j_config)
Expand Down
24 changes: 16 additions & 8 deletions spec/active_graph/core/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,22 @@ def port
subject { driver }

describe '#initialize' do
let_context(url: 'url') { subject_should_raise ArgumentError, /Invalid URL/ }
let_context(url: :symbol) { subject_should_raise ArgumentError, /Invalid URL/ }
let_context(url: 123) { subject_should_raise ArgumentError, /Invalid URL/ }

let_context(url: "http://localhost:#{port}") { subject_should_raise ArgumentError, /Invalid URL/ }
let_context(url: "http://foo:bar@localhost:#{port}") { subject_should_raise ArgumentError, /Invalid URL/ }
let_context(url: "https://localhost:#{port}") { subject_should_raise ArgumentError, /Invalid URL/ }
let_context(url: "https://foo:bar@localhost:#{port}") { subject_should_raise ArgumentError, /Invalid URL/ }
let_context(url: 'url') { subject_should_raise ArgumentError, /Invalid address format/ }
let_context(url: :symbol) { subject_should_raise ArgumentError, /bad argument/ }
let_context(url: 123) { subject_should_raise ArgumentError, /bad argument/ }

let_context(url: "http://localhost:#{port}") do
subject_should_raise Neo4j::Driver::Exceptions::ClientException, /Unsupported URI scheme/
end
let_context(url: "http://foo:bar@localhost:#{port}") do
subject_should_raise Neo4j::Driver::Exceptions::ClientException, /Unsupported URI scheme/
end
let_context(url: "https://localhost:#{port}") do
subject_should_raise Neo4j::Driver::Exceptions::ClientException, /Unsupported URI scheme/
end
let_context(url: "https://foo:bar@localhost:#{port}") do
subject_should_raise Neo4j::Driver::Exceptions::ClientException, /Unsupported URI scheme/
end

let_context(url: 'bolt://foo@localhost:') { port == '7687' ? subject_should_not_raise : subject_should_raise }
let_context(url: "bolt://:foo@localhost:#{port}") { subject_should_not_raise }
Expand Down
66 changes: 0 additions & 66 deletions spec/active_graph/core/has_uri_spec.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/e2e/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Rails
let_env_variable(:NEO4J_URL) { 'bolt://localhost:7472' }

it 'calls ActiveGraph::Base' do
expect(ActiveGraph::Base).to have_received(:new_driver).with('bolt://localhost:7472', {})
expect(ActiveGraph::Base).to have_received(:new_driver).with('bolt://localhost:7472', Object, {})
end
end
end
Expand Down
10 changes: 2 additions & 8 deletions spec/test_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@ class TestDriver < ActiveGraph::Core::Driver
close_all
end

default_url('bolt://neo4:neo4j@localhost:7687')

validate_uri do |uri|
uri.scheme == 'bolt'
end

class << self
def new_instance(url, options = {})
cache[url] ||= super(url, options.merge(encryption: false))
def new_instance(url, auth_token, options = {})
cache[url] ||= super(url, auth_token, options.merge(encryption: false))
end

def close_all
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 20f8ea8

Please sign in to comment.