token_action 0.0.1 → 0.0.2
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/LICENSE +1 -1
- data/README.md +11 -18
- data/Rakefile +13 -4
- data/app/controllers/token_action/tokens_controller.rb +19 -11
- data/config/routes.rb +1 -1
- data/lib/generators/active_record/templates/migration.rb +1 -1
- data/lib/generators/templates/README +1 -1
- data/lib/token_action.rb +2 -2
- data/lib/token_action/{mixins → concerns}/model.rb +0 -0
- data/lib/token_action/{mixins → concerns}/token_generator.rb +0 -0
- data/lib/token_action/orm/mongoid.rb +6 -6
- data/lib/token_action/version.rb +1 -1
- data/spec/controllers/token_action/tokens_controller_spec.rb +159 -157
- data/spec/dummy/Rakefile +1 -2
- data/spec/dummy/app/assets/javascripts/application.js +4 -4
- data/spec/dummy/app/assets/stylesheets/application.css +6 -4
- data/spec/dummy/app/controllers/application_controller.rb +3 -1
- data/spec/dummy/app/controllers/hello_controller.rb +6 -6
- data/spec/dummy/app/mongoid/cat.rb +1 -1
- data/spec/dummy/app/views/layouts/application.html.erb +2 -2
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/config.ru +2 -2
- data/spec/dummy/config/application.rb +11 -42
- data/spec/dummy/config/boot.rb +4 -9
- data/spec/dummy/config/database.yml +14 -7
- data/spec/dummy/config/environment.rb +3 -3
- data/spec/dummy/config/environments/development.rb +22 -18
- data/spec/dummy/config/environments/production.rb +46 -34
- data/spec/dummy/config/environments/test.rb +18 -17
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +1 -6
- data/spec/dummy/config/initializers/wrap_parameters.rb +6 -6
- data/spec/dummy/config/locales/en.yml +20 -2
- data/spec/dummy/config/mongoid.yml +4 -60
- data/spec/dummy/config/routes.rb +6 -6
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/db/migrate/20130104151630_create_token_action_tokens.rb +1 -1
- data/spec/dummy/db/schema.rb +32 -0
- data/spec/dummy/{db/development.sqlite3 → log/.keep} +0 -0
- data/spec/dummy/public/404.html +54 -13
- data/spec/dummy/public/422.html +54 -13
- data/spec/dummy/public/500.html +53 -12
- data/spec/factories.rb +1 -1
- data/spec/rails_helper.rb +102 -0
- data/spec/routing/token_action/tokens_routing_spec.rb +23 -23
- data/spec/spec_helper.rb +85 -87
- data/spec/token_action/concerns/model_spec.rb +14 -0
- data/spec/token_action/concerns/token_generator_spec.rb +13 -0
- data/spec/token_action/orm/active_record_spec.rb +4 -2
- data/spec/token_action/orm/mongoid_spec.rb +4 -2
- data/spec/token_action_spec.rb +9 -9
- metadata +109 -158
- data/spec/dummy/config/initializers/secret_token.rb +0 -7
- data/spec/dummy/log/development.log +0 -16
- data/spec/dummy/log/test.log +0 -4739
- data/spec/dummy/script/rails +0 -6
- data/spec/token_action/mixins/model_spec.rb +0 -13
- data/spec/token_action/mixins/token_generator_spec.rb +0 -11
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 85d900369c77734a24aa10bc37527ce4bb7a47c5
|
4
|
+
data.tar.gz: 0dafd5321105460b8eb6dec7179406dfd024ffe2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f44e1b756a0917db2b7a65d4e9852cd2d86a40b977f4fdb2756bedde6edfefe961cbfb1da0b2e854b655cef11f5ea8bac84e8b74d8a4c10ad747ce07f5188bb8
|
7
|
+
data.tar.gz: c0f1dd85cdd3d9f4a7e7446ef3bfc2886bb28e4ad038310fb5b526f02de37454e44f8065b7187223535c5652e4ec148b452c2d44e103486ab296dedd943a2acb
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# TokenAction
|
2
2
|
|
3
|
-
[](https://badge.fury.io/rb/token_action)
|
4
|
+
[](https://travis-ci.org/jpmckinney/token_action)
|
5
|
+
[](https://gemnasium.com/jpmckinney/token_action)
|
6
|
+
[](https://coveralls.io/r/jpmckinney/token_action)
|
7
|
+
[](https://codeclimate.com/github/jpmckinney/token_action)
|
6
8
|
|
7
9
|
TokenAction lets you create tokens to be used to authenticate actions. For example:
|
8
10
|
|
@@ -29,7 +31,7 @@ If you are using ActiveRecord, run the migration:
|
|
29
31
|
|
30
32
|
Create a shared secret to confirm a user account, for example:
|
31
33
|
|
32
|
-
token = TokenAction::Token.create! :
|
34
|
+
token = TokenAction::Token.create! kind: 'User', args: [1, :confirm]
|
33
35
|
token.token # a 20-character alphanumeric string like "j7NtCaYfUpZXyDCseKG2"
|
34
36
|
|
35
37
|
You can then send the token with instructions to the user via email. When the user visits `/tokens/j7NtCaYfUpZXyDCseKG2/confirm`, TokenAction will look up the token. If not found, it will log an informational message to the Rails logger, set `flash[:alert]` and redirect to the `root_path`. If found, it will call the `redeem_token` method on the `User` class, passing in the arguments `1` and `:confirm`. You must implement the public `redeem_token` method. For example:
|
@@ -54,7 +56,7 @@ You can customize the redirect URLs, the routes, the flash messages and the toke
|
|
54
56
|
|
55
57
|
To change the default success and failure URLs from `root_path`, edit the `config/initializers/token_action.rb` file created by `rails generate token_action`. You may also set success and failure URLs for each token, by creating tokens with `:success_url` and `:failure_url` arguments:
|
56
58
|
|
57
|
-
token = TokenAction::Token.create! :
|
59
|
+
token = TokenAction::Token.create! kind: 'Cat', success_url: cat_path(1), failure_url: '/oops'
|
58
60
|
|
59
61
|
**Note:** If you change your URL structure after creating tokens, TokenAction may attempt to redirect to an unroutable path. If a path is unroutable, TokenAction will redirect to another URL in this order of precedence:
|
60
62
|
|
@@ -72,13 +74,13 @@ If an exception was raised and a path is unroutable, it will redirect in this or
|
|
72
74
|
|
73
75
|
The TokenAction generator will add `mount TokenAction::Engine => '/token_action'` to your routes, which defines:
|
74
76
|
|
75
|
-
get 'tokens/:token/*path', :
|
77
|
+
get 'tokens/:token/*path', to: 'tokens#redeem'
|
76
78
|
|
77
79
|
As such, you can write URLs like `tokens/xxx/confirm` or `tokens/xxx/unsubscribe` or `tokens/xxx/a/b/c`.
|
78
80
|
|
79
81
|
To customize the first part of the route, replace `mount TokenAction::Engine => '/token_action'` with something like:
|
80
82
|
|
81
|
-
get 'jetons/:token/*path', :
|
83
|
+
get 'jetons/:token/*path', to: 'token_action/tokens#redeem'
|
82
84
|
|
83
85
|
### Flash messages
|
84
86
|
|
@@ -110,7 +112,7 @@ To customize the controller without monkey patching, create a new controller lik
|
|
110
112
|
|
111
113
|
And replace the route with something like:
|
112
114
|
|
113
|
-
get 'tokens/:token/*path', :
|
115
|
+
get 'tokens/:token/*path', to: 'tokens#redeem'
|
114
116
|
|
115
117
|
You can then override the `redeem` method in your new controller.
|
116
118
|
|
@@ -131,13 +133,4 @@ If using Sequel, you will want to use the [orm_adapter-sequel](https://github.co
|
|
131
133
|
* If you change the name of a class, update the `kind` attribute on its tokens to avoid making them unprocessable.
|
132
134
|
* Be careful when changing the behavior of a `redeem_token` method, to avoid making tokens unprocessable.
|
133
135
|
|
134
|
-
|
135
|
-
|
136
|
-
* The tokens controller only responds to HTML requests. Pull requests to add JSON and XML responders are welcome!
|
137
|
-
* The generators do not have specs.
|
138
|
-
|
139
|
-
## Bugs? Questions?
|
140
|
-
|
141
|
-
This engine's main repository is on GitHub: [http://github.com/opennorth/token_action](http://github.com/opennorth/token_action), where your contributions, forks, bug reports, feature requests, and feedback are greatly welcomed.
|
142
|
-
|
143
|
-
Copyright (c) 2012 Open North Inc., released under the MIT license
|
136
|
+
Copyright (c) 2012 James McKinney, released under the MIT license
|
data/Rakefile
CHANGED
@@ -1,10 +1,19 @@
|
|
1
|
-
|
2
|
-
|
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
|
+
APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
|
8
|
+
load 'rails/tasks/engine.rake'
|
9
|
+
|
10
|
+
require 'thor'
|
11
|
+
helper = Bundler::GemHelper.new
|
12
|
+
helper.install
|
3
13
|
|
4
14
|
require 'rspec/core/rake_task'
|
5
15
|
RSpec::Core::RakeTask.new(:spec)
|
6
|
-
|
7
|
-
task :default => :spec
|
16
|
+
task default: :spec
|
8
17
|
|
9
18
|
begin
|
10
19
|
require 'yard'
|
@@ -1,20 +1,22 @@
|
|
1
|
+
require_dependency 'token_action/application_controller'
|
2
|
+
|
1
3
|
module TokenAction
|
2
4
|
class TokensController < ApplicationController
|
3
5
|
def redeem
|
4
|
-
@token = TokenAction::Token.to_adapter.find_first(:
|
6
|
+
@token = TokenAction::Token.to_adapter.find_first(token: params[:token])
|
5
7
|
if @token
|
6
8
|
begin
|
7
9
|
# Do this in the controller's scope to allow access to request methods.
|
8
10
|
ActiveSupport::Inflector.constantize(@token.kind).redeem_token(*@token.args)
|
9
|
-
redirect_to success_url, :
|
11
|
+
redirect_to success_url, notice: translate(:success)
|
10
12
|
rescue => e
|
11
|
-
key = ActiveSupport::Inflector.underscore(e.class).gsub('/', I18n.default_separator)
|
13
|
+
key = ActiveSupport::Inflector.underscore(e.class.name).gsub('/', I18n.default_separator)
|
12
14
|
logger.warn "TokenAction failed to perform the action for the token '#{params[:token]}': #{e.message}"
|
13
|
-
redirect_to failure_url, :
|
15
|
+
redirect_to failure_url, alert: translate(key, :failure)
|
14
16
|
end
|
15
17
|
else
|
16
18
|
logger.info "TokenAction failed to find the token '#{params[:token]}'"
|
17
|
-
redirect_to failure_url, :
|
19
|
+
redirect_to failure_url, alert: translate(:not_found)
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
@@ -39,23 +41,29 @@ module TokenAction
|
|
39
41
|
end
|
40
42
|
|
41
43
|
key, *default = *keys
|
42
|
-
I18n.translate(key, :
|
44
|
+
I18n.translate(key, default: default)
|
43
45
|
end
|
44
46
|
|
45
47
|
# Returns the success URL.
|
46
48
|
#
|
47
49
|
# @return [String] a URL
|
48
50
|
def success_url
|
49
|
-
[@token && @token.success_url, TokenAction.success_url]
|
50
|
-
url.present? && routable?(url)
|
51
|
-
end || main_app.root_path
|
51
|
+
redirect_url([@token && @token.success_url, TokenAction.success_url])
|
52
52
|
end
|
53
53
|
|
54
54
|
# Returns the failure URL.
|
55
55
|
#
|
56
56
|
# @return [String] a URL
|
57
57
|
def failure_url
|
58
|
-
[@token && @token.failure_url, TokenAction.failure_url]
|
58
|
+
redirect_url([@token && @token.failure_url, TokenAction.failure_url])
|
59
|
+
end
|
60
|
+
|
61
|
+
# Returns the routeable URL with the highest preference.
|
62
|
+
#
|
63
|
+
# @param [Array<String>] a list of URLs in order of preference
|
64
|
+
# @return [String] the routable URL with the highest preference
|
65
|
+
def redirect_url(urls)
|
66
|
+
urls.find do |url|
|
59
67
|
url.present? && routable?(url)
|
60
68
|
end || main_app.root_path
|
61
69
|
end
|
@@ -65,7 +73,7 @@ module TokenAction
|
|
65
73
|
# @param [String] path a path
|
66
74
|
# @return [Boolean] whether the path is routable
|
67
75
|
def routable?(path)
|
68
|
-
!!Rails.application.routes.recognize_path(path, :
|
76
|
+
!!Rails.application.routes.recognize_path(path, method: :get)
|
69
77
|
rescue ActionController::RoutingError
|
70
78
|
false
|
71
79
|
end
|
data/config/routes.rb
CHANGED
data/lib/token_action.rb
CHANGED
File without changes
|
File without changes
|
@@ -10,25 +10,25 @@ module TokenAction
|
|
10
10
|
|
11
11
|
# A shared secret.
|
12
12
|
# @return [String] a token
|
13
|
-
field :token, :
|
13
|
+
field :token, type: String
|
14
14
|
|
15
15
|
# The class on which to perform the action.
|
16
16
|
# @return [String] a class name
|
17
17
|
# @note `class` clashes with `Object#class` and Mongoid reserves `klass`.
|
18
|
-
field :kind, :
|
18
|
+
field :kind, type: String
|
19
19
|
|
20
20
|
# Any additional arguments for the action.
|
21
21
|
# @return [Array] a list of arguments
|
22
|
-
field :args, :
|
22
|
+
field :args, type: Array
|
23
23
|
|
24
24
|
# The URL to redirect to after performing the action successfully.
|
25
25
|
# @return [String] a URL or path
|
26
|
-
field :success_url, :
|
26
|
+
field :success_url, type: String
|
27
27
|
|
28
28
|
# The URL to redirect to after failing to perform the action.
|
29
29
|
# @return [String] a URL or path
|
30
|
-
field :failure_url, :
|
30
|
+
field :failure_url, type: String
|
31
31
|
|
32
|
-
index({:
|
32
|
+
index({token: 1}, unique: true)
|
33
33
|
end
|
34
34
|
end
|
data/lib/token_action/version.rb
CHANGED
@@ -1,171 +1,173 @@
|
|
1
|
-
require '
|
1
|
+
require 'rails_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module TokenAction
|
4
|
+
RSpec.describe TokensController, type: :controller do
|
5
|
+
before :each do
|
6
|
+
@routes = TokenAction::Engine.routes
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
9
|
+
describe 'GET redeem' do
|
10
|
+
context 'after failing to perform the action' do
|
11
|
+
let :token_with_failure_url do
|
12
|
+
FactoryGirl.create(:token, args: ['upgrade'], failure_url: '/hello/token_failure').token
|
13
|
+
end
|
14
|
+
let :token_with_unroutable_failure_url do
|
15
|
+
FactoryGirl.create(:token, args: ['upgrade'], failure_url: '/missing').token
|
16
|
+
end
|
17
|
+
let :token_without_failure_url do
|
18
|
+
FactoryGirl.create(:token, args: ['upgrade']).token
|
19
|
+
end
|
20
|
+
let :token_with_error do
|
21
|
+
FactoryGirl.create(:token, args: ['metamorphose']).token
|
22
|
+
end
|
23
|
+
let :token_with_namespaced_error do
|
24
|
+
FactoryGirl.create(:token, args: ['fly']).token
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
27
|
+
it "redirects to the token's failure URL" do
|
28
|
+
TokenAction.failure_url = '/hello/failure'
|
29
|
+
get :redeem, token: token_with_failure_url, path: 'redeem'
|
30
|
+
expect(response).to redirect_to('/hello/token_failure')
|
31
|
+
end
|
32
|
+
it "redirects to the default failure URL if the token's failure URL is not routable" do
|
33
|
+
TokenAction.failure_url = '/hello/failure'
|
34
|
+
get :redeem, token: token_with_unroutable_failure_url, path: 'redeem'
|
35
|
+
expect(response).to redirect_to('/hello/failure')
|
36
|
+
end
|
37
|
+
it "redirects to the default failure URL if the token's failure URL is not set" do
|
38
|
+
TokenAction.failure_url = '/hello/failure'
|
39
|
+
get :redeem, token: token_without_failure_url, path: 'redeem'
|
40
|
+
expect(response).to redirect_to('/hello/failure')
|
41
|
+
end
|
42
|
+
it "redirects to the application's root_path if the default failure URL is not routable" do
|
43
|
+
TokenAction.failure_url = '/missing'
|
44
|
+
get :redeem, token: token_without_failure_url, path: 'redeem'
|
45
|
+
expect(response).to redirect_to(Rails.application.routes.url_for(controller: 'hello', action: 'hello', only_path: true))
|
46
|
+
end
|
47
|
+
it "redirects to the application's root_path if the default failure URL is not set" do
|
48
|
+
TokenAction.failure_url = nil
|
49
|
+
get :redeem, token: token_without_failure_url, path: 'redeem'
|
50
|
+
expect(response).to redirect_to(Rails.application.routes.url_for(controller: 'hello', action: 'hello', only_path: true))
|
51
|
+
end
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
53
|
+
it 'flashes a path-based, error-based failure message' do
|
54
|
+
get :redeem, token: token_with_error, path: 'confirm'
|
55
|
+
expect(flash[:notice]).to eq(nil)
|
56
|
+
expect(flash[:alert]).to eq('This is an path-based, error-based failure message.')
|
57
|
+
end
|
58
|
+
it 'flashes a error-based failure message' do
|
59
|
+
get :redeem, token: token_with_error, path: 'redeem'
|
60
|
+
expect(flash[:notice]).to eq(nil)
|
61
|
+
expect(flash[:alert]).to eq('This is an error-based failure message.')
|
62
|
+
end
|
63
|
+
it 'flashes a long error-based failure message' do
|
64
|
+
get :redeem, token: token_with_namespaced_error, path: 'redeem'
|
65
|
+
expect(flash[:notice]).to eq(nil)
|
66
|
+
expect(flash[:alert]).to eq('This is another error-based failure message.')
|
67
|
+
end
|
68
|
+
it 'flashes a path-based failure message' do
|
69
|
+
get :redeem, token: token_without_failure_url, path: 'confirm'
|
70
|
+
expect(flash[:notice]).to eq(nil)
|
71
|
+
expect(flash[:alert]).to eq('This is a path-based failure message.')
|
72
|
+
end
|
73
|
+
it 'flashes a long path-based failure message' do
|
74
|
+
get :redeem, token: token_without_failure_url, path: 'a/b/c'
|
75
|
+
expect(flash[:notice]).to eq(nil)
|
76
|
+
expect(flash[:alert]).to eq('This is another path-based failure message.')
|
77
|
+
end
|
78
|
+
it 'flashes the default failure message' do
|
79
|
+
get :redeem, token: token_without_failure_url, path: 'redeem'
|
80
|
+
expect(flash[:notice]).to eq(nil)
|
81
|
+
expect(flash[:alert]).to eq('An error occurred while processing your request.')
|
82
|
+
end
|
81
83
|
end
|
82
|
-
end
|
83
84
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
85
|
+
context 'when the token is not found' do
|
86
|
+
it 'redirects to the default failure URL' do
|
87
|
+
TokenAction.failure_url = '/hello/not_found'
|
88
|
+
get :redeem, token: 'missing', path: 'redeem'
|
89
|
+
expect(response).to redirect_to('/hello/not_found')
|
90
|
+
end
|
91
|
+
it "redirects to the application's root_path if the default failure URL is not routable" do
|
92
|
+
TokenAction.failure_url = '/missing'
|
93
|
+
get :redeem, token: 'missing', path: 'redeem'
|
94
|
+
expect(response).to redirect_to(Rails.application.routes.url_for(controller: 'hello', action: 'hello', only_path: true))
|
95
|
+
end
|
96
|
+
it "redirects to the application's root_path if the default failure URL is not set" do
|
97
|
+
TokenAction.failure_url = nil
|
98
|
+
get :redeem, token: 'missing', path: 'redeem'
|
99
|
+
expect(response).to redirect_to(Rails.application.routes.url_for(controller: 'hello', action: 'hello', only_path: true))
|
100
|
+
end
|
100
101
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
102
|
+
it 'flashes a path-based failure message' do
|
103
|
+
get :redeem, token: 'missing', path: 'confirm'
|
104
|
+
expect(flash[:notice]).to eq(nil)
|
105
|
+
expect(flash[:alert]).to eq('This is a path-based not found message.')
|
106
|
+
end
|
107
|
+
it 'flashes a long path-based failure message' do
|
108
|
+
get :redeem, token: 'missing', path: 'a/b/c'
|
109
|
+
expect(flash[:notice]).to eq(nil)
|
110
|
+
expect(flash[:alert]).to eq('This is another path-based not found message.')
|
111
|
+
end
|
112
|
+
it 'flashes the default failure message if no path-based failure message is set' do
|
113
|
+
get :redeem, token: 'missing', path: 'redeem'
|
114
|
+
expect(flash[:notice]).to eq(nil)
|
115
|
+
expect(flash[:alert]).to eq('Sorry, your request could not be processed.')
|
116
|
+
end
|
110
117
|
end
|
111
|
-
it 'flashes the default failure message if no path-based failure message is set' do
|
112
|
-
get :redeem, :token => 'missing', :path => 'redeem'
|
113
|
-
flash[:notice].should be_nil
|
114
|
-
flash[:alert].should == 'Sorry, your request could not be processed.'
|
115
|
-
end
|
116
|
-
end
|
117
118
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
119
|
+
context 'after performing the action successfully' do
|
120
|
+
let :token_with_success_url do
|
121
|
+
FactoryGirl.create(:token, success_url: '/hello/token_success').token
|
122
|
+
end
|
123
|
+
let :token_with_unroutable_success_url do
|
124
|
+
FactoryGirl.create(:token, success_url: '/missing').token
|
125
|
+
end
|
126
|
+
let :token_without_success_url do
|
127
|
+
FactoryGirl.create(:token).token
|
128
|
+
end
|
128
129
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
130
|
+
it "redirects to the token's success URL" do
|
131
|
+
TokenAction.success_url = '/hello/success'
|
132
|
+
get :redeem, token: token_with_success_url, path: 'redeem'
|
133
|
+
expect(response).to redirect_to('/hello/token_success')
|
134
|
+
end
|
135
|
+
it "redirects to the default success URL if the token's success URL is not routable" do
|
136
|
+
TokenAction.success_url = '/hello/success'
|
137
|
+
get :redeem, token: token_with_unroutable_success_url, path: 'redeem'
|
138
|
+
expect(response).to redirect_to('/hello/success')
|
139
|
+
end
|
140
|
+
it "redirects to the default success URL if the token's success URL is not set" do
|
141
|
+
TokenAction.success_url = '/hello/success'
|
142
|
+
get :redeem, token: token_without_success_url, path: 'redeem'
|
143
|
+
expect(response).to redirect_to('/hello/success')
|
144
|
+
end
|
145
|
+
it "redirects to the application's root_path if the default success URL is not routable" do
|
146
|
+
TokenAction.success_url = '/missing'
|
147
|
+
get :redeem, token: token_without_success_url, path: 'redeem'
|
148
|
+
expect(response).to redirect_to(Rails.application.routes.url_for(controller: 'hello', action: 'hello', only_path: true))
|
149
|
+
end
|
150
|
+
it "redirects to the application's root_path if the default success URL is not set" do
|
151
|
+
TokenAction.success_url = nil
|
152
|
+
get :redeem, token: token_without_success_url, path: 'redeem'
|
153
|
+
expect(response).to redirect_to(Rails.application.routes.url_for(controller: 'hello', action: 'hello', only_path: true))
|
154
|
+
end
|
154
155
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
156
|
+
it 'flashes a path-based success message' do
|
157
|
+
get :redeem, token: token_without_success_url, path: 'confirm'
|
158
|
+
expect(flash[:notice]).to eq('This is a path-based success message.')
|
159
|
+
expect(flash[:alert]).to eq(nil)
|
160
|
+
end
|
161
|
+
it 'flashes a long path-based success message' do
|
162
|
+
get :redeem, token: token_without_success_url, path: 'a/b/c'
|
163
|
+
expect(flash[:notice]).to eq('This is another path-based success message.')
|
164
|
+
expect(flash[:alert]).to eq(nil)
|
165
|
+
end
|
166
|
+
it 'flashes the default success message if no path-based success message is set' do
|
167
|
+
get :redeem, token: token_without_success_url, path: 'redeem'
|
168
|
+
expect(flash[:notice]).to eq('Your request was successfully processed.')
|
169
|
+
expect(flash[:alert]).to eq(nil)
|
170
|
+
end
|
169
171
|
end
|
170
172
|
end
|
171
173
|
end
|