]> projects.mako.cc - selectricity-live/blob - vendor/plugins/attachment_fu/test/backends/remote/cloudfiles_test.rb
Fix errors when viewing results as election creator before election close.
[selectricity-live] / vendor / plugins / attachment_fu / test / backends / remote / cloudfiles_test.rb
1 require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
2 require 'net/http'
3
4 class CloudfilesTest < Test::Unit::TestCase
5   def self.test_CloudFiles?
6     true unless ENV["TEST_CLOUDFILES"] == "false"
7   end
8   
9   if test_CloudFiles? && File.exist?(File.join(File.dirname(__FILE__), '../../rackspace_cloudfiles.yml'))
10     include BaseAttachmentTests
11     attachment_model CloudFilesAttachment
12
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
17     end
18
19     test_against_subclass :test_should_create_correct_container_name, CloudFilesAttachment
20     
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
25     end
26
27     test_against_subclass :test_should_create_default_path_prefix, CloudFilesAttachment
28     
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
33     end
34
35     test_against_subclass :test_should_create_custom_path_prefix, CloudFilesWithPathPrefixAttachment
36     
37     
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)
42     end
43     
44     test_against_subclass :test_should_create_valid_url, CloudFilesAttachment
45     
46     def test_should_save_attachment(klass = CloudFilesAttachment)
47       attachment_model klass
48       assert_created do
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)
54       end
55     end
56
57     test_against_subclass :test_should_save_attachment, CloudFilesAttachment
58     
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'
62
63       urls = [attachment.cloudfiles_url] + attachment.thumbnails.collect(&:cloudfiles_url)
64
65       urls.each {|url| assert_kind_of Net::HTTPOK, http_response_for(url) }
66       attachment.destroy
67       urls.each do |url|
68         begin
69           http_response_for(url)
70         rescue Net::HTTPForbidden, Net::HTTPNotFound
71           nil
72         end
73       end
74     end
75
76     test_against_subclass :test_should_delete_attachment_from_cloud_files_when_attachment_record_destroyed, CloudFilesAttachment
77     
78     
79     
80     protected
81       def http_response_for(url)
82         url = URI.parse(url)
83         Net::HTTP.start(url.host, url.port) {|http| http.request_head(url.path) }
84       end
85       
86       def s3_protocol
87         Technoweenie::AttachmentFu::Backends::S3Backend.protocol
88       end
89       
90       def s3_hostname
91         Technoweenie::AttachmentFu::Backends::S3Backend.hostname
92       end
93
94       def s3_port_string
95         Technoweenie::AttachmentFu::Backends::S3Backend.port_string
96       end
97   else
98     def test_flunk_s3
99       puts "s3 config file not loaded, tests not running"
100     end
101   end
102 end

Benjamin Mako Hill || Want to submit a patch?