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 / 3d / 3d5312afd2b7a1501b0ec17ef249ac149a4a5a3f.svn-base @ 1298:4f746d8966dd

History | View | Annotate | Download (12.1 KB)

1
# 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
require 'attachments_controller'
22

    
23
# Re-raise errors caught by the controller.
24
class AttachmentsController; def rescue_action(e) raise e end; end
25

    
26
class AttachmentsControllerTest < ActionController::TestCase
27
  fixtures :users, :projects, :roles, :members, :member_roles,
28
           :enabled_modules, :issues, :trackers, :attachments,
29
           :versions, :wiki_pages, :wikis, :documents
30

    
31
  def setup
32
    @controller = AttachmentsController.new
33
    @request    = ActionController::TestRequest.new
34
    @response   = ActionController::TestResponse.new
35
    User.current = nil
36
    set_fixtures_attachments_directory
37
  end
38

    
39
  def teardown
40
    set_tmp_attachments_directory
41
  end
42

    
43
  def test_show_diff
44
    ['inline', 'sbs'].each do |dt|
45
      # 060719210727_changeset_utf8.diff
46
      get :show, :id => 14, :type => dt
47
      assert_response :success
48
      assert_template 'diff'
49
      assert_equal 'text/html', @response.content_type
50
      assert_tag 'th',
51
        :attributes => {:class => /filename/},
52
        :content => /issues_controller.rb\t\(révision 1484\)/
53
      assert_tag 'td',
54
        :attributes => {:class => /line-code/},
55
        :content => /Demande créée avec succès/
56
    end
57
    set_tmp_attachments_directory
58
  end
59

    
60
  def test_show_diff_replcace_cannot_convert_content
61
    with_settings :repositories_encodings => 'UTF-8' do
62
      ['inline', 'sbs'].each do |dt|
63
        # 060719210727_changeset_iso8859-1.diff
64
        get :show, :id => 5, :type => dt
65
        assert_response :success
66
        assert_template 'diff'
67
        assert_equal 'text/html', @response.content_type
68
        assert_tag 'th',
69
          :attributes => {:class => "filename"},
70
          :content => /issues_controller.rb\t\(r\?vision 1484\)/
71
        assert_tag 'td',
72
          :attributes => {:class => /line-code/},
73
          :content => /Demande cr\?\?e avec succ\?s/
74
      end
75
    end
76
    set_tmp_attachments_directory
77
  end
78

    
79
  def test_show_diff_latin_1
80
    with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
81
      ['inline', 'sbs'].each do |dt|
82
        # 060719210727_changeset_iso8859-1.diff
83
        get :show, :id => 5, :type => dt
84
        assert_response :success
85
        assert_template 'diff'
86
        assert_equal 'text/html', @response.content_type
87
        assert_tag 'th',
88
          :attributes => {:class => "filename"},
89
          :content => /issues_controller.rb\t\(révision 1484\)/
90
        assert_tag 'td',
91
          :attributes => {:class => /line-code/},
92
          :content => /Demande créée avec succès/
93
      end
94
    end
95
    set_tmp_attachments_directory
96
  end
97

    
98
  def test_save_diff_type
99
    user1 = User.find(1)
100
    user1.pref[:diff_type] = nil
101
    user1.preference.save
102
    user = User.find(1)
103
    assert_nil user.pref[:diff_type]
104

    
105
    @request.session[:user_id] = 1 # admin
106
    get :show, :id => 5
107
    assert_response :success
108
    assert_template 'diff'
109
    user.reload
110
    assert_equal "inline", user.pref[:diff_type]
111
    get :show, :id => 5, :type => 'sbs'
112
    assert_response :success
113
    assert_template 'diff'
114
    user.reload
115
    assert_equal "sbs", user.pref[:diff_type]
116
  end
117

    
118
  def test_diff_show_filename_in_mercurial_export
119
    set_tmp_attachments_directory
120
    a = Attachment.new(:container => Issue.find(1),
121
                       :file => uploaded_test_file("hg-export.diff", "text/plain"),
122
                       :author => User.find(1))
123
    assert a.save
124
    assert_equal 'hg-export.diff', a.filename
125

    
126
    get :show, :id => a.id, :type => 'inline'
127
    assert_response :success
128
    assert_template 'diff'
