tarquinn 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +15 -3
  3. data/.gitignore +9 -0
  4. data/.rubocop.yml +29 -0
  5. data/.rubocop_todo.yml +13 -0
  6. data/Dockerfile +2 -2
  7. data/Gemfile +15 -0
  8. data/README.md +5 -1
  9. data/Rakefile +5 -0
  10. data/config/check_specs.yml +3 -0
  11. data/config/rubycritc.rb +12 -0
  12. data/config/yardstick.rb +13 -0
  13. data/config/yardstick.yml +33 -0
  14. data/lib/tarquinn/class_methods.rb +43 -12
  15. data/lib/tarquinn/condition/action_checker.rb +24 -7
  16. data/lib/tarquinn/condition/method_caller.rb +25 -8
  17. data/lib/tarquinn/condition/proc_runner.rb +18 -7
  18. data/lib/tarquinn/condition.rb +57 -4
  19. data/lib/tarquinn/controller.rb +56 -13
  20. data/lib/tarquinn/redirection_config.rb +92 -0
  21. data/lib/tarquinn/redirection_handler.rb +97 -0
  22. data/lib/tarquinn/request_handler.rb +61 -0
  23. data/lib/tarquinn/request_handler_builder.rb +84 -0
  24. data/lib/tarquinn/version.rb +3 -1
  25. data/lib/tarquinn.rb +74 -8
  26. data/spec/dummy/Rakefile +8 -0
  27. data/spec/dummy/app/assets/images/.keep +0 -0
  28. data/spec/dummy/app/assets/stylesheets/application.css +1 -0
  29. data/spec/dummy/app/channels/application_cable/channel.rb +6 -0
  30. data/spec/dummy/app/channels/application_cable/connection.rb +6 -0
  31. data/spec/dummy/app/controllers/application_controller.rb +4 -0
  32. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  33. data/spec/dummy/app/controllers/tarquinn/dummy_controller.rb +43 -0
  34. data/spec/dummy/app/controllers/tarquinn/dummy_route_controller.rb +37 -0
  35. data/spec/dummy/app/helpers/application_helper.rb +4 -0
  36. data/spec/dummy/app/jobs/application_job.rb +9 -0
  37. data/spec/dummy/app/mailers/application_mailer.rb +6 -0
  38. data/spec/dummy/app/models/application_record.rb +5 -0
  39. data/spec/dummy/app/models/concerns/.keep +0 -0
  40. data/spec/dummy/app/views/layouts/application.html.erb +15 -0
  41. data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
  42. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  43. data/spec/dummy/app/views/tarquinn/dummy_route/index.html +0 -0
  44. data/spec/dummy/app/views/tarquinn/dummy_route/new.html +0 -0
  45. data/spec/dummy/bin/rails +6 -0
  46. data/spec/dummy/bin/rake +6 -0
  47. data/spec/dummy/bin/setup +35 -0
  48. data/spec/dummy/config/application.rb +24 -0
  49. data/spec/dummy/config/boot.rb +7 -0
  50. data/spec/dummy/config/cable.yml +10 -0
  51. data/spec/dummy/config/database.yml +25 -0
  52. data/spec/dummy/config/environment.rb +7 -0
  53. data/spec/dummy/config/environments/development.rb +69 -0
  54. data/spec/dummy/config/environments/production.rb +89 -0
  55. data/spec/dummy/config/environments/test.rb +62 -0
  56. data/spec/dummy/config/initializers/content_security_policy.rb +26 -0
  57. data/spec/dummy/config/initializers/filter_parameter_logging.rb +10 -0
  58. data/spec/dummy/config/initializers/inflections.rb +17 -0
  59. data/spec/dummy/config/initializers/permissions_policy.rb +12 -0
  60. data/spec/dummy/config/locales/en.yml +33 -0
  61. data/spec/dummy/config/puma.rb +45 -0
  62. data/spec/dummy/config/routes.rb +8 -0
  63. data/spec/dummy/config/storage.yml +34 -0
  64. data/spec/dummy/config.ru +8 -0
  65. data/spec/dummy/db/schema.rb +16 -0
  66. data/spec/dummy/lib/assets/.keep +0 -0
  67. data/spec/dummy/log/.keep +0 -0
  68. data/spec/dummy/public/404.html +67 -0
  69. data/spec/dummy/public/422.html +67 -0
  70. data/spec/dummy/public/500.html +66 -0
  71. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  72. data/spec/dummy/public/apple-touch-icon.png +0 -0
  73. data/spec/dummy/public/favicon.ico +0 -0
  74. data/spec/dummy/storage/.keep +0 -0
  75. data/spec/dummy/tmp/.keep +0 -0
  76. data/spec/dummy/tmp/pids/.keep +0 -0
  77. data/spec/dummy/tmp/storage/.keep +0 -0
  78. data/spec/lib/tarquinn/condition/action_checker_spec.rb +4 -2
  79. data/spec/lib/tarquinn/condition/method_caller_spec.rb +5 -3
  80. data/spec/lib/tarquinn/condition/proc_runner_spec.rb +56 -4
  81. data/spec/lib/tarquinn/condition_spec.rb +53 -0
  82. data/spec/lib/tarquinn/controller_spec.rb +83 -10
  83. data/spec/lib/tarquinn/{config_spec.rb → redirection_config_spec.rb} +3 -1
  84. data/spec/lib/tarquinn/{handler_spec.rb → redirection_handler_spec.rb} +21 -3
  85. data/spec/lib/tarquinn/request_handler_builder_spec.rb +15 -0
  86. data/spec/lib/tarquinn/{engine_spec.rb → request_handler_spec.rb} +5 -3
  87. data/spec/lib/tarquinn_spec.rb +176 -13
  88. data/spec/spec_helper.rb +16 -6
  89. data/spec/support/shared_examples/config.rb +4 -2
  90. data/tarquinn.gemspec +8 -12
  91. metadata +73 -100
  92. data/lib/tarquinn/builder.rb +0 -28
  93. data/lib/tarquinn/concern.rb +0 -17
  94. data/lib/tarquinn/config.rb +0 -39
  95. data/lib/tarquinn/engine.rb +0 -31
  96. data/lib/tarquinn/handler.rb +0 -49
  97. data/spec/lib/tarquinn/builder_spec.rb +0 -13
  98. data/spec/support/models/tarquinn/dummy_controller.rb +0 -43
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Tarquinn::Condition::ActionChecker do
@@ -34,7 +36,7 @@ describe Tarquinn::Condition::ActionChecker do
34
36
  end
