test_session_manager 1.0.0

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: 13e1784c029e1b5e47f9ebcb37f2e3256834583f
4
+ data.tar.gz: 4fd59a7702e8b2ca7ed26d77dc32c9e6c5e3add3
5
+ SHA512:
6
+ metadata.gz: 8da626a35382af89bed41b55e2f2826b1f812cda59155fa6e0ab987593337ad0c8828607cd9afce0fb929718d8e0d9abe791ed2c7b07b20986ca6e56f592e397
7
+ data.tar.gz: 8794464d99956b7e3a0a7941ed973c893d8aa0f11cd1b59b1a1ea0a9453e115e99a2065ccf653fe2841e76f8cf4e475755bf465521b6ec9c624ca0f40fd80a92
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,128 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ test_session_manager (1.0.0)
5
+ rails (>= 4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.2.1)
11
+ actionpack (= 5.2.1)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailer (5.2.1)
15
+ actionpack (= 5.2.1)
16
+ actionview (= 5.2.1)
17
+ activejob (= 5.2.1)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 2.0)
20
+ actionpack (5.2.1)
21
+ actionview (= 5.2.1)
22
+ activesupport (= 5.2.1)
23
+ rack (~> 2.0)
24
+ rack-test (>= 0.6.3)
25
+ rails-dom-testing (~> 2.0)
26
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
+ actionview (5.2.1)
28
+ activesupport (= 5.2.1)
29
+ builder (~> 3.1)
30
+ erubi (~> 1.4)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
+ activejob (5.2.1)
34
+ activesupport (= 5.2.1)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.2.1)
37
+ activesupport (= 5.2.1)
38
+ activerecord (5.2.1)
39
+ activemodel (= 5.2.1)
40
+ activesupport (= 5.2.1)
41
+ arel (>= 9.0)
42
+ activestorage (5.2.1)
43
+ actionpack (= 5.2.1)
44
+ activerecord (= 5.2.1)
45
+ marcel (~> 0.3.1)
46
+ activesupport (5.2.1)
47
+ concurrent-ruby (~> 1.0, >= 1.0.2)
48
+ i18n (>= 0.7, < 2)
49
+ minitest (~> 5.1)
50
+ tzinfo (~> 1.1)
51
+ arel (9.0.0)
52
+ builder (3.2.3)
53
+ concurrent-ruby (1.0.5)
54
+ crass (1.0.4)
55
+ erubi (1.7.1)
56
+ globalid (0.4.1)
57
+ activesupport (>= 4.2.0)
58
+ i18n (1.1.0)
59
+ concurrent-ruby (~> 1.0)
60
+ loofah (2.2.2)
61
+ crass (~> 1.0.2)
62
+ nokogiri (>= 1.5.9)
63
+ mail (2.7.0)
64
+ mini_mime (>= 0.1.1)
65
+ marcel (0.3.2)
66
+ mimemagic (~> 0.3.2)
67
+ method_source (0.9.0)
68
+ mimemagic (0.3.2)
69
+ mini_mime (1.0.1)
70
+ mini_portile2 (2.3.0)
71
+ minitest (5.11.3)
72
+ nio4r (2.3.1)
73
+ nokogiri (1.8.4)
74
+ mini_portile2 (~> 2.3.0)
75
+ rack (2.0.5)
76
+ rack-test (1.1.0)
77
+ rack (>= 1.0, < 3)
78
+ rails (5.2.1)
79
+ actioncable (= 5.2.1)
80
+ actionmailer (= 5.2.1)
81
+ actionpack (= 5.2.1)
82
+ actionview (= 5.2.1)
83
+ activejob (= 5.2.1)
84
+ activemodel (= 5.2.1)
85
+ activerecord (= 5.2.1)
86
+ activestorage (= 5.2.1)
87
+ activesupport (= 5.2.1)
88
+ bundler (>= 1.3.0)
89
+ railties (= 5.2.1)
90
+ sprockets-rails (>= 2.0.0)
91
+ rails-dom-testing (2.0.3)
92
+ activesupport (>= 4.2.0)
93
+ nokogiri (>= 1.6)
94
+ rails-html-sanitizer (1.0.4)
95
+ loofah (~> 2.2, >= 2.2.2)
96
+ railties (5.2.1)
97
+ actionpack (= 5.2.1)
98
+ activesupport (= 5.2.1)
99
+ method_source
100
+ rake (>= 0.8.7)
101
+ thor (>= 0.19.0, < 2.0)
102
+ rake (12.3.1)
103
+ sprockets (3.7.2)
104
+ concurrent-ruby (~> 1.0)
105
+ rack (> 1, < 3)
106
+ sprockets-rails (3.2.1)
107
+ actionpack (>= 4.0)
108
+ activesupport (>= 4.0)
109
+ sprockets (>= 3.0.0)
110
+ thor (0.20.0)
111
+ thread_safe (0.3.6)
112
+ tzinfo (1.2.5)
113
+ thread_safe (~> 0.1)
114
+ websocket-driver (0.7.0)
115
+ websocket-extensions (>= 0.1.0)
116
+ websocket-extensions (0.1.3)
117
+
118
+ PLATFORMS
119
+ ruby
120
+
121
+ DEPENDENCIES
122
+ minitest
123
+ rack-test
124
+ rake
125
+ test_session_manager!
126
+
127
+ BUNDLED WITH
128
+ 1.16.2
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Jamis Buck
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,61 @@
1
+ # Test Session Manager
2
+
3
+ Have you ever wanted to inject session information into a request in your Rails tests? Sure, best practices recommend you do so by first making a request to a resource that sets the desired session information for you, but sometimes...well, sometimes you just want a shortcut.
4
+
5
+ That's what the Test Session Manager gem does for you: lets your tests specify the contents of the session explicitly before making a request.
6
+
7
+
8
+ ## Installation, Configuration, and Usage
9
+
10
+ Add the "test_session_manager" gem to your Gemfile:
11
+
12
+ ```ruby
13
+ group :test
14
+ gem 'test_session_manager'
15
+ end
16
+ ```
17
+
18
+ Add the Test Session Manager middleware to `config/application.rb`:
19
+
20
+ ```ruby
21
+ if Rails.env.test?
22
+ # initialize the manager that your tests will use
23
+ manager = TestSessionManager.new
24
+
25
+ # set it in your application's config, so your tests can find it
26
+ config.test_session_manager = manager
27
+
28
+ # install the middleware
29
+ config.middleware.use TestSessionManager::Middleware, manager
30
+ end
31
+ ```
32
+
33
+ Add a minimal helper to your tests:
34
+
35
+ ```ruby
36
+ class ActiveSupport::TestCase
37
+ # ...
38
+
39
+ def next_request
40
+ Rails.application.config.test_session_manager
41
+ end
42
+ end
43
+ ```
44
+
45
+ Then, use the `next_request` helper to set session and flash values in your tests!
46
+
47
+ ```ruby
48
+ test 'show that session and flash can be set in tests' do
49
+ next_request.flash[:error] = "Something died!"
50
+ next_request.session[:favorite_color] = "green"
51
+
52
+ get '/path/to/test'
53
+ assert_select '.alert-error', 'Something died!'
54
+ assert_select '.favorite-color', 'green'
55
+ end
56
+ ```
57
+
58
+ ## License
59
+
60
+ BulkInsert is released under the MIT license (see MIT-LICENSE) by
61
+ Jamis Buck (jamis@jamisbuck.org).
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ Bundler::GemHelper.install_tasks
10
+
11
+ require 'rake/testtask'
12
+
13
+ Rake::TestTask.new(:test) do |t|
14
+ t.libs << 'lib'
15
+ t.libs << 'test'
16
+ t.pattern = 'test/**/*_test.rb'
17
+ t.verbose = true
18
+ end
19
+
20
+ task default: :test
@@ -0,0 +1,14 @@
1
+ require 'test_session_manager/middleware'
2
+
3
+ class TestSessionManager
4
+ attr_reader :flash, :session
5
+
6
+ def initialize
7
+ reset!
8
+ end
9
+
10
+ def reset!
11
+ @flash = {}
12
+ @session = {}
13
+ end
14
+ end
@@ -0,0 +1,36 @@
1
+ require 'action_dispatch/middleware/flash'
2
+
3
+ class TestSessionManager
4
+ class Middleware
5
+ def initialize(app, manager)
6
+ @app = app
7
+ @manager = manager
8
+ end
9
+
10
+ def call(env)
11
+ session = env['rack.session']
12
+
13
+ if @manager.flash.any?
14
+ flash = flash_from_session(session['flash'])
15
+ flash.update(@manager.flash)
16
+ session['flash'] = flash.to_session_value
17
+ end
18
+
19
+ @manager.session.each do |key, value|
20
+ session[key] = value
21
+ end
22
+
23
+ @manager.reset!
24
+
25
+ @app.call(env)
26
+ end
27
+
28
+ def flash_from_session(value)
29
+ if value.is_a?(Hash)
30
+ ActionDispatch::Flash::FlashHash.new(value['flashes'], value['discard'] || [])
31
+ else
32
+ ActionDispatch::Flash::FlashHash.new
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ class TestSessionManager
2
+ module Version
3
+ STRING = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,65 @@
1
+ require 'test_helper'
2
+
3
+ class MiddlewareTest < ActiveSupport::TestCase
4
+ setup do
5
+ @app = ->(env) { :done }
6
+ @session = {}
7
+ @env = { 'rack.session' => @session }
8
+
9
+ @manager = TestSessionManager.new
10
+ @middleware = TestSessionManager::Middleware.new(@app, @manager)
11
+ end
12
+
13
+ test 'middleware with no manager data added' do
14
+ assert @manager.flash.empty?
15
+ assert @manager.session.empty?
16
+
17
+ assert @session['flash'].blank?
18
+ assert @session['session'].blank?
19
+
20
+ @middleware.call(@env)
21
+
22
+ assert @session['flash'].blank?
23
+ assert @session['session'].blank?
24
+ end
25
+
26
+ test 'middleware adds session values' do
27
+ @manager.session['first'] = '1st'
28
+ @manager.session['second'] = '2nd'
29
+
30
+ @middleware.call(@env)
31
+
32
+ assert_equal '1st', @session['first']
33
+ assert_equal '2nd', @session['second']
34
+ end
35
+
36
+ test 'middleware adds flash values' do
37
+ @manager.flash['first'] = '1st'
38
+ @manager.flash['second'] = '2nd'
39
+
40
+ @middleware.call(@env)
41
+
42
+ assert_equal({ 'flashes' => { 'first' => '1st', 'second' => '2nd' }, 'discard' => [] }, @session['flash'])
43
+ end
44
+
45
+ test 'middleware preserves existing flash values' do
46
+ @session['flash'] = { 'flashes' => { 'original' => 'keep', 'old' => 'lose' }, 'discard' => [ 'old' ] }
47
+
48
+ @manager.flash['first'] = '1st'
49
+ @manager.flash['second'] = '2nd'
50
+
51
+ @middleware.call(@env)
52
+
53
+ assert_equal({ 'flashes' => { 'original' => 'keep', 'first' => '1st', 'second' => '2nd' }, 'discard' => [] }, @session['flash'])
54
+ end
55
+
56
+ test 'middleware resets manager on each request' do
57
+ @manager.session['a'] = 'b'
58
+ @manager.flash['b'] = 'a'
59
+
60
+ @middleware.call(@env)
61
+
62
+ assert @manager.session.blank?
63
+ assert @manager.flash.blank?
64
+ end
65
+ end
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+
3
+ class SessionManagerTest < ActiveSupport::TestCase
4
+ setup do
5
+ @manager = TestSessionManager.new
6
+ end
7
+
8
+ test 'test session manager starts with empty hashes' do
9
+ assert @manager.flash.empty?
10
+ assert @manager.session.empty?
11
+ end
12
+
13
+ test 'reset! removes all values from hashes' do
14
+ @manager.flash[:a] = "something"
15
+ @manager.session[:b] = "something"
16
+
17
+ refute @manager.flash.empty?
18
+ refute @manager.session.empty?
19
+
20
+ @manager.reset!
21
+
22
+ assert @manager.flash.empty?
23
+ assert @manager.session.empty?
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ require 'minitest/autorun'
2
+ require 'active_support'
3
+ require 'rack/test'
4
+
5
+ require 'test_session_manager'
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "test_session_manager/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.version = TestSessionManager::Version::STRING
7
+ gem.name = "test_session_manager"
8
+ gem.authors = ["Jamis Buck"]
9
+ gem.email = ["jamis@jamisbuck.org"]
10
+ gem.homepage = "http://github.com/jamis/test-session-manager"
11
+ gem.summary = "Inject session data into test requests"
12
+ gem.description = "Allow tests for Rails applications to inject session data (including flash) into test requests"
13
+ gem.license = 'MIT'
14
+
15
+ gem.files = `git ls-files`.split($\)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^test/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "rails", ">= 4.0"
21
+
22
+ ##
23
+ # Development dependencies
24
+ #
25
+ gem.add_development_dependency "rake"
26
+ gem.add_development_dependency "minitest"
27
+ gem.add_development_dependency "rack-test"
28
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: test_session_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jamis Buck
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: minitest
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: rack-test
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: Allow tests for Rails applications to inject session data (including
70
+ flash) into test requests
71
+ email:
72
+ - jamis@jamisbuck.org
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - MIT-LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - lib/test_session_manager.rb
83
+ - lib/test_session_manager/middleware.rb
84
+ - lib/test_session_manager/version.rb
85
+ - test/middleware_test.rb
86
+ - test/session_manager_test.rb
87
+ - test/test_helper.rb
88
+ - test_session_manager.gemspec
89
+ homepage: http://github.com/jamis/test-session-manager
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.6.14
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Inject session data into test requests
113
+ test_files:
114
+ - test/middleware_test.rb
115
+ - test/session_manager_test.rb
116
+ - test/test_helper.rb