129
    assert_equal 'text/html', @response.content_type
130
    assert_select 'th.filename', :text => 'test1.txt'
131
  end
132

    
133
  def test_show_text_file
134
    get :show, :id => 4
135
    assert_response :success
136
    assert_template 'file'
137
    assert_equal 'text/html', @response.content_type
138
    set_tmp_attachments_directory
139
  end
140

    
141
  def test_show_text_file_utf_8
142
    set_tmp_attachments_directory
143
    a = Attachment.new(:container => Issue.find(1),
144
                       :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
145
                       :author => User.find(1))
146
    assert a.save
147
    assert_equal 'japanese-utf-8.txt', a.filename
148

    
149
    str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
150
    str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding)
151

    
152
    get :show, :id => a.id
153
    assert_response :success
154
    assert_template 'file'
155
    assert_equal 'text/html', @response.content_type
156
    assert_tag :tag => 'th',
157
               :content => '1',
158
               :attributes => { :class => 'line-num' },
159
               :sibling => { :tag => 'td', :content => /#{str_japanese}/ }
160
  end
161

    
162
  def test_show_text_file_replcace_cannot_convert_content
163
    set_tmp_attachments_directory
164
    with_settings :repositories_encodings => 'UTF-8' do
165
      a = Attachment.new(:container => Issue.find(1),
166
                         :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
167
                         :author => User.find(1))
168
      assert a.save
169
      assert_equal 'iso8859-1.txt', a.filename
170

    
171
      get :show, :id => a.id
172
      assert_response :success
173
      assert_template 'file'
174
      assert_equal 'text/html', @response.content_type
175
      assert_tag :tag => 'th',
176
                 :content => '7',
177
                 :attributes => { :class => 'line-num' },
178
                 :sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ }
179
    end
180
  end
181

    
182
  def test_show_text_file_latin_1
183
    set_tmp_attachments_directory
184
    with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
185
      a = Attachment.new(:container => Issue.find(1),
186
                         :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
187
                         :author => User.find(1))
188
      assert a.save
189
      assert_equal 'iso8859-1.txt', a.filename
190

    
191
      get :show, :id => a.id
192
      assert_response :success
193
      assert_template 'file'
194
      assert_equal 'text/html', @response.content_type
195
      assert_tag :tag => 'th',
196
                 :content => '7',
197
                 :attributes => { :class => 'line-num' },
198
                 :sibling => { :tag => 'td', :content => /Demande créée avec succès/ }
199
      end
200
  end
201

    
202
  def test_show_text_file_should_send_if_too_big
203
    Setting.file_max_size_displayed = 512
204
    Attachment.find(4).update_attribute :filesize, 754.kilobyte
205

    
206
    get :show, :id => 4
207
    assert_response :success
208
    assert_equal 'application/x-ruby', @response.content_type
209
    set_tmp_attachments_directory
210
  end
211

    
212
  def test_show_other
213
    get :show, :id => 6
214
    assert_response :success
215
    assert_equal 'application/octet-stream', @response.content_type
216
    set_tmp_attachments_directory
217
  end
218

    
219
  def test_show_file_from_private_issue_without_permission
220
    get :show, :id => 15
221
    assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
222
    set_tmp_attachments_directory
223
  end
224

    
225
  def test_show_file_from_private_issue_with_permission
226
    @request.session[:user_id] = 2
227
    get :show, :id => 15
228
    assert_response :success
229
    assert_tag 'h2', :content => /private.diff/
230
    set_tmp_attachments_directory
231
  end
232

    
233
  def test_show_file_without_container_should_be_denied
234
    set_tmp_attachments_directory
235
    attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
236

    
237
    @request.session[:user_id] = 2
238
    get :show, :id => attachment.id
239
    assert_response 403
240
  end
241

    
242
  def test_show_invalid_should_respond_with_404
243
    get :show, :id => 999
244
    assert_response 404
245
  end
246

    
247
  def test_download_text_file
248
    get :download, :id => 4
249
    assert_response :success
250
    assert_equal 'application/x-ruby', @response.content_type
251
    set_tmp_attachments_directory
252
  end
253

    
254
  def test_download_version_file_with_issue_tracking_disabled
255
    Project.find(1).disable_module! :issue_tracking
