rack-back_door 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6fa079c5c2922e21326349f5d466e6fb44d659d9
4
- data.tar.gz: 3a2efb08f13cd34864635b607291ab41e5637d26
3
+ metadata.gz: 8484b357ba7880ae4d782c8755c817b2929c46ed
4
+ data.tar.gz: 80ab747a444db352fb75707b7e8133ff87f95b81
5
5
  SHA512:
6
- metadata.gz: 720251a4cf23cab0b8fe96ba80acbfb0289bd0490c44eceb48198a2350ef65d0398d1100d12f2a569a8d2955b2bb06d5805a5b14184b841246305ba3787264e6
7
- data.tar.gz: 9e30496cb73550dd2f469426c3227d23fc5384d56fba4741e8c964dabe589dede7ff069d1d4141a1d0273d5b554d9dce0352bb4a12ab47bd163dc8d84cba41ee
6
+ metadata.gz: e5873ec981c95c5f7712a7fd36ea02cab60f9ce520a0c6772961aad36a017fd5f376c2ef9a33c05ddfb0c644ee4df4dce5585376ab37703d2395cba5c0e3cd76
7
+ data.tar.gz: 3fb7325a8437f54dd584d18a42939f1847394b4470da5f33487408667d2a21a6a15a509a68abb20372e00446c0f45ae9e40bca93020e283627468115d8776f64
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - jruby-19mode
6
+ - rbx-2
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Rack::BackDoor
2
2
 
