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