test/unit/test_media.rb in spontaneous-0.2.0.alpha7 vs test/unit/test_media.rb in spontaneous-0.2.0.beta1
- old
+ new
@@ -7,18 +7,18 @@
def setup
@site = setup_site
end
def teardown
+ ::Content.delete
teardown_site
- S::Content.delete
end
context "Utility methods" do
should "be able to sanitise filenames" do
filename = "Something with-dodgy 'characters'.many.jpg"
- Media.to_filename(filename).should == "Something-with-dodgy-characters.many.jpg"
+ S::Media.to_filename(filename).should == "Something-with-dodgy-characters.many.jpg"
end
end
context "All media files" do
should "know their mimetype" do
@@ -43,11 +43,11 @@
:public_host => "http://media.example.com"
}
@storage = Spontaneous::Storage::Cloud.new(@aws_credentials, 'media.example.com')
@storage.backend.directories.create(:key => @bucket_name)
@site.stubs(:storage).with(anything).returns(@storage)
- @content = S::Content.create
+ @content = ::Piece.create
@content.stubs(:id).returns(99)
Spontaneous::State.stubs(:revision).returns(853)
end
should "return an absolute path for the url" do
file = Spontaneous::Media::File.new(@content, "file name.txt")
@@ -106,11 +106,11 @@
context "Local media files" do
setup do
@media_dir = Dir.mktmpdir
@storage = Spontaneous::Storage::Local.new(@media_dir, '/media')
@site.stubs(:storage).with(anything).returns(@storage)
- @content = S::Content.create
+ @content = ::Piece.create
@content.stubs(:id).returns(99)
Spontaneous::State.stubs(:revision).returns(853)
end
should "return an absolute path for the url" do
@@ -165,64 +165,89 @@
end
File.read(file_path).should == content_string
end
end
- context "Content items" do
+ context "temporary media items" do
setup do
- # @media_dir = File.expand_path(File.join(File.dirname(__FILE__), "../../tmp/media"))
- # Spontaneous.media_dir = @media_dir
- Site.stubs(:working_revision).returns(74)
- @instance = Content.new
- @instance.stubs(:id).returns(101)
+ # Setup cloud storage as default to ensure that the temp files
+ # are bypassing this and being written locally
+ Fog.mock!
+ @bucket_name = "media.example.com"
+ @aws_credentials = {
+ :provider=>"AWS",
+ :aws_secret_access_key=>"SECRET_ACCESS_KEY",
+ :aws_access_key_id=>"ACCESS_KEY_ID",
+ :public_host => "http://media.example.com"
+ }
+ cloud = Spontaneous::Storage::Cloud.new(@aws_credentials, 'media.example.com')
+ cloud.backend.directories.create(:key => @bucket_name)
+ @site.stubs(:storage).with(anything).returns(cloud)
+ @media_dir = Dir.mktmpdir
+ @storage = Spontaneous::Storage::Local.new(@media_dir, '/media')
+ @site.stubs(:local_storage).with(anything).returns(@storage)
+ @site.stubs(:default_storage).with(anything).returns(@storage)
+ @content = ::Piece.create
+ @content.stubs(:id).returns(99)
end
- should "be able to generate a revision and id based media path" do
- @instance.media_filepath("something.jpg").should == File.join(@site.media_dir, "00101/0074/something.jpg")
- @instance.media_urlpath("something.jpg").should == "/media/00101/0074/something.jpg"
+ should "return an absolute path for the url" do
+ file = Spontaneous::Media::TempFile.new(@content, "file name.txt")
+ file.url.should == "/media/tmp/00099/file-name.txt"
end
- context "file manipulation" do
- setup do
- @tmp_dir = Pathname.new(Spontaneous.media_dir)
- @src_image = Pathname.new(File.join(File.dirname(__FILE__), "../fixtures/images/rose.jpg")).realpath
- @upload_dir = @tmp_dir + "tmp/1234"
- @upload_dir.mkpath
- @origin_image = @upload_dir + "rose.jpg"
- FileUtils.cp(@src_image.to_s, @origin_image.to_s)
- end
+ should "place files into its configured root" do
+ file = Spontaneous::Media::TempFile.new(@content, "file name.txt")
+ file.path.should == File.join(@media_dir, "/tmp/00099/file-name.txt")
+ end
- teardown do
- # (@tmp_dir + "..").rmtree
- end
+ should "be able to copy a file into place if passed the path of an existing file" do
+ file_path = File.join(@media_dir, "/tmp/00099/file-name.txt")
+ existing_file = File.expand_path("../../fixtures/images/rose.jpg", __FILE__)
+ ::File.exist?(file_path).should be_false
+ ::File.exist?(existing_file).should be_true
+ file = Spontaneous::Media::TempFile.new(@content, "file name.txt")
+ file.copy(existing_file)
+ ::File.exist?(file_path).should be_true
+ file.source.should == existing_file
+ end
- should "be able to move a file into the media path" do
- @instance.make_media_file(@origin_image)
- @origin_image.exist?.should be_true
- dest_image = @tmp_dir + "00101/0074/rose.jpg"
- dest_image.exist?.should be_true
+ should "be able to copy a file into place if passed the handle of an existing file" do
+ file_path = File.join(@media_dir, "/tmp/00099/file-name.txt")
+ existing_file = File.expand_path("../../fixtures/images/rose.jpg", __FILE__)
+ ::File.exist?(file_path).should be_false
+ ::File.exist?(existing_file).should be_true
+ file = Spontaneous::Media::TempFile.new(@content, "file name.txt")
+ File.open(existing_file, 'rb') do |f|
+ file.copy(f)
end
+ ::File.exist?(file_path).should be_true
+ file.source.should == existing_file
+ end
- should "honour the filename parameter when creating media files" do
- @instance.make_media_file(@origin_image, 'crysanthemum.jpg')
- @origin_image.exist?.should be_true
- dest_image = @tmp_dir + "00101/0074/crysanthemum.jpg"
- dest_image.exist?.should be_true
+ should "provide an open method that writes files to the correct location" do
+ file_path = File.join(@media_dir, "/tmp/00099/file-name.txt")
+ ::File.exist?(file_path).should be_false
+ file = Spontaneous::Media::TempFile.new(@content, "file name.txt")
+ content_string = "Hello"
+ file.open do |f|
+ f.write(content_string)
end
+ File.read(file_path).should == content_string
+ end
+ end
- should "sanitise filenames" do
- origin_image = @upload_dir + "illegal filename!.jpg"
- FileUtils.cp(@src_image.to_s, origin_image.to_s)
- @instance.make_media_file(origin_image)
- dest_image = @tmp_dir + "00101/0074/illegal-filename.jpg"
- dest_image.exist?.should be_true
- end
- should "sanitise custom filenames" do
- origin_image = @upload_dir + "rose.jpg"
- FileUtils.cp(@src_image.to_s, origin_image.to_s)
- @instance.make_media_file(origin_image, "other, 'illegal' filename!.jpg")
- dest_image = @tmp_dir + "00101/0074/other-illegal-filename.jpg"
- dest_image.exist?.should be_true
- end
+ context "Content items" do
+ setup do
+ # @media_dir = File.expand_path(File.join(File.dirname(__FILE__), "../../tmp/media"))
+ # Spontaneous.media_dir = @media_dir
+ S::Site.stubs(:working_revision).returns(74)
+ @instance = ::Piece.new
+ @instance.stubs(:id).returns(101)
+ end
+
+ should "be able to generate a revision and id based media path" do
+ @instance.media_filepath("something.jpg").should == File.join(@site.media_dir, "00101/0074/something.jpg")
+ @instance.media_urlpath("something.jpg").should == "/media/00101/0074/something.jpg"
end
end
end