tfe-cloudfiles 1.4.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,198 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class CloudfilesContainerTest < Test::Unit::TestCase
4
+
5
+ def test_object_creation
6
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
7
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5'}
8
+ response.stubs(:code).returns('204')
9
+ connection.stubs(:cfreq => response)
10
+ @container = CloudFiles::Container.new(connection, 'test_container')
11
+ assert_equal @container.name, 'test_container'
12
+ assert_equal @container.class, CloudFiles::Container
13
+ assert_equal @container.public?, false
14
+ assert_equal @container.cdn_url, false
15
+ assert_equal @container.cdn_ttl, false
16
+ end
17
+
18
+ def test_object_creation_no_such_container
19
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
20
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5'}
21
+ response.stubs(:code).returns('999')
22
+ connection.stubs(:cfreq => response)
23
+ assert_raise(NoSuchContainerException) do
24
+ @container = CloudFiles::Container.new(connection, 'test_container')
25
+ end
26
+ end
27
+
28
+ def test_object_creation_with_cdn
29
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
30
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5', 'x-cdn-enabled' => 'True', 'x-cdn-uri' => 'http://cdn.test.example/container', 'x-ttl' => '86400'}
31
+ response.stubs(:code).returns('204')
32
+ connection.stubs(:cfreq => response)
33
+ @container = CloudFiles::Container.new(connection, 'test_container')
34
+ assert_equal @container.name, 'test_container'
35
+ assert_equal @container.cdn_enabled, true
36
+ assert_equal @container.public?, true
37
+ assert_equal @container.cdn_url, 'http://cdn.test.example/container'
38
+ assert_equal @container.cdn_ttl, 86400
39
+ end
40
+
41
+ def test_to_s
42
+ build_net_http_object
43
+ assert_equal @container.to_s, 'test_container'
44
+ end
45
+
46
+ def test_make_private_succeeds
47
+ build_net_http_object(:code => '201')
48
+ assert_nothing_raised do
49
+ @container.make_private
50
+ end
51
+ end
52
+
53
+ def test_make_private_fails
54
+ build_net_http_object(:code => '999')
55
+ assert_raises(NoSuchContainerException) do
56
+ @container.make_private
57
+ end
58
+ end
59
+
60
+ def test_make_public_succeeds
61
+ build_net_http_object(:code => '201')
62
+ assert_nothing_raised do
63
+ @container.make_public
64
+ end
65
+ end
66
+
67
+ def test_make_public_fails
68
+ build_net_http_object(:code => '999')
69
+ assert_raises(NoSuchContainerException) do
70
+ @container.make_public
71
+ end
72
+ end
73
+
74
+ def test_empty_is_false
75
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
76
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5'}
77
+ response.stubs(:code).returns('204')
78
+ connection.stubs(:cfreq => response)
79
+ @container = CloudFiles::Container.new(connection, 'test_container')
80
+ assert_equal @container.empty?, false
81
+ end
82
+
83
+ def test_empty_is_true
84
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
85
+ response = {'x-container-bytes-used' => '0', 'x-container-object-count' => '0'}
86
+ response.stubs(:code).returns('204')
87
+ connection.stubs(:cfreq => response)
88
+ @container = CloudFiles::Container.new(connection, 'test_container')
89
+ assert_equal @container.empty?, true
90
+ end
91
+
92
+ def test_log_retention_is_true
93
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
94
+ response = {'x-container-bytes-used' => '0', 'x-container-object-count' => '0', 'x-cdn-enabled' => 'True', 'x-log-retention' => 'True'}
95
+ response.stubs(:code).returns('204')
96
+ connection.stubs(:cfreq => response)
97
+ @container = CloudFiles::Container.new(connection, 'test_container')
98
+ assert_equal @container.log_retention?, true
99
+ end
100
+
101
+ def test_object_fetch
102
+ build_net_http_object(:code => '204', :response => {'last-modified' => 'Wed, 28 Jan 2009 16:16:26 GMT'})
103
+ object = @container.object('good_object')
104
+ assert_equal object.class, CloudFiles::StorageObject
105
+ end
106
+
107
+ def test_create_object
108
+ build_net_http_object()
109
+ object = @container.create_object('new_object')
110
+ assert_equal object.class, CloudFiles::StorageObject
111
+ end
112
+
113
+ def test_object_exists_true
114
+ build_net_http_object
115
+ assert_equal @container.object_exists?('good_object'), true
116
+ end
117
+
118
+ def test_object_exists_false
119
+ build_net_http_object(:code => '999')
120
+ assert_equal @container.object_exists?('bad_object'), false
121
+ end
122
+
123
+ def test_delete_object_succeeds
124
+ build_net_http_object
125
+ assert_nothing_raised do
126
+ @container.delete_object('good_object')
127
+ end
128
+ end
129
+
130
+ def test_delete_invalid_object_fails
131
+ build_net_http_object(:code => '404')
132
+ assert_raise(NoSuchObjectException) do
133
+ @container.delete_object('nonexistent_object')
134
+ end
135
+ end
136
+
137
+ def test_delete_invalid_response_code_fails
138
+ build_net_http_object(:code => '999')
139
+ assert_raise(InvalidResponseException) do
140
+ @container.delete_object('broken_object')
141
+ end
142
+ end
143
+
144
+ def test_fetch_objects
145
+ build_net_http_object(:code => '200', :body => "foo\nbar\nbaz")
146
+ objects = @container.objects
147
+ assert_equal objects.class, Array
148
+ assert_equal objects.size, 3
149
+ assert_equal objects.first, 'foo'
150
+ end
151
+
152
+ def test_fetch_object_detail
153
+ body = %{<?xml version="1.0" encoding="UTF-8"?>
154
+ <container name="video">
155
+ <object><name>kisscam.mov</name><hash>96efd5a0d78b74cfe2a911c479b98ddd</hash><bytes>9196332</bytes><content_type>video/quicktime</content_type><last_modified>2008-12-18T10:34:43.867648</last_modified></object>
156
+ <object><name>penaltybox.mov</name><hash>d2a4c0c24d8a7b4e935bee23080e0685</hash><bytes>24944966</bytes><content_type>video/quicktime</content_type><last_modified>2008-12-18T10:35:19.273927</last_modified></object>
157
+ </container>
158
+ }
159
+ build_net_http_object(:code => '200', :body => body)
160
+ details = @container.objects_detail
161
+ assert_equal details.size, 2
162
+ assert_equal details['kisscam.mov'][:bytes], '9196332'
163
+ end
164
+
165
+ def test_fetch_object_detail_empty
166
+ build_net_http_object
167
+ details = @container.objects_detail
168
+ assert_equal details, {}
169
+ end
170
+
171
+ def test_fetch_object_detail_error
172
+ build_net_http_object(:code => '999')
173
+ assert_raise(InvalidResponseException) do
174
+ details = @container.objects_detail
175
+ end
176
+ end
177
+
178
+ def test_setting_log_retention
179
+ build_net_http_object(:code => '201')
180
+ assert(@container.log_retention='false')
181
+ end
182
+
183
+ private
184
+
185
+ def build_net_http_object(args={:code => '204' })
186
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
187
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
188
+ args[:response] = {} unless args[:response]
189
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token', 'last-modified' => Time.now.to_s}.merge(args[:response])
190
+ response.stubs(:code).returns(args[:code])
191
+ response.stubs(:body).returns args[:body] || nil
192
+ connection.stubs(:cfreq => response)
193
+ @container = CloudFiles::Container.new(connection, 'test_container')
194
+ @container.stubs(:connection).returns(connection)
195
+ end
196
+
197
+
198
+ end
@@ -0,0 +1,209 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class CloudfilesStorageObjectTest < Test::Unit::TestCase
4
+
5
+ def test_object_creation
6
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
7
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5', 'last-modified' => Time.now.to_s}
8
+ response.stubs(:code).returns('204')
9
+ connection.stubs(:cfreq => response)
10
+ container = CloudFiles::Container.new(connection, 'test_container')
11
+ @object = CloudFiles::StorageObject.new(container, 'test_object')
12
+ assert_equal @object.name, 'test_object'
13
+ assert_equal @object.class, CloudFiles::StorageObject
14
+ assert_equal @object.to_s, 'test_object'
15
+ end
16
+
17
+ def test_object_creation_with_invalid_name
18
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
19
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5', 'last-modified' => Time.now.to_s}
20
+ response.stubs(:code).returns('204')
21
+ connection.stubs(:cfreq => response)
22
+ container = CloudFiles::Container.new(connection, 'test_container')
23
+ assert_raises SyntaxException do
24
+ @object = CloudFiles::StorageObject.new(container, 'test_object?')
25
+ end
26
+ end
27
+
28
+
29
+ def test_public_url_exists
30
+ build_net_http_object(:public => true, :name => 'test object')
31
+ assert_equal @object.public_url, "http://cdn.test.example/test%20object"
32
+ end
33
+
34
+ def test_public_url_does_not_exist
35
+ build_net_http_object
36
+ assert_equal @object.public_url, nil
37
+ end
38
+
39
+ def test_data_succeeds
40
+ build_net_http_object(:code => '200', :body => 'This is good data')
41
+ assert_equal @object.data, 'This is good data'
42
+ end
43
+
44
+ def test_data_with_offset_succeeds
45
+ build_net_http_object(:code => '200', :body => 'Thi')
46
+ assert_equal @object.data(3), 'Thi'
47
+ end
48
+
49
+ def test_data_fails
50
+ build_net_http_object(:code => '999', :body => 'This is bad data')
51
+ assert_raise(NoSuchObjectException) do
52
+ @object.data
53
+ end
54
+ end
55
+
56
+ def test_data_stream_succeeds
57
+ build_net_http_object(:code => '200', :body => 'This is good data')
58
+ data = ""
59
+ assert_nothing_raised do
60
+ @object.data_stream { |chunk|
61
+ data += chunk
62
+ }
63
+ end
64
+ end
65
+
66
+ def test_data_stream_with_offset_succeeds
67
+ build_net_http_object(:code => '200', :body => 'This ')
68
+ data = ""
69
+ assert_nothing_raised do
70
+ @object.data_stream(5) { |chunk|
71
+ data += chunk
72
+ }
73
+ end
74
+ end
75
+
76
+ # Need to find a way to simulate this properly
77
+ def data_stream_fails
78
+ build_net_http_object(:code => '999', :body => 'This is bad data')
79
+ data = ""
80
+ assert_raise(NoSuchObjectException) do
81
+ @object.data_stream { |chunk|
82
+ data += chunk
83
+ }
84
+ end
85
+ end
86
+
87
+ def test_set_metadata_succeeds
88
+ CloudFiles::StorageObject.any_instance.stubs(:populate).returns(true)
89
+ build_net_http_object(:code => '202')
90
+ assert_nothing_raised do
91
+ @object.set_metadata({'Foo' =>'bar'})
92
+ end
93
+ end
94
+
95
+ def test_set_metadata_invalid_object
96
+ build_net_http_object(:code => '404')
97
+ assert_raise(NoSuchObjectException) do
98
+ @object.set_metadata({'Foo' =>'bar'})
99
+ end
100
+ end
101
+
102
+ def test_set_metadata_fails
103
+ build_net_http_object(:code => '999')
104
+ assert_raise(InvalidResponseException) do
105
+ @object.set_metadata({'Foo' =>'bar'})
106
+ end
107
+ end
108
+
109
+ def test_read_metadata_succeeds
110
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
111
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5', 'x-object-meta-foo' => 'Bar', 'last-modified' => Time.now.to_s}
112
+ response.stubs(:code).returns('204')
113
+ connection.stubs(:cfreq => response)
114
+ container = CloudFiles::Container.new(connection, 'test_container')
115
+ @object = CloudFiles::StorageObject.new(container, 'test_object')
116
+ assert_equal @object.metadata, {'foo' => 'Bar'}
117
+ end
118
+
119
+ def test_write_succeeds
120
+ CloudFiles::StorageObject.any_instance.stubs(:populate).returns(true)
121
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
122
+ build_net_http_object(:code => '201')
123
+ assert_nothing_raised do
124
+ @object.write("This is test data")
125
+ end
126
+ end
127
+
128
+ def test_write_with_make_path
129
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
130
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5', 'last-modified' => Time.now.to_s}
131
+ response.stubs(:code).returns('204').then.returns('204').then.returns('201').then.returns('204')
132
+ connection.stubs(:cfreq => response)
133
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
134
+ container = CloudFiles::Container.new(connection, 'test_container')
135
+ @object = CloudFiles::StorageObject.new(container, 'path/to/my/test_object', false, true)
136
+ assert_nothing_raised do
137
+ @object.write("This is path test data")
138
+ end
139
+ end
140
+
141
+ def test_load_from_filename_succeeds
142
+ require 'tempfile'
143
+ out = Tempfile.new('test')
144
+ out.write("This is test data")
145
+ out.close
146
+ CloudFiles::StorageObject.any_instance.stubs(:populate).returns(true)
147
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
148
+ build_net_http_object(:code => '201')
149
+ assert_nothing_raised do
150
+ @object.load_from_filename(out.path)
151
+ end
152
+ end
153
+
154
+ def test_write_sets_mime_type
155
+ CloudFiles::StorageObject.any_instance.stubs(:populate).returns(true)
156
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
157
+ build_net_http_object(:name => 'myfile.xml', :code => '201')
158
+ assert_nothing_raised do
159
+ @object.write("This is test data")
160
+ end
161
+ end
162
+
163
+ def test_write_with_no_data_dies
164
+ build_net_http_object
165
+ assert_raise(SyntaxException) do
166
+ @object.write
167
+ end
168
+ end
169
+
170
+ def test_write_with_invalid_content_length_dies
171
+ build_net_http_object(:code => '412')
172
+ assert_raise(InvalidResponseException) do
173
+ @object.write('Test Data')
174
+ end
175
+ end
176
+
177
+ def test_write_with_mismatched_md5_dies
178
+ build_net_http_object(:code => '422')
179
+ assert_raise(MisMatchedChecksumException) do
180
+ @object.write('Test Data')
181
+ end
182
+ end
183
+
184
+ def test_write_with_invalid_response_dies
185
+ build_net_http_object(:code => '999')
186
+ assert_raise(InvalidResponseException) do
187
+ @object.write('Test Data')
188
+ end
189
+ end
190
+
191
+ private
192
+
193
+ def build_net_http_object(args={:code => '204' })
194
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
195
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
196
+ args[:response] = {} unless args[:response]
197
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token', 'last-modified' => Time.now.to_s}.merge(args[:response])
198
+ response.stubs(:code).returns(args[:code])
199
+ response.stubs(:body).returns args[:body] || nil
200
+ connection.stubs(:cfreq => response)
201
+ container = CloudFiles::Container.new(connection, 'test_container')
202
+ container.stubs(:connection).returns(connection)
203
+ container.stubs(:public?).returns(args[:public] || false)
204
+ container.stubs(:cdn_url).returns('http://cdn.test.example')
205
+ @object = CloudFiles::StorageObject.new(container, args[:name] || 'test_object')
206
+ end
207
+
208
+
209
+ end
@@ -0,0 +1,5 @@
1
+ require 'test/unit'
2
+ $:.unshift File.dirname(__FILE__) + '/../lib'
3
+ require 'cloudfiles'
4
+ require 'rubygems'
5
+ require 'mocha'
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{tfe-cloudfiles}
8
+ s.version = "1.4.7"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["H. Wade Minter", "Rackspace Hosting", "Todd Eichel"]
12
+ s.date = %q{2010-05-27}
13
+ s.description = %q{A Ruby version of the Rackspace Cloud Files API.}
14
+ s.email = ["wade.minter@rackspace.com", "todd@toddeichel.com"]
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc",
17
+ "TODO"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "COPYING",
22
+ "Manifest",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "TODO",
26
+ "VERSION",
27
+ "lib/cloudfiles.rb",
28
+ "lib/cloudfiles/authentication.rb",
29
+ "lib/cloudfiles/connection.rb",
30
+ "lib/cloudfiles/container.rb",
31
+ "lib/cloudfiles/storage_object.rb",
32
+ "test/cf-testunit.rb",
33
+ "test/cloudfiles_authentication_test.rb",
34
+ "test/cloudfiles_connection_test.rb",
35
+ "test/cloudfiles_container_test.rb",
36
+ "test/cloudfiles_storage_object_test.rb",
37
+ "test/test_helper.rb",
38
+ "tfe-cloudfiles.gemspec"
39
+ ]
40
+ s.homepage = %q{http://www.rackspacecloud.com/cloud_hosting_products/files}
41
+ s.rdoc_options = ["--charset=UTF-8"]
42
+ s.require_paths = ["lib"]
43
+ s.rubygems_version = %q{1.3.6}
44
+ s.summary = %q{A Ruby API into Rackspace Cloud Files}
45
+ s.test_files = [
46
+ "test/cf-testunit.rb",
47
+ "test/cloudfiles_authentication_test.rb",
48
+ "test/cloudfiles_connection_test.rb",
49
+ "test/cloudfiles_container_test.rb",
50
+ "test/cloudfiles_storage_object_test.rb",
51
+ "test/test_helper.rb"
52
+ ]
53
+
54
+ if s.respond_to? :specification_version then
55
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
56
+ s.specification_version = 3
57
+
58
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
59
+ s.add_runtime_dependency(%q<mime-types>, [">= 1.16"])
60
+ else
61
+ s.add_dependency(%q<mime-types>, [">= 1.16"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<mime-types>, [">= 1.16"])
65
+ end
66
+ end
67
+