35
37
 
36
38
  context 'when initialized with more routes' do
37
- let(:routes) { [:show, :view] }
39
+ let(:routes) { %i[show view] }
38
40
  let(:subject) { described_class.new(routes) }
39
41
 
40
42
  context 'when receiving a request for one of the given action' do
@@ -44,7 +46,7 @@ describe Tarquinn::Condition::ActionChecker do
44
46
  end
45
47
 
46
48
  context 'when receiving a request for another action' do
47
- let(:routes) { [:update, :view] }
49
+ let(:routes) { %i[update view] }
48
50
  it do
49
51
  expect(subject.check?(controller)).to be_falsey
50
52
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Tarquinn::Condition::MethodCaller do
@@ -24,7 +26,7 @@ describe Tarquinn::Condition::MethodCaller do
24
26
  end
25
27
 
26
28
  context 'when initialized with more methods' do
27
- let(:methods) { [:true, :false] }
29
+ let(:methods) { %i[true false] }
28
30
  let(:subject) { described_class.new(methods) }
29
31
 
30
32
  context 'when one return true and the other false' do
@@ -34,14 +36,14 @@ describe Tarquinn::Condition::MethodCaller do
34
36
  end
35
37
 
36
38
  context 'when all return true' do
37
- let(:methods) { [:true, :true] }
39
+ let(:methods) { %i[true true] }
38
40
  it do
