sunspot-rails-http-basic-auth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2@sunspot-rails-http-basic-auth --create
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Justin Ko
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # sunspot-rails-http-basic-auth
2
+
3
+ All this gem does is patch rsolr and sunspot_rails to support
4
+ http basic authentication. In your `sunspot.yml` file, you can add
5
+ `user` and `password` keys:
6
+
7
+ development:
8
+ solr:
9
+ host: localhost
10
+ port: 8982
11
+ path: /solr
12
+ user: theuser
13
+ password: thepassword
14
+ log_level: INFO
15
+
16
+ ## Copyright
17
+
18
+ Copyright (c) 2011 Justin Ko. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ require 'rsolr'
2
+ require 'sunspot/rails'
3
+
4
+ require 'sunspot-rails-http-basic-auth/rsolr/connection/net_http'
5
+ require 'sunspot-rails-http-basic-auth/sunspot/rails'
6
+ require 'sunspot-rails-http-basic-auth/sunspot/rails/configuration'
@@ -0,0 +1,42 @@
1
+ module RSolr
2
+ module Connection
3
+ class NetHttp
4
+
5
+ def get(path, params={})
6
+ url = build_url(path, params)
7
+ net_http_response = basic_auth_get_request(url)
8
+ create_http_context(net_http_response, url, path, params)
9
+ end
10
+
11
+ def post(path, data, params={}, headers={})
12
+ url = build_url(path, params)
13
+ net_http_response = basic_auth_post_request(url, data, headers)
14
+ create_http_context(net_http_response, url, path, params, data, headers)
15
+ end
16
+
17
+ private
18
+
19
+ def basic_auth_get_request(url)
20
+ rails_logger "SOLR GET: #{url}"
21
+ req = add_basic_auth(Net::HTTP::Get.new(url))
22
+ connection.request(req)
23
+ end
24
+
25
+ def basic_auth_post_request(url, data, headers)
26
+ rails_logger "SOLR POST: #{url}"
27
+ req = add_basic_auth(Net::HTTP::Post.new(url, headers))
28
+ connection.request(req, data)
29
+ end
30
+
31
+ def add_basic_auth(req)
32
+ req.basic_auth(uri.user, uri.password) if uri.user and uri.password
33
+ req
34
+ end
35
+
36
+ def rails_logger(text)
37
+ Rails.logger.debug(text) if defined?(Rails)
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,35 @@
1
+ module Sunspot
2
+ module Rails
3
+ class <<self
4
+ private
5
+
6
+ def master_config(sunspot_rails_configuration)
7
+ build_config do |config|
8
+ config.solr.url = URI::HTTP.build(
9
+ :host => sunspot_rails_configuration.master_hostname,
10
+ :port => sunspot_rails_configuration.master_port,
11
+ :path => sunspot_rails_configuration.master_path,
12
+ :userinfo => [sunspot_rails_configuration.master_user, sunspot_rails_configuration.master_password]
13
+ ).to_s
14
+ end
15
+ end
16
+
17
+ def slave_config(sunspot_rails_configuration)
18
+ build_config do |config|
19
+ config.solr.url = URI::HTTP.build(
20
+ :host => sunspot_rails_configuration.hostname,
21
+ :port => sunspot_rails_configuration.port,
22
+ :path => sunspot_rails_configuration.path,
23
+ :userinfo => [sunspot_rails_configuration.user, sunspot_rails_configuration.password]
24
+ ).to_s
25
+ end
26
+ end
27
+
28
+ def build_config
29
+ config = Sunspot::Configuration.build
30
+ yield config
31
+ config
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ module Sunspot
2
+ module Rails
3
+ class Configuration
4
+
5
+ def user
6
+ @user ||= user_configuration_from_key('solr', 'user')
7
+ end
8
+
9
+ def master_user
10
+ @master_user ||= user_configuration_from_key('master_solr', 'user')
11
+ end
12
+
13
+ def password
14
+ @password ||= user_configuration_from_key('solr', 'password')
15
+ end
16
+
17
+ def master_password
18
+ @master_password ||= user_configuration_from_key('master_solr', 'password')
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+ require 'fakeweb'
3
+
4
+ module RSolr
5
+ module Connection
6
+ describe NetHttp do
7
+
8
+ describe '#get' do
9
+ context 'with valid basic auth credentials' do
10
+ before do
11
+ FakeWeb.register_uri :get,
12
+ 'http://user:pass@127.0.0.1:8983/solr/select?q=a',
13
+ :body => 'Authorized'
14
+ end
15
+
16
+ let(:net_http) { described_class.new(:url => 'http://user:pass@127.0.0.1:8983/solr') }
17
+
18
+ let(:result) { net_http.get('/select', :q => 'a') }
19
+
20
+ specify { result[:data].should be_nil }
21
+ specify { result[:body].should eq('Authorized') }
22
+ specify { result[:status_code].should eq(200) }
23
+ specify { result[:path].should eq('/select') }
24
+ specify { result[:url].should eq('http://127.0.0.1:8983/solr/select?q=a') }
25
+ specify { result[:headers].should be_empty }
26
+ specify { result[:params].should eq({:q => 'a'}) }
27
+ specify { result[:message].should eq('OK') }
28
+ end
29
+
30
+ context 'with invalid basic auth credentials' do
31
+ before do
32
+ FakeWeb.register_uri :get,
33
+ 'http://user:pass@127.0.0.1:8983/solr/select?q=a',
34
+ :body => 'Unauthorized',
35
+ :status => [401, 'Unauthorized']
36
+ end
37
+
38
+ let(:net_http) { described_class.new(:url => 'http://user:pass@127.0.0.1:8983/solr') }
39
+
40
+ let(:result) { net_http.get('/select', :q => 'a') }
41
+
42
+ specify { result[:data].should be_nil }
43
+ specify { result[:body].should eq('Unauthorized') }
44
+ specify { result[:status_code].should eq(401) }
45
+ specify { result[:path].should eq('/select') }
46
+ specify { result[:url].should eq('http://127.0.0.1:8983/solr/select?q=a') }
47
+ specify { result[:headers].should be_empty }
48
+ specify { result[:params].should eq({:q => 'a'}) }
49
+ specify { result[:message].should eq('Unauthorized') }
50
+ end
51
+ end
52
+
53
+ describe '#post' do
54
+ context 'with valid basic auth credentials' do
55
+ before do
56
+ FakeWeb.register_uri :post,
57
+ 'http://user:pass@127.0.0.1:8983/solr/update',
58
+ :body => 'Authorized'
59
+ end
60
+
61
+ let(:net_http) { described_class.new(:url => 'http://user:pass@127.0.0.1:8983/solr') }
62
+
63
+ let(:result) { net_http.post('/update', '<rollback/>') }
64
+
65
+ specify { result[:data].should eq('<rollback/>') }
66
+ specify { result[:body].should eq('Authorized') }
67
+ specify { result[:status_code].should eq(200) }
68
+ specify { result[:path].should eq('/update') }
69
+ specify { result[:url].should eq('http://127.0.0.1:8983/solr/update') }
70
+ specify { result[:headers].should be_empty }
71
+ specify { result[:params].should be_empty }
72
+ specify { result[:message].should eq('OK') }
73
+ end
74
+
75
+ context 'with invalid basic auth credentials' do
76
+ before do
77
+ FakeWeb.register_uri :post,
78
+ 'http://user:pass@127.0.0.1:8983/solr/update',
79
+ :body => 'Unauthorized',
80
+ :status => [401, 'Unauthorized']
81
+ end
82
+
83
+ let(:net_http) { described_class.new(:url => 'http://user:pass@127.0.0.1:8983/solr') }
84
+
85
+ let(:result) { net_http.post('/update', '<rollback/>') }
86
+
87
+ specify { result[:data].should eq('<rollback/>') }
88
+ specify { result[:body].should eq('Unauthorized') }
89
+ specify { result[:status_code].should eq(401) }
90
+ specify { result[:path].should eq('/update') }
91
+ specify { result[:url].should eq('http://127.0.0.1:8983/solr/update') }
92
+ specify { result[:headers].should be_empty }
93
+ specify { result[:params].should be_empty }
94
+ specify { result[:message].should eq('Unauthorized') }
95
+ end
96
+ end
97
+
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+
5
+ require 'sunspot-rails-http-basic-auth'
6
+
7
+ Dir['./spec/support/*.rb'].map {|f| require f }
8
+
9
+ RSpec.configure do |config|
10
+ config.color_enabled = true
11
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ module Sunspot
4
+ module Rails
5
+ describe Configuration do
6
+ let(:config) { subject }
7
+
8
+ before do
9
+ config.user_configuration = {
10
+ 'solr' => { 'user' => 'the_user', 'password' => 'the_password' },
11
+ 'master_solr' => { 'user' => 'the_master_user', 'password' => 'the_master_password' }
12
+ }
13
+ end
14
+
15
+ describe '#user' do
16
+ specify { config.user.should eq('the_user') }
17
+ end
18
+
19
+ describe '#master_user' do
20
+ specify { config.master_user.should eq('the_master_user') }
21
+ end
22
+
23
+ describe '#password' do
24
+ specify { config.password.should eq('the_password') }
25
+ end
26
+
27
+ describe '#master_password' do
28
+ specify { config.master_password.should eq('the_master_password') }
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ module Sunspot
4
+ describe Rails do
5
+ describe '.master_config' do
6
+ let(:configuration) do
7
+ double 'configuration', :master_hostname => 'masterlocalhost',
8
+ :master_port => 6666,
9
+ :master_path => '/mastersolr',
10
+ :master_user => 'masteruser',
11
+ :master_password => 'masterpassword'
12
+ end
13
+
14
+ let(:master_config) { subject.send(:master_config, configuration) }
15
+
16
+ specify { master_config.should be_an_instance_of(LightConfig::Configuration) }
17
+ specify { master_config.solr.url.should eq('http://masteruser:masterpassword@masterlocalhost:6666/mastersolr') }
18
+ end
19
+
20
+ describe '.slave_config' do
21
+ let(:configuration) do
22
+ double 'configuration', :hostname => 'localhost',
23
+ :port => 5555,
24
+ :path => '/solr',
25
+ :user => 'user',
26
+ :password => 'password'
27
+ end
28
+
29
+ let(:slave_config) { subject.send(:slave_config, configuration) }
30
+
31
+ specify { slave_config.should be_an_instance_of(LightConfig::Configuration) }
32
+ specify { slave_config.solr.url.should eq('http://user:password@localhost:5555/solr') }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'sunspot-rails-http-basic-auth'
6
+ s.version = '0.0.1'
7
+ s.platform = Gem::Platform::RUBY
8
+ s.author = 'Justin Ko'
9
+ s.email = 'jko170@gmail.com'
10
+ s.homepage = 'https://github.com/justinko/sunspot-rails-http-basic-auth'
11
+ s.summary = 'Patches rsolr and sunspot_rails to support HTTP Basic Authentication'
12
+ s.description = 'HTTP Basic Authentication support for sunspot_rails'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_path = 'lib'
18
+
19
+ s.add_dependency 'sunspot_rails', '~> 1.2.1'
20
+
21
+ s.add_development_dependency 'rspec', '~> 2.5'
22
+ s.add_development_dependency 'fakeweb'
23
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sunspot-rails-http-basic-auth
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Justin Ko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-09 00:00:00 -06:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: sunspot_rails
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: 1.2.1
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: "2.5"
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: fakeweb
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id003
49
+ description: HTTP Basic Authentication support for sunspot_rails
50
+ email: jko170@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - .rvmrc
60
+ - Gemfile
61
+ - LICENSE
62
+ - README.md
63
+ - Rakefile
64
+ - lib/sunspot-rails-http-basic-auth.rb
65
+ - lib/sunspot-rails-http-basic-auth/rsolr/connection/net_http.rb
66
+ - lib/sunspot-rails-http-basic-auth/sunspot/rails.rb
67
+ - lib/sunspot-rails-http-basic-auth/sunspot/rails/configuration.rb
68
+ - spec/rsolr/connection/net_http_spec.rb
69
+ - spec/spec_helper.rb
70
+ - spec/sunspot/rails/configuration_spec.rb
71
+ - spec/sunspot/rails_spec.rb
72
+ - sunspot-rails-http-basic-auth.gemspec
73
+ has_rdoc: true
74
+ homepage: https://github.com/justinko/sunspot-rails-http-basic-auth
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options: []
79
+
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.6.2
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Patches rsolr and sunspot_rails to support HTTP Basic Authentication
101
+ test_files:
102
+ - spec/rsolr/connection/net_http_spec.rb
103
+ - spec/spec_helper.rb
104
+ - spec/sunspot/rails/configuration_spec.rb
105
+ - spec/sunspot/rails_spec.rb