rack-back_door 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6fa079c5c2922e21326349f5d466e6fb44d659d9
4
+ data.tar.gz: 3a2efb08f13cd34864635b607291ab41e5637d26
5
+ SHA512:
6
+ metadata.gz: 720251a4cf23cab0b8fe96ba80acbfb0289bd0490c44eceb48198a2350ef65d0398d1100d12f2a569a8d2955b2bb06d5805a5b14184b841246305ba3787264e6
7
+ data.tar.gz: 9e30496cb73550dd2f469426c3227d23fc5384d56fba4741e8c964dabe589dede7ff069d1d4141a1d0273d5b554d9dce0352bb4a12ab47bd163dc8d84cba41ee
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-back_door.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Sean Doyle
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # Rack::BackDoor
2
+
3
+ Inject a user into a session for integration and controller tests.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rack-back_door'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```console
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```console
22
+ $ gem install rack-back_door
23
+ ```
24
+
25
+ # Usage
26
+
27
+ By default, `Rack::BackDoor` will handle the following URL:
28
+
29
+ ```
30
+ http://example.com?as=1
31
+ ```
32
+
33
+ By injecting `1` into the `session` as `user_id`
34
+
35
+ ```ruby
36
+ session[:user_id] # => 1
37
+ ```
38
+
39
+ ## Setup in Rails tests
40
+
41
+ Add the middleware to your stack in the `config/environments/test.rb` file
42
+
43
+ ```ruby
44
+ # config/environments/test.rb
45
+
46
+ MyApplication::Application.configure do |config|
47
+ # ...
48
+ config.middleware.use Rack::BackDoor
49
+ # ...
50
+ end
51
+ ```
52
+
53
+ or, in `rspec` spec helper
54
+
55
+ ```ruby
56
+ # spec/spec_helper.rb
57
+ MyApplication::Application.configure do |config|
58
+ config.middleware.use Rack::BackDoor
59
+ end
60
+ ```
61
+
62
+ ## Setup in Sinatra Tests
63
+
64
+ Add to your sinatra application:
65
+
66
+ ```ruby
67
+ # app.rb
68
+ class MySinatraApplication < Sinatra::Base
69
+ enable :sessions
70
+ use Rack::BackDoor if environment == :test
71
+ # ...
72
+ end
73
+ ```
74
+
75
+ If you use rspec you may prefer to inject middleware only for `rspec` tests:
76
+
77
+ Put into `spec/spec_helper.rb`:
78
+
79
+ ```ruby
80
+ # spec/spec_helper.rb
81
+ MySinatraApplication.configure do
82
+ use Rack::BackDoor
83
+ end
84
+ ```
85
+
86
+ ## Configuration
87
+
88
+ You can configure the following:
89
+
90
+ * `session_key` - The key used to hold the `user_id` in the session
91
+ * `url_parameter` - The query parameter the middleware will use as the `user_id` value
92
+
93
+ ```ruby
94
+ # When configured with these values and passed the following URL
95
+ #
96
+ # http://example.com?login=1
97
+ #
98
+ # The middleware will set the session like so
99
+ #
100
+ # session[:user] # => 1
101
+ #
102
+ Rack::BackDoor.configure do |config|
103
+ config.session_key = "user"
104
+ config.url_parameter = "login"
105
+ end
106
+ ```
107
+
108
+ In your `spec/spec_helper.rb`, or any other testing setup file
109
+
110
+ ## Contributing
111
+
112
+ 1. Fork it
113
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
114
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
115
+ 4. Push to the branch (`git push origin my-new-feature`)
116
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,68 @@
1
+ require 'rack/utils'
2
+
3
+ # Force a User to be authenticated
4
+ # ala http://robots.thoughtbot.com/post/37907699673/faster-tests-sign-in-through-the-back-door
5
+ class Rack::BackDoor
6
+ class << self
7
+ attr_accessor :configuration
8
+
9
+ def configure
10
+ yield configuration if block_given?
11
+ end
12
+
13
+ def configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ def url_parameter
18
+ configuration.url_parameter
19
+ end
20
+
21
+ def session_key
22
+ configuration.session_key
23
+ end
24
+ end
25
+
26
+ class Configuration
27
+ attr_accessor :url_parameter, :session_key
28
+
29
+ def initialize
30
+ @url_parameter = "as"
31
+ @session_key = "user_id"
32
+ end
33
+ end
34
+
35
+ def initialize(app)
36
+ @app = app
37
+ end
38
+
39
+ def call(env)
40
+ @env = env
41
+ sign_in_through_the_back_door
42
+ @app.call(@env)
43
+ end
44
+
45
+ private
46
+
47
+ def session_key
48
+ self.class.session_key.to_s
49
+ end
50
+
51
+ def url_parameter
52
+ self.class.url_parameter.to_s
53
+ end
54
+
55
+ def sign_in_through_the_back_door
56
+ if user_id = query_params[url_parameter]
57
+ session[session_key] = user_id.to_s
58
+ end
59
+ end
60
+
61
+ def session
62
+ @env['rack.session'] ||= {}
63
+ end
64
+
65
+ def query_params
66
+ Rack::Utils.parse_query(@env['QUERY_STRING'])
67
+ end
68
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rack-back_door"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Sean Doyle"]
9
+ spec.email = ["sean.p.doyle24@gmail.com"]
10
+ spec.summary = %q{Inject an authenticated user into a Rack session}
11
+ spec.description = %q{For unit, controller, or integration tests, inject an authenticated user into a Rack session}
12
+ spec.homepage = "https://github.com/seanpdoyle/rack-back_door"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "rack"
21
+
22
+ spec.add_development_dependency "bundler"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+ require 'pry'
3
+
4
+ describe Rack::BackDoor do
5
+ let(:app) { ->(env) { [200, env, "app"] } }
6
+
7
+ let :middleware do
8
+ Rack::BackDoor.new(app)
9
+ end
10
+
11
+ before do
12
+ Rack::BackDoor.configuration = nil
13
+ end
14
+
15
+ context "without the backdoor URL parameter" do
16
+ it "does nothing to the session" do
17
+ code, env = middleware.call env_for('http://example.com')
18
+
19
+ expect(env).not_to be_authenticated_with "user_id" => 1
20
+ end
21
+ end
22
+
23
+ context "with the backdoor URL parameter" do
24
+ it "stuffs the session" do
25
+ code, env = middleware.call env_for('http://example.com?as=1')
26
+
27
+ expect(env).to be_authenticated_with "user_id" => 1
28
+ end
29
+
30
+ context "when the URL parameter is configured" do
31
+ before do
32
+ Rack::BackDoor.configure do |config|
33
+ config.url_parameter = :login
34
+ end
35
+ end
36
+
37
+ it "stuffs the session with the default key" do
38
+ code, env = middleware.call env_for('http://example.com?login=1')
39
+
40
+ expect(env).to be_authenticated_with "user_id" => 1
41
+ end
42
+ end
43
+
44
+ context "when the session key is configured" do
45
+ before do
46
+ Rack::BackDoor.configure do |config|
47
+ config.session_key = :user
48
+ end
49
+ end
50
+
51
+ it "stuffs the session with the configured key" do
52
+ code, env = middleware.call env_for('http://example.com?as=1')
53
+
54
+ expect(env).to be_authenticated_with "user" => 1
55
+ end
56
+ end
57
+ end
58
+
59
+ def env_for(url, opts={})
60
+ Rack::MockRequest.env_for(url, opts)
61
+ end
62
+ end
@@ -0,0 +1,17 @@
1
+ require './lib/rack/back_door'
2
+
3
+ require 'rack/mock'
4
+
5
+ Dir["./spec/support/**/*.rb"].each {|f| require f }
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = "random"
17
+ end
@@ -0,0 +1,9 @@
1
+ RSpec::Matchers.define :be_authenticated_with do |options|
2
+ match do |env|
3
+ session = env.fetch("rack.session", {})
4
+ session_key = options.keys.first.to_s
5
+ user_id = options.values.first.to_i
6
+
7
+ expect(session[session_key].to_i).to eq user_id.to_i
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-back_door
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sean Doyle
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: For unit, controller, or integration tests, inject an authenticated user
70
+ into a Rack session
71
+ email:
72
+ - sean.p.doyle24@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rspec
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/rack/back_door.rb
84
+ - rack-back_door.gemspec
85
+ - spec/lib/rack/back_door_spec.rb
86
+ - spec/spec_helper.rb
87
+ - spec/support/matchers/be_authenticated_with.rb
88
+ homepage: https://github.com/seanpdoyle/rack-back_door
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.0
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Inject an authenticated user into a Rack session
112
+ test_files:
113
+ - spec/lib/rack/back_door_spec.rb
114
+ - spec/spec_helper.rb
115
+ - spec/support/matchers/be_authenticated_with.rb