]> projects.mako.cc - selectricity-live/blob - vendor/plugins/attachment_fu/test/backends/file_system_test.rb
Fix errors when viewing results as election creator before election close.
[selectricity-live] / vendor / plugins / attachment_fu / test / backends / file_system_test.rb
1 require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2 require 'digest/sha2'
3
4 class FileSystemTest < Test::Unit::TestCase
5   include BaseAttachmentTests
6   attachment_model FileAttachment
7
8   def test_filesystem_size_for_file_attachment(klass = FileAttachment)
9     attachment_model klass
10     assert_created 1 do
11       attachment = upload_file :filename => '/files/rails.png'
12       assert_equal attachment.size, File.open(attachment.full_filename).stat.size
13     end
14   end
15   
16   test_against_subclass :test_filesystem_size_for_file_attachment, FileAttachment
17
18   def test_should_not_overwrite_file_attachment(klass = FileAttachment)
19     attachment_model klass
20     assert_created 2 do
21       real = upload_file :filename => '/files/rails.png'
22       assert_valid real
23       assert !real.new_record?, real.errors.full_messages.join("\n")
24       assert !real.size.zero?
25       
26       fake = upload_file :filename => '/files/fake/rails.png'
27       assert_valid fake
28       assert !fake.size.zero?
29       
30       assert_not_equal File.open(real.full_filename).stat.size, File.open(fake.full_filename).stat.size
31     end
32   end
33   
34   test_against_subclass :test_should_not_overwrite_file_attachment, FileAttachment
35
36   def test_should_store_file_attachment_in_filesystem(klass = FileAttachment)
37     attachment_model klass
38     attachment = nil
39     assert_created do
40       attachment = upload_file :filename => '/files/rails.png'
41       assert_valid attachment
42       assert File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"    
43     end
44     attachment
45   end
46   
47   test_against_subclass :test_should_store_file_attachment_in_filesystem, FileAttachment
48   
49   def test_should_delete_old_file_when_updating(klass = FileAttachment)
50     attachment_model klass
51     attachment   = upload_file :filename => '/files/rails.png'
52     old_filename = attachment.full_filename
53     assert_not_created do
54       use_temp_file 'files/rails.png' do |file|
55         attachment.filename        = 'rails2.png'
56         attachment.temp_paths.unshift File.join(fixture_path, file)
57         attachment.save!
58         assert  File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"    
59         assert !File.exists?(old_filename),             "#{old_filename} still exists"
60       end
61     end
62   end
63   
64   test_against_subclass :test_should_delete_old_file_when_updating, FileAttachment
65   
66   def test_should_delete_old_file_when_renaming(klass = FileAttachment)
67     attachment_model klass
68     attachment   = upload_file :filename => '/files/rails.png'
69     old_filename = attachment.full_filename
70     assert_not_created do
71       attachment.filename        = 'rails2.png'
72       attachment.save
73       assert  File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"    
74       assert !File.exists?(old_filename),             "#{old_filename} still exists"
75       assert !attachment.reload.size.zero?
76       assert_equal 'rails2.png', attachment.filename
77     end
78   end
79   
80   test_against_subclass :test_should_delete_old_file_when_renaming, FileAttachment
81   
82   def test_path_partitioning_works_on_integer_id(klass = FileAttachment)
83     attachment_model klass
84     
85     # Create a random attachment object, doesn't matter what.
86     attachment = upload_file :filename => '/files/rails.png'
87     old_id = attachment.id
88     attachment.id = 1
89     
90     begin
91       assert_equal ["0000", "0001", "bar.txt"], attachment.send(:partitioned_path, "bar.txt")
92     ensure
93       attachment.id = old_id
94     end
95   end
96   
97   test_against_subclass :test_path_partitioning_works_on_integer_id, FileAttachment
98   
99   def test_path_partitioning_with_string_id_works_by_generating_hash(klass = FileAttachmentWithStringId)
100     attachment_model klass
101     
102     # Create a random attachment object, doesn't matter what.
103     attachment = upload_file :filename => '/files/rails.png'
104     old_id = attachment.id
105     attachment.id = "hello world some long string"
106     hash = Digest::SHA512.hexdigest("hello world some long string")
107     
108     begin
109       assert_equal [
110           hash[0..31],
111           hash[32..63],
112           hash[64..95],
113           hash[96..127],
114           "bar.txt"
115         ], attachment.send(:partitioned_path, "bar.txt")
116     ensure
117       attachment.id = old_id
118     end
119   end
120   
121   test_against_subclass :test_path_partitioning_with_string_id_works_by_generating_hash, FileAttachmentWithStringId
122   
123   def test_path_partition_string_id_hashing_is_turned_off_if_id_is_uuid(klass = FileAttachmentWithUuid)
124     attachment_model klass
125     
126     # Create a random attachment object, doesn't matter what.
127     attachment = upload_file :filename => '/files/rails.png'
128     old_id = attachment.id
129     attachment.id = "0c0743b698483569dc65909a8cdb3bf9"
130     
131     begin
132       assert_equal [
133           "0c0743b698483569",
134           "dc65909a8cdb3bf9",
135           "bar.txt"
136         ], attachment.send(:partitioned_path, "bar.txt")
137     ensure
138       attachment.id = old_id
139     end
140   end
141   
142   test_against_subclass :test_path_partition_string_id_hashing_is_turned_off_if_id_is_uuid, FileAttachmentWithUuid
143 end

Benjamin Mako Hill || Want to submit a patch?