To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 06 / 06191abb72622bec9f1387de935e8ce335158e32.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (9.19 KB)

1 1296:038ba2d95de8 Chris
# encoding: utf-8
2
#
3
# Redmine - project management software
4
# Copyright (C) 2006-2012  Jean-Philippe Lang
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
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
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
20
require File.expand_path('../../test_helper', __FILE__)
21
22
class AttachmentTest < ActiveSupport::TestCase
23
  fixtures :users, :projects, :roles, :members, :member_roles,
24
           :enabled_modules, :issues, :trackers, :attachments
25
26
  class MockFile
27
    attr_reader :original_filename, :content_type, :content, :size
28
29
    def initialize(attributes)
30
      @original_filename = attributes[:original_filename]
31
      @content_type = attributes[:content_type]
32
      @content = attributes[:content] || "Content"
33
      @size = content.size
34
    end
35
  end
36
37
  def setup
38
    set_tmp_attachments_directory
39
  end
40
41
  def test_container_for_new_attachment_should_be_nil
42
    assert_nil Attachment.new.container
43
  end
44
45
  def test_create
46
    a = Attachment.new(:container => Issue.find(1),
47
                       :file => uploaded_test_file("testfile.txt", "text/plain"),
48
                       :author => User.find(1))
49
    assert a.save
50
    assert_equal 'testfile.txt', a.filename
51
    assert_equal 59, a.filesize
52
    assert_equal 'text/plain', a.content_type
53
    assert_equal 0, a.downloads
54
    assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
55
    assert File.exist?(a.diskfile)
56
    assert_equal 59, File.size(a.diskfile)
57
  end
58
59
  def test_size_should_be_validated_for_new_file
60
    with_settings :attachment_max_size => 0 do
61
      a = Attachment.new(:container => Issue.find(1),
62
                         :file => uploaded_test_file("testfile.txt", "text/plain"),
63
                         :author => User.find(1))
64
      assert !a.save
65
    end
66
  end
67
68
  def test_size_should_not_be_validated_when_copying
69
    a = Attachment.create!(:container => Issue.find(1),
70
                           :file => uploaded_test_file("testfile.txt", "text/plain"),
71
                           :author => User.find(1))
72
    with_settings :attachment_max_size => 0 do
73
      copy = a.copy
74
      assert copy.save
75
    end
76
  end
77
78
  def test_description_length_should_be_validated
79
    a = Attachment.new(:description => 'a' * 300)
80
    assert !a.save
81
    assert_not_nil a.errors[:description]
82
  end
83
84
  def test_destroy
85
    a = Attachment.new(:container => Issue.find(1),
86
                       :file => uploaded_test_file("testfile.txt", "text/plain"),
87
                       :author => User.find(1))
88
    assert a.save
89
    assert_equal 'testfile.txt', a.filename
90
    assert_equal 59, a.filesize
91
    assert_equal 'text/plain', a.content_type
92
    assert_equal 0, a.downloads
93
    assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
94
    diskfile = a.diskfile
95
    assert File.exist?(diskfile)
96
    assert_equal 59, File.size(a.diskfile)
97
    assert a.destroy
98
    assert !File.exist?(diskfile)
99
  end
100
101
  def test_destroy_should_not_delete_file_referenced_by_other_attachment
102
    a = Attachment.create!(:container => Issue.find(1),
103
                           :file => uploaded_test_file("testfile.txt", "text/plain"),
104
                           :author => User.find(1))
105
    diskfile = a.diskfile
106
107
    copy = a.copy
108
    copy.save!
109
110
    assert File.exists?(diskfile)
111
    a.destroy
112
    assert File.exists?(diskfile)
113
    copy.destroy
114
    assert !File.exists?(diskfile)
115
  end
116
117
  def test_create_should_auto_assign_content_type
118
    a = Attachment.new(:container => Issue.find(1),
119
                       :file => uploaded_test_file("testfile.txt", ""),
120
                       :author => User.find(1))
121
    assert a.save
122
    assert_equal 'text/plain', a.content_type
123
  end
124
125
  def test_identical_attachments_at_the_same_time_should_not_overwrite
126
    a1 = Attachment.create!(:container => Issue.find(1),
127
                            :file => uploaded_test_file("testfile.txt", ""),
128
                            :author => User.find(1))
129
    a2 = Attachment.create!(:container => Issue.find(1),
130
                            :file => uploaded_test_file("testfile.txt", ""),
131
                            :author => User.find(1))
