1 require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
4 class CloudfilesTest < Test::Unit::TestCase
5 def self.test_CloudFiles?
6 true unless ENV["TEST_CLOUDFILES"] == "false"
9 if test_CloudFiles? && File.exist?(File.join(File.dirname(__FILE__), '../../rackspace_cloudfiles.yml'))
10 include BaseAttachmentTests
11 attachment_model CloudFilesAttachment
13 def test_should_create_correct_container_name(klass = CloudFilesAttachment)
14 attachment_model klass
15 attachment = upload_file :filename => '/files/rails.png'
16 assert_equal attachment.cloudfiles_config[:container_name], attachment.container_name
19 test_against_subclass :test_should_create_correct_container_name, CloudFilesAttachment
21 def test_should_create_default_path_prefix(klass = CloudFilesAttachment)
22 attachment_model klass
23 attachment = upload_file :filename => '/files/rails.png'
24 assert_equal File.join(attachment_model.table_name, attachment.attachment_path_id), attachment.base_path
27 test_against_subclass :test_should_create_default_path_prefix, CloudFilesAttachment
29 def test_should_create_custom_path_prefix(klass = CloudFilesWithPathPrefixAttachment)
30 attachment_model klass
31 attachment = upload_file :filename => '/files/rails.png'
32 assert_equal File.join('some/custom/path/prefix', attachment.attachment_path_id), attachment.base_path
35 test_against_subclass :test_should_create_custom_path_prefix, CloudFilesWithPathPrefixAttachment
38 def test_should_create_valid_url(klass = CloudFilesAttachment)
39 attachment_model klass
40 attachment = upload_file :filename => '/files/rails.png'
41 assert_match(%r!http://cdn.cloudfiles.mosso.com/(.*?)/cloud_files_attachments/1/rails.png!, attachment.cloudfiles_url)
44 test_against_subclass :test_should_create_valid_url, CloudFilesAttachment
46 def test_should_save_attachment(klass = CloudFilesAttachment)
47 attachment_model klass
49 attachment = upload_file :filename => '/files/rails.png'
50 assert_valid attachment
51 assert attachment.image?
52 assert !attachment.size.zero?
53 assert_kind_of Net::HTTPOK, http_response_for(attachment.cloudfiles_url)
57 test_against_subclass :test_should_save_attachment, CloudFilesAttachment
59 def test_should_delete_attachment_from_cloud_files_when_attachment_record_destroyed(klass = CloudFilesAttachment)
60 attachment_model klass
61 attachment = upload_file :filename => '/files/rails.png'
63 urls = [attachment.cloudfiles_url] + attachment.thumbnails.collect(&:cloudfiles_url)
65 urls.each {|url| assert_kind_of Net::HTTPOK, http_response_for(url) }
69 http_response_for(url)
70 rescue Net::HTTPForbidden, Net::HTTPNotFound
76 test_against_subclass :test_should_delete_attachment_from_cloud_files_when_attachment_record_destroyed, CloudFilesAttachment
81 def http_response_for(url)
83 Net::HTTP.start(url.host, url.port) {|http| http.request_head(url.path) }
87 Technoweenie::AttachmentFu::Backends::S3Backend.protocol
91 Technoweenie::AttachmentFu::Backends::S3Backend.hostname
95 Technoweenie::AttachmentFu::Backends::S3Backend.port_string
99 puts "s3 config file not loaded, tests not running"