1 require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
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
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
15 test_against_subclass :test_should_create_correct_bucket_name, S3Attachment
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
23 test_against_subclass :test_should_create_default_path_prefix, S3Attachment
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
31 test_against_subclass :test_should_create_custom_path_prefix, S3WithPathPrefixAttachment
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
39 test_against_subclass :test_should_create_valid_url, S3Attachment
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)
47 test_against_subclass :test_should_create_authenticated_url, S3Attachment
49 def test_should_save_attachment(klass = S3Attachment)
50 attachment_model klass
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)
60 test_against_subclass :test_should_save_attachment, S3Attachment
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'
66 urls = [attachment.s3_url] + attachment.thumbnails.collect(&:s3_url)
68 urls.each {|url| assert_kind_of Net::HTTPOK, http_response_for(url) }
72 http_response_for(url)
73 rescue Net::HTTPForbidden, Net::HTTPNotFound
79 test_against_subclass :test_should_delete_attachment_from_s3_when_attachment_record_destroyed, S3Attachment
82 def http_response_for(url)
84 Net::HTTP.start(url.host, url.port) {|http| http.request_head(url.path) }
88 Technoweenie::AttachmentFu::Backends::S3Backend.protocol
92 Technoweenie::AttachmentFu::Backends::S3Backend.hostname
96 Technoweenie::AttachmentFu::Backends::S3Backend.port_string
100 puts "s3 config file not loaded, tests not running"