PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/test/storage_test.rb

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