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 / 13 / 134ab6c377c7865f7eab83c298719a41c97ec009.svn-base @ 1298:4f746d8966dd

History | View | Annotate | Download (5.01 KB)

1 1295:622f24f53b42 Chris
# Redmine - project management software
2
# Copyright (C) 2006-2013  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18
require File.expand_path('../../test_helper', __FILE__)
19
20
class AttachmentsTest < ActionController::IntegrationTest
21
  fixtures :projects, :enabled_modules,
22
           :users, :roles, :members, :member_roles,
23
           :trackers, :projects_trackers,
24
           :issue_statuses, :enumerations
25
26
  def test_upload_as_js_and_attach_to_an_issue
27
    log_user('jsmith', 'jsmith')
28
29
    token = ajax_upload('myupload.txt', 'File content')
30
31
    assert_difference 'Issue.count' do
32
      post '/projects/ecookbook/issues', {
33
          :issue => {:tracker_id => 1, :subject => 'Issue with upload'},
34
          :attachments => {'1' => {:filename => 'myupload.txt', :description => 'My uploaded file', :token => token}}
35
        }
36
      assert_response 302
37
    end
38
39
    issue = Issue.order('id DESC').first
40
    assert_equal 'Issue with upload', issue.subject
41
    assert_equal 1, issue.attachments.count
42
43
    attachment = issue.attachments.first
44
    assert_equal 'myupload.txt', attachment.filename
45
    assert_equal 'My uploaded file', attachment.description
46
    assert_equal 'File content'.length, attachment.filesize
47
  end
48
49
  def test_upload_as_js_and_preview_as_inline_attachment
50
    log_user('jsmith', 'jsmith')
51
52
    token = ajax_upload('myupload.jpg', 'JPEG content')
53
54
    post '/issues/preview/new/ecookbook', {
55
        :issue => {:tracker_id => 1, :description => 'Inline upload: !myupload.jpg!'},
56
        :attachments => {'1' => {:filename => 'myupload.jpg', :description => 'My uploaded file', :token => token}}
57
      }
58
    assert_response :success
59
60
    attachment_path = response.body.match(%r{<img src="(/attachments/download/\d+/myupload.jpg)"})[1]
61
    assert_not_nil token, "No attachment path found in response:\n#{response.body}"
62
63
    get attachment_path
64
    assert_response :success
65
    assert_equal 'JPEG content', response.body
66
  end
67
68
  def test_upload_and_resubmit_after_validation_failure
69
    log_user('jsmith', 'jsmith')
70
71
    token = ajax_upload('myupload.txt', 'File content')
72
73
    assert_no_difference 'Issue.count' do
74
      post '/projects/ecookbook/issues', {
75
          :issue => {:tracker_id => 1, :subject => ''},
76
          :attachments => {'1' => {:filename => 'myupload.txt', :description => 'My uploaded file', :token => token}}
77
        }
78
      assert_response :success
79
    end
80
    assert_select 'input[type=hidden][name=?][value=?]', 'attachments[p0][token]', token
81
    assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'myupload.txt'
82
    assert_select 'input[name=?][value=?]', 'attachments[p0][description]', 'My uploaded file'
83
84
    assert_difference 'Issue.count' do
85
      post '/projects/ecookbook/issues', {
86
          :issue => {:tracker_id => 1, :subject => 'Issue with upload'},
87
          :attachments => {'p0' => {:filename => 'myupload.txt', :description => 'My uploaded file', :token => token}}
88
        }
89
      assert_response 302
90
    end
91
92
    issue = Issue.order('id DESC').first
93
    assert_equal 'Issue with upload', issue.subject
94
    assert_equal 1, issue.attachments.count
95
96
    attachment = issue.attachments.first
97
    assert_equal 'myupload.txt', attachment.filename
98
    assert_equal 'My uploaded file', attachment.description
99
    assert_equal 'File content'.length, attachment.filesize
100
  end
101
102
  def test_upload_as_js_and_destroy
103
    log_user('jsmith', 'jsmith')
104
105
    token = ajax_upload('myupload.txt', 'File content')
106
107
    attachment = Attachment.order('id DESC').first
108
    attachment_path = "/attachments/#{attachment.id}.js?attachment_id=1"
109
    assert_include "href: '#{attachment_path}'", response.body, "Path to attachment: #{attachment_path} not found in response:\n#{response.body}"
110
111
    assert_difference 'Attachment.count', -1 do
112
      delete attachment_path
113
      assert_response :success
114
    end
115
116
    assert_include "$('#attachments_1').remove();", response.body
117
  end
118
119
  private
120
121
  def ajax_upload(filename, content, attachment_id=1)
122
    assert_difference 'Attachment.count' do
123
      post "/uploads.js?attachment_id=#{attachment_id}&filename=#{filename}", content, {"CONTENT_TYPE" => 'application/octet-stream'}
124
      assert_response :success
125
      assert_equal 'text/javascript', response.content_type
126
    end
127
128
    token = response.body.match(/\.val\('(\d+\.[0-9a-f]+)'\)/)[1]
129
    assert_not_nil token, "No upload token found in response:\n#{response.body}"
130
    token
131
  end
132
end