39
41
  expect(subject.check?(controller)).to be_truthy
40
42
  end
41
43
  end
42
44
 
43
45
  context 'when all return false' do
44
- let(:methods) { [:false, :false] }
46
+ let(:methods) { %i[false false] }
45
47
  it do
46
48
  expect(subject.check?(controller)).to be_falsey
47
49
  end
@@ -1,22 +1,74 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Tarquinn::Condition::ProcRunner do
4
- let(:controller) { double }
5
- let(:value) { true }
6
- let(:subject) { described_class.new { value } }
6
+ let(:rails_controller) { rails_controller_class.new }
7
+ let(:controller) { Tarquinn::Controller.new(rails_controller) }
8
+ let(:subject) { described_class.new(&block) }
9
+ let(:rails_controller_class) do
10
+ Class.new(ActionController::Base)
11
+ end
7
12
 
8
13
  describe '#check?' do
9
14
  context 'when block evaluates into true' do
15
+ let(:block) { proc { true } }
16
+
10
17
  it do
11
18
  expect(subject.check?(controller)).to be_truthy
12
19
  end
13
20
  end
14
21
 
15
22
  context 'when block evaluates into false' do
16
- let(:value) { false }
23
+ let(:block) { proc { false } }
24
+
17
25
  it do
18
26
  expect(subject.check?(controller)).to be_falsey
19
27
  end
20
28
  end
29
+
30
+ context 'when the block gets calls the controller method' do
31
+ let(:block) { proc { match? } }
32
+
33
+ context 'when the method returns true' do
34
+ let(:rails_controller_class) do
35
+ Class.new(ActionController::Base) do
36
+ def match?
37
+ true
38
+ end
39
+ end
40
+ end
41
+
42
+ it do
43
+ expect(subject.check?(controller)).to be_truthy
44
+ end
45
+ end
46
+
47
+ context 'when the method returns false' do
48
+ let(:rails_controller_class) do
49
+ Class.new(ActionController::Base) do
50
+ def match?
51
+ false
52
+ end
53
+ end
54
+ end
55
+
56
+ it do
57
+ expect(subject.check?(controller)).to be_falsey
58
+ end
59
+ end
60
+ end
61
+
62
+ context 'when the block references a value defined outside' do
63
+ let(:block) do
64
+ val = value
65
+ proc { val }
66
+ end
67
+ let(:value) { [false, true].sample }
68
+
69
+ it do
70
+ expect(subject.check?(controller)).to eq(value)
71
+ end
72
+ end
21
73
  end
22
74
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Tarquinn::Condition do
6
+ subject(:condition) { described_class.new }
7
+
8
+ let(:controller) { instance_double(Tarquinn::Controller) }
9
+
10
+ describe '.method_caller' do
11
+ context 'when methods are given' do
12
+ let(:methods) { %i[method1 method2] }
13
+
14
+ it do
15
+ expect(described_class.method_caller(methods))
16
+ .to be_a(Tarquinn::Condition::MethodCaller)
17
+ end
18
+
19
+ it do
20
+ expect(described_class.method_caller(methods))
21
+ .to eq(Tarquinn::Condition::MethodCaller.new(methods))
22
+ end
23
+ end
24
+
25
+ context 'when no methods are given' do
26
+ let(:methods) { %i[] }
27
+
28
+ it do
29
+ expect(described_class.method_caller(methods)).to be_nil
30
+ end
31
+ end
32
+ end
33
+
34
+ describe '.action_checker' do
35
+ let(:actions) { %i[index show] }
36
+
37
+ it do
38
+ expect(described_class.action_checker(actions))
39
+ .to be_a(Tarquinn::Condition::ActionChecker)
40
+ end
41
+
42
+ it do
43
+ expect(described_class.action_checker(actions))
44
+ .to eq(Tarquinn::Condition::ActionChecker.new(actions))
45
+ end
46
+ end
47
+
48
+ describe '#check?' do
49
+ it do
50
+ expect { condition.check?(controller) }.to raise_error(NotImplementedError)
51
+ end
52
+ end
53
+ end
@@ -1,38 +1,111 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Tarquinn::Controller do
4
- let(:controller) { Tarquinn::DummyController.new }
5
- let(:subject) { described_class.new(controller) }
6
+ subject(:controller) { described_class.new(rails_controller) }
7
+
8
+ let(:rails_controller) { Tarquinn::DummyController.new }
6
9
 
