Permalink
Browse files

Add tests for the Cloud Files backend.

  • Loading branch information...
1 parent 142e697 commit 9002414ad9ffa6f126c91e4fec83e6e9ff06c4f3 @minter minter committed Mar 25, 2009
Showing with 127 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +102 −0 test/backends/remote/cloudfiles_test.rb
  3. +11 −0 test/fixtures/attachment.rb
  4. +13 −0 test/schema.rb
View
@@ -1,2 +1,3 @@
test/amazon_s3.yml
+test/rackspace_cloudfiles.yml
test/debug.log
@@ -0,0 +1,102 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
+require 'net/http'
+
+class CloudfilesTest < Test::Unit::TestCase
+ def self.test_CloudFiles?
+ true unless ENV["TEST_CLOUDFILES"] == "false"
+ end
+
+ if test_CloudFiles? && File.exist?(File.join(File.dirname(__FILE__), '../../rackspace_cloudfiles.yml'))
+ include BaseAttachmentTests
+ attachment_model CloudFilesAttachment
+
+ def test_should_create_correct_container_name(klass = CloudFilesAttachment)
+ attachment_model klass
+ attachment = upload_file :filename => '/files/rails.png'
+ assert_equal attachment.cloudfiles_config[:container_name], attachment.container_name
+ end
+
+ test_against_subclass :test_should_create_correct_container_name, CloudFilesAttachment
+
+ def test_should_create_default_path_prefix(klass = CloudFilesAttachment)
+ attachment_model klass
+ attachment = upload_file :filename => '/files/rails.png'
+ assert_equal File.join(attachment_model.table_name, attachment.attachment_path_id), attachment.base_path
+ end
+
+ test_against_subclass :test_should_create_default_path_prefix, CloudFilesAttachment
+
+ def test_should_create_custom_path_prefix(klass = CloudFilesWithPathPrefixAttachment)
+ attachment_model klass
+ attachment = upload_file :filename => '/files/rails.png'
+ assert_equal File.join('some/custom/path/prefix', attachment.attachment_path_id), attachment.base_path
+ end
+
+ test_against_subclass :test_should_create_custom_path_prefix, CloudFilesWithPathPrefixAttachment
+
+
+ def test_should_create_valid_url(klass = CloudFilesAttachment)
+ attachment_model klass
+ attachment = upload_file :filename => '/files/rails.png'
+ assert_match(%r!http://cdn.cloudfiles.mosso.com/(.*?)/cloud_files_attachments/1/rails.png!, attachment.cloudfiles_url)
+ end
+
+ test_against_subclass :test_should_create_valid_url, CloudFilesAttachment
+
+ def test_should_save_attachment(klass = CloudFilesAttachment)
+ attachment_model klass
+ assert_created do
+ attachment = upload_file :filename => '/files/rails.png'
+ assert_valid attachment
+ assert attachment.image?
+ assert !attachment.size.zero?
+ assert_kind_of Net::HTTPOK, http_response_for(attachment.cloudfiles_url)
+ end
+ end
+
+ test_against_subclass :test_should_save_attachment, CloudFilesAttachment
+
+ def test_should_delete_attachment_from_cloud_files_when_attachment_record_destroyed(klass = CloudFilesAttachment)
+ attachment_model klass
+ attachment = upload_file :filename => '/files/rails.png'
+
+ urls = [attachment.cloudfiles_url] + attachment.thumbnails.collect(&:cloudfiles_url)
+
+ urls.each {|url| assert_kind_of Net::HTTPOK, http_response_for(url) }
+ attachment.destroy
+ urls.each do |url|
+ begin
+ http_response_for(url)
+ rescue Net::HTTPForbidden, Net::HTTPNotFound
+ nil
+ end
+ end
+ end
+
+ test_against_subclass :test_should_delete_attachment_from_cloud_files_when_attachment_record_destroyed, CloudFilesAttachment
+
+
+
+ protected
+ def http_response_for(url)
+ url = URI.parse(url)
+ Net::HTTP.start(url.host, url.port) {|http| http.request_head(url.path) }
+ end
+
+ def s3_protocol
+ Technoweenie::AttachmentFu::Backends::S3Backend.protocol
+ end
+
+ def s3_hostname
+ Technoweenie::AttachmentFu::Backends::S3Backend.hostname
+ end
+
+ def s3_port_string
+ Technoweenie::AttachmentFu::Backends::S3Backend.port_string
+ end
+ else
+ def test_flunk_s3
+ puts "s3 config file not loaded, tests not running"
+ end
+ end
+end
@@ -205,11 +205,22 @@ class S3Attachment < ActiveRecord::Base
has_attachment :storage => :s3, :processor => :rmagick, :s3_config_path => File.join(File.dirname(__FILE__), '../amazon_s3.yml')
validates_as_attachment
end
+
+ class CloudFilesAttachment < ActiveRecord::Base
+ has_attachment :storage => :cloud_files, :processor => :rmagick, :cloudfiles_config_path => File.join(File.dirname(__FILE__), '../rackspace_cloudfiles.yml')
+ validates_as_attachment
+ end
class S3WithPathPrefixAttachment < S3Attachment
has_attachment :storage => :s3, :path_prefix => 'some/custom/path/prefix', :processor => :rmagick
validates_as_attachment
end
+
+ class CloudFilesWithPathPrefixAttachment < CloudFilesAttachment
+ has_attachment :storage => :cloud_files, :path_prefix => 'some/custom/path/prefix', :processor => :rmagick
+ validates_as_attachment
+ end
+
rescue
puts "S3 error: #{$!}"
end
View
@@ -118,4 +118,17 @@
t.column :type, :string
t.column :aspect_ratio, :float
end
+
+ create_table :cloud_files_attachments, :force => true do |t|
+ t.column :parent_id, :integer
+ t.column :thumbnail, :string
+ t.column :filename, :string, :limit => 255
+ t.column :content_type, :string, :limit => 255
+ t.column :size, :integer
+ t.column :width, :integer
+ t.column :height, :integer
+ t.column :type, :string
+ t.column :aspect_ratio, :float
+ end
+
end

0 comments on commit 9002414

Please sign in to comment.