1 require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
4 class S3Test < Test::Unit::TestCase
6 true unless ENV["TEST_S3"] == "false"
9 if test_S3? && File.exist?(File.join(File.dirname(__FILE__), '../../amazon_s3.yml'))
10 include BaseAttachmentTests
11 attachment_model S3Attachment
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
19 test_against_subclass :test_should_create_correct_bucket_name, S3Attachment
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
27 test_against_subclass :test_should_create_default_path_prefix, S3Attachment
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
35 test_against_subclass :test_should_create_custom_path_prefix, S3WithPathPrefixAttachment
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
43 test_against_subclass :test_should_create_valid_url, S3Attachment
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)
51 test_against_subclass :test_should_create_authenticated_url, S3Attachment
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|
58 /^http.+rails_large\.png.+AWSAccessKeyId.+Expires.+Signature/,
59 attachment.authenticated_s3_url(thumbnail),
60 "authenticated_s3_url failed with #{thumbnail.class} parameter"
65 def test_should_save_attachment(klass = S3Attachment)
66 attachment_model klass
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)
76 test_against_subclass :test_should_save_attachment, S3Attachment
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'
82 urls = [attachment.s3_url] + attachment.thumbnails.collect(&:s3_url)
84 urls.each {|url| assert_kind_of Net::HTTPOK, http_response_for(url) }
88 http_response_for(url)
89 rescue Net::HTTPForbidden, Net::HTTPNotFound
95 test_against_subclass :test_should_delete_attachment_from_s3_when_attachment_record_destroyed, S3Attachment
98 def http_response_for(url)
100 Net::HTTP.start(url.host, url.port) {|http| http.request_head(url.path) }
104 Technoweenie::AttachmentFu::Backends::S3Backend.protocol
108 Technoweenie::AttachmentFu::Backends::S3Backend.hostname
112 Technoweenie::AttachmentFu::Backends::S3Backend.port_string
116 puts "s3 config file not loaded, tests not running"