Mercurial > hg > soundsoftware-site
comparison test/unit/attachment_test.rb @ 1526:404aa68d4227
Merge from live branch
author | Chris Cannam |
---|---|
date | Thu, 11 Sep 2014 12:46:20 +0100 |
parents | fb9a13467253 |
children |
comparison
equal
deleted
inserted
replaced
1493:a5f2bdf3b486 | 1526:404aa68d4227 |
---|---|
1 # encoding: utf-8 | 1 # encoding: utf-8 |
2 # | 2 # |
3 # Redmine - project management software | 3 # Redmine - project management software |
4 # Copyright (C) 2006-2012 Jean-Philippe Lang | 4 # Copyright (C) 2006-2014 Jean-Philippe Lang |
5 # | 5 # |
6 # This program is free software; you can redistribute it and/or | 6 # This program is free software; you can redistribute it and/or |
7 # modify it under the terms of the GNU General Public License | 7 # modify it under the terms of the GNU General Public License |
8 # as published by the Free Software Foundation; either version 2 | 8 # as published by the Free Software Foundation; either version 2 |
9 # of the License, or (at your option) any later version. | 9 # of the License, or (at your option) any later version. |
20 require File.expand_path('../../test_helper', __FILE__) | 20 require File.expand_path('../../test_helper', __FILE__) |
21 | 21 |
22 class AttachmentTest < ActiveSupport::TestCase | 22 class AttachmentTest < ActiveSupport::TestCase |
23 fixtures :users, :projects, :roles, :members, :member_roles, | 23 fixtures :users, :projects, :roles, :members, :member_roles, |
24 :enabled_modules, :issues, :trackers, :attachments | 24 :enabled_modules, :issues, :trackers, :attachments |
25 | 25 |
26 class MockFile | 26 class MockFile |
27 attr_reader :original_filename, :content_type, :content, :size | 27 attr_reader :original_filename, :content_type, :content, :size |
28 | 28 |
29 def initialize(attributes) | 29 def initialize(attributes) |
30 @original_filename = attributes[:original_filename] | 30 @original_filename = attributes[:original_filename] |
31 @content_type = attributes[:content_type] | 31 @content_type = attributes[:content_type] |
32 @content = attributes[:content] || "Content" | 32 @content = attributes[:content] || "Content" |
33 @size = content.size | 33 @size = content.size |
38 set_tmp_attachments_directory | 38 set_tmp_attachments_directory |
39 end | 39 end |
40 | 40 |
41 def test_container_for_new_attachment_should_be_nil | 41 def test_container_for_new_attachment_should_be_nil |
42 assert_nil Attachment.new.container | 42 assert_nil Attachment.new.container |
43 end | |
44 | |
45 def test_filename_should_remove_eols | |
46 assert_equal "line_feed", Attachment.new(:filename => "line\nfeed").filename | |
47 assert_equal "line_feed", Attachment.new(:filename => "some\npath/line\nfeed").filename | |
48 assert_equal "carriage_return", Attachment.new(:filename => "carriage\rreturn").filename | |
49 assert_equal "carriage_return", Attachment.new(:filename => "some\rpath/carriage\rreturn").filename | |
43 end | 50 end |
44 | 51 |
45 def test_create | 52 def test_create |
46 a = Attachment.new(:container => Issue.find(1), | 53 a = Attachment.new(:container => Issue.find(1), |
47 :file => uploaded_test_file("testfile.txt", "text/plain"), | 54 :file => uploaded_test_file("testfile.txt", "text/plain"), |
50 assert_equal 'testfile.txt', a.filename | 57 assert_equal 'testfile.txt', a.filename |
51 assert_equal 59, a.filesize | 58 assert_equal 59, a.filesize |
52 assert_equal 'text/plain', a.content_type | 59 assert_equal 'text/plain', a.content_type |
53 assert_equal 0, a.downloads | 60 assert_equal 0, a.downloads |
54 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest | 61 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest |
62 | |
63 assert a.disk_directory | |
64 assert_match %r{\A\d{4}/\d{2}\z}, a.disk_directory | |
65 | |
55 assert File.exist?(a.diskfile) | 66 assert File.exist?(a.diskfile) |
56 assert_equal 59, File.size(a.diskfile) | 67 assert_equal 59, File.size(a.diskfile) |
68 end | |
69 | |
70 def test_copy_should_preserve_attributes | |
71 a = Attachment.find(1) | |
72 copy = a.copy | |
73 | |
74 assert_save copy | |
75 copy = Attachment.order('id DESC').first | |
76 %w(filename filesize content_type author_id created_on description digest disk_filename disk_directory diskfile).each do |attribute| | |
77 assert_equal a.send(attribute), copy.send(attribute), "#{attribute} was different" | |
78 end | |
57 end | 79 end |
58 | 80 |
59 def test_size_should_be_validated_for_new_file | 81 def test_size_should_be_validated_for_new_file |
60 with_settings :attachment_max_size => 0 do | 82 with_settings :attachment_max_size => 0 do |
61 a = Attachment.new(:container => Issue.find(1), | 83 a = Attachment.new(:container => Issue.find(1), |
76 end | 98 end |
77 | 99 |
78 def test_description_length_should_be_validated | 100 def test_description_length_should_be_validated |
79 a = Attachment.new(:description => 'a' * 300) | 101 a = Attachment.new(:description => 'a' * 300) |
80 assert !a.save | 102 assert !a.save |
81 assert_not_nil a.errors[:description] | 103 assert_not_equal [], a.errors[:description] |
82 end | 104 end |
83 | 105 |
84 def test_destroy | 106 def test_destroy |
85 a = Attachment.new(:container => Issue.find(1), | 107 a = Attachment.new(:container => Issue.find(1), |
86 :file => uploaded_test_file("testfile.txt", "text/plain"), | 108 :file => uploaded_test_file("testfile.txt", "text/plain"), |
129 a2 = Attachment.create!(:container => Issue.find(1), | 151 a2 = Attachment.create!(:container => Issue.find(1), |
130 :file => uploaded_test_file("testfile.txt", ""), | 152 :file => uploaded_test_file("testfile.txt", ""), |
131 :author => User.find(1)) | 153 :author => User.find(1)) |
132 assert a1.disk_filename != a2.disk_filename | 154 assert a1.disk_filename != a2.disk_filename |
133 end | 155 end |
134 | 156 |
135 def test_filename_should_be_basenamed | 157 def test_filename_should_be_basenamed |
136 a = Attachment.new(:file => MockFile.new(:original_filename => "path/to/the/file")) | 158 a = Attachment.new(:file => MockFile.new(:original_filename => "path/to/the/file")) |
137 assert_equal 'file', a.filename | 159 assert_equal 'file', a.filename |
138 end | 160 end |
139 | 161 |
140 def test_filename_should_be_sanitized | 162 def test_filename_should_be_sanitized |
141 a = Attachment.new(:file => MockFile.new(:original_filename => "valid:[] invalid:?%*|\"'<>chars")) | 163 a = Attachment.new(:file => MockFile.new(:original_filename => "valid:[] invalid:?%*|\"'<>chars")) |
142 assert_equal 'valid_[] invalid_chars', a.filename | 164 assert_equal 'valid_[] invalid_chars', a.filename |
143 end | 165 end |
144 | 166 |
166 assert_difference 'Attachment.count', -2 do | 188 assert_difference 'Attachment.count', -2 do |
167 Attachment.prune | 189 Attachment.prune |
168 end | 190 end |
169 end | 191 end |
170 | 192 |
171 context "Attachmnet.attach_files" do | 193 def test_move_from_root_to_target_directory_should_move_root_files |
172 should "attach the file" do | 194 a = Attachment.find(20) |
173 issue = Issue.first | 195 assert a.disk_directory.blank? |
174 assert_difference 'Attachment.count' do | 196 # Create a real file for this fixture |
175 Attachment.attach_files(issue, | 197 File.open(a.diskfile, "w") do |f| |
176 '1' => { | 198 f.write "test file at the root of files directory" |
177 'file' => uploaded_test_file('testfile.txt', 'text/plain'), | 199 end |
178 'description' => 'test' | 200 assert a.readable? |
179 }) | 201 Attachment.move_from_root_to_target_directory |
180 end | 202 |
181 | 203 a.reload |
182 attachment = Attachment.first(:order => 'id DESC') | 204 assert_equal '2012/05', a.disk_directory |
183 assert_equal issue, attachment.container | 205 assert a.readable? |
184 assert_equal 'testfile.txt', attachment.filename | 206 end |
185 assert_equal 59, attachment.filesize | 207 |
186 assert_equal 'test', attachment.description | 208 test "Attachmnet.attach_files should attach the file" do |
187 assert_equal 'text/plain', attachment.content_type | 209 issue = Issue.first |
188 assert File.exists?(attachment.diskfile) | 210 assert_difference 'Attachment.count' do |
189 assert_equal 59, File.size(attachment.diskfile) | 211 Attachment.attach_files(issue, |
190 end | 212 '1' => { |
191 | 213 'file' => uploaded_test_file('testfile.txt', 'text/plain'), |
192 should "add unsaved files to the object as unsaved attachments" do | 214 'description' => 'test' |
193 # Max size of 0 to force Attachment creation failures | 215 }) |
194 with_settings(:attachment_max_size => 0) do | 216 end |
195 @project = Project.find(1) | 217 attachment = Attachment.order('id DESC').first |
196 response = Attachment.attach_files(@project, { | 218 assert_equal issue, attachment.container |
197 '1' => {'file' => mock_file, 'description' => 'test'}, | 219 assert_equal 'testfile.txt', attachment.filename |
198 '2' => {'file' => mock_file, 'description' => 'test'} | 220 assert_equal 59, attachment.filesize |
199 }) | 221 assert_equal 'test', attachment.description |
200 | 222 assert_equal 'text/plain', attachment.content_type |
201 assert response[:unsaved].present? | 223 assert File.exists?(attachment.diskfile) |
202 assert_equal 2, response[:unsaved].length | 224 assert_equal 59, File.size(attachment.diskfile) |
203 assert response[:unsaved].first.new_record? | 225 end |
204 assert response[:unsaved].second.new_record? | 226 |
205 assert_equal response[:unsaved], @project.unsaved_attachments | 227 test "Attachmnet.attach_files should add unsaved files to the object as unsaved attachments" do |
206 end | 228 # Max size of 0 to force Attachment creation failures |
207 end | 229 with_settings(:attachment_max_size => 0) do |
230 @project = Project.find(1) | |
231 response = Attachment.attach_files(@project, { | |
232 '1' => {'file' => mock_file, 'description' => 'test'}, | |
233 '2' => {'file' => mock_file, 'description' => 'test'} | |
234 }) | |
235 | |
236 assert response[:unsaved].present? | |
237 assert_equal 2, response[:unsaved].length | |
238 assert response[:unsaved].first.new_record? | |
239 assert response[:unsaved].second.new_record? | |
240 assert_equal response[:unsaved], @project.unsaved_attachments | |
241 end | |
242 end | |
243 | |
244 test "Attachment.attach_files should preserve the content_type of attachments added by token" do | |
245 @project = Project.find(1) | |
246 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago) | |
247 assert_equal 'text/plain', attachment.content_type | |
248 Attachment.attach_files(@project, { '1' => {'token' => attachment.token } }) | |
249 attachment.reload | |
250 assert_equal 'text/plain', attachment.content_type | |
208 end | 251 end |
209 | 252 |
210 def test_latest_attach | 253 def test_latest_attach |
211 set_fixtures_attachments_directory | 254 set_fixtures_attachments_directory |
212 a1 = Attachment.find(16) | 255 a1 = Attachment.find(16) |