]> projects.mako.cc - selectricity-live/blob - vendor/plugins/attachment_fu/test/backends/remote/s3_test.rb
improved dependency information
[selectricity-live] / vendor / plugins / attachment_fu / test / backends / remote / s3_test.rb
1 require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
2 require 'net/http'
3
4 class S3Test < Test::Unit::TestCase
5   def self.test_S3?
6     true unless ENV["TEST_S3"] == "false"
7   end
8   
9   if test_S3? && File.exist?(File.join(File.dirname(__FILE__), '../../amazon_s3.yml'))
10     include BaseAttachmentTests
11     attachment_model S3Attachment
12
13     def test_should_create_correct_bucket_name(klass = S3Attachment)
14       attachment_model klass
15       attachment = upload_file :filename => '/files/rails.png'
16       assert_equal attachment.s3_config[:bucket_name], attachment.bucket_name
17     end
18
19     test_against_subclass :test_should_create_correct_bucket_name, S3Attachment
20
21     def test_should_create_default_path_prefix(klass = S3Attachment)
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, S3Attachment
28
29     def test_should_create_custom_path_prefix(klass = S3WithPathPrefixAttachment)
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, S3WithPathPrefixAttachment
36
37     def test_should_create_valid_url(klass = S3Attachment)
38       attachment_model klass
39       attachment = upload_file :filename => '/files/rails.png'
40       assert_equal "#{s3_protocol}#{s3_hostname}#{s3_port_string}/#{attachment.bucket_name}/#{attachment.full_filename}", attachment.s3_url
41     end
42
43     test_against_subclass :test_should_create_valid_url, S3Attachment
44
45     def test_should_create_authenticated_url(klass = S3Attachment)
46       attachment_model klass
47       attachment = upload_file :filename => '/files/rails.png'
48       assert_match /^http.+AWSAccessKeyId.+Expires.+Signature.+/, attachment.authenticated_s3_url(:use_ssl => true)
49     end
50
51     test_against_subclass :test_should_create_authenticated_url, S3Attachment
52     
53     def test_should_create_authenticated_url_for_thumbnail(klass = S3Attachment)
54       attachment_model klass
55       attachment = upload_file :filename => '/files/rails.png'
56       ['large', :large].each do |thumbnail|
57         assert_match(
58           /^http.+rails_large\.png.+AWSAccessKeyId.+Expires.+Signature/, 
59           attachment.authenticated_s3_url(thumbnail), 
60           "authenticated_s3_url failed with #{thumbnail.class} parameter"
61         )
62       end
63     end
64
65     def test_should_save_attachment(klass = S3Attachment)
66       attachment_model klass
67       assert_created do
68         attachment = upload_file :filename => '/files/rails.png'
69         assert_valid attachment
70         assert attachment.image?
71         assert !attachment.size.zero?
72         assert_kind_of Net::HTTPOK, http_response_for(attachment.s3_url)
73       end
74     end
75
76     test_against_subclass :test_should_save_attachment, S3Attachment
77
78     def test_should_delete_attachment_from_s3_when_attachment_record_destroyed(klass = S3Attachment)
79       attachment_model klass
80       attachment = upload_file :filename => '/files/rails.png'
81
82       urls = [attachment.s3_url] + attachment.thumbnails.collect(&:s3_url)
83
84       urls.each {|url| assert_kind_of Net::HTTPOK, http_response_for(url) }
85       attachment.destroy
86       urls.each do |url|
87         begin
88           http_response_for(url)
89         rescue Net::HTTPForbidden, Net::HTTPNotFound
90           nil
91         end
92       end
93     end
94
95     test_against_subclass :test_should_delete_attachment_from_s3_when_attachment_record_destroyed, S3Attachment
96
97     protected
98       def http_response_for(url)
99         url = URI.parse(url)
100         Net::HTTP.start(url.host, url.port) {|http| http.request_head(url.path) }
101       end
102       
103       def s3_protocol
104         Technoweenie::AttachmentFu::Backends::S3Backend.protocol
105       end
106       
107       def s3_hostname
108         Technoweenie::AttachmentFu::Backends::S3Backend.hostname
109       end
110
111       def s3_port_string
112         Technoweenie::AttachmentFu::Backends::S3Backend.port_string
113       end
114   else
115     def test_flunk_s3
116       puts "s3 config file not loaded, tests not running"
117     end
118   end
119 end

Benjamin Mako Hill || Want to submit a patch?