diff options
Diffstat (limited to 'vendor/plugins/paperclip/test/storage_test.rb')
| -rw-r--r-- | vendor/plugins/paperclip/test/storage_test.rb | 277 |
1 files changed, 0 insertions, 277 deletions
diff --git a/vendor/plugins/paperclip/test/storage_test.rb b/vendor/plugins/paperclip/test/storage_test.rb deleted file mode 100644 index e2ed8a4..0000000 --- a/vendor/plugins/paperclip/test/storage_test.rb +++ /dev/null | |||
| @@ -1,277 +0,0 @@ | |||
| 1 | require 'test/helper' | ||
| 2 | |||
| 3 | class StorageTest < Test::Unit::TestCase | ||
| 4 | context "Parsing S3 credentials" do | ||
| 5 | setup do | ||
| 6 | rebuild_model :storage => :s3, | ||
| 7 | :bucket => "testing", | ||
| 8 | :s3_credentials => {:not => :important} | ||
| 9 | |||
| 10 | @dummy = Dummy.new | ||
| 11 | @avatar = @dummy.avatar | ||
| 12 | |||
| 13 | @current_env = ENV['RAILS_ENV'] | ||
| 14 | end | ||
| 15 | |||
| 16 | teardown do | ||
| 17 | ENV['RAILS_ENV'] = @current_env | ||
| 18 | end | ||
| 19 | |||
| 20 | should "get the correct credentials when RAILS_ENV is production" do | ||
| 21 | ENV['RAILS_ENV'] = 'production' | ||
| 22 | assert_equal({:key => "12345"}, | ||
| 23 | @avatar.parse_credentials('production' => {:key => '12345'}, | ||
| 24 | :development => {:key => "54321"})) | ||
| 25 | end | ||
| 26 | |||
| 27 | should "get the correct credentials when RAILS_ENV is development" do | ||
| 28 | ENV['RAILS_ENV'] = 'development' | ||
| 29 | assert_equal({:key => "54321"}, | ||
| 30 | @avatar.parse_credentials('production' => {:key => '12345'}, | ||
| 31 | :development => {:key => "54321"})) | ||
| 32 | end | ||
| 33 | |||
| 34 | should "return the argument if the key does not exist" do | ||
| 35 | ENV['RAILS_ENV'] = "not really an env" | ||
| 36 | assert_equal({:test => "12345"}, @avatar.parse_credentials(:test => "12345")) | ||
| 37 | end | ||
| 38 | end | ||
| 39 | |||
| 40 | context "" do | ||
| 41 | setup do | ||
| 42 | rebuild_model :storage => :s3, | ||
| 43 | :s3_credentials => {}, | ||
| 44 | :bucket => "bucket", | ||
| 45 | :path => ":attachment/:basename.:extension", | ||
| 46 | :url => ":s3_path_url" | ||
| 47 | @dummy = Dummy.new | ||
| 48 | @dummy.avatar = StringIO.new(".") | ||
| 49 | end | ||
| 50 | |||
| 51 | should "return a url based on an S3 path" do | ||
| 52 | assert_match %r{^http://s3.amazonaws.com/bucket/avatars/stringio.txt}, @dummy.avatar.url | ||
| 53 | end | ||
| 54 | end | ||
| 55 | context "" do | ||
| 56 | setup do | ||
| 57 | rebuild_model :storage => :s3, | ||
| 58 | :s3_credentials => {}, | ||
| 59 | :bucket => "bucket", | ||
| 60 | :path => ":attachment/:basename.:extension", | ||
| 61 | :url => ":s3_domain_url" | ||
| 62 | @dummy = Dummy.new | ||
| 63 | @dummy.avatar = StringIO.new(".") | ||
| 64 | end | ||
| 65 | |||
| 66 | should "return a url based on an S3 subdomain" do | ||
| 67 | assert_match %r{^http://bucket.s3.amazonaws.com/avatars/stringio.txt}, @dummy.avatar.url | ||
| 68 | end | ||
| 69 | end | ||
| 70 | context "" do | ||
| 71 | setup do | ||
| 72 | rebuild_model :storage => :s3, | ||
| 73 | :s3_credentials => { | ||
| 74 | :production => { :bucket => "prod_bucket" }, | ||
| 75 | :development => { :bucket => "dev_bucket" } | ||
| 76 | }, | ||
| 77 | :s3_host_alias => "something.something.com", | ||
| 78 | :path => ":attachment/:basename.:extension", | ||
| 79 | :url => ":s3_alias_url" | ||
| 80 | @dummy = Dummy.new | ||
| 81 | @dummy.avatar = StringIO.new(".") | ||
| 82 | end | ||
| 83 | |||
| 84 | should "return a url based on the host_alias" do | ||
| 85 | assert_match %r{^http://something.something.com/avatars/stringio.txt}, @dummy.avatar.url | ||
| 86 | end | ||
| 87 | end | ||
| 88 | |||
| 89 | context "Parsing S3 credentials with a bucket in them" do | ||
| 90 | setup do | ||
| 91 | rebuild_model :storage => :s3, | ||
| 92 | :s3_credentials => { | ||
| 93 | :production => { :bucket => "prod_bucket" }, | ||
| 94 | :development => { :bucket => "dev_bucket" } | ||
| 95 | } | ||
| 96 | @dummy = Dummy.new | ||
| 97 | end | ||
| 98 | |||
| 99 | should "get the right bucket in production", :before => lambda{ ENV.expects(:[]).returns('production') } do | ||
| 100 | assert_equal "prod_bucket", @dummy.avatar.bucket_name | ||
| 101 | end | ||
| 102 | |||
| 103 | should "get the right bucket in development", :before => lambda{ ENV.expects(:[]).returns('development') } do | ||
| 104 | assert_equal "dev_bucket", @dummy.avatar.bucket_name | ||
| 105 | end | ||
| 106 | end | ||
| 107 | |||
| 108 | context "An attachment with S3 storage" do | ||
| 109 | setup do | ||
| 110 | rebuild_model :storage => :s3, | ||
| 111 | :bucket => "testing", | ||
| 112 | :path => ":attachment/:style/:basename.:extension", | ||
| 113 | :s3_credentials => { | ||
| 114 | 'access_key_id' => "12345", | ||
| 115 | 'secret_access_key' => "54321" | ||
| 116 | } | ||
| 117 | end | ||
| 118 | |||
| 119 | should "be extended by the S3 module" do | ||
| 120 | assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3) | ||
| 121 | end | ||
| 122 | |||
| 123 | should "not be extended by the Filesystem module" do | ||
| 124 | assert ! Dummy.new.avatar.is_a?(Paperclip::Storage::Filesystem) | ||
| 125 | end | ||
| 126 | |||
| 127 | context "when assigned" do | ||
| 128 | setup do | ||
| 129 | @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb') | ||
| 130 | @dummy = Dummy.new | ||
| 131 | @dummy.avatar = @file | ||
| 132 | end | ||
| 133 | |||
| 134 | teardown { @file.close } | ||
| 135 | |||
| 136 | should "not get a bucket to get a URL" do | ||
| 137 | @dummy.avatar.expects(:s3).never | ||
| 138 | @dummy.avatar.expects(:s3_bucket).never | ||
| 139 | assert_match %r{^http://s3\.amazonaws\.com/testing/avatars/original/5k\.png}, @dummy.avatar.url | ||
| 140 | end | ||
| 141 | |||
| 142 | context "and saved" do | ||
| 143 | setup do | ||
| 144 | @s3_mock = stub | ||
| 145 | @bucket_mock = stub | ||
| 146 | RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock) | ||
| 147 | @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock) | ||
| 148 | @key_mock = stub | ||
| 149 | @bucket_mock.expects(:key).returns(@key_mock) | ||
| 150 | @key_mock.expects(:data=) | ||
| 151 | @key_mock.expects(:put).with(nil, 'public-read', 'Content-type' => 'image/png') | ||
| 152 | @dummy.save | ||
| 153 | end | ||
| 154 | |||
| 155 | should "succeed" do | ||
| 156 | assert true | ||
| 157 | end | ||
| 158 | end | ||
| 159 | |||
| 160 | context "and remove" do | ||
| 161 | setup do | ||
| 162 | @s3_mock = stub | ||
| 163 | @bucket_mock = stub | ||
| 164 | RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock) | ||
| 165 | @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock) | ||
| 166 | @key_mock = stub | ||
| 167 | @bucket_mock.expects(:key).at_least(2).returns(@key_mock) | ||
| 168 | @key_mock.expects(:delete) | ||
| 169 | @dummy.destroy_attached_files | ||
| 170 | end | ||
| 171 | |||
| 172 | should "succeed" do | ||
| 173 | assert true | ||
| 174 | end | ||
| 175 | end | ||
| 176 | end | ||
| 177 | end | ||
| 178 | |||
| 179 | context "An attachment with S3 storage and bucket defined as a Proc" do | ||
| 180 | setup do | ||
| 181 | rebuild_model :storage => :s3, | ||
| 182 | :bucket => lambda { |attachment| "bucket_#{attachment.instance.other}" }, | ||
| 183 | :s3_credentials => {:not => :important} | ||
| 184 | end | ||
| 185 | |||
| 186 | should "get the right bucket name" do | ||
| 187 | assert "bucket_a", Dummy.new(:other => 'a').avatar.bucket_name | ||
| 188 | assert "bucket_b", Dummy.new(:other => 'b').avatar.bucket_name | ||
| 189 | end | ||
| 190 | end | ||
| 191 | |||
| 192 | context "An attachment with S3 storage and specific s3 headers set" do | ||
| 193 | setup do | ||
| 194 | rebuild_model :storage => :s3, | ||
| 195 | :bucket => "testing", | ||
| 196 | :path => ":attachment/:style/:basename.:extension", | ||
| 197 | :s3_credentials => { | ||
| 198 | 'access_key_id' => "12345", | ||
| 199 | 'secret_access_key' => "54321" | ||
| 200 | }, | ||
| 201 | :s3_headers => {'Cache-Control' => 'max-age=31557600'} | ||
| 202 | end | ||
| 203 | |||
| 204 | context "when assigned" do | ||
| 205 | setup do | ||
| 206 | @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb') | ||
| 207 | @dummy = Dummy.new | ||
| 208 | @dummy.avatar = @file | ||
| 209 | end | ||
| 210 | |||
| 211 | teardown { @file.close } | ||
| 212 | |||
| 213 | context "and saved" do | ||
| 214 | setup do | ||
| 215 | @s3_mock = stub | ||
| 216 | @bucket_mock = stub | ||
| 217 | RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock) | ||
| 218 | @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock) | ||
| 219 | @key_mock = stub | ||
| 220 | @bucket_mock.expects(:key).returns(@key_mock) | ||
| 221 | @key_mock.expects(:data=) | ||
| 222 | @key_mock.expects(:put).with(nil, | ||
| 223 | 'public-read', | ||
| 224 | 'Content-type' => 'image/png', | ||
| 225 | 'Cache-Control' => 'max-age=31557600') | ||
| 226 | @dummy.save | ||
| 227 | end | ||
| 228 | |||
| 229 | should "succeed" do | ||
| 230 | assert true | ||
| 231 | end | ||
| 232 | end | ||
| 233 | end | ||
| 234 | end | ||
| 235 | |||
| 236 | unless ENV["S3_TEST_BUCKET"].blank? | ||
| 237 | context "Using S3 for real, an attachment with S3 storage" do | ||
| 238 | setup do | ||
| 239 | rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" }, | ||
| 240 | :storage => :s3, | ||
| 241 | :bucket => ENV["S3_TEST_BUCKET"], | ||
| 242 | :path => ":class/:attachment/:id/:style.:extension", | ||
| 243 | :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml")) | ||
| 244 | |||
| 245 | Dummy.delete_all | ||
| 246 | @dummy = Dummy.new | ||
| 247 | end | ||
| 248 | |||
| 249 | should "be extended by the S3 module" do | ||
| 250 | assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3) | ||
| 251 | end | ||
| 252 | |||
| 253 | context "when assigned" do | ||
| 254 | setup do | ||
| 255 | @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb') | ||
| 256 | @dummy.avatar = @file | ||
| 257 | end | ||
| 258 | |||
| 259 | teardown { @file.close } | ||
| 260 | |||
| 261 | should "still return a Tempfile when sent #to_io" do | ||
| 262 | assert_equal Tempfile, @dummy.avatar.to_io.class | ||
| 263 | end | ||
| 264 | |||
| 265 | context "and saved" do | ||
| 266 | setup do | ||
| 267 | @dummy.save | ||
| 268 | end | ||
| 269 | |||
| 270 | should "be on S3" do | ||
| 271 | assert true | ||
| 272 | end | ||
| 273 | end | ||
| 274 | end | ||
| 275 | end | ||
| 276 | end | ||
| 277 | end | ||