256
    get :download, :id => 9
257
    assert_response :success
258
  end
259

    
260
  def test_download_should_assign_content_type_if_blank
261
    Attachment.find(4).update_attribute(:content_type, '')
262

    
263
    get :download, :id => 4
264
    assert_response :success
265
    assert_equal 'text/x-ruby', @response.content_type
266
    set_tmp_attachments_directory
267
  end
268

    
269
  def test_download_missing_file
270
    get :download, :id => 2
271
    assert_response 404
272
    set_tmp_attachments_directory
273
  end
274

    
275
  def test_download_should_be_denied_without_permission
276
    get :download, :id => 7
277
    assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
278
    set_tmp_attachments_directory
279
  end
280

    
281
  if convert_installed?
282
    def test_thumbnail
283
      Attachment.clear_thumbnails
284
      @request.session[:user_id] = 2
285

    
286
      get :thumbnail, :id => 16
287
      assert_response :success
288
      assert_equal 'image/png', response.content_type
289
    end
290

    
291
    def test_thumbnail_should_not_exceed_maximum_size
292
      Redmine::Thumbnail.expects(:generate).with {|source, target, size| size == 800}
293

    
294
      @request.session[:user_id] = 2
295
      get :thumbnail, :id => 16, :size => 2000
296
    end
297

    
298
    def test_thumbnail_should_round_size
299
      Redmine::Thumbnail.expects(:generate).with {|source, target, size| size == 250}
300

    
301
      @request.session[:user_id] = 2
302
      get :thumbnail, :id => 16, :size => 260
303
    end
304

    
305
    def test_thumbnail_should_return_404_for_non_image_attachment
306
      @request.session[:user_id] = 2
307

    
308
      get :thumbnail, :id => 15
309
      assert_response 404
310
    end
311

    
312
    def test_thumbnail_should_return_404_if_thumbnail_generation_failed
313
      Attachment.any_instance.stubs(:thumbnail).returns(nil)
314
      @request.session[:user_id] = 2
315

    
316
      get :thumbnail, :id => 16
317
      assert_response 404
318
    end
319

    
320
    def test_thumbnail_should_be_denied_without_permission
321
      get :thumbnail, :id => 16
322
      assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fthumbnail%2F16'
323
    end
324
  else
325
    puts '(ImageMagick convert not available)'
326
  end
327

    
328
  def test_destroy_issue_attachment
329
    set_tmp_attachments_directory
330
    issue = Issue.find(3)
331
    @request.session[:user_id] = 2
332

    
333
    assert_difference 'issue.attachments.count', -1 do
334
      assert_difference 'Journal.count' do
335
        delete :destroy, :id => 1
336
        assert_redirected_to '/projects/ecookbook'
337
      end
338
    end
339
    assert_nil Attachment.find_by_id(1)
340
    j = Journal.first(:order => 'id DESC')
341
    assert_equal issue, j.journalized
342
    assert_equal 'attachment', j.details.first.property
343
    assert_equal '1', j.details.first.prop_key
344
    assert_equal 'error281.txt', j.details.first.old_value
345
    assert_equal User.find(2), j.user
346
  end
347

    
348
  def test_destroy_wiki_page_attachment
349
    set_tmp_attachments_directory
350
    @request.session[:user_id] = 2
351
    assert_difference 'Attachment.count', -1 do
352
      delete :destroy, :id => 3
353
      assert_response 302
354
    end
355
  end
356

    
357
  def test_destroy_project_attachment
358
    set_tmp_attachments_directory
359
    @request.session[:user_id] = 2
360
    assert_difference 'Attachment.count', -1 do
361
      delete :destroy, :id => 8
362
      assert_response 302
363
    end
364
  end
365

    
366
  def test_destroy_version_attachment
367
    set_tmp_attachments_directory
368
    @request.session[:user_id] = 2
369
    assert_difference 'Attachment.count', -1 do
370
      delete :destroy, :id => 9
371
      assert_response 302
372
    end
373
  end
374

    
375
  def test_destroy_without_permission
376
    set_tmp_attachments_directory
377
    assert_no_difference 'Attachment.count' do
378
      delete :destroy, :id => 3
379
    end
380
    assert_response 302
381
    assert Attachment.find_by_id(3)
382
  end
383
end