forked from thoughtbot/shoulda-matchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
allow_mass_assignment_of_matcher.rb
159 lines (144 loc) · 4.49 KB
/
allow_mass_assignment_of_matcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
51
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
154
155
156
157
158
159
module Shoulda
module Matchers
module ActiveModel
# The `allow_mass_assignment_of` matcher tests usage of Rails 3's
# `attr_accessible` and `attr_protected` macros, asserting that an
# attribute in your model is contained in either the whitelist or
# blacklist and thus can or cannot be set via mass assignment.
#
# class Post
# include ActiveModel::Model
# include ActiveModel::MassAssignmentSecurity
# attr_accessor :title
#
# attr_accessible :title
# end
#
# class User
# include ActiveModel::Model
# include ActiveModel::MassAssignmentSecurity
# attr_accessor :encrypted_password
#
# attr_protected :encrypted_password
# end
#
# # RSpec
# RSpec.describe Post, type: :model do
# it { should allow_mass_assignment_of(:title) }
# end
#
# RSpec.describe User, type: :model do
# it { should_not allow_mass_assignment_of(:encrypted_password) }
# end
#
# # Minitest (Shoulda)
# class PostTest < ActiveSupport::TestCase
# should allow_mass_assignment_of(:title)
# end
#
# class UserTest < ActiveSupport::TestCase
# should_not allow_mass_assignment_of(:encrypted_password)
# end
#
# #### Optional qualifiers
#
# ##### as
#
# Use `as` if your mass-assignment rules apply only under a certain role
# *(Rails >= 3.1 only)*.
#
# class Post
# include ActiveModel::Model
# include ActiveModel::MassAssignmentSecurity
# attr_accessor :title
#
# attr_accessible :title, as: :admin
# end
#
# # RSpec
# RSpec.describe Post, type: :model do
# it { should allow_mass_assignment_of(:title).as(:admin) }
# end
#
# # Minitest (Shoulda)
# class PostTest < ActiveSupport::TestCase
# should allow_mass_assignment_of(:title).as(:admin)
# end
#
# @return [AllowMassAssignmentOfMatcher]
#
def allow_mass_assignment_of(value)
AllowMassAssignmentOfMatcher.new(value)
end
# @private
class AllowMassAssignmentOfMatcher
attr_reader :failure_message, :failure_message_when_negated
def initialize(attribute)
@attribute = attribute.to_s
@options = {}
end
def as(role)
@options[:role] = role
self
end
def matches?(subject)
@subject = subject
if attr_mass_assignable?
if whitelisting?
@failure_message_when_negated = "#{@attribute} was made accessible"
else
if protected_attributes.empty?
@failure_message_when_negated = 'no attributes were protected'
else
@failure_message_when_negated = "#{class_name} is protecting " <<
"#{protected_attributes.to_a.to_sentence}, " <<
"but not #{@attribute}."
end
end
true
else
if whitelisting?
@failure_message = "Expected #{@attribute} to be accessible"
else
@failure_message = "Did not expect #{@attribute} to be protected"
end
false
end
end
def description
[base_description, role_description].compact.join(' ')
end
private
def base_description
"allow mass assignment of #{@attribute}"
end
def role_description
if role != :default
"as #{role}"
end
end
def role
@options[:role] || :default
end
def protected_attributes
@protected_attributes ||= (@subject.class.protected_attributes || [])
end
def accessible_attributes
@accessible_attributes ||= (@subject.class.accessible_attributes || [])
end
def whitelisting?
authorizer.kind_of?(::ActiveModel::MassAssignmentSecurity::WhiteList)
end
def attr_mass_assignable?
!authorizer.deny?(@attribute)
end
def authorizer
@subject.class.active_authorizer[role]
end
def class_name
@subject.class.name
end
end
end
end
end