pundit 1.1.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/pundit/rspec.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/array/conversions"
2
4
 
3
5
  module Pundit
@@ -5,10 +7,11 @@ module Pundit
5
7
  module Matchers
6
8
  extend ::RSpec::Matchers::DSL
7
9
 
10
+ # rubocop:disable Metrics/BlockLength
8
11
  matcher :permit do |user, record|
9
12
  match_proc = lambda do |policy|
10
13
  @violating_permissions = permissions.find_all do |permission|
11
- not policy.new(user, record).public_send(permission)
14
+ !policy.new(user, record).public_send(permission)
12
15
  end
13
16
  @violating_permissions.empty?
14
17
  end
@@ -22,14 +25,14 @@ module Pundit
22
25
 
23
26
  failure_message_proc = lambda do |policy|
24
27
  was_were = @violating_permissions.count > 1 ? "were" : "was"
25
- "Expected #{policy} to grant #{permissions.to_sentence} on \
26
- #{record} but #{@violating_permissions.to_sentence} #{was_were} not granted"
28
+ "Expected #{policy} to grant #{permissions.to_sentence} on " \
29
+ "#{record} but #{@violating_permissions.to_sentence} #{was_were} not granted"
27
30
  end
28
31
 
29
32
  failure_message_when_negated_proc = lambda do |policy|
30
33
  was_were = @violating_permissions.count > 1 ? "were" : "was"
31
- "Expected #{policy} not to grant #{permissions.to_sentence} on \
32
- #{record} but #{@violating_permissions.to_sentence} #{was_were} granted"
34
+ "Expected #{policy} not to grant #{permissions.to_sentence} on " \
35
+ "#{record} but #{@violating_permissions.to_sentence} #{was_were} granted"
33
36
  end
34
37
 
35
38
  if respond_to?(:match_when_negated)
@@ -49,6 +52,7 @@ module Pundit
49
52
  current_example.metadata[:permissions]
50
53
  end
51
54
  end
55
+ # rubocop:enable Metrics/BlockLength
52
56
  end
53
57
 
54
58
  module DSL
@@ -71,12 +75,14 @@ end
71
75
 
72
76
  RSpec.configure do |config|
73
77
  if RSpec::Core::Version::STRING.split(".").first.to_i >= 3