7
10
  describe '#call' do
8
- it 'redirects a method call to the controller' do
9
- expect(controller).to receive(:redirect_to)
10
- subject.call(:redirect_to)
11
+ context 'when no arguments are given' do
12
+ before do
13
+ allow(rails_controller).to receive(:redirect_to).with(no_args)
14
+ end
15
+
16
+ it 'delegate a method call to the controller' do
17
+ subject.call(:redirect_to)
18
+ expect(rails_controller).to have_received(:redirect_to)
19
+ end
20
+ end
21
+
22
+ context 'when arguments are given' do
23
+ before do
24
+ allow(rails_controller)
25
+ .to receive(:redirect_to)
26
+ .with(1, 2, opt: 3)
27
+ end
28
+
29
+ it 'delegate a method call to the controller' do
30
+ subject.call(:redirect_to, 1, 2, opt: 3)
31
+
32
+ expect(rails_controller).to have_received(:redirect_to)
33
+ end
34
+ end
35
+
36
+ context 'when a block is given' do
37
+ let(:block) { proc { 12 } }
38
+
39
+ before do
40
+ allow(rails_controller)
41
+ .to receive(:redirect_to)
42
+ .with(no_args).and_yield
43
+ end
44
+
45
+ it 'delegate a method call to the controller' do
46
+ subject.call(:redirect_to, &block)
47
+
48
+ expect(rails_controller).to have_received(:redirect_to)
49
+ end
50
+
51
+ it 'passes the block to be executed' do
52
+ expect(subject.call(:redirect_to, &block)).to eq(12)
53
+ end
54
+ end
55
+ end
56
+
57
+ describe '#run' do
58
+ context 'when block does not call any controller method' do
59
+ let(:block) { proc { 12 } }
60
+
61
+ it 'returns the value from the block' do
62
+ expect(controller.run(&block)).to eq(12)
63
+ end
64
+ end
65
+
66
+ context 'when block references a value outside' do
67
+ let(:block) do
68
+ value = 15
69
+ proc { value }
70
+ end
71
+
72
+ it 'returns the value' do
73
+ expect(controller.run(&block)).to eq(15)
74
+ end
75
+ end
76
+
77
+ context 'when block references a method in the controller' do
78
+ let(:block) { proc { params } }
79
+
80
+ it 'returns the value' do
81
+ expect(controller.run(&block))
82
+ .to eq(ActionController::Parameters.new(action: 'show'))
83
+ end
11
84
  end
12
85
  end
13
86
 
14
87
  describe '#params' do
15
88
  it 'returns the instance params call' do
16
- expect(subject.params).to eq(action: 'show')
89
+ expect(subject.params).to eq(ActionController::Parameters.new(action: 'show'))
17
90
  end
18
91
  end
19
92
 
20
- describe '#has_method?' do
93
+ describe '#method?' do
21
94
  context 'when calling for a public method that exists' do
22
95
  it do
23
- expect(subject.has_method?(:parse_request)).to be_truthy
96
+ expect(subject.method?(:parse_request)).to be_truthy
24
97
  end
25
98
  end
26
99
 
27
100
  context 'when calling for a private method that exists' do
28
101
  it do
29
- expect(subject.has_method?(:redirection_path)).to be_truthy
102
+ expect(subject.method?(:redirection_path)).to be_truthy
30
103
  end
31
104
  end
32
105
 
33
106
  context 'when calling for a non existing method' do
34
107
  it do
