@@ -1,207 +1,209 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | # |
|
3 | 3 | # Redmine - project management software |
|
4 | 4 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
5 | 5 | # |
|
6 | 6 | # This program is free software; you can redistribute it and/or |
|
7 | 7 | # modify it under the terms of the GNU General Public License |
|
8 | 8 | # as published by the Free Software Foundation; either version 2 |
|
9 | 9 | # of the License, or (at your option) any later version. |
|
10 | 10 | # |
|
11 | 11 | # This program is distributed in the hope that it will be useful, |
|
12 | 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 | 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 14 | # GNU General Public License for more details. |
|
15 | 15 | # |
|
16 | 16 | # You should have received a copy of the GNU General Public License |
|
17 | 17 | # along with this program; if not, write to the Free Software |
|
18 | 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
19 | 19 | |
|
20 | 20 | require File.expand_path('../../test_helper', __FILE__) |
|
21 | 21 | require 'attachments_controller' |
|
22 | 22 | |
|
23 | 23 | # Re-raise errors caught by the controller. |
|
24 | 24 | class AttachmentsController; def rescue_action(e) raise e end; end |
|
25 | 25 | |
|
26 | 26 | class AttachmentsControllerTest < ActionController::TestCase |
|
27 | 27 | fixtures :users, :projects, :roles, :members, :member_roles, |
|
28 | 28 | :enabled_modules, :issues, :trackers, :attachments, |
|
29 | 29 | :versions, :wiki_pages, :wikis, :documents |
|
30 | 30 | |
|
31 | 31 | def setup |
|
32 | 32 | @controller = AttachmentsController.new |
|
33 | 33 | @request = ActionController::TestRequest.new |
|
34 | 34 | @response = ActionController::TestResponse.new |
|
35 | 35 | Attachment.storage_path = "#{Rails.root}/test/fixtures/files" |
|
36 | 36 | User.current = nil |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | def test_show_diff |
|
40 | 40 | get :show, :id => 14 # 060719210727_changeset_utf8.diff |
|
41 | 41 | assert_response :success |
|
42 | 42 | assert_template 'diff' |
|
43 | 43 | assert_equal 'text/html', @response.content_type |
|
44 | 44 | |
|
45 | 45 | assert_tag 'th', |
|
46 | 46 | :attributes => {:class => /filename/}, |
|
47 | 47 | :content => /issues_controller.rb\t\(rΓ©vision 1484\)/ |
|
48 | 48 | assert_tag 'td', |
|
49 | 49 | :attributes => {:class => /line-code/}, |
|
50 | 50 | :content => /Demande créée avec succès/ |
|
51 | 51 | end |
|
52 | 52 | |
|
53 | 53 | def test_show_diff_should_strip_non_utf8_content |
|
54 | 54 | get :show, :id => 5 # 060719210727_changeset_iso8859-1.diff |
|
55 | 55 | assert_response :success |
|
56 | 56 | assert_template 'diff' |
|
57 | 57 | assert_equal 'text/html', @response.content_type |
|
58 | 58 | |
|
59 | 59 | assert_tag 'th', |
|
60 | 60 | :attributes => {:class => /filename/}, |
|
61 | 61 | :content => /issues_controller.rb\t\(rvision 1484\)/ |
|
62 | 62 | assert_tag 'td', |
|
63 | 63 | :attributes => {:class => /line-code/}, |
|
64 | 64 | :content => /Demande cre avec succs/ |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | def test_show_text_file |
|
68 | 68 | get :show, :id => 4 |
|
69 | 69 | assert_response :success |
|
70 | 70 | assert_template 'file' |
|
71 | 71 | assert_equal 'text/html', @response.content_type |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | def test_show_text_file_utf_8 |
|
75 | set_tmp_attachments_directory | |
|
75 | 76 | a = Attachment.new(:container => Issue.find(1), |
|
76 | 77 | :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"), |
|
77 | 78 | :author => User.find(1)) |
|
78 | 79 | assert a.save |
|
79 | 80 | assert_equal 'japanese-utf-8.txt', a.filename |
|
80 | 81 | |
|
81 | 82 | str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" |
|
82 | 83 | str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding) |
|
83 | 84 | |
|
84 | 85 | get :show, :id => a.id |
|
85 | 86 | assert_response :success |
|
86 | 87 | assert_template 'file' |
|
87 | 88 | assert_equal 'text/html', @response.content_type |
|
88 | 89 | assert_tag :tag => 'th', |
|
89 | 90 | :content => '1', |
|
90 | 91 | :attributes => { :class => 'line-num' }, |
|
91 | 92 | :sibling => { :tag => 'td', :content => /#{str_japanese}/ } |
|
92 | 93 | end |
|
93 | 94 | |
|
94 | 95 | def test_show_text_file_should_strip_non_utf8_content |
|
96 | set_tmp_attachments_directory | |
|
95 | 97 | a = Attachment.new(:container => Issue.find(1), |
|
96 | 98 | :file => uploaded_test_file("iso8859-1.txt", "text/plain"), |
|
97 | 99 | :author => User.find(1)) |
|
98 | 100 | assert a.save |
|
99 | 101 | assert_equal 'iso8859-1.txt', a.filename |
|
100 | 102 | |
|
101 | 103 | get :show, :id => a.id |
|
102 | 104 | assert_response :success |
|
103 | 105 | assert_template 'file' |
|
104 | 106 | assert_equal 'text/html', @response.content_type |
|
105 | 107 | assert_tag :tag => 'th', |
|
106 | 108 | :content => '7', |
|
107 | 109 | :attributes => { :class => 'line-num' }, |
|
108 | 110 | :sibling => { :tag => 'td', :content => /Demande cre avec succs/ } |
|
109 | 111 | end |
|
110 | 112 | |
|
111 | 113 | def test_show_text_file_should_send_if_too_big |
|
112 | 114 | Setting.file_max_size_displayed = 512 |
|
113 | 115 | Attachment.find(4).update_attribute :filesize, 754.kilobyte |
|
114 | 116 | |
|
115 | 117 | get :show, :id => 4 |
|
116 | 118 | assert_response :success |
|
117 | 119 | assert_equal 'application/x-ruby', @response.content_type |
|
118 | 120 | end |
|
119 | 121 | |
|
120 | 122 | def test_show_other |
|
121 | 123 | get :show, :id => 6 |
|
122 | 124 | assert_response :success |
|
123 | 125 | assert_equal 'application/octet-stream', @response.content_type |
|
124 | 126 | end |
|
125 | 127 | |
|
126 | 128 | def test_show_file_from_private_issue_without_permission |
|
127 | 129 | get :show, :id => 15 |
|
128 | 130 | assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15' |
|
129 | 131 | end |
|
130 | 132 | |
|
131 | 133 | def test_show_file_from_private_issue_with_permission |
|
132 | 134 | @request.session[:user_id] = 2 |
|
133 | 135 | get :show, :id => 15 |
|
134 | 136 | assert_response :success |
|
135 | 137 | assert_tag 'h2', :content => /private.diff/ |
|
136 | 138 | end |
|
137 | 139 | |
|
138 | 140 | def test_download_text_file |
|
139 | 141 | get :download, :id => 4 |
|
140 | 142 | assert_response :success |
|
141 | 143 | assert_equal 'application/x-ruby', @response.content_type |
|
142 | 144 | end |
|
143 | 145 | |
|
144 | 146 | def test_download_should_assign_content_type_if_blank |
|
145 | 147 | Attachment.find(4).update_attribute(:content_type, '') |
|
146 | 148 | |
|
147 | 149 | get :download, :id => 4 |
|
148 | 150 | assert_response :success |
|
149 | 151 | assert_equal 'text/x-ruby', @response.content_type |
|
150 | 152 | end |
|
151 | 153 | |
|
152 | 154 | def test_download_missing_file |
|
153 | 155 | get :download, :id => 2 |
|
154 | 156 | assert_response 404 |
|
155 | 157 | end |
|
156 | 158 | |
|
157 | 159 | def test_anonymous_on_private_private |
|
158 | 160 | get :download, :id => 7 |
|
159 | 161 | assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7' |
|
160 | 162 | end |
|
161 | 163 | |
|
162 | 164 | def test_destroy_issue_attachment |
|
163 | 165 | issue = Issue.find(3) |
|
164 | 166 | @request.session[:user_id] = 2 |
|
165 | 167 | |
|
166 | 168 | assert_difference 'issue.attachments.count', -1 do |
|
167 | 169 | post :destroy, :id => 1 |
|
168 | 170 | end |
|
169 | 171 | # no referrer |
|
170 | 172 | assert_redirected_to '/projects/ecookbook' |
|
171 | 173 | assert_nil Attachment.find_by_id(1) |
|
172 | 174 | j = issue.journals.find(:first, :order => 'created_on DESC') |
|
173 | 175 | assert_equal 'attachment', j.details.first.property |
|
174 | 176 | assert_equal '1', j.details.first.prop_key |
|
175 | 177 | assert_equal 'error281.txt', j.details.first.old_value |
|
176 | 178 | end |
|
177 | 179 | |
|
178 | 180 | def test_destroy_wiki_page_attachment |
|
179 | 181 | @request.session[:user_id] = 2 |
|
180 | 182 | assert_difference 'Attachment.count', -1 do |
|
181 | 183 | post :destroy, :id => 3 |
|
182 | 184 | assert_response 302 |
|
183 | 185 | end |
|
184 | 186 | end |
|
185 | 187 | |
|
186 | 188 | def test_destroy_project_attachment |
|
187 | 189 | @request.session[:user_id] = 2 |
|
188 | 190 | assert_difference 'Attachment.count', -1 do |
|
189 | 191 | post :destroy, :id => 8 |
|
190 | 192 | assert_response 302 |
|
191 | 193 | end |
|
192 | 194 | end |
|
193 | 195 | |
|
194 | 196 | def test_destroy_version_attachment |
|
195 | 197 | @request.session[:user_id] = 2 |
|
196 | 198 | assert_difference 'Attachment.count', -1 do |
|
197 | 199 | post :destroy, :id => 9 |
|
198 | 200 | assert_response 302 |
|
199 | 201 | end |
|
200 | 202 | end |
|
201 | 203 | |
|
202 | 204 | def test_destroy_without_permission |
|
203 | 205 | post :destroy, :id => 3 |
|
204 | 206 | assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdestroy%2F3' |
|
205 | 207 | assert Attachment.find_by_id(3) |
|
206 | 208 | end |
|
207 | 209 | end |
General Comments 0
You need to be logged in to leave comments.
Login now