@@ -1,106 +1,106 | |||||
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 | end |
|
26 | end | |
27 |
|
27 | |||
28 | def test_create |
|
28 | def test_create | |
29 | a = Attachment.new(:container => Issue.find(1), |
|
29 | a = Attachment.new(:container => Issue.find(1), | |
30 | :file => uploaded_test_file("testfile.txt", "text/plain"), |
|
30 | :file => uploaded_test_file("testfile.txt", "text/plain"), | |
31 | :author => User.find(1)) |
|
31 | :author => User.find(1)) | |
32 | assert a.save |
|
32 | assert a.save | |
33 | assert_equal 'testfile.txt', a.filename |
|
33 | assert_equal 'testfile.txt', a.filename | |
34 | assert_equal 59, a.filesize |
|
34 | assert_equal 59, a.filesize | |
35 | assert_equal 'text/plain', a.content_type |
|
35 | assert_equal 'text/plain', a.content_type | |
36 | assert_equal 0, a.downloads |
|
36 | assert_equal 0, a.downloads | |
37 | assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest |
|
37 | assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest | |
38 | assert File.exist?(a.diskfile) |
|
38 | assert File.exist?(a.diskfile) | |
39 | assert_equal 59, File.size(a.diskfile) |
|
39 | assert_equal 59, File.size(a.diskfile) | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
42 | def test_create_should_auto_assign_content_type |
|
42 | def test_create_should_auto_assign_content_type | |
43 | a = Attachment.new(:container => Issue.find(1), |
|
43 | a = Attachment.new(:container => Issue.find(1), | |
44 | :file => uploaded_test_file("testfile.txt", ""), |
|
44 | :file => uploaded_test_file("testfile.txt", ""), | |
45 | :author => User.find(1)) |
|
45 | :author => User.find(1)) | |
46 | assert a.save |
|
46 | assert a.save | |
47 | assert_equal 'text/plain', a.content_type |
|
47 | assert_equal 'text/plain', a.content_type | |
48 | end |
|
48 | end | |
49 |
|
49 | |||
50 | def test_identical_attachments_at_the_same_time_should_not_overwrite |
|
50 | def test_identical_attachments_at_the_same_time_should_not_overwrite | |
51 | a1 = Attachment.create!(:container => Issue.find(1), |
|
51 | a1 = Attachment.create!(:container => Issue.find(1), | |
52 | :file => uploaded_test_file("testfile.txt", ""), |
|
52 | :file => uploaded_test_file("testfile.txt", ""), | |
53 | :author => User.find(1)) |
|
53 | :author => User.find(1)) | |
54 | a2 = Attachment.create!(:container => Issue.find(1), |
|
54 | a2 = Attachment.create!(:container => Issue.find(1), | |
55 | :file => uploaded_test_file("testfile.txt", ""), |
|
55 | :file => uploaded_test_file("testfile.txt", ""), | |
56 | :author => User.find(1)) |
|
56 | :author => User.find(1)) | |
57 | assert a1.disk_filename != a2.disk_filename |
|
57 | assert a1.disk_filename != a2.disk_filename | |
58 | end |
|
58 | end | |
59 |
|
59 | |||
60 | def test_diskfilename |
|
60 | def test_diskfilename | |
61 | assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/ |
|
61 | assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/ | |
62 | assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1] |
|
62 | assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1] | |
63 | assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentuΓ©.txt")[13..-1] |
|
63 | assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentuΓ©.txt")[13..-1] | |
64 | assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentuΓ©")[13..-1] |
|
64 | assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentuΓ©")[13..-1] | |
65 | assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentuΓ©.Γ§a")[13..-1] |
|
65 | assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentuΓ©.Γ§a")[13..-1] | |
66 | end |
|
66 | end | |
67 |
|
67 | |||
68 | context "Attachmnet.attach_files" do |
|
68 | context "Attachmnet.attach_files" do | |
69 | should "attach the file" do |
|
69 | should "attach the file" do | |
70 | issue = Issue.first |
|
70 | issue = Issue.first | |
71 | assert_difference 'Attachment.count' do |
|
71 | assert_difference 'Attachment.count' do | |
72 | Attachment.attach_files(issue, |
|
72 | Attachment.attach_files(issue, | |
73 | '1' => { |
|
73 | '1' => { | |
74 | 'file' => uploaded_test_file('testfile.txt', 'text/plain'), |
|
74 | 'file' => uploaded_test_file('testfile.txt', 'text/plain'), | |
75 | 'description' => 'test' |
|
75 | 'description' => 'test' | |
76 | }) |
|
76 | }) | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
79 | attachment = Attachment.first(:order => 'id DESC') |
|
79 | attachment = Attachment.first(:order => 'id DESC') | |
80 | assert_equal issue, attachment.container |
|
80 | assert_equal issue, attachment.container | |
81 | assert_equal 'testfile.txt', attachment.filename |
|
81 | assert_equal 'testfile.txt', attachment.filename | |
82 | assert_equal 59, attachment.filesize |
|
82 | assert_equal 59, attachment.filesize | |
83 | assert_equal 'test', attachment.description |
|
83 | assert_equal 'test', attachment.description | |
84 | assert_equal 'text/plain', attachment.content_type |
|
84 | assert_equal 'text/plain', attachment.content_type | |
85 | assert File.exists?(attachment.diskfile) |
|
85 | assert File.exists?(attachment.diskfile) | |
86 | assert_equal 59, File.size(attachment.diskfile) |
|
86 | assert_equal 59, File.size(attachment.diskfile) | |
87 | end |
|
87 | end | |
88 |
|
88 | |||
89 | should "add unsaved files to the object as unsaved attachments" do |
|
89 | should "add unsaved files to the object as unsaved attachments" do | |
90 | # Max size of 0 to force Attachment creation failures |
|
90 | # Max size of 0 to force Attachment creation failures | |
91 | with_settings(:attachment_max_size => 0) do |
|
91 | with_settings(:attachment_max_size => 0) do | |
92 | @project = Project.generate! |
|
92 | @project = Project.generate! | |
93 | response = Attachment.attach_files(@project, { |
|
93 | response = Attachment.attach_files(@project, { | |
94 | '1' => {'file' => mock_file, 'description' => 'test'}, |
|
94 | '1' => {'file' => mock_file, 'description' => 'test'}, | |
95 | '2' => {'file' => mock_file, 'description' => 'test'} |
|
95 | '2' => {'file' => mock_file, 'description' => 'test'} | |
96 | }) |
|
96 | }) | |
97 |
|
97 | |||
98 | assert response[:unsaved].present? |
|
98 | assert response[:unsaved].present? | |
99 | assert_equal 2, response[:unsaved].length |
|
99 | assert_equal 2, response[:unsaved].length | |
100 | assert response[:unsaved].first.new_record? |
|
100 | assert response[:unsaved].first.new_record? | |
101 | assert response[:unsaved].second.new_record? |
|
101 | assert response[:unsaved].second.new_record? | |
102 | assert_equal response[:unsaved], @project.unsaved_attachments |
|
102 | assert_equal response[:unsaved], @project.unsaved_attachments | |
103 | end |
|
103 | end | |
104 | end |
|
104 | end | |
105 | end |
|
105 | end | |
106 | end |
|
106 | end |
General Comments 0
You need to be logged in to leave comments.
Login now