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