Created
April 25, 2010 15:43
-
-
Save phillipkoebbe/378489 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'model_helper' | |
describe Medium do | |
describe "verifying database columns" do | |
should_have_column :type, :type => :string, :null => false | |
should_have_column :mime_type_id, :type => :integer, :null => false | |
should_have_column :added_by_user_id, :type => :integer, :null => false | |
should_have_column :name, :type => :string, :null => false | |
should_have_column :description, :type => :string | |
should_have_column :path, :type => :string, :null => false | |
should_have_column :original_file_name, :type => :string, :null => false | |
should_have_column :is_approved, :type => :boolean, :null => false, :default => false | |
should_have_timestamp_columns | |
end | |
describe 'validations' do | |
class MediumNoSaveUpload < Medium | |
private | |
def before_create_process | |
true | |
end | |
end | |
before(:each) do | |
MediumNoSaveUpload.create(valid_medium_hash) | |
end | |
should_validate_presence_of :name | |
should_validate_uniqueness_of :name, :case_sensitive => false, :scope => :type | |
end | |
describe 'associations' do | |
should_belong_to :mime_type | |
should_belong_to :added_by, :class_name => 'User', :foreign_key => :added_by_user_id | |
end | |
describe 'new with uploaded file' do | |
before(:each) do | |
# create a file in /tmp | |
@temp_file_path = '/tmp/test.txt' | |
@temp_file_content = 'Temp Content' | |
File.open(@temp_file_path, 'w') {|f| f.write(@temp_file_content) } | |
@uploaded_file = mock(ActionController::UploadedStringIO) | |
@uploaded_file.stub(:path).and_return(@temp_file_path) | |
@uploaded_file.stub(:original_filename).and_return(File.basename(@temp_file_path)) | |
@uploaded_file.stub(:read).and_return(@temp_file_content) | |
@medium = Medium.new(valid_medium_hash) | |
@medium.added_by_user_id = 1 | |
@medium.temp_file = @uploaded_file | |
MimeType.create(:name => 'Text', :extensions => 'txt', :browser_hint => 'text/plain', :medium_type => 'Document') | |
end | |
after(:each) do | |
File.delete(@temp_file_path) if File.exists?(@temp_file_path) | |
end | |
context 'in general' do | |
before(:each) do | |
@medium.mime_type_id = nil | |
@medium.type = nil | |
new_app_config = APP_CONFIG.dup.merge(:media_root_path => '/tmp') | |
@old_app_config = set_app_config(new_app_config) | |
@medium.save | |
end | |
after(:each) do | |
File.delete("#{APP_CONFIG[:media_root_path]}/#{@medium.path}") if File.exists?("#{APP_CONFIG[:media_root_path]}/#{@medium.path}") | |
set_app_config(@old_app_config) | |
end | |
it 'should have the correct original file name' do | |
@medium.original_file_name.should == @uploaded_file.original_filename | |
end | |
it 'should have the correct mime type' do | |
@medium.mime_type.name.should == 'Text' | |
end | |
it 'should have the correct type' do | |
@medium.type.should == 'Document' | |
end | |
it 'should have the correct path' do | |
hash = Digest::SHA1.hexdigest(@uploaded_file.read) | |
@medium.path.should == "#{hash[0..1]}/#{hash}" | |
end | |
it 'should save the file' do | |
File.exist?("#{APP_CONFIG[:media_root_path]}/#{@medium.path}").should be_true | |
end | |
it 'should have the correct contents' do | |
File.open("#{APP_CONFIG[:media_root_path]}/#{@medium.path}", 'r') { |f| f.read.should == @uploaded_file.read } | |
end | |
it 'should delete the temp file' do | |
File.exist?(@temp_file_path).should_not be_true | |
end | |
end | |
context 'when mime type is provided' do | |
before(:each) do | |
@mime_type = create_mime_type | |
@medium.mime_type_id = @mime_type.id | |
@medium.type = nil | |
new_app_config = APP_CONFIG.dup.merge(:media_root_path => '/tmp') | |
@old_app_config = set_app_config(new_app_config) | |
@medium.save | |
end | |
it 'should not overwrite it' do | |
@medium.mime_type.name.should == @mime_type.name | |
end | |
end | |
context 'when type is provided' do | |
before(:each) do | |
@medium.mime_type_id = nil | |
@medium.type = 'Audio' | |
new_app_config = APP_CONFIG.dup.merge(:media_root_path => '/tmp') | |
@old_app_config = set_app_config(new_app_config) | |
@medium.save | |
end | |
it 'should not overwrite it' do | |
@medium.type.should == 'Audio' | |
end | |
end | |
end # describe 'new with uploaded file' | |
describe 'destroying' do | |
before(:each) do | |
# create a file in /tmp | |
@temp_file_path = '/tmp/test.txt' | |
@temp_file_content = 'Temp Content' | |
File.open(@temp_file_path, 'w') {|f| f.write(@temp_file_content) } | |
@uploaded_file = mock(ActionController::UploadedStringIO) | |
@uploaded_file.stub(:path).and_return(@temp_file_path) | |
@uploaded_file.stub(:original_filename).and_return(File.basename(@temp_file_path)) | |
@uploaded_file.stub(:read).and_return(@temp_file_content) | |
@medium = Medium.new(valid_medium_hash) | |
@medium.added_by_user_id = 1 | |
@medium.temp_file = @uploaded_file | |
MimeType.create(:name => 'Text', :extensions => 'txt', :browser_hint => 'text/plain', :medium_type => 'Document') | |
@medium.mime_type_id = nil | |
@medium.type = nil | |
new_app_config = APP_CONFIG.dup.merge(:media_root_path => '/tmp') | |
@old_app_config = set_app_config(new_app_config) | |
@medium.save | |
@medium.destroy | |
end | |
after(:each) do | |
File.delete("#{APP_CONFIG[:media_root_path]}/#{@medium.path}") if File.exists?("#{APP_CONFIG[:media_root_path]}/#{@medium.path}") | |
set_app_config(@old_app_config) | |
end | |
it 'should delete the file' do | |
File.exist?("#{APP_CONFIG[:media_root_path]}/#{@medium.path}").should_not be_true | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment