PageRenderTime 69ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/episode-154/blog/vendor/plugins/paperclip/test/storage_test.rb

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