testy_cookie 1.0.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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +37 -0
- data/Rakefile +15 -0
- data/lib/testy_cookie/helper.rb +8 -0
- data/lib/testy_cookie/jar.rb +52 -0
- data/lib/testy_cookie/proxy.rb +45 -0
- data/lib/testy_cookie/railtie.rb +14 -0
- data/lib/testy_cookie/version.rb +3 -0
- data/lib/testy_cookie.rb +8 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 384c7f2580df5b1770079715553e53e3b70b086b926ffd041a048b3d29b4ba86
|
4
|
+
data.tar.gz: 63ae8ea44b06a00779f96fe6e8a90bb10296efdde57f54501d1d24d08c543945
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b5357627e4477c395d3373cfae8b8bdd39c6c94b26e7d6c10867acff294bf0f586fef2d40319980d3e8cd9a8ed78005eb19fd2a7b48336903b524e7bb638aadd
|
7
|
+
data.tar.gz: 690bb781ff91473eb1b8b96a7d7e28a1208472acd775650477066921d6317c6b8fa20c3575b73e4c4ecfcf42c9382d936d83ddf33f3054af99d7b7547f902864
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright Lucas Mendelowski
|
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,37 @@
|
|
1
|
+
# TestyCookie
|
2
|
+
|
3
|
+
`TestyCookie` provides a helper to access plain, permanent, signed and encrypted cookies consistently in Rails controller / integration / request tests.
|
4
|
+
|
5
|
+
## Why do I need a custom helper?
|
6
|
+
|
7
|
+
`ActionDispatch::IntegrationTest` based tests and RSpec request specs do not provide `encrypted`, `permanent` and `signed` stores on the default `cookies` jar. `TestyCookie` workaround it by initializing `ActionDispatch::Cookies::CookieJar` instance and propagating all changes back to the original `cookies` object (`Rack::Test::CookieJar` instance).
|
8
|
+
|
9
|
+
In `ActionController::TestCase` tests and RSpec controller specs (which are going to be deprecated), it's just an alias for the default `cookies` method.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Inside your controller / integration / request test, call `cookies_jar` helper to access cookies jar:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
cookies_jar.encrypted[:key]
|
17
|
+
cookies_jar.signed[:key] = value
|
18
|
+
cookies_jar.signed.encrypted.permanent[:key] = value
|
19
|
+
```
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
|
23
|
+
Add this line to your application's Gemfile:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
gem "testy_cookie"
|
27
|
+
```
|
28
|
+
|
29
|
+
And then execute:
|
30
|
+
|
31
|
+
```bash
|
32
|
+
$ bundle
|
33
|
+
```
|
34
|
+
|
35
|
+
## License
|
36
|
+
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path("test", __dir__))
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "bundler/gem_tasks"
|
5
|
+
require "rails/plugin/test"
|
6
|
+
|
7
|
+
task :test_dummy do
|
8
|
+
Dir.chdir("test/dummy") do
|
9
|
+
system "bin/rake test"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
Rake::Task[:test].enhance [:test_dummy]
|
14
|
+
|
15
|
+
task default: :test
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module TestyCookie
|
2
|
+
class Jar
|
3
|
+
# @param context [ActionController::TestCase, ActionDispatch::IntegrationTest, RSpec::ExampleGroup] the test context
|
4
|
+
def initialize(context)
|
5
|
+
@context = context
|
6
|
+
end
|
7
|
+
|
8
|
+
# @return [ActionDispatch::Cookies::CookieJar] the cookie jar to use
|
9
|
+
def cookies
|
10
|
+
if @context.cookies.is_a?(ActionDispatch::Cookies::CookieJar)
|
11
|
+
@context.cookies
|
12
|
+
else
|
13
|
+
Proxy.new(response_cookies || request_cookies, @context.cookies, nil)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def request
|
20
|
+
# Memoize the request object until the next request is made and cookies jar needs to be reevaluated
|
21
|
+
if @request != @context.request
|
22
|
+
@request = @context.request
|
23
|
+
@request_cookies = nil
|
24
|
+
end
|
25
|
+
|
26
|
+
@request
|
27
|
+
end
|
28
|
+
|
29
|
+
def request_cookies
|
30
|
+
@request_cookies ||= ActionDispatch::Request.new(Rails.application.env_config.deep_dup).cookie_jar
|
31
|
+
end
|
32
|
+
|
33
|
+
def response
|
34
|
+
# Memoize the response object until the next request is made and cookies jar needs to be reevaluated
|
35
|
+
if @response != @context.response
|
36
|
+
@response = @context.response
|
37
|
+
@response_cookies = nil
|
38
|
+
end
|
39
|
+
|
40
|
+
@response
|
41
|
+
end
|
42
|
+
|
43
|
+
def response_cookies
|
44
|
+
return unless response.present?
|
45
|
+
# We need to check for `path_parameters` in request because controller tests initialize
|
46
|
+
# request and response objects before the actual controller action is called.
|
47
|
+
return unless request&.path_parameters.present?
|
48
|
+
|
49
|
+
@response_cookies ||= ActionDispatch::Cookies::CookieJar.build(request, @context.cookies.to_hash)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module TestyCookie
|
2
|
+
# Proxy for propagating cookie changes to `Rack::Test::CookieJar`
|
3
|
+
class Proxy < SimpleDelegator
|
4
|
+
# @param cookies [ActionDispatch::Cookies::CookieJar] the cookie jar to delegate to
|
5
|
+
# @param raw_cookies [Rack::Test::CookieJar] the cookies to propagate changes to
|
6
|
+
# @param parent [Proxy, nil] the parent proxy to chain cookie operations
|
7
|
+
def initialize(cookies, raw_cookies, parent)
|
8
|
+
super(cookies)
|
9
|
+
@parent = parent
|
10
|
+
@raw_cookies = raw_cookies
|
11
|
+
end
|
12
|
+
|
13
|
+
# @param key [String] the cookie key
|
14
|
+
# @param value [String] the cookie value
|
15
|
+
def []=(key, value)
|
16
|
+
super
|
17
|
+
assign(key)
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [Proxy] proxy to the encrypted cookie jar
|
21
|
+
def encrypted
|
22
|
+
self.class.new(__getobj__.encrypted, @raw_cookies, self)
|
23
|
+
end
|
24
|
+
|
25
|
+
# @return [Proxy] proxy to the permanent cookie jar
|
26
|
+
def permanent
|
27
|
+
self.class.new(__getobj__.permanent, @raw_cookies, self)
|
28
|
+
end
|
29
|
+
|
30
|
+
# @return [Proxy] proxy the signed cookie jar
|
31
|
+
def signed
|
32
|
+
self.class.new(__getobj__.signed, @raw_cookies, self)
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def assign(key)
|
38
|
+
if @parent.present?
|
39
|
+
@parent.assign(key)
|
40
|
+
else
|
41
|
+
@raw_cookies[key] = __getobj__[key]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module TestyCookie
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
initializer "testy_cookie.add_helpers" do
|
4
|
+
ActiveSupport::TestCase.send(:include, TestyCookie::Helper)
|
5
|
+
|
6
|
+
if defined?(RSpec) && RSpec.respond_to?(:configure)
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.include TestyCookie::Helper, type: :controller
|
9
|
+
config.include TestyCookie::Helper, type: :request
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/testy_cookie.rb
ADDED
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: testy_cookie
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lucas Mendelowski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-02-22 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: 7.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 7.0.0
|
27
|
+
description: |-
|
28
|
+
TestyCookie provides a helper to access plain, permanent, signed and encrypted cookies
|
29
|
+
in Rails controller / integration / request tests.
|
30
|
+
email:
|
31
|
+
- lucas@mendelowski.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- MIT-LICENSE
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- lib/testy_cookie.rb
|
40
|
+
- lib/testy_cookie/helper.rb
|
41
|
+
- lib/testy_cookie/jar.rb
|
42
|
+
- lib/testy_cookie/proxy.rb
|
43
|
+
- lib/testy_cookie/railtie.rb
|
44
|
+
- lib/testy_cookie/version.rb
|
45
|
+
homepage: https://github.com/lcmen/testy_cookie
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata:
|
49
|
+
homepage_uri: https://github.com/lcmen/testy_cookie
|
50
|
+
source_code_uri: https://github.com/lcmen/testy_cookie
|
51
|
+
changelog_uri: https://github.com/lcmen/testy_cookie/blob/main/CHANGELOG.md
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubygems_version: 3.5.3
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: Helpers for accessing cookies in Rails tests.
|
71
|
+
test_files: []
|