ghazel-aws-s3 0.6.4 → 0.6.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,66 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class TypecastingTest < Test::Unit::TestCase
4
+ # Make it easier to call methods in tests
5
+ Parsing::Typecasting.public_instance_methods.each do |method|
6
+ Parsing::Typecasting.send(:module_function, method)
7
+ end
8
+
9
+ def test_array_with_one_element_that_is_a_hash
10
+ value = [{'Available' => 'true'}]
11
+ assert_equal [{'available' => true}], Parsing::Typecasting.typecast(value)
12
+ end
13
+
14
+ def test_hash_with_one_key_whose_value_is_an_array
15
+ value = {
16
+ 'Bucket' =>
17
+ [
18
+ {'Available' => 'true'}
19
+ ]
20
+ }
21
+
22
+ expected = {
23
+ 'bucket' =>
24
+ [
25
+ {'available' => true}
26
+ ]
27
+ }
28
+ assert_equal expected, Parsing::Typecasting.typecast(value)
29
+ end
30
+
31
+ end
32
+
33
+ class XmlParserTest < Test::Unit::TestCase
34
+ def test_bucket_is_always_forced_to_be_an_array_unless_empty
35
+ one_bucket = Parsing::XmlParser.new(Fixtures::Buckets.bucket_list_with_one_bucket)
36
+ more_than_one = Parsing::XmlParser.new(Fixtures::Buckets.bucket_list_with_more_than_one_bucket)
37
+
38
+ [one_bucket, more_than_one].each do |bucket_list|
39
+ assert_kind_of Array, bucket_list['buckets']['bucket']
40
+ end
41
+
42
+ no_buckets = Parsing::XmlParser.new(Fixtures::Buckets.empty_bucket_list)
43
+ assert no_buckets.has_key?('buckets')
44
+ assert_nil no_buckets['buckets']
45
+ end
46
+
47
+ def test_bucket_contents_are_forced_to_be_an_array_unless_empty
48
+ one_key = Parsing::XmlParser.new(Fixtures::Buckets.bucket_with_one_key)
49
+ more_than_one = Parsing::XmlParser.new(Fixtures::Buckets.bucket_with_more_than_one_key)
50
+ [one_key, more_than_one].each do |bucket_with_contents|
51
+ assert_kind_of Array, bucket_with_contents['contents']
52
+ end
53
+
54
+ no_keys = Parsing::XmlParser.new(Fixtures::Buckets.empty_bucket)
55
+ assert !no_keys.has_key?('contents')
56
+ end
57
+
58
+ def test_policy_grants_are_always_an_array
59
+ policy = Parsing::XmlParser.new(Fixtures::Policies.policy_with_one_grant)
60
+ assert_kind_of Array, policy['access_control_list']['grant']
61
+ end
62
+
63
+ def test_empty_xml_response_is_not_parsed
64
+ assert_equal({}, Parsing::XmlParser.new(''))
65
+ end
66
+ end
@@ -0,0 +1,117 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class RemoteACLTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ establish_real_connection
7
+ end
8
+
9
+ def teardown
10
+ disconnect!
11
+ end
12
+
13
+ def test_acl
14
+ Bucket.create(TEST_BUCKET) # Wipe out the existing bucket's ACL
15
+
16
+ bucket_policy = Bucket.acl(TEST_BUCKET)
17
+ assert_equal 1, bucket_policy.grants.size
18
+ assert !bucket_policy.grants.include?(:public_read_acp)
19
+
20
+ bucket_policy.grants << ACL::Grant.grant(:public_read_acp)
21
+
22
+ assert_nothing_raised do
23
+ Bucket.acl(TEST_BUCKET, bucket_policy)
24
+ end
25
+
26
+ bucket = Bucket.find(TEST_BUCKET)
27
+ assert bucket.acl.grants.include?(:public_read_acp)
28
+
29
+ bucket.acl.grants.pop # Get rid of the newly added grant
30
+
31
+ assert !bucket.acl.grants.include?(:public_read_acp)
32
+ bucket.acl(bucket.acl) # Update its acl
33
+ assert Service.response.success?
34
+
35
+ bucket_policy = Bucket.acl(TEST_BUCKET)
36
+ assert_equal 1, bucket_policy.grants.size
37
+ assert !bucket_policy.grants.include?(:public_read_acp)
38
+
39
+ S3Object.store('testing-acls', 'the test data', TEST_BUCKET, :content_type => 'text/plain')
40
+ acl = S3Object.acl('testing-acls', TEST_BUCKET)
41
+
42
+ # Confirm object has the default policy
43
+
44
+ assert !acl.grants.empty?
45
+ assert_equal 1, acl.grants.size
46
+ grant = acl.grants.first
47
+
48
+ assert_equal 'FULL_CONTROL', grant.permission
49
+
50
+ grantee = grant.grantee
51
+
52
+ assert acl.owner.id
53
+ assert acl.owner.display_name
54
+ assert grantee.id
55
+ assert grantee.display_name
56
+
57
+ assert_equal acl.owner.id, grantee.id
58
+ assert_equal acl.owner.display_name, grantee.display_name
59
+
60
+ assert_equal Owner.current, acl.owner
61
+
62
+
63
+ # Manually add read access to an Amazon customer by email address
64
+
65
+ new_grant = ACL::Grant.new
66
+ new_grant.permission = 'READ'
67
+ new_grant_grantee = ACL::Grantee.new
68
+ new_grant_grantee.email_address = 'marcel@vernix.org'
69
+ new_grant.grantee = new_grant_grantee
70
+ acl.grants << new_grant
71
+
72
+ assert_nothing_raised do
73
+ S3Object.acl('testing-acls', TEST_BUCKET, acl)
74
+ end
75
+
76
+ # Confirm the acl was updated successfully
77
+
78
+ assert Service.response.success?
79
+
80
+ acl = S3Object.acl('testing-acls', TEST_BUCKET)
81
+ assert !acl.grants.empty?
82
+ assert_equal 2, acl.grants.size
83
+ new_grant = acl.grants.last
84
+ assert_equal 'READ', new_grant.permission
85
+
86
+ # Confirm instance method has same result
87
+
88
+ assert_equal acl.grants, S3Object.find('testing-acls', TEST_BUCKET).acl.grants
89
+
90
+ # Get rid of the grant we just added
91
+
92
+ acl.grants.pop
93
+
94
+ # Confirm acl class method sees that the bucket option is being used to put a new acl
95
+
96
+ assert_nothing_raised do
97
+ TestS3Object.acl('testing-acls', acl)
98
+ end
99
+
100
+ assert Service.response.success?
101
+
102
+ acl = TestS3Object.acl('testing-acls')
103
+
104
+ # Confirm added grant was removed from the policy
105
+
106
+ assert !acl.grants.empty?
107
+ assert_equal 1, acl.grants.size
108
+ grant = acl.grants.first
109
+ assert_equal 'FULL_CONTROL', grant.permission
110
+
111
+ assert_nothing_raised do
112
+ S3Object.delete('testing-acls', TEST_BUCKET)
113
+ end
114
+
115
+ assert Service.response.success?
116
+ end
117
+ end
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class RemoteBittorrentTest < Test::Unit::TestCase
4
+ def setup
5
+ establish_real_connection
6
+ end
7
+
8
+ def teardown
9
+ disconnect!
10
+ end
11
+
12
+ def test_bittorrent
13
+ bt_test_key = 'testing-bittorrent'
14
+ S3Object.create(bt_test_key, 'foo', TEST_BUCKET)
15
+
16
+ # Confirm we can fetch a bittorrent file for this object
17
+
18
+ torrent_file = nil
19
+ assert_nothing_raised do
20
+ torrent_file = S3Object.torrent_for(bt_test_key, TEST_BUCKET)
21
+ end
22
+ assert torrent_file
23
+ assert torrent_file['tracker']
24
+
25
+ # Make object accessible to the public via a torrent
26
+
27
+ policy = S3Object.acl(bt_test_key, TEST_BUCKET)
28
+
29
+ assert !policy.grants.include?(:public_read)
30
+
31
+ assert_nothing_raised do
32
+ S3Object.grant_torrent_access_to(bt_test_key, TEST_BUCKET)
33
+ end
34
+
35
+ policy = S3Object.acl(bt_test_key, TEST_BUCKET)
36
+
37
+ assert policy.grants.include?(:public_read)
38
+
39
+ # Confirm instance method wraps class method
40
+
41
+ assert_equal torrent_file, S3Object.find(bt_test_key, TEST_BUCKET).torrent
42
+
43
+ S3Object.delete(bt_test_key, TEST_BUCKET)
44
+ end
45
+ end
@@ -0,0 +1,146 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class RemoteBucketTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ establish_real_connection
7
+ assert Bucket.find(TEST_BUCKET).delete_all
8
+ end
9
+
10
+ def teardown
11
+ disconnect!
12
+ end
13
+
14
+ def test_bucket
15
+ # Fetch the testing bucket
16
+
17
+ bucket = nil
18
+ assert_nothing_raised do
19
+ bucket = Bucket.find(TEST_BUCKET)
20
+ end
21
+
22
+ assert bucket
23
+
24
+ # Confirm we can fetch the bucket implicitly
25
+
26
+ bucket = nil
27
+ assert_nothing_raised do
28
+ bucket = TestBucket.find
29
+ end
30
+
31
+ assert bucket
32
+
33
+ # Confirm the bucket has the right name
34
+
35
+ assert_equal TEST_BUCKET, bucket.name
36
+
37
+ assert bucket.empty?
38
+ assert_equal 0, bucket.size
39
+
40
+ # Add some files to the bucket
41
+
42
+ assert_nothing_raised do
43
+ %w(a m z).each do |file_name|
44
+ S3Object.create(file_name, file_name, bucket.name, :content_type => 'text/plain')
45
+ end
46
+ end
47
+
48
+ # Confirm that we can reload the objects
49
+
50
+ assert_nothing_raised do
51
+ bucket.objects(:reload)
52
+ end
53
+
54
+ assert !bucket.empty?
55
+ assert_equal 3, bucket.size
56
+
57
+ assert_nothing_raised do
58
+ bucket.objects(:marker => 'm')
59
+ end
60
+
61
+ assert_equal 1, bucket.size
62
+ assert bucket['z']
63
+
64
+ assert_equal 1, Bucket.find(TEST_BUCKET, :max_keys => 1).size
65
+
66
+ assert_nothing_raised do
67
+ bucket.objects(:reload)
68
+ end
69
+
70
+ assert_equal 3, bucket.size
71
+
72
+ # Ensure the reloaded buckets have been repatriated
73
+
74
+ assert_equal bucket, bucket.objects.first.bucket
75
+
76
+ # Confirm that we can delete one of the objects and it will be removed
77
+
78
+ object_to_be_deleted = bucket.objects.last
79
+ assert_nothing_raised do
80
+ object_to_be_deleted.delete
81
+ end
82
+
83
+ assert !bucket.objects.include?(object_to_be_deleted)
84
+
85
+ # Confirm that we can add an object
86
+
87
+ object = bucket.new_object(:value => 'hello')
88
+
89
+ assert_raises(NoKeySpecified) do
90
+ object.store
91
+ end
92
+
93
+ object.key = 'abc'
94
+ assert_nothing_raised do
95
+ object.store
96
+ end
97
+
98
+ assert bucket.objects.include?(object)
99
+
100
+ # Confirm that the object is still there after reloading its objects
101
+
102
+ assert_nothing_raised do
103
+ bucket.objects(:reload)
104
+ end
105
+ assert bucket.objects.include?(object)
106
+
107
+ # Check that TestBucket has the same objects fetched implicitly
108
+
109
+ assert_equal bucket.objects, TestBucket.objects
110
+
111
+ # Empty out bucket
112
+
113
+ assert_nothing_raised do
114
+ bucket.delete_all
115
+ end
116
+
117
+ assert bucket.empty?
118
+
119
+ bucket = nil
120
+ assert_nothing_raised do
121
+ bucket = Bucket.find(TEST_BUCKET)
122
+ end
123
+
124
+ assert bucket.empty?
125
+ end
126
+
127
+ def test_bucket_name_is_switched_with_options_when_bucket_is_implicit_and_options_are_passed
128
+ Object.const_set(:ImplicitlyNamedBucket, Class.new(Bucket))
129
+ ImplicitlyNamedBucket.current_bucket = TEST_BUCKET
130
+ assert ImplicitlyNamedBucket.objects.empty?
131
+
132
+ %w(a b c).each {|key| S3Object.store(key, 'value does not matter', TEST_BUCKET)}
133
+
134
+ assert_equal 3, ImplicitlyNamedBucket.objects.size
135
+
136
+ objects = nil
137
+ assert_nothing_raised do
138
+ objects = ImplicitlyNamedBucket.objects(:max_keys => 1)
139
+ end
140
+
141
+ assert objects
142
+ assert_equal 1, objects.size
143
+ ensure
144
+ %w(a b c).each {|key| S3Object.delete(key, TEST_BUCKET)}
145
+ end
146
+ end
@@ -0,0 +1,82 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class RemoteLoggingTest < Test::Unit::TestCase
4
+ def setup
5
+ establish_real_connection
6
+ end
7
+
8
+ def teardown
9
+ disconnect!
10
+ end
11
+
12
+ def test_logging
13
+ Bucket.create(TEST_BUCKET) # Clear out any custom grants
14
+
15
+ # Confirm that logging is not enabled on the test bucket
16
+
17
+ assert !Bucket.logging_enabled_for?(TEST_BUCKET)
18
+ assert !Bucket.find(TEST_BUCKET).logging_enabled?
19
+
20
+ assert_equal [], Bucket.logs_for(TEST_BUCKET)
21
+
22
+ # Confirm the current bucket doesn't have logging grants
23
+
24
+ policy = Bucket.acl(TEST_BUCKET)
25
+ assert !policy.grants.include?(:logging_read_acp)
26
+ assert !policy.grants.include?(:logging_write)
27
+
28
+ # Confirm that we can enable logging
29
+
30
+ assert_nothing_raised do
31
+ Bucket.enable_logging_for TEST_BUCKET
32
+ end
33
+
34
+ # Confirm enabling logging worked
35
+
36
+ assert Service.response.success?
37
+
38
+ assert Bucket.logging_enabled_for?(TEST_BUCKET)
39
+ assert Bucket.find(TEST_BUCKET).logging_enabled?
40
+
41
+ # Confirm the appropriate grants were added
42
+
43
+ policy = Bucket.acl(TEST_BUCKET)
44
+ assert policy.grants.include?(:logging_read_acp)
45
+ assert policy.grants.include?(:logging_write)
46
+
47
+ # Confirm logging status used defaults
48
+
49
+ logging_status = Bucket.logging_status_for TEST_BUCKET
50
+ assert_equal TEST_BUCKET, logging_status.target_bucket
51
+ assert_equal 'log-', logging_status.target_prefix
52
+
53
+ # Confirm we can update the logging status
54
+
55
+ logging_status.target_prefix = 'access-log-'
56
+
57
+ assert_nothing_raised do
58
+ Bucket.logging_status_for TEST_BUCKET, logging_status
59
+ end
60
+
61
+ assert Service.response.success?
62
+
63
+ logging_status = Bucket.logging_status_for TEST_BUCKET
64
+ assert_equal 'access-log-', logging_status.target_prefix
65
+
66
+ # Confirm we can make a request for the bucket's logs
67
+
68
+ assert_nothing_raised do
69
+ Bucket.logs_for TEST_BUCKET
70
+ end
71
+
72
+ # Confirm we can disable logging
73
+
74
+ assert_nothing_raised do
75
+ Bucket.disable_logging_for(TEST_BUCKET)
76
+ end
77
+
78
+ assert Service.response.success?
79
+
80
+ assert !Bucket.logging_enabled_for?(TEST_BUCKET)
81
+ end
82
+ end