74
- config.include(Pundit::RSpec::PolicyExampleGroup,
78
+ config.include(
79
+ Pundit::RSpec::PolicyExampleGroup,
75
80
  type: :policy,
76
81
  file_path: %r{spec/policies}
77
82
  )
78
83
  else
79
- config.include(Pundit::RSpec::PolicyExampleGroup,
84
+ config.include(
85
+ Pundit::RSpec::PolicyExampleGroup,
80
86
  type: :policy,
81
87
  example_group: { file_path: %r{spec/policies} }
82
88
  )
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Pundit
2
- VERSION = "1.1.0"
4
+ VERSION = "2.1.0".freeze
3
5
  end
data/pundit.gemspec CHANGED
@@ -1,30 +1,31 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path("../lib", __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require "pundit/version"
5
6
 
6
7
  Gem::Specification.new do |gem|
7
8
  gem.name = "pundit"
8
9
  gem.version = Pundit::VERSION
9
- gem.authors = ["Jonas Nicklas", "Elabs AB"]
10
+ gem.authors = ["Jonas Nicklas", "Varvet AB"]
10
11
  gem.email = ["jonas.nicklas@gmail.com", "dev@elabs.se"]
11
12
  gem.description = "Object oriented authorization for Rails applications"
12
13
  gem.summary = "OO authorization for Rails"
13
- gem.homepage = "https://github.com/elabs/pundit"
14
+ gem.homepage = "https://github.com/varvet/pundit"
14
15
  gem.license = "MIT"
15
16
 
16
- gem.files = `git ls-files`.split($/)
17
+ gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
18
  gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
18
19
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
20
  gem.require_paths = ["lib"]
20
21
 
21
22
  gem.add_dependency "activesupport", ">= 3.0.0"
22
- gem.add_development_dependency "activemodel", ">= 3.0.0"
23
23
  gem.add_development_dependency "actionpack", ">= 3.0.0"
24
- gem.add_development_dependency "bundler", "~> 1.3"
25
- gem.add_development_dependency "rspec", ">=2.0.0"
24
+ gem.add_development_dependency "activemodel", ">= 3.0.0"
25
+ gem.add_development_dependency "bundler"
26
26
  gem.add_development_dependency "pry"
27
27
  gem.add_development_dependency "rake"
28
+ gem.add_development_dependency "rspec", ">= 2.0.0"
29
+ gem.add_development_dependency "rubocop", "0.57.2"
28
30
  gem.add_development_dependency "yard"
29
- gem.add_development_dependency "rubocop"
30
31
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
 
3
5
  describe PostPolicy do
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Pundit::PolicyFinder do
6
+ let(:user) { double }
7
+ let(:post) { Post.new(user) }
8
+ let(:comment) { CommentFourFiveSix.new }
9
+ let(:article) { Article.new }
10
+
11
+ describe "#scope" do
12
+ subject { described_class.new(post) }
13
+
14
+ it "returns a policy scope" do
15
+ expect(subject.scope).to eq PostPolicy::Scope
16
+ end
17
+
18
+ context "policy is nil" do
19
+ it "returns nil" do
20
+ allow(subject).to receive(:policy).and_return nil
21
+ expect(subject.scope).to eq nil
22
+ end
23
+ end
24
+ end
25
+
26
+ describe "#policy" do
27
+ subject { described_class.new(post) }
28
+
29
+ it "returns a policy" do
30
+ expect(subject.policy).to eq PostPolicy
31
+ end
32
+
33
+ context "with a string" do
34
+ it "returns a policy" do
35
+ allow(subject).to receive(:find).and_return "PostPolicy"
36
+ expect(subject.policy).to eq PostPolicy
37
+ end
38
+ end
39
+
40
+ context "with a class" do
41
+ it "returns a policy" do
42
+ allow(subject).to receive(:find).and_return PostPolicy
43
+ expect(subject.policy).to eq PostPolicy
44
+ end
45
+ end
46
+
47
+ context "with nil" do
48
+ it "returns nil" do
49
+ allow(subject).to receive(:find).and_return nil
50
+ expect(subject.policy).to eq nil
51
+ end
52
+ end
53
+
54
+ context "with a string that can't be constantized" do
55
+ it "returns nil" do
56
+ allow(subject).to receive(:find).and_return "FooPolicy"
57
+ expect(subject.policy).to eq nil
58
+ end
59
+ end
60
+ end
61
+
62
+ describe "#scope!" do
63
+ context "@object is nil" do
64
+ subject { described_class.new(nil) }
65
+
66
+ it "returns the NilClass policy's scope class" do
67
+ expect(subject.scope!).to eq NilClassPolicy::Scope
68
+ end
69
+ end
70
+
71
+ context "@object is defined" do
72
+ subject { described_class.new(post) }
73
+
74
+ it "returns the scope" do
75
+ expect(subject.scope!).to eq PostPolicy::Scope
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "#param_key" do
81
+ context "object responds to model_name" do
82
+ subject { described_class.new(comment) }
83
+
84
+ it "returns the param_key" do
85
+ expect(subject.object).to respond_to(:model_name)
86
+ expect(subject.param_key).to eq "comment_four_five_six"
87
+ end
88
+ end
89
+
90
+ context "object is a class" do
91
+ subject { described_class.new(Article) }
92
+
93
+ it "returns the param_key" do
94
+ expect(subject.object).not_to respond_to(:model_name)
95
+ expect(subject.object).to be_a Class
96
+ expect(subject.param_key).to eq "article"
97
+ end
98
+ end
99
+
100
+ context "object is an instance of a class" do
101
+ subject { described_class.new(article) }
102
+
103
+ it "returns the param_key" do
104
+ expect(subject.object).not_to respond_to(:model_name)
105
+ expect(subject.object).not_to be_a Class
106
+ expect(subject.object).to be_an_instance_of Article
107
+
108
+ expect(subject.param_key).to eq "article"
109
+ end
110
+ end
111
+
112
+ context "object is an array" do
113
+ subject { described_class.new([:project, article]) }
114
+
115
+ it "returns the param_key for the last element of the array" do
116
+ expect(subject.object).not_to respond_to(:model_name)
117
+ expect(subject.object).not_to be_a Class
118
+ expect(subject.object).to be_an_instance_of Array
119
+
120
+ expect(subject.param_key).to eq "article"
121
+ end
122
+ end
123
+ end
124
+ end
data/spec/pundit_spec.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
 
3
5
  describe Pundit do
@@ -8,19 +10,25 @@ describe Pundit do
8
10
  let(:comment) { Comment.new }
9
11
  let(:comment_four_five_six) { CommentFourFiveSix.new }
10
12
  let(:article) { Article.new }
11
- let(:controller) { Controller.new(user, action: "update") }
13
+ let(:controller) { Controller.new(user, "update", {}) }
12
14
  let(:artificial_blog) { ArtificialBlog.new }
13
15
  let(:article_tag) { ArticleTag.new }
14
16
  let(:comments_relation) { CommentsRelation.new }
15
17
  let(:empty_comments_relation) { CommentsRelation.new(true) }
16
18
  let(:tag_four_five_six) { ProjectOneTwoThree::TagFourFiveSix.new(user) }
17
19
  let(:avatar_four_five_six) { ProjectOneTwoThree::AvatarFourFiveSix.new }
20
+ let(:wiki) { Wiki.new }
21
+ let(:thread) { Thread.new }
18
22
 
19
23
  describe ".authorize" do
20
24
  it "infers the policy and authorizes based on it" do
21
25
  expect(Pundit.authorize(user, post, :update?)).to be_truthy
22
26
  end
23
27
 
28
+ it "can be given a different policy class" do
29
+ expect(Pundit.authorize(user, post, :create?, policy_class: PublicationPolicy)).to be_truthy
30
+ end
31
+
24
32
  it "works with anonymous class policies" do
25
33
  expect(Pundit.authorize(user, article_tag, :show?)).to be_truthy
26
34
  expect { Pundit.authorize(user, article_tag, :destroy?) }.to raise_error(Pundit::NotAuthorizedError)
@@ -30,11 +38,18 @@ describe Pundit do
30
38
  # rubocop:disable Style/MultilineBlockChain
31
39
  expect do
32
40
  Pundit.authorize(user, post, :destroy?)
33
- end.to raise_error(Pundit::NotAuthorizedError, "not allowed to destroy? this #<Post>") do |error|
41
+ end.to raise_error(Pundit::NotAuthorizedError, "not allowed to destroy? this Post") do |error|
34
42
  expect(error.query).to eq :destroy?
35
43
  expect(error.record).to eq post
36
44
  expect(error.policy).to eq Pundit.policy(user, post)
37
45
  end
46
+ # rubocop:enable Style/MultilineBlockChain
47
+ end
48
+
49
+ it "raises an error with a invalid policy constructor" do
50
+ expect do
51
+ Pundit.authorize(user, wiki, :update?)
52
+ end.to raise_error(Pundit::InvalidConstructorError, "Invalid #<WikiPolicy> constructor is called")
38
53
  end
39
54
  end
40
55
 
@@ -44,23 +59,43 @@ describe Pundit do
44
59
  end
45
60
 
46
61
  it "returns an instantiated policy scope given an active model class" do
47
- expect(Pundit.policy_scope(user, Comment)).to eq Comment
62
+ expect(Pundit.policy_scope(user, Comment)).to eq CommentScope.new(Comment)
48
63
  end
49
64
 
50
65
  it "returns an instantiated policy scope given an active record relation" do
51
- expect(Pundit.policy_scope(user, comments_relation)).to eq comments_relation
66
+ expect(Pundit.policy_scope(user, comments_relation)).to eq CommentScope.new(comments_relation)
52
67
  end
53
68
 
54
69
  it "returns an instantiated policy scope given an empty active record relation" do
55
- expect(Pundit.policy_scope(user, empty_comments_relation)).to eq empty_comments_relation
70
+ expect(Pundit.policy_scope(user, empty_comments_relation)).to eq CommentScope.new(empty_comments_relation)
71
+ end
72
+
73
+ it "returns an instantiated policy scope given an array of a symbol and plain model class" do
74
+ expect(Pundit.policy_scope(user, [:project, Post])).to eq :read
75
+ end
76
+
77
+ it "returns an instantiated policy scope given an array of a symbol and active model class" do
78
+ expect(Pundit.policy_scope(user, [:project, Comment])).to eq Comment
56
79
  end
57
80
 
58
81
  it "returns nil if the given policy scope can't be found" do
59
82
  expect(Pundit.policy_scope(user, Article)).to be_nil
60
83
  end
61
84
 
62
- it "returns nil if blank object given" do
63
- expect(Pundit.policy_scope(user, nil)).to be_nil
85
+ it "raises an exception if nil object given" do
86
+ expect { Pundit.policy_scope(user, nil) }.to raise_error(Pundit::NotDefinedError)
87
+ end
88
+
89
+ it "raises an error with a invalid policy scope constructor" do
90
+ expect do
91
+ Pundit.policy_scope(user, Wiki)
92
+ end.to raise_error(Pundit::InvalidConstructorError, "Invalid #<WikiPolicy::Scope> constructor is called")
93
+ end
94
+
95
+ it "raises an original error with a policy scope that contains error" do
96
+ expect do
97
+ Pundit.policy_scope(user, Thread)
98
+ end.to raise_error(ArgumentError)
64
99
  end
65
100
  end
66
101
 
@@ -70,7 +105,7 @@ describe Pundit do
70
105
  end
71
106
 
72
107
  it "returns an instantiated policy scope given an active model class" do
73
- expect(Pundit.policy_scope!(user, Comment)).to eq Comment
108
+ expect(Pundit.policy_scope!(user, Comment)).to eq CommentScope.new(Comment)
74
109
  end
75
110
 
76
111
  it "throws an exception if the given policy scope can't be found" do
@@ -84,7 +119,21 @@ describe Pundit do
84
119
  it "throws an exception if the given policy scope is nil" do
85
120
  expect do
86
121
  Pundit.policy_scope!(user, nil)
87
- end.to raise_error(Pundit::NotDefinedError, "unable to find policy scope of nil")
122
+ end.to raise_error(Pundit::NotDefinedError, "Cannot scope NilClass")
123
+ end
124
+
125
+ it "returns an instantiated policy scope given an array of a symbol and plain model class" do
126
+ expect(Pundit.policy_scope!(user, [:project, Post])).to eq :read
127
+ end
128
+
129
+ it "returns an instantiated policy scope given an array of a symbol and active model class" do
130
+ expect(Pundit.policy_scope!(user, [:project, Comment])).to eq Comment
131
+ end
132
+
133
+ it "raises an error with a invalid policy scope constructor" do
134
+ expect do
135
+ Pundit.policy_scope(user, Wiki)
136
+ end.to raise_error(Pundit::InvalidConstructorError, "Invalid #<WikiPolicy::Scope> constructor is called")
88
137
  end
89
138
  end
90
139
 
@@ -121,42 +170,62 @@ describe Pundit do
121
170
  end
122
171
 
123
172
  it "returns an instantiated policy given an array of symbols" do
124
- policy = Pundit.policy(user, [:project, :criteria])
173
+ policy = Pundit.policy(user, %i[project criteria])
125
174
  expect(policy.class).to eq Project::CriteriaPolicy
126
175
  expect(policy.user).to eq user
127
- expect(policy.criteria).to eq [:project, :criteria]
176
+ expect(policy.criteria).to eq :criteria
128
177
  end
129
178
 
130
179
  it "returns an instantiated policy given an array of a symbol and plain model instance" do
131
180
  policy = Pundit.policy(user, [:project, post])
132
181
  expect(policy.class).to eq Project::PostPolicy
133
182
  expect(policy.user).to eq user
134
- expect(policy.post).to eq [:project, post]
183
+ expect(policy.post).to eq post
184
+ end
185
+
186
+ it "returns an instantiated policy given an array of a symbol and a model instance with policy_class override" do
187
+ policy = Pundit.policy(user, [:project, customer_post])
188
+ expect(policy.class).to eq Project::PostPolicy
189
+ expect(policy.user).to eq user
190
+ expect(policy.post).to eq customer_post
135
191
  end
136
192
 
137
193
  it "returns an instantiated policy given an array of a symbol and an active model instance" do
138
194
  policy = Pundit.policy(user, [:project, comment])
139
195
  expect(policy.class).to eq Project::CommentPolicy
140
196
  expect(policy.user).to eq user
141
- expect(policy.post).to eq [:project, comment]
197
+ expect(policy.comment).to eq comment
142
198
  end
143
199
 
144
200
  it "returns an instantiated policy given an array of a symbol and a plain model class" do
145
201
  policy = Pundit.policy(user, [:project, Post])
146
202
  expect(policy.class).to eq Project::PostPolicy
147
203
  expect(policy.user).to eq user
148
- expect(policy.post).to eq [:project, Post]
204
+ expect(policy.post).to eq Post
205
+ end
206
+
207
+ it "raises an error with a invalid policy constructor" do
208
+ expect do
209
+ Pundit.policy(user, Wiki)
210
+ end.to raise_error(Pundit::InvalidConstructorError, "Invalid #<WikiPolicy> constructor is called")
149
211
  end
150
212
 
151
213
  it "returns an instantiated policy given an array of a symbol and an active model class" do
152
214
  policy = Pundit.policy(user, [:project, Comment])
153
215
  expect(policy.class).to eq Project::CommentPolicy
154
216
  expect(policy.user).to eq user
155
- expect(policy.post).to eq [:project, Comment]
217
+ expect(policy.comment).to eq Comment
218
+ end
219
+
220
+ it "returns an instantiated policy given an array of a symbol and a class with policy_class override" do
221
+ policy = Pundit.policy(user, [:project, Customer::Post])
222
+ expect(policy.class).to eq Project::PostPolicy
223
+ expect(policy.user).to eq user
224
+ expect(policy.post).to eq Customer::Post
156
225
  end
157
226
 
158
227
  it "returns correct policy class for an array of a multi-word symbols" do
159
- policy = Pundit.policy(user, [:project_one_two_three, :criteria_four_five_six])
228
+ policy = Pundit.policy(user, %i[project_one_two_three criteria_four_five_six])
160
229
  expect(policy.class).to eq ProjectOneTwoThree::CriteriaFourFiveSixPolicy
161
230
  end
162
231
 
@@ -205,8 +274,8 @@ describe Pundit do
205
274
  expect(Pundit.policy(user, Article)).to be_nil
206
275
  end
207
276
 
208
- it "returns nil if the given policy is nil" do
209
- expect(Pundit.policy(user, nil)).to be_nil
277
+ it "returns the specified NilClassPolicy for nil" do
278
+ expect(Pundit.policy(user, nil)).to be_a NilClassPolicy
210
279
  end
211
280
 
212
281
  describe "with .policy_class set on the model" do
@@ -269,10 +338,10 @@ describe Pundit do
269
338
  end
270
339
 
271
340
  it "returns an instantiated policy given an array of symbols" do
272
- policy = Pundit.policy!(user, [:project, :criteria])
341
+ policy = Pundit.policy!(user, %i[project criteria])
273
342
  expect(policy.class).to eq Project::CriteriaPolicy
274
343
  expect(policy.user).to eq user
275
- expect(policy.criteria).to eq [:project, :criteria]
344
+ expect(policy.criteria).to eq :criteria
276
345
  end
277
346
 
278
347
  it "throws an exception if the given policy can't be found" do
@@ -280,8 +349,14 @@ describe Pundit do
280
349
  expect { Pundit.policy!(user, Article) }.to raise_error(Pundit::NotDefinedError)
281
350
  end
282
351
 
283
- it "throws an exception if the given policy is nil" do
284
- expect { Pundit.policy!(user, nil) }.to raise_error(Pundit::NotDefinedError, "unable to find policy of nil")
352
+ it "returns the specified NilClassPolicy for nil" do
353
+ expect(Pundit.policy!(user, nil)).to be_a NilClassPolicy
354
+ end
355
+
356
+ it "raises an error with a invalid policy constructor" do
357
+ expect do
358
+ Pundit.policy(user, Wiki)
359
+ end.to raise_error(Pundit::InvalidConstructorError, "Invalid #<WikiPolicy> constructor is called")
285
360
  end
286
361
  end
287
362
 
@@ -334,11 +409,19 @@ describe Pundit do
334
409
  expect(controller.authorize(post)).to be_truthy
335
410
  end
336
411
 
412
+ it "returns the record on successful authorization" do
413
+ expect(controller.authorize(post)).to be(post)
414
+ end
415
+
337
416
  it "can be given a different permission to check" do
338
417
  expect(controller.authorize(post, :show?)).to be_truthy
339
418
  expect { controller.authorize(post, :destroy?) }.to raise_error(Pundit::NotAuthorizedError)
340
419
  end
341
420
 
421
+ it "can be given a different policy class" do
422
+ expect(controller.authorize(post, :create?, policy_class: PublicationPolicy)).to be_truthy
423
+ end
424
+
342
425
  it "works with anonymous class policies" do
343
426
  expect(controller.authorize(article_tag, :show?)).to be_truthy
344
427
  expect { controller.authorize(article_tag, :destroy?) }.to raise_error(Pundit::NotAuthorizedError)
@@ -359,7 +442,11 @@ describe Pundit do
359
442
  end
360
443
 
361
444
  it "raises an error when the given record is nil" do
362
- expect { controller.authorize(nil, :destroy?) }.to raise_error(Pundit::NotDefinedError)
445
+ expect { controller.authorize(nil, :destroy?) }.to raise_error(Pundit::NotAuthorizedError)
446
+ end
447
+
448
+ it "raises an error with a invalid policy constructor" do
449
+ expect { controller.authorize(wiki, :destroy?) }.to raise_error(Pundit::InvalidConstructorError)
363
450
  end
364
451
  end
365
452
 
@@ -394,6 +481,10 @@ describe Pundit do
394
481
  expect { controller.policy(article) }.to raise_error(Pundit::NotDefinedError)
395
482
  end
396
483
 
484
+ it "raises an error with a invalid policy constructor" do
485
+ expect { controller.policy(wiki) }.to raise_error(Pundit::InvalidConstructorError)
486
+ end
487
+
397
488
  it "allows policy to be injected" do
398
489
  new_policy = OpenStruct.new
399
490
  controller.policies[post] = new_policy
@@ -407,10 +498,18 @@ describe Pundit do
407
498
  expect(controller.policy_scope(Post)).to eq :published
408
499
  end
409
500
 
501
+ it "allows policy scope class to be overriden" do
502
+ expect(controller.policy_scope(Post, policy_scope_class: PublicationPolicy::Scope)).to eq :published
503
+ end
504
+
410
505
  it "throws an exception if the given policy can't be found" do
411
506
  expect { controller.policy_scope(Article) }.to raise_error(Pundit::NotDefinedError)
412
507
  end
413
508
 
509
+ it "raises an error with a invalid policy scope constructor" do
510
+ expect { controller.policy_scope(Wiki) }.to raise_error(Pundit::InvalidConstructorError)
511
+ end
512
+
414
513
  it "allows policy_scope to be injected" do
415
514
  new_scope = OpenStruct.new
416
515
  controller.policy_scopes[Post] = new_scope
@@ -421,49 +520,71 @@ describe Pundit do
421
520
 
422
521
  describe "#permitted_attributes" do
423
522
  it "checks policy for permitted attributes" do
424
- params = ActionController::Parameters.new(action: "update", post: {
425
- title: "Hello",
426
- votes: 5,
427
- admin: true
428
- })
523
+ params = ActionController::Parameters.new(
524
+ post: {
525
+ title: "Hello",
526
+ votes: 5,
527
+ admin: true
528
+ }
529
+ )
530
+
531
+ action = "update"
429
532
 
430
- expect(Controller.new(user, params).permitted_attributes(post)).to eq("title" => "Hello", "votes" => 5)
431
- expect(Controller.new(double, params).permitted_attributes(post)).to eq("votes" => 5)
533
+ expect(Controller.new(user, action, params).permitted_attributes(post).to_h).to eq(
534
+ "title" => "Hello",
535
+ "votes" => 5
536
+ )
537
+ expect(Controller.new(double, action, params).permitted_attributes(post).to_h).to eq("votes" => 5)
432
538
  end
433
539
 
434
540
  it "checks policy for permitted attributes for record of a ActiveModel type" do
435
- params = ActionController::Parameters.new(action: "update", customer_post: {
436
- title: "Hello",
437
- votes: 5,
438
- admin: true
439
- })
440
-
441
- expect(Controller.new(user, params).permitted_attributes(customer_post)).to eq("title" => "Hello", "votes" => 5)
442
- expect(Controller.new(double, params).permitted_attributes(customer_post)).to eq("votes" => 5)
541
+ params = ActionController::Parameters.new(
542
+ customer_post: {
543
+ title: "Hello",
544
+ votes: 5,
545
+ admin: true
546
+ }
547
+ )
548
+
549
+ action = "update"
550
+
551
+ expect(Controller.new(user, action, params).permitted_attributes(customer_post).to_h).to eq(
552
+ "title" => "Hello",
553
+ "votes" => 5
554
+ )
555
+ expect(Controller.new(double, action, params).permitted_attributes(customer_post).to_h).to eq(
556
+ "votes" => 5
557
+ )
443
558
  end
444
559
  end
445
560
 
446
561
  describe "#permitted_attributes_for_action" do
447
562
  it "is checked if it is defined in the policy" do
448
- params = ActionController::Parameters.new(action: "revise", post: {
449
- title: "Hello",
450
- body: "blah",
451
- votes: 5,
452
- admin: true
453
- })
563
+ params = ActionController::Parameters.new(
564
+ post: {
565
+ title: "Hello",
566
+ body: "blah",
567
+ votes: 5,
568
+ admin: true
569
+ }
570
+ )
454
571
 
455
- expect(Controller.new(user, params).permitted_attributes(post)).to eq("body" => "blah")
572
+ action = "revise"
573
+ expect(Controller.new(user, action, params).permitted_attributes(post).to_h).to eq("body" => "blah")
456
574
  end
457
575
 
458
576
  it "can be explicitly set" do
459
- params = ActionController::Parameters.new(action: "update", post: {
460
- title: "Hello",
461
- body: "blah",
462
- votes: 5,
463
- admin: true
464
- })
465
-
466
- expect(Controller.new(user, params).permitted_attributes(post, :revise)).to eq("body" => "blah")
577
+ params = ActionController::Parameters.new(
578
+ post: {
579
+ title: "Hello",
580
+ body: "blah",
581
+ votes: 5,
582
+ admin: true
583
+ }
584
+ )
585
+
586
+ action = "update"
587
+ expect(Controller.new(user, action, params).permitted_attributes(post, :revise).to_h).to eq("body" => "blah")
467
588
  end
468
589
  end
469
590