PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/plugins/attachment_fu/test/backends/remote/s3_test.rb

https://github.com/grantneufeld/wayground-old
Ruby | 107 lines | 85 code | 22 blank | 0 comment | 3 complexity | 43ec25acd7e46e588ef21e9810a98731 MD5 | raw file
  1. require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
  2. require 'net/http'
  3. class S3Test < Test::Unit::TestCase
  4. def self.test_S3?
  5. true unless ENV["TEST_S3"] == "false"
  6. end
  7. if test_S3? && File.exist?(File.join(File.dirname(__FILE__), '../../amazon_s3.yml'))
  8. include BaseAttachmentTests
  9. attachment_model S3Attachment
  10. def test_should_create_correct_bucket_name(klass = S3Attachment)
  11. attachment_model klass
  12. attachment = upload_file :filename => '/files/rails.png'
  13. assert_equal attachment.s3_config[:bucket_name], attachment.bucket_name
  14. end
  15. test_against_subclass :test_should_create_correct_bucket_name, S3Attachment
  16. def test_should_create_default_path_prefix(klass = S3Attachment)
  17. attachment_model klass
  18. attachment = upload_file :filename => '/files/rails.png'
  19. assert_equal File.join(attachment_model.table_name, attachment.attachment_path_id), attachment.base_path
  20. end
  21. test_against_subclass :test_should_create_default_path_prefix, S3Attachment
  22. def test_should_create_custom_path_prefix(klass = S3WithPathPrefixAttachment)
  23. attachment_model klass
  24. attachment = upload_file :filename => '/files/rails.png'
  25. assert_equal File.join('some/custom/path/prefix', attachment.attachment_path_id), attachment.base_path
  26. end
  27. test_against_subclass :test_should_create_custom_path_prefix, S3WithPathPrefixAttachment
  28. def test_should_create_valid_url(klass = S3Attachment)
  29. attachment_model klass
  30. attachment = upload_file :filename => '/files/rails.png'
  31. assert_equal "#{s3_protocol}#{s3_hostname}#{s3_port_string}/#{attachment.bucket_name}/#{attachment.full_filename}", attachment.s3_url
  32. end
  33. test_against_subclass :test_should_create_valid_url, S3Attachment
  34. def test_should_create_authenticated_url(klass = S3Attachment)
  35. attachment_model klass
  36. attachment = upload_file :filename => '/files/rails.png'
  37. assert_match /^http.+AWSAccessKeyId.+Expires.+Signature.+/, attachment.authenticated_s3_url(:use_ssl => true)
  38. end
  39. test_against_subclass :test_should_create_authenticated_url, S3Attachment
  40. def test_should_save_attachment(klass = S3Attachment)
  41. attachment_model klass
  42. assert_created do
  43. attachment = upload_file :filename => '/files/rails.png'
  44. assert_valid attachment
  45. assert attachment.image?
  46. assert !attachment.size.zero?
  47. assert_kind_of Net::HTTPOK, http_response_for(attachment.s3_url)
  48. end
  49. end
  50. test_against_subclass :test_should_save_attachment, S3Attachment
  51. def test_should_delete_attachment_from_s3_when_attachment_record_destroyed(klass = S3Attachment)
  52. attachment_model klass
  53. attachment = upload_file :filename => '/files/rails.png'
  54. urls = [attachment.s3_url] + attachment.thumbnails.collect(&:s3_url)
  55. urls.each {|url| assert_kind_of Net::HTTPOK, http_response_for(url) }
  56. attachment.destroy
  57. urls.each do |url|
  58. begin
  59. http_response_for(url)
  60. rescue Net::HTTPForbidden, Net::HTTPNotFound
  61. nil
  62. end
  63. end
  64. end
  65. test_against_subclass :test_should_delete_attachment_from_s3_when_attachment_record_destroyed, S3Attachment
  66. protected
  67. def http_response_for(url)
  68. url = URI.parse(url)
  69. Net::HTTP.start(url.host, url.port) {|http| http.request_head(url.path) }
  70. end
  71. def s3_protocol
  72. Technoweenie::AttachmentFu::Backends::S3Backend.protocol
  73. end
  74. def s3_hostname
  75. Technoweenie::AttachmentFu::Backends::S3Backend.hostname
  76. end
  77. def s3_port_string
  78. Technoweenie::AttachmentFu::Backends::S3Backend.port_string
  79. end
  80. else
  81. def test_flunk_s3
  82. puts "s3 config file not loaded, tests not running"
  83. end
  84. end
  85. end