@@ -1,144 +1,145 | |||||
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 :users, :projects, :roles, :members, :member_roles, | |
|
24 | :enabled_modules, :issues, :trackers, :attachments | |||
24 |
|
25 | |||
25 | def setup |
|
26 | def setup | |
26 | set_tmp_attachments_directory |
|
27 | set_tmp_attachments_directory | |
27 | end |
|
28 | end | |
28 |
|
29 | |||
29 | def test_create |
|
30 | def test_create | |
30 | a = Attachment.new(:container => Issue.find(1), |
|
31 | a = Attachment.new(:container => Issue.find(1), | |
31 | :file => uploaded_test_file("testfile.txt", "text/plain"), |
|
32 | :file => uploaded_test_file("testfile.txt", "text/plain"), | |
32 | :author => User.find(1)) |
|
33 | :author => User.find(1)) | |
33 | assert a.save |
|
34 | assert a.save | |
34 | assert_equal 'testfile.txt', a.filename |
|
35 | assert_equal 'testfile.txt', a.filename | |
35 | assert_equal 59, a.filesize |
|
36 | assert_equal 59, a.filesize | |
36 | assert_equal 'text/plain', a.content_type |
|
37 | assert_equal 'text/plain', a.content_type | |
37 | assert_equal 0, a.downloads |
|
38 | assert_equal 0, a.downloads | |
38 | assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest |
|
39 | assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest | |
39 | assert File.exist?(a.diskfile) |
|
40 | assert File.exist?(a.diskfile) | |
40 | assert_equal 59, File.size(a.diskfile) |
|
41 | assert_equal 59, File.size(a.diskfile) | |
41 | end |
|
42 | end | |
42 |
|
43 | |||
43 | def test_destroy |
|
44 | def test_destroy | |
44 | a = Attachment.new(:container => Issue.find(1), |
|
45 | a = Attachment.new(:container => Issue.find(1), | |
45 | :file => uploaded_test_file("testfile.txt", "text/plain"), |
|
46 | :file => uploaded_test_file("testfile.txt", "text/plain"), | |
46 | :author => User.find(1)) |
|
47 | :author => User.find(1)) | |
47 | assert a.save |
|
48 | assert a.save | |
48 | assert_equal 'testfile.txt', a.filename |
|
49 | assert_equal 'testfile.txt', a.filename | |
49 | assert_equal 59, a.filesize |
|
50 | assert_equal 59, a.filesize | |
50 | assert_equal 'text/plain', a.content_type |
|
51 | assert_equal 'text/plain', a.content_type | |
51 | assert_equal 0, a.downloads |
|
52 | assert_equal 0, a.downloads | |
52 | assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest |
|
53 | assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest | |
53 | diskfile = a.diskfile |
|
54 | diskfile = a.diskfile | |
54 | assert File.exist?(diskfile) |
|
55 | assert File.exist?(diskfile) | |
55 | assert_equal 59, File.size(a.diskfile) |
|
56 | assert_equal 59, File.size(a.diskfile) | |
56 | assert a.destroy |
|
57 | assert a.destroy | |
57 | assert !File.exist?(diskfile) |
|
58 | assert !File.exist?(diskfile) | |
58 | end |
|
59 | end | |
59 |
|
60 | |||
60 | def test_create_should_auto_assign_content_type |
|
61 | def test_create_should_auto_assign_content_type | |
61 | a = Attachment.new(:container => Issue.find(1), |
|
62 | a = Attachment.new(:container => Issue.find(1), | |
62 | :file => uploaded_test_file("testfile.txt", ""), |
|
63 | :file => uploaded_test_file("testfile.txt", ""), | |
63 | :author => User.find(1)) |
|
64 | :author => User.find(1)) | |
64 | assert a.save |
|
65 | assert a.save | |
65 | assert_equal 'text/plain', a.content_type |
|
66 | assert_equal 'text/plain', a.content_type | |
66 | end |
|
67 | end | |
67 |
|
68 | |||
68 | def test_identical_attachments_at_the_same_time_should_not_overwrite |
|
69 | def test_identical_attachments_at_the_same_time_should_not_overwrite | |
69 | a1 = Attachment.create!(:container => Issue.find(1), |
|
70 | a1 = Attachment.create!(:container => Issue.find(1), | |
70 | :file => uploaded_test_file("testfile.txt", ""), |
|
71 | :file => uploaded_test_file("testfile.txt", ""), | |
71 | :author => User.find(1)) |
|
72 | :author => User.find(1)) | |
72 | a2 = Attachment.create!(:container => Issue.find(1), |
|
73 | a2 = Attachment.create!(:container => Issue.find(1), | |
73 | :file => uploaded_test_file("testfile.txt", ""), |
|
74 | :file => uploaded_test_file("testfile.txt", ""), | |
74 | :author => User.find(1)) |
|
75 | :author => User.find(1)) | |
75 | assert a1.disk_filename != a2.disk_filename |
|
76 | assert a1.disk_filename != a2.disk_filename | |
76 | end |
|
77 | end | |
77 |
|
78 | |||
78 | def test_diskfilename |
|
79 | def test_diskfilename | |
79 | assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/ |
|
80 | assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/ | |
80 | assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1] |
|
81 | assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1] | |
81 | assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentuΓ©.txt")[13..-1] |
|
82 | assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentuΓ©.txt")[13..-1] | |
82 | assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentuΓ©")[13..-1] |
|
83 | assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentuΓ©")[13..-1] | |
83 | assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentuΓ©.Γ§a")[13..-1] |
|
84 | assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentuΓ©.Γ§a")[13..-1] | |
84 | end |
|
85 | end | |
85 |
|
86 | |||
86 | context "Attachmnet.attach_files" do |
|
87 | context "Attachmnet.attach_files" do | |
87 | should "attach the file" do |
|
88 | should "attach the file" do | |
88 | issue = Issue.first |
|
89 | issue = Issue.first | |
89 | assert_difference 'Attachment.count' do |
|
90 | assert_difference 'Attachment.count' do | |
90 | Attachment.attach_files(issue, |
|
91 | Attachment.attach_files(issue, | |
91 | '1' => { |
|
92 | '1' => { | |
92 | 'file' => uploaded_test_file('testfile.txt', 'text/plain'), |
|
93 | 'file' => uploaded_test_file('testfile.txt', 'text/plain'), | |
93 | 'description' => 'test' |
|
94 | 'description' => 'test' | |
94 | }) |
|
95 | }) | |
95 | end |
|
96 | end | |
96 |
|
97 | |||
97 | attachment = Attachment.first(:order => 'id DESC') |
|
98 | attachment = Attachment.first(:order => 'id DESC') | |
98 | assert_equal issue, attachment.container |
|
99 | assert_equal issue, attachment.container | |
99 | assert_equal 'testfile.txt', attachment.filename |
|
100 | assert_equal 'testfile.txt', attachment.filename | |
100 | assert_equal 59, attachment.filesize |
|
101 | assert_equal 59, attachment.filesize | |
101 | assert_equal 'test', attachment.description |
|
102 | assert_equal 'test', attachment.description | |
102 | assert_equal 'text/plain', attachment.content_type |
|
103 | assert_equal 'text/plain', attachment.content_type | |
103 | assert File.exists?(attachment.diskfile) |
|
104 | assert File.exists?(attachment.diskfile) | |
104 | assert_equal 59, File.size(attachment.diskfile) |
|
105 | assert_equal 59, File.size(attachment.diskfile) | |
105 | end |
|
106 | end | |
106 |
|
107 | |||
107 | should "add unsaved files to the object as unsaved attachments" do |
|
108 | should "add unsaved files to the object as unsaved attachments" do | |
108 | # Max size of 0 to force Attachment creation failures |
|
109 | # Max size of 0 to force Attachment creation failures | |
109 | with_settings(:attachment_max_size => 0) do |
|
110 | with_settings(:attachment_max_size => 0) do | |
110 | @project = Project.generate! |
|
111 | @project = Project.generate! | |
111 | response = Attachment.attach_files(@project, { |
|
112 | response = Attachment.attach_files(@project, { | |
112 | '1' => {'file' => mock_file, 'description' => 'test'}, |
|
113 | '1' => {'file' => mock_file, 'description' => 'test'}, | |
113 | '2' => {'file' => mock_file, 'description' => 'test'} |
|
114 | '2' => {'file' => mock_file, 'description' => 'test'} | |
114 | }) |
|
115 | }) | |
115 |
|
116 | |||
116 | assert response[:unsaved].present? |
|
117 | assert response[:unsaved].present? | |
117 | assert_equal 2, response[:unsaved].length |
|
118 | assert_equal 2, response[:unsaved].length | |
118 | assert response[:unsaved].first.new_record? |
|
119 | assert response[:unsaved].first.new_record? | |
119 | assert response[:unsaved].second.new_record? |
|
120 | assert response[:unsaved].second.new_record? | |
120 | assert_equal response[:unsaved], @project.unsaved_attachments |
|
121 | assert_equal response[:unsaved], @project.unsaved_attachments | |
121 | end |
|
122 | end | |
122 | end |
|
123 | end | |
123 | end |
|
124 | end | |
124 |
|
125 | |||
125 | def test_latest_attach |
|
126 | def test_latest_attach | |
126 | Attachment.storage_path = "#{Rails.root}/test/fixtures/files" |
|
127 | Attachment.storage_path = "#{Rails.root}/test/fixtures/files" | |
127 | a1 = Attachment.find(16) |
|
128 | a1 = Attachment.find(16) | |
128 | assert_equal "testfile.png", a1.filename |
|
129 | assert_equal "testfile.png", a1.filename | |
129 | assert a1.readable? |
|
130 | assert a1.readable? | |
130 | assert (! a1.visible?(User.anonymous)) |
|
131 | assert (! a1.visible?(User.anonymous)) | |
131 | assert a1.visible?(User.find(2)) |
|
132 | assert a1.visible?(User.find(2)) | |
132 | a2 = Attachment.find(17) |
|
133 | a2 = Attachment.find(17) | |
133 | assert_equal "testfile.PNG", a2.filename |
|
134 | assert_equal "testfile.PNG", a2.filename | |
134 | assert a2.readable? |
|
135 | assert a2.readable? | |
135 | assert (! a2.visible?(User.anonymous)) |
|
136 | assert (! a2.visible?(User.anonymous)) | |
136 | assert a2.visible?(User.find(2)) |
|
137 | assert a2.visible?(User.find(2)) | |
137 | assert a1.created_on < a2.created_on |
|
138 | assert a1.created_on < a2.created_on | |
138 |
|
139 | |||
139 | la1 = Attachment.latest_attach([a1, a2], "testfile.png") |
|
140 | la1 = Attachment.latest_attach([a1, a2], "testfile.png") | |
140 | assert_equal 17, la1.id |
|
141 | assert_equal 17, la1.id | |
141 | la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG") |
|
142 | la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG") | |
142 | assert_equal 17, la2.id |
|
143 | assert_equal 17, la2.id | |
143 | end |
|
144 | end | |
144 | end |
|
145 | end |
General Comments 0
You need to be logged in to leave comments.
Login now