132
    assert a1.disk_filename != a2.disk_filename
133
  end
134
135
  def test_filename_should_be_basenamed
136
    a = Attachment.new(:file => MockFile.new(:original_filename => "path/to/the/file"))
137
    assert_equal 'file', a.filename
138
  end
139
140
  def test_filename_should_be_sanitized
141
    a = Attachment.new(:file => MockFile.new(:original_filename => "valid:[] invalid:?%*|\"'<>chars"))
142
    assert_equal 'valid_[] invalid_chars', a.filename
143
  end
144
145
  def test_diskfilename
146
    assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
147
    assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
148
    assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1]
149
    assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1]
150
    assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1]
151
  end
152
153
  def test_title
154
    a = Attachment.new(:filename => "test.png")
155
    assert_equal "test.png", a.title
156
157
    a = Attachment.new(:filename => "test.png", :description => "Cool image")
158
    assert_equal "test.png (Cool image)", a.title
159
  end
160
161
  def test_prune_should_destroy_old_unattached_attachments
162
    Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
163
    Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
164
    Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
165
166
    assert_difference 'Attachment.count', -2 do
167
      Attachment.prune
168
    end
169
  end
170
171
  context "Attachmnet.attach_files" do
172
    should "attach the file" do
173
      issue = Issue.first
174
      assert_difference 'Attachment.count' do
175
        Attachment.attach_files(issue,
176
          '1' => {
177
            'file' => uploaded_test_file('testfile.txt', 'text/plain'),
178
            'description' => 'test'
179
          })
180
      end
181
182
      attachment = Attachment.first(:order => 'id DESC')
183
      assert_equal issue, attachment.container
184
      assert_equal 'testfile.txt', attachment.filename
185
      assert_equal 59, attachment.filesize
186
      assert_equal 'test', attachment.description
187
      assert_equal 'text/plain', attachment.content_type
188
      assert File.exists?(attachment.diskfile)
189
      assert_equal 59, File.size(attachment.diskfile)
190
    end
191
192
    should "add unsaved files to the object as unsaved attachments" do
193
      # Max size of 0 to force Attachment creation failures
194
      with_settings(:attachment_max_size => 0) do
195
        @project = Project.find(1)
196
        response = Attachment.attach_files(@project, {
197
                                             '1' => {'file' => mock_file, 'description' => 'test'},
198
                                             '2' => {'file' => mock_file, 'description' => 'test'}
199
                                           })
200
201
        assert response[:unsaved].present?
202
        assert_equal 2, response[:unsaved].length
203
        assert response[:unsaved].first.new_record?
204
        assert response[:unsaved].second.new_record?
205
        assert_equal response[:unsaved], @project.unsaved_attachments
206
      end
207
    end
208
  end
209
210
  def test_latest_attach
211
    set_fixtures_attachments_directory
212
    a1 = Attachment.find(16)
213
    assert_equal "testfile.png", a1.filename
214
    assert a1.readable?
215
    assert (! a1.visible?(User.anonymous))
216
    assert a1.visible?(User.find(2))
217
    a2 = Attachment.find(17)
218
    assert_equal "testfile.PNG", a2.filename
219
    assert a2.readable?
220
    assert (! a2.visible?(User.anonymous))
221
    assert a2.visible?(User.find(2))
222
    assert a1.created_on < a2.created_on
223
224
    la1 = Attachment.latest_attach([a1, a2], "testfile.png")
225
    assert_equal 17, la1.id
226
    la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG")
227
    assert_equal 17, la2.id
228
229
    set_tmp_attachments_directory
230
  end
231
232
  def test_thumbnailable_should_be_true_for_images
233
    assert_equal true, Attachment.new(:filename => 'test.jpg').thumbnailable?
234
  end
235
236
  def test_thumbnailable_should_be_true_for_non_images
237
    assert_equal false, Attachment.new(:filename => 'test.txt').thumbnailable?
238
  end
239
240
  if convert_installed?
241
    def test_thumbnail_should_generate_the_thumbnail
242
      set_fixtures_attachments_directory
243
      attachment = Attachment.find(16)
244
      Attachment.clear_thumbnails
245
246
      assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do
247
        thumbnail = attachment.thumbnail
248
        assert_equal "16_8e0294de2441577c529f170b6fb8f638_100.thumb", File.basename(thumbnail)
249
        assert File.exists?(thumbnail)
250
      end
251
    end
252
  else
253
    puts '(ImageMagick convert not available)'
254
  end
255
end