35
- expect(subject.has_method?(:non_existing)).to be_falsey
108
+ expect(subject.method?(:non_existing)).to be_falsey
36
109
  end
37
110
  end
38
111
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
- describe Tarquinn::Config do
5
+ describe Tarquinn::RedirectionConfig do
4
6
  let(:subject) { described_class.new(:redirect) }
5
7
 
6
8
  describe '#add_redirection_rules' do
@@ -1,14 +1,32 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
- describe Tarquinn::Handler do
5
+ describe Tarquinn::RedirectionHandler do
4
6
  let(:redirection_path) { '/path' }
5
7
  let(:controller) { Tarquinn::DummyController.new }
6
- let(:config) { Tarquinn::Config.new(:redirection_path) }
8
+ let(:config) { Tarquinn::RedirectionConfig.new(:redirection_path) }
7
9
  let(:subject) do
8
10
  described_class.new config, Tarquinn::Controller.new(controller)
9
11
  end
10
12
 
11
13
  describe '#perform_redirect?' do
14
+ context 'when there are no rules' do
15
+ it do
16
+ expect(subject.perform_redirect?).to be_truthy
17
+ end
18
+
19
+ context 'but some grant a skip' do
20
+ before do
21
+ config.add_skip_rules { true }
22
+ end
23
+
24
+ it do
25
+ expect(subject.perform_redirect?).to be_falsey
26
+ end
27
+ end
28
+ end
29
+
12
30
  context 'when rules allow for redirection' do
13
31
  before do
14
32
  config.add_redirection_rules { true }
@@ -98,7 +116,7 @@ describe Tarquinn::Handler do
98
116
  end
99
117
 
100
118
  context 'when configured with a method that does not exist in the controller' do
101
- let(:config) { Tarquinn::Config.new('/new_path') }
119
+ let(:config) { Tarquinn::RedirectionConfig.new('/new_path') }
102
120
 
103
121
  it 'calls for redirection using static path' do
104
122
  expect(controller).to receive(:redirect_to).with('/new_path')
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Tarquinn::RequestHandlerBuilder do
6
+ describe '#build' do
7
+ let(:controller) do
8
+ double('controller')
9
+ end
10
+
11
+ it do
12
+ expect(subject.build(controller)).to be_a(Tarquinn::RequestHandler)
13
+ end
14
+ end
15
+ end
@@ -1,13 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
- describe Tarquinn::Engine do
5
+ describe Tarquinn::RequestHandler do
4
6
  let(:redirection_path) { '/path' }
5
7
  let(:redirection_path2) { '/path2' }
6
8
  let(:controller) do
7
9
  double('controller', redirect_path: redirection_path, redirect_path2: redirection_path2)
8
10
  end
9
- let(:config) { Tarquinn::Config.new(:redirect_path) }
10
- let(:config2) { Tarquinn::Config.new(:redirect_path2) }
11
+ let(:config) { Tarquinn::RedirectionConfig.new(:redirect_path) }
12
+ let(:config2) { Tarquinn::RedirectionConfig.new(:redirect_path2) }
11
13
  let(:configs) { { redirect_path: config, redirect_path2: config2 } }
12
14
  let(:subject) do
13
15
  described_class.new configs, Tarquinn::Controller.new(controller)
