##// END OF EJS Templates
use temporary attachments directory in unit attachment test...
Toshi MARUYAMA -
r7772:b8b2c1cce4df
parent child
Show More
@@ -1,123 +1,124
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # Redmine - project management software
3 # Redmine - project management software
4 # Copyright (C) 2006-2011 Jean-Philippe Lang
4 # Copyright (C) 2006-2011 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.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
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 :issues, :users
23 fixtures :issues, :users
24
24
25 def setup
25 def setup
26 set_tmp_attachments_directory
26 end
27 end
27
28
28 def test_create
29 def test_create
29 a = Attachment.new(:container => Issue.find(1),
30 a = Attachment.new(:container => Issue.find(1),
30 :file => uploaded_test_file("testfile.txt", "text/plain"),
31 :file => uploaded_test_file("testfile.txt", "text/plain"),
31 :author => User.find(1))
32 :author => User.find(1))
32 assert a.save
33 assert a.save
33 assert_equal 'testfile.txt', a.filename
34 assert_equal 'testfile.txt', a.filename
34 assert_equal 59, a.filesize
35 assert_equal 59, a.filesize
35 assert_equal 'text/plain', a.content_type
36 assert_equal 'text/plain', a.content_type
36 assert_equal 0, a.downloads
37 assert_equal 0, a.downloads
37 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
38 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
38 assert File.exist?(a.diskfile)
39 assert File.exist?(a.diskfile)
39 assert_equal 59, File.size(a.diskfile)
40 assert_equal 59, File.size(a.diskfile)
40 end
41 end
41
42
42 def test_destroy
43 def test_destroy
43 a = Attachment.new(:container => Issue.find(1),
44 a = Attachment.new(:container => Issue.find(1),
44 :file => uploaded_test_file("testfile.txt", "text/plain"),
45 :file => uploaded_test_file("testfile.txt", "text/plain"),
45 :author => User.find(1))
46 :author => User.find(1))
46 assert a.save
47 assert a.save
47 assert_equal 'testfile.txt', a.filename
48 assert_equal 'testfile.txt', a.filename
48 assert_equal 59, a.filesize
49 assert_equal 59, a.filesize
49 assert_equal 'text/plain', a.content_type
50 assert_equal 'text/plain', a.content_type
50 assert_equal 0, a.downloads
51 assert_equal 0, a.downloads
51 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
52 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
52 diskfile = a.diskfile
53 diskfile = a.diskfile
53 assert File.exist?(diskfile)
54 assert File.exist?(diskfile)
54 assert_equal 59, File.size(a.diskfile)
55 assert_equal 59, File.size(a.diskfile)
55 assert a.destroy
56 assert a.destroy
56 assert !File.exist?(diskfile)
57 assert !File.exist?(diskfile)
57 end
58 end
58
59
59 def test_create_should_auto_assign_content_type
60 def test_create_should_auto_assign_content_type
60 a = Attachment.new(:container => Issue.find(1),
61 a = Attachment.new(:container => Issue.find(1),
61 :file => uploaded_test_file("testfile.txt", ""),
62 :file => uploaded_test_file("testfile.txt", ""),
62 :author => User.find(1))
63 :author => User.find(1))
63 assert a.save
64 assert a.save
64 assert_equal 'text/plain', a.content_type
65 assert_equal 'text/plain', a.content_type
65 end
66 end
66
67
67 def test_identical_attachments_at_the_same_time_should_not_overwrite
68 def test_identical_attachments_at_the_same_time_should_not_overwrite
68 a1 = Attachment.create!(:container => Issue.find(1),
69 a1 = Attachment.create!(:container => Issue.find(1),
69 :file => uploaded_test_file("testfile.txt", ""),
70 :file => uploaded_test_file("testfile.txt", ""),
70 :author => User.find(1))
71 :author => User.find(1))
71 a2 = Attachment.create!(:container => Issue.find(1),
72 a2 = Attachment.create!(:container => Issue.find(1),
72 :file => uploaded_test_file("testfile.txt", ""),
73 :file => uploaded_test_file("testfile.txt", ""),
73 :author => User.find(1))
74 :author => User.find(1))
74 assert a1.disk_filename != a2.disk_filename
75 assert a1.disk_filename != a2.disk_filename
75 end
76 end
76
77
77 def test_diskfilename
78 def test_diskfilename
78 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
79 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
79 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
80 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
80 assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentuΓ©.txt")[13..-1]
81 assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentuΓ©.txt")[13..-1]
81 assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentuΓ©")[13..-1]
82 assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentuΓ©")[13..-1]
82 assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentuΓ©.Γ§a")[13..-1]
83 assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentuΓ©.Γ§a")[13..-1]
83 end
84 end
84
85
85 context "Attachmnet.attach_files" do
86 context "Attachmnet.attach_files" do
86 should "attach the file" do
87 should "attach the file" do
87 issue = Issue.first
88 issue = Issue.first
88 assert_difference 'Attachment.count' do
89 assert_difference 'Attachment.count' do
89 Attachment.attach_files(issue,
90 Attachment.attach_files(issue,
90 '1' => {
91 '1' => {
91 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
92 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
92 'description' => 'test'
93 'description' => 'test'
93 })
94 })
94 end
95 end
95
96
96 attachment = Attachment.first(:order => 'id DESC')
97 attachment = Attachment.first(:order => 'id DESC')
97 assert_equal issue, attachment.container
98 assert_equal issue, attachment.container
98 assert_equal 'testfile.txt', attachment.filename
99 assert_equal 'testfile.txt', attachment.filename
99 assert_equal 59, attachment.filesize
100 assert_equal 59, attachment.filesize
100 assert_equal 'test', attachment.description
101 assert_equal 'test', attachment.description
101 assert_equal 'text/plain', attachment.content_type
102 assert_equal 'text/plain', attachment.content_type
102 assert File.exists?(attachment.diskfile)
103 assert File.exists?(attachment.diskfile)
103 assert_equal 59, File.size(attachment.diskfile)
104 assert_equal 59, File.size(attachment.diskfile)
104 end
105 end
105
106
106 should "add unsaved files to the object as unsaved attachments" do
107 should "add unsaved files to the object as unsaved attachments" do
107 # Max size of 0 to force Attachment creation failures
108 # Max size of 0 to force Attachment creation failures
108 with_settings(:attachment_max_size => 0) do
109 with_settings(:attachment_max_size => 0) do
109 @project = Project.generate!
110 @project = Project.generate!
110 response = Attachment.attach_files(@project, {
111 response = Attachment.attach_files(@project, {
111 '1' => {'file' => mock_file, 'description' => 'test'},
112 '1' => {'file' => mock_file, 'description' => 'test'},
112 '2' => {'file' => mock_file, 'description' => 'test'}
113 '2' => {'file' => mock_file, 'description' => 'test'}
113 })
114 })
114
115
115 assert response[:unsaved].present?
116 assert response[:unsaved].present?
116 assert_equal 2, response[:unsaved].length
117 assert_equal 2, response[:unsaved].length
117 assert response[:unsaved].first.new_record?
118 assert response[:unsaved].first.new_record?
118 assert response[:unsaved].second.new_record?
119 assert response[:unsaved].second.new_record?
119 assert_equal response[:unsaved], @project.unsaved_attachments
120 assert_equal response[:unsaved], @project.unsaved_attachments
120 end
121 end
121 end
122 end
122 end
123 end
123 end
124 end
General Comments 0
You need to be logged in to leave comments. Login now