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