@@ -1,24 +1,187 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
- describe Tarquinn do
4
- let(:controller) { Tarquinn::DummyController.new }
5
+ describe Tarquinn, type: :controller do
6
+ describe '.redirection_rule' do
7
+ context 'when there is only one condition' do
8
+ controller(Tarquinn::DummyRouteController) do
9
+ redirection_rule :redirection, :should_redirect?
10
+ end
11
+
12
+ before { get :index, params: parameters }
13
+
14
+ context 'when request indicates a redirection' do
15
+ let(:parameters) { { should_redirect: true } }
16
+
17
+ it 'performs a redirect' do
18
+ expect(response).to redirect_to('/path')
19
+ end
20
+ end
21
+
22
+ context 'when request does not indicate a redirection' do
23
+ let(:parameters) { {} }
24
+
25
+ it 'does not performs a redirect' do
26
+ expect(response).not_to redirect_to('/path')
27
+ end
28
+ end
29
+ end
30
+
31
+ # TODO: fix this condition
32
+ context 'when there is only one block condition' do
33
+ controller(Tarquinn::DummyRouteController) do
34
+ redirection_rule :redirection do
35
+ params[:redirect_block]
36
+ end
37
+ end
38
+
39
+ before { get :index, params: parameters }
40
+
41
+ context 'when request indicates a redirection' do
42
+ let(:parameters) { { redirect_block: true } }
43
+
44
+ it 'performs a redirect' do
45
+ expect(response).to redirect_to('/path')
46
+ end
47
+ end
48
+
49
+ context 'when request does not indicate a redirection' do
50
+ let(:parameters) { {} }
5
51
 
6
- describe 'redirection' do
7
- context 'when configuration calls for a method that allows redirection' do
8
- it 'redirects to redirection path given by the method' do
9
- expect(controller).to receive(:redirect_to).with('/path')
10
- controller.parse_request
52
+ it 'does not performs a redirect' do
53
+ expect(response).not_to redirect_to('/path')
54
+ end
11
55
  end
12
56
  end
13
57
 
14
- context 'when redirection skip method returns true' do
15
- before do
16
- allow(controller).to receive(:should_skip_redirect?) { true }
58
+ context 'when there are more conditions' do
59
+ controller(Tarquinn::DummyRouteController) do
60
+ redirection_rule :redirection, :should_redirect?, :condition2
17
61
  end
18
62
 
19
- it 'does not redirect to redirection path given by the method' do
20
- expect(controller).not_to receive(:redirect_to)
21
- controller.parse_request
63
+ before { get :index, params: parameters }
64
+
65
+ context 'when request indicates a redirection through first condition' do
66
+ let(:parameters) { { should_redirect: true } }
67
+
68
+ it 'performs a redirect' do
69
+ expect(response).to redirect_to('/path')
70
+ end
71
+ end
72
+
73
+ context 'when request indicates a redirection through second condition' do
74
+ let(:parameters) { { redirect: true } }
75
+
76
+ it 'performs a redirect' do
77
+ expect(response).to redirect_to('/path')
78
+ end
79
+ end
80
+
81
+ context 'when request does not indicate a redirection' do
82
+ let(:parameters) { {} }
83
+
84
+ it 'does not performs a redirect' do
85
+ expect(response).not_to redirect_to('/path')
86
+ end
87
+ end
88
+ end
89
+
90
+ # TODO: fix this condition
91
+ context 'when there are no conditions' do
92
+ controller(Tarquinn::DummyRouteController) do
93
+ redirection_rule :redirection
94
+ end
95
+
96
+ before { get :index }
97
+
98
+ it 'performs a redirect' do
99
+ expect(response).to redirect_to('/path')
100
+ end
101
+ end
102
+ end
103
+
104
+ describe '.skip_redirection' do
105
+ context 'when there is only one skip condition' do
106
+ controller(Tarquinn::DummyRouteController) do
107
+ redirection_rule :redirection, :always_redirect
108
+ skip_redirection_rule :redirection, :should_skip?
109
+ end
110
+
111
+ before { get :index, params: parameters }
112
+
113
+ context 'when requesting with parameters that do not skip' do
114
+ let(:parameters) { {} }
115
+
116
+ it 'performs a redirect' do
117
+ expect(response).to redirect_to('/path')
118
+ end
119
+ end
120
+
121
+ context 'when request does not indicate a redirection' do
122
+ let(:parameters) { { should_skip: true } }
123
+
124
+ it 'does not performs a redirect' do
125
+ expect(response).not_to redirect_to('/path')
126
+ end
127
+ end
128
+ end
129
+
130
+ context 'when there are more than oneskip condition' do
131
+ controller(Tarquinn::DummyRouteController) do
132
+ redirection_rule :redirection, :always_redirect
133
+ skip_redirection_rule :redirection, :should_skip?, :do_skip?
134
+ end
135
+
136
+ before { get :index, params: parameters }
137
+
138
+ context 'when requesting with parameters that do not skip' do
139
+ let(:parameters) { {} }
140
+
141
+ it 'performs a redirect' do
142
+ expect(response).to redirect_to('/path')
143
+ end
144
+ end
145
+
146
+ context 'when request skips redirection using first condition' do
147
+ let(:parameters) { { should_skip: true } }
148
+
149
+ it 'does not performs a redirect' do
150
+ expect(response).not_to redirect_to('/path')
151
+ end
152
+ end
153
+
154
+ context 'when request skips redirection using second condition' do
155
+ let(:parameters) { { skip: true } }
156
+
157
+ it 'does not performs a redirect' do
158
+ expect(response).not_to redirect_to('/path')
159
+ end
160
+ end
161
+ end
162
+ end
163
+
164
+ describe '.skip_redirection_rule' do
165
+ controller(Tarquinn::DummyRouteController) do
166
+ redirection_rule :redirection, :always_redirect
167
+ skip_redirection :redirection, :index, :delete
168
+ end
169
+
170
+ before { get action }
171
+
172
+ context 'when requestin a path that redirects' do
173
+ let(:action) { :new }
174
+
175
+ it 'performs a redirect' do
176
+ expect(response).to redirect_to('/path')
177
+ end
178
+ end
179
+
180
+ context 'when request does not indicate a redirection' do
181
+ let(:action) { :index }
182
+
183
+ it 'does not performs a redirect' do
184
+ expect(response).not_to redirect_to('/path')
22
185
  end
