forked from marcel/aws-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacl_test.rb
254 lines (214 loc) · 7.22 KB
/
acl_test.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
require File.dirname(__FILE__) + '/test_helper'
class PolicyReadingTest < Test::Unit::TestCase
def setup
@policy = prepare_policy
end
def test_policy_owner
assert_kind_of Owner, @policy.owner
assert_equal 'bb2041a25975c3d4ce9775fe9e93e5b77a6a9fad97dc7e00686191f3790b13f1', @policy.owner.id
assert_equal '[email protected]', @policy.owner.display_name
end
def test_grants
assert @policy.grants
assert [email protected]?
grant = @policy.grants.first
assert_kind_of ACL::Grant, grant
assert_equal 'FULL_CONTROL', grant.permission
end
def test_grants_have_grantee
grant = @policy.grants.first
assert grantee = grant.grantee
assert_kind_of ACL::Grantee, grantee
assert_equal 'bb2041a25975c3d4ce9775fe9e93e5b77a6a9fad97dc7e00686191f3790b13f1', grantee.id
assert_equal '[email protected]', grantee.display_name
assert_equal 'CanonicalUser', grantee.type
end
def test_grantee_always_responds_to_email_address
assert_nothing_raised do
@policy.grants.first.grantee.email_address
end
end
private
def prepare_policy
ACL::Policy.new(parsed_policy)
end
def parsed_policy
Parsing::XmlParser.new Fixtures::Policies.policy_with_one_grant
end
end
class PolicyWritingTest < PolicyReadingTest
def setup
policy = prepare_policy
# Dump the policy to xml and retranslate it back from the xml then run all the tests in the xml reading
# test. This round tripping indirectly asserts that the original xml document is the same as the to_xml
# dump.
@policy = ACL::Policy.new(Parsing::XmlParser.new(policy.to_xml))
end
end
class PolicyTest < Test::Unit::TestCase
def test_building_policy_by_hand
policy = grant = grantee = nil
assert_nothing_raised do
policy = ACL::Policy.new
grant = ACL::Grant.new
grantee = ACL::Grantee.new
grantee.email_address = '[email protected]'
grant.permission = 'READ_ACP'
grant.grantee = grantee
policy.grants << grant
policy.owner = Owner.new('id' => '123456789', 'display_name' => 'noradio')
end
assert_nothing_raised do
policy.to_xml
end
assert !policy.grants.empty?
assert_equal 1, policy.grants.size
assert_equal 'READ_ACP', policy.grants.first.permission
end
def test_include?
policy = ACL::Policy.new(Parsing::XmlParser.new(Fixtures::Policies.policy_with_one_grant))
assert !policy.grants.include?(:public_read)
policy.grants << ACL::Grant.grant(:public_read)
assert policy.grants.include?(:public_read)
assert policy.grants.include?(ACL::Grant.grant(:public_read))
[false, 1, '1'].each do |non_grant|
assert !policy.grants.include?(non_grant)
end
end
def test_delete
policy = ACL::Policy.new(Parsing::XmlParser.new(Fixtures::Policies.policy_with_one_grant))
policy.grants << ACL::Grant.grant(:public_read)
assert policy.grants.include?(:public_read)
assert policy.grants.delete(:public_read)
assert !policy.grants.include?(:public_read)
[false, 1, '1'].each do |non_grant|
assert_nil policy.grants.delete(non_grant)
end
end
def test_grant_list_comparison
policy = ACL::Policy.new
policy2 = ACL::Policy.new
grant_names = [:public_read, :public_read_acp, :authenticated_write]
grant_names.each {|grant_name| policy.grants << ACL::Grant.grant(grant_name)}
grant_names.reverse_each {|grant_name| policy2.grants << ACL::Grant.grant(grant_name)}
assert_equal policy.grants, policy2.grants
end
end
class GrantTest < Test::Unit::TestCase
def test_permission_must_be_valid
grant = ACL::Grant.new
assert_nothing_raised do
grant.permission = 'READ_ACP'
end
assert_raises(InvalidAccessControlLevel) do
grant.permission = 'not a valid permission'
end
end
def test_stock_grants
assert_raises(ArgumentError) do
ACL::Grant.grant :this_is_not_a_stock_grant
end
grant = nil
assert_nothing_raised do
grant = ACL::Grant.grant(:public_read)
end
assert grant
assert_kind_of ACL::Grant, grant
assert_equal 'READ', grant.permission
assert grant.grantee
assert_kind_of ACL::Grantee, grant.grantee
assert_equal 'AllUsers', grant.grantee.group
end
end
class GranteeTest < Test::Unit::TestCase
def test_type_inference
grantee = ACL::Grantee.new
assert_nothing_raised do
grantee.type
end
assert_nil grantee.type
grantee.group = 'AllUsers'
assert_equal 'AllUsers', grantee.group
assert_equal 'Group', grantee.type
grantee.email_address = '[email protected]'
assert_equal 'AmazonCustomerByEmail', grantee.type
grantee.display_name = 'noradio'
assert_equal 'AmazonCustomerByEmail', grantee.type
grantee.id = '123456789'
assert_equal 'CanonicalUser', grantee.type
end
def test_type_is_extracted_if_present
grantee = ACL::Grantee.new('xsi:type' => 'CanonicalUser')
assert_equal 'CanonicalUser', grantee.type
end
def test_type_representation
grantee = ACL::Grantee.new('uri' => 'http://acs.amazonaws.com/groups/global/AllUsers')
assert_equal 'AllUsers Group', grantee.type_representation
grantee.group = 'AuthenticatedUsers'
assert_equal 'AuthenticatedUsers Group', grantee.type_representation
grantee.email_address = '[email protected]'
assert_equal '[email protected]', grantee.type_representation
grantee.display_name = 'noradio'
grantee.id = '123456789'
assert_equal 'noradio', grantee.type_representation
end
end
class ACLOptionProcessorTest < Test::Unit::TestCase
def test_empty_options
options = {}
assert_nothing_raised do
process! options
end
assert_equal({}, options)
end
def test_invalid_access_level
options = {:access => :foo}
assert_raises(InvalidAccessControlLevel) do
process! options
end
end
def test_valid_access_level_is_normalized
valid_access_levels = [
{:access => :private},
{'access' => 'private'},
{:access => 'private'},
{'access' => :private},
{'x-amz-acl' => 'private'},
{:x_amz_acl => :private},
{:x_amz_acl => 'private'},
{'x_amz_acl' => :private}
]
valid_access_levels.each do |options|
assert_nothing_raised do
process! options
end
assert_equal 'private', acl(options)
end
valid_hyphenated_access_levels = [
{:access => :public_read},
{'access' => 'public_read'},
{'access' => 'public-read'},
{:access => 'public_read'},
{:access => 'public-read'},
{'access' => :public_read},
{'x-amz-acl' => 'public_read'},
{:x_amz_acl => :public_read},
{:x_amz_acl => 'public_read'},
{:x_amz_acl => 'public-read'},
{'x_amz_acl' => :public_read}
]
valid_hyphenated_access_levels.each do |options|
assert_nothing_raised do
process! options
end
assert_equal 'public-read', acl(options)
end
end
private
def process!(options)
ACL::OptionProcessor.process!(options)
end
def acl(options)
options['x-amz-acl']
end
end