Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 102 lines (79 sloc) 3.732 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
require 'net/http'

class CloudfilesTest < Test::Unit::TestCase
  def self.test_CloudFiles?
    true unless ENV["TEST_CLOUDFILES"] == "false"
  end
  
  if test_CloudFiles? && File.exist?(File.join(File.dirname(__FILE__), '../../rackspace_cloudfiles.yml'))
    include BaseAttachmentTests
    attachment_model CloudFilesAttachment

    def test_should_create_correct_container_name(klass = CloudFilesAttachment)
      attachment_model klass
      attachment = upload_file :filename => '/files/rails.png'
      assert_equal attachment.cloudfiles_config[:container_name], attachment.container_name
    end

    test_against_subclass :test_should_create_correct_container_name, CloudFilesAttachment
    
    def test_should_create_default_path_prefix(klass = CloudFilesAttachment)
      attachment_model klass
      attachment = upload_file :filename => '/files/rails.png'
      assert_equal File.join(attachment_model.table_name, attachment.attachment_path_id), attachment.base_path
    end

    test_against_subclass :test_should_create_default_path_prefix, CloudFilesAttachment
    
    def test_should_create_custom_path_prefix(klass = CloudFilesWithPathPrefixAttachment)
      attachment_model klass
      attachment = upload_file :filename => '/files/rails.png'
      assert_equal File.join('some/custom/path/prefix', attachment.attachment_path_id), attachment.base_path
    end

    test_against_subclass :test_should_create_custom_path_prefix, CloudFilesWithPathPrefixAttachment
    
    
    def test_should_create_valid_url(klass = CloudFilesAttachment)
      attachment_model klass
      attachment = upload_file :filename => '/files/rails.png'
      assert_match(%r!http://cdn.cloudfiles.mosso.com/(.*?)/cloud_files_attachments/1/rails.png!, attachment.cloudfiles_url)
    end
    
    test_against_subclass :test_should_create_valid_url, CloudFilesAttachment
    
    def test_should_save_attachment(klass = CloudFilesAttachment)
      attachment_model klass
      assert_created do
        attachment = upload_file :filename => '/files/rails.png'
        assert_valid attachment
        assert attachment.image?
        assert !attachment.size.zero?
        assert_kind_of Net::HTTPOK, http_response_for(attachment.cloudfiles_url)
      end
    end

    test_against_subclass :test_should_save_attachment, CloudFilesAttachment
    
    def test_should_delete_attachment_from_cloud_files_when_attachment_record_destroyed(klass = CloudFilesAttachment)
      attachment_model klass
      attachment = upload_file :filename => '/files/rails.png'

      urls = [attachment.cloudfiles_url] + attachment.thumbnails.collect(&:cloudfiles_url)

      urls.each {|url| assert_kind_of Net::HTTPOK, http_response_for(url) }
      attachment.destroy
      urls.each do |url|
        begin
          http_response_for(url)
        rescue Net::HTTPForbidden, Net::HTTPNotFound
          nil
        end
      end
    end

    test_against_subclass :test_should_delete_attachment_from_cloud_files_when_attachment_record_destroyed, CloudFilesAttachment
    
    
    
    protected
      def http_response_for(url)
        url = URI.parse(url)
        Net::HTTP.start(url.host, url.port) {|http| http.request_head(url.path) }
      end
      
      def s3_protocol
        Technoweenie::AttachmentFu::Backends::S3Backend.protocol
      end
      
      def s3_hostname
        Technoweenie::AttachmentFu::Backends::S3Backend.hostname
      end

      def s3_port_string
        Technoweenie::AttachmentFu::Backends::S3Backend.port_string
      end
  else
    def test_flunk_s3
      puts "s3 config file not loaded, tests not running"
    end
  end
end
Something went wrong with that request. Please try again.