23
186
  end
24
187
  end
data/spec/spec_helper.rb CHANGED
@@ -1,16 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'simplecov'
2
4
 
3
- SimpleCov.profiles.define 'gem' do
5
+ SimpleCov.start do
4
6
  add_filter '/spec/'
5
7
  end
6
8
 
7
- SimpleCov.start 'gem'
8
-
9
9
  require 'tarquinn'
10
10
  require 'pry-nav'
11
11
 
12
+ require 'active_record'
13
+ ActiveRecord::Base.establish_connection(
14
+ adapter: 'sqlite3', database: ':memory:'
15
+ )
16
+
17
+ require File.expand_path('spec/dummy/config/environment')
18
+ # require File.expand_path('spec/dummy/db/schema.rb')
19
+ require 'rspec/rails'
20
+ require 'active_support/railtie'
21
+
12
22
  support_files = File.expand_path('spec/support/**/*.rb')
13
- Dir[support_files].each { |file| require file }
23
+ Dir[support_files].each { |file| require file }
14
24
 
15
25
  RSpec.configure do |config|
16
26
  config.treat_symbols_as_metadata_keys_with_true_values = true
@@ -20,6 +30,6 @@ RSpec.configure do |config|
20
30
 
21
31
  config.order = 'random'
22
32
 
23
- config.before do
24
- end
33
+ # config.before do
34
+ # end
25
35
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  shared_examples 'a method that adds a redirection rule' do |expected_class|
2
4
  it_behaves_like 'a method that adds a rule', :redirection, expected_class
3
5
  end
@@ -11,7 +13,7 @@ shared_examples 'a method that adds a rule' do |rule, expected_class|
11
13
  it do
12
14
  expect do
13
15
  call_method
14
- end.to change { subject.public_send("#{rule}_blocks") }
16
+ end.to change(subject, "#{rule}_blocks")
15
17
  end
16
18
 
17
19
  it do
@@ -22,6 +24,6 @@ shared_examples 'a method that adds a rule' do |rule, expected_class|
22
24
  it do
23
25
  expect do
24
26
  call_method
25
- end.not_to change { subject.public_send("#{reverse_rule}_blocks") }
27
+ end.not_to change(subject, "#{reverse_rule}_blocks")
26
28
  end
27
29
  end