3
+ [![Build
4
+ Status](https://travis-ci.org/seanpdoyle/rack-back_door.svg)](https://travis-ci.org/seanpdoyle/rack-back_door)
5
+
3
6
  Inject a user into a session for integration and controller tests.
4
7
 
5
8
  ## Installation
@@ -50,15 +53,6 @@ MyApplication::Application.configure do |config|
50
53
  end
51
54
  ```
52
55
 
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
56
  ## Setup in Sinatra Tests
63
57
 
64
58
  Add to your sinatra application:
@@ -90,22 +84,41 @@ You can configure the following:
90
84
  * `session_key` - The key used to hold the `user_id` in the session
91
85
  * `url_parameter` - The query parameter the middleware will use as the `user_id` value
92
86
 
87
+ When configured with these values and passed the following URL
88
+
89
+ http://example.com?login=1
90
+
91
+ The middleware will set the session like so
92
+
93
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"
94
+ session[:user] #=> 1
95
+ ```
96
+
97
+ To configure the middleware in a `sinatra` app:
98
+
99
+ ```ruby
100
+ # app.rb
101
+ class MySinatraApplication < Sinatra::Base
102
+ enable :sessions
103
+
104
+ if environment == "test"
105
+ use Rack::BackDoor, session_key: "user", url_parameter: "login"
106
+ end
107
+
108
+ # ...
105
109
  end
106
110
  ```
107
111
 
108
- In your `spec/spec_helper.rb`, or any other testing setup file
112
+ To configure the middleware in a `rails` app:
113
+
114
+ ```ruby
115
+ # config/test.rb
116
+ MyApplication::Application.configure do |config|
117
+ # ...
118
+ config.middleware.use "Rack::BackDoor", session_key: "user", url_parameter: "login"
119
+ # ...
120
+ end
121
+ ```
109
122
 
110
123
  ## Contributing
111
124
 
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.test_files = Dir.glob("test/**/*_test.rb")
6
+ end
7
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ require "sinatra"
2
+ require "rack/back_door"
3
+
4
+ enable :sessions
5
+
6
+ use Rack::BackDoor, session_key: "user", url_parameter: "login"
7
+
8
+ get "/" do
9
+ user_id = session["user"]
10
+
11
+ session.clear
12
+
13
+ user_id
14
+ end
@@ -1,56 +1,25 @@
1
- require 'rack/utils'
1
+ require "rack/utils"
2
2
 
3
3
  # Force a User to be authenticated
4
4
  # ala http://robots.thoughtbot.com/post/37907699673/faster-tests-sign-in-through-the-back-door
5
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)
6
+ def initialize(app, options = {})
36
7
  @app = app
8
+ @url_parameter = options.fetch(:url_parameter, "as")
9
+ @session_key = options.fetch(:session_key, "user_id")
37
10
  end
38
11
 
39
12
  def call(env)
40
13
  @env = env
14
+
41
15
  sign_in_through_the_back_door
42
- @app.call(@env)
16
+
17
+ app.call(env)
43
18
  end
44
19
 
45
20
  private
46
21
 
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
22
+ attr_reader :app, :url_parameter, :session_key, :env
54
23
 
55
24
  def sign_in_through_the_back_door
56
25
  if user_id = query_params[url_parameter]
@@ -59,10 +28,10 @@ class Rack::BackDoor
59
28
  end
60
29
 
61
30
  def session
62
- @env['rack.session'] ||= {}
31
+ env['rack.session'] ||= {}
63
32
  end
64
33
 
65
34
  def query_params
66
- Rack::Utils.parse_query(@env['QUERY_STRING'])
35
+ Rack::Utils.parse_query(env['QUERY_STRING'])
67
36
  end
68
37
  end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "rack-back_door"
7
- spec.version = "0.0.1"
7
+ spec.version = "0.1.0"
8
8
  spec.authors = ["Sean Doyle"]
9
9
  spec.email = ["sean.p.doyle24@gmail.com"]
10
10
  spec.summary = %q{Inject an authenticated user into a Rack session}
@@ -21,5 +21,6 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_development_dependency "bundler"
23
23
  spec.add_development_dependency "rake"
24
- spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "minitest"
25
+ spec.add_development_dependency "sinatra"
25
26
  end
@@ -0,0 +1,60 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../../lib", __FILE__)
2
+
3
+ require "minitest/autorun"
4
+ require "rack/mock"
5
+ require "rack/back_door"
6
+
7
+ class Rack::BackDoorTest < Minitest::Test
8
+ def test_does_nothing_without_url_parameter
9
+ middleware = Rack::BackDoor.new(app)
10
+ request = request_for("http://example.com")
11
+
12
+ _, env = middleware.call(request)
13
+
14
+ refute env_authenticated_with?(env, "user_id" => 1)
15
+ end
16
+
17
+ def test_authenticates_user_from_url_parameter
18
+ middleware = Rack::BackDoor.new(app)
19
+ request = request_for("http://example.com?as=1")
20
+
21
+ _, env = middleware.call(request)
22
+
23
+ assert env_authenticated_with?(env, "user_id" => 1)
24
+ end
25
+
26
+ def test_url_parameter_is_configurable
27
+ middleware = Rack::BackDoor.new(app, url_parameter: "login")
28
+ request = request_for("http://example.com?login=1")
29
+
30
+ _, env = middleware.call(request)
31
+
32
+ assert env_authenticated_with?(env, "user_id" => 1)
33
+ end
34
+
35
+ def test_session_key_is_configurable
36
+ middleware = Rack::BackDoor.new(app, session_key: "user")
37
+ request = request_for("http://example.com?as=1")
38
+
39
+ _, env = middleware.call(request)
40
+
41
+ assert env_authenticated_with?(env, "user" => 1)
42
+ end
43
+
44
+ def request_for(url, **options)
45
+ Rack::MockRequest.env_for(url, options)
46
+ end
47
+
48
+ def env_authenticated_with?(env, auth_hash)
49
+ session = env.fetch("rack.session", {})
50
+ session_key, user_id = auth_hash.first.map(&:to_s)
51
+
52
+ signed_in_user = session[session_key].to_i
53
+
54
+ user_id.to_i == signed_in_user
55
+ end
56
+
57
+ def app
58
+ ->(env) { [200, env, "success!"] }
59
+ end
60
+ end
metadata CHANGED
@@ -1,69 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-back_door
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Doyle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-22 00:00:00.000000000 Z
11
+ date: 2014-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rspec
56
+ name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sinatra
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  description: For unit, controller, or integration tests, inject an authenticated user
@@ -74,17 +88,17 @@ executables: []
74
88
  extensions: []
75
89
  extra_rdoc_files: []
76
90
  files:
77
- - .gitignore
78
- - .rspec
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
79
94
  - Gemfile
80
95
  - LICENSE.txt
81
96
  - README.md
82
97
  - Rakefile
98
+ - examples/sinatra.rb
83
99
  - lib/rack/back_door.rb
84
100
  - 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
101
+ - test/rack/back_door_test.rb
88
102
  homepage: https://github.com/seanpdoyle/rack-back_door
89
103
  licenses:
90
104
  - MIT
@@ -95,21 +109,20 @@ require_paths:
95
109
  - lib
96
110
  required_ruby_version: !ruby/object:Gem::Requirement
97
111
  requirements:
98
- - - '>='
112
+ - - ">="
99
113
  - !ruby/object:Gem::Version
100
114
  version: '0'
101
115
  required_rubygems_version: !ruby/object:Gem::Requirement
102
116
  requirements:
103
- - - '>='
117
+ - - ">="
104
118
  - !ruby/object:Gem::Version
105
119
  version: '0'
106
120
  requirements: []
107
121
  rubyforge_project:
108
- rubygems_version: 2.2.0
122
+ rubygems_version: 2.4.1
109
123
  signing_key:
110
124
  specification_version: 4
111
125
  summary: Inject an authenticated user into a Rack session
112
126
  test_files:
113
- - spec/lib/rack/back_door_spec.rb
114
- - spec/spec_helper.rb
115
- - spec/support/matchers/be_authenticated_with.rb
127
+ - test/rack/back_door_test.rb
128
+ has_rdoc:
@@ -1,62 +0,0 @@
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
data/spec/spec_helper.rb DELETED
@@ -1,17 +0,0 @@
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
@@ -1,9 +0,0 @@
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