merged in changes from live version
[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   if File.exist?(File.join(File.dirname(__FILE__), '../../amazon_s3.yml'))
6     include BaseAttachmentTests
7     attachment_model S3Attachment
8
9     def test_should_create_correct_bucket_name(klass = S3Attachment)
10       attachment_model klass
11       attachment = upload_file :filename => '/files/rails.png'
12       assert_equal attachment.s3_config[:bucket_name], attachment.bucket_name
13     end
14
15     test_against_subclass :test_should_create_correct_bucket_name, S3Attachment
16
17     def test_should_create_default_path_prefix(klass = S3Attachment)
18       attachment_model klass
19       attachment = upload_file :filename => '/files/rails.png'
20       assert_equal File.join(attachment_model.table_name, attachment.attachment_path_id), attachment.base_path
21     end
22
23     test_against_subclass :test_should_create_default_path_prefix, S3Attachment
24
25     def test_should_create_custom_path_prefix(klass = S3WithPathPrefixAttachment)
26       attachment_model klass
27       attachment = upload_file :filename => '/files/rails.png'
28       assert_equal File.join('some/custom/path/prefix', attachment.attachment_path_id), attachment.base_path
29     end
30
31     test_against_subclass :test_should_create_custom_path_prefix, S3WithPathPrefixAttachment
32
33     def test_should_create_valid_url(klass = S3Attachment)
34       attachment_model klass
35       attachment = upload_file :filename => '/files/rails.png'
36       assert_equal "#{s3_protocol}#{s3_hostname}#{s3_port_string}/#{attachment.bucket_name}/#{attachment.full_filename}", attachment.s3_url
37     end
38
39     test_against_subclass :test_should_create_valid_url, S3Attachment
40
41     def test_should_create_authenticated_url(klass = S3Attachment)
42       attachment_model klass
43       attachment = upload_file :filename => '/files/rails.png'
44       assert_match /^http.+AWSAccessKeyId.+Expires.+Signature.+/, attachment.authenticated_s3_url(:use_ssl => true)
45     end
46
47     test_against_subclass :test_should_create_authenticated_url, S3Attachment
48
49     def test_should_save_attachment(klass = S3Attachment)
50       attachment_model klass
51       assert_created do
52         attachment = upload_file :filename => '/files/rails.png'
53         assert_valid attachment
54         assert attachment.image?
55         assert !attachment.size.zero?
56         assert_kind_of Net::HTTPOK, http_response_for(attachment.s3_url)
57       end
58     end
59
60     test_against_subclass :test_should_save_attachment, S3Attachment
61
62     def test_should_delete_attachment_from_s3_when_attachment_record_destroyed(klass = S3Attachment)
63       attachment_model klass
64       attachment = upload_file :filename => '/files/rails.png'
65
66       urls = [attachment.s3_url] + attachment.thumbnails.collect(&:s3_url)
67
68       urls.each {|url| assert_kind_of Net::HTTPOK, http_response_for(url) }
69       attachment.destroy
70       urls.each do |url|
71         begin
72           http_response_for(url)
73         rescue Net::HTTPForbidden, Net::HTTPNotFound
74           nil
75         end
76       end
77     end
78
79     test_against_subclass :test_should_delete_attachment_from_s3_when_attachment_record_destroyed, S3Attachment
80
81     protected
82       def http_response_for(url)
83         url = URI.parse(url)
84         Net::HTTP.start(url.host, url.port) {|http| http.request_head(url.path) }
85       end
86       
87       def s3_protocol
88         Technoweenie::AttachmentFu::Backends::S3Backend.protocol
89       end
90       
91       def s3_hostname
92         Technoweenie::AttachmentFu::Backends::S3Backend.hostname
93       end
94
95       def s3_port_string
96         Technoweenie::AttachmentFu::Backends::S3Backend.port_string
97       end
98   else
99     def test_flunk_s3
100       puts "s3 config file not loaded, tests not running"
101     end
102   end
103 end

Benjamin Mako Hill || Want to submit a patch?