|
1 |
# frozen_string_literal: true
|
|
2 |
|
|
3 |
# Redmine - project management software
|
|
4 |
# Copyright (C) 2006- 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_relative '../../test_helper'
|
|
21 |
|
|
22 |
class Redmine::ApiTest::DocumentsTest < Redmine::ApiTest::Base
|
|
23 |
test "GET /projects/:project_id/documents.xml should return documents" do
|
|
24 |
get '/projects/ecookbook/documents.xml'
|
|
25 |
|
|
26 |
assert_response :success
|
|
27 |
assert_equal 'application/xml', response.media_type
|
|
28 |
assert_select 'documents[type=array][total_count=?]', Project.find(1).documents.count.to_s do
|
|
29 |
assert_select 'document', 3
|
|
30 |
# documents are sorted by descending creation date
|
|
31 |
assert_select 'document:first-of-type' do
|
|
32 |
assert_select 'id', :text => '3'
|
|
33 |
assert_select 'project[id=1][name=eCookbook]'
|
|
34 |
assert_select 'category[id=3][name="Technical documentation"]'
|
|
35 |
assert_select 'title', :text => 'An other document 2'
|
|
36 |
assert_select 'created_on', :text => Document.find(3).created_on.iso8601
|
|
37 |
assert_select 'updated_on', :text => Document.find(3).updated_on.iso8601
|
|
38 |
end
|
|
39 |
end
|
|
40 |
end
|
|
41 |
|
|
42 |
test "GET /projects/:project_id/documents.json should return documents" do
|
|
43 |
get '/projects/ecookbook/documents.json'
|
|
44 |
|
|
45 |
assert_response :success
|
|
46 |
json = ActiveSupport::JSON.decode(response.body)
|
|
47 |
assert_kind_of Hash, json
|
|
48 |
assert_kind_of Array, json['documents']
|
|
49 |
assert_equal Project.find(1).documents.count, json['total_count']
|
|
50 |
# documents are sorted by descending creation date
|
|
51 |
assert_equal 3, json['documents'].first['id']
|
|
52 |
assert_equal({'id' => 3, 'name' => 'Technical documentation'}, json['documents'].first['category'])
|
|
53 |
end
|
|
54 |
|
|
55 |
test "GET /projects/:project_id/documents.xml should paginate" do
|
|
56 |
get '/projects/ecookbook/documents.xml', :params => {:limit => 2, :offset => 1}
|
|
57 |
|
|
58 |
assert_response :success
|
|
59 |
assert_select 'documents[type=array][total_count="3"][offset="1"][limit="2"]' do
|
|
60 |
assert_select 'document', 2
|
|
61 |
assert_select 'document id', :text => '2'
|
|
62 |
assert_select 'document id', :text => '1'
|
|
63 |
end
|
|
64 |
end
|
|
65 |
|
|
66 |
test "GET /projects/:project_id/documents.xml should deny access without permission" do
|
|
67 |
Role.anonymous.remove_permission! :view_documents
|
|
68 |
|
|
69 |
get '/projects/ecookbook/documents.xml'
|
|
70 |
assert_response :unauthorized
|
|
71 |
end
|
|
72 |
|
|
73 |
test "GET /projects/:project_id/documents.xml without permission should return 403" do
|
|
74 |
Role.find(1).remove_permission! :view_documents
|
|
75 |
|
|
76 |
get '/projects/ecookbook/documents.xml', :headers => credentials('jsmith')
|
|
77 |
|
|
78 |
assert_response :forbidden
|
|
79 |
end
|
|
80 |
|
|
81 |
test "GET /documents/:id.xml should return the document" do
|
|
82 |
get '/documents/1.xml'
|
|
83 |
|
|
84 |
assert_response :success
|
|
85 |
assert_equal 'application/xml', response.media_type
|
|
86 |
assert_select 'document' do
|
|
87 |
assert_select 'id', :text => '1'
|
|
88 |
assert_select 'project[id=1][name=eCookbook]'
|
|
89 |
assert_select 'category[id=1][name=Uncategorized]'
|
|
90 |
assert_select 'title', :text => 'Test document'
|
|
91 |
assert_select 'description', :text => 'Document description'
|
|
92 |
assert_select 'created_on', :text => Document.find(1).created_on.iso8601
|
|
93 |
assert_select 'updated_on', :text => Document.find(1).updated_on.iso8601
|
|
94 |
assert_select 'attachments', 0
|
|
95 |
end
|
|
96 |
end
|
|
97 |
|
|
98 |
test "GET /documents/:id.json should return the document" do
|
|
99 |
get '/documents/1.json'
|
|
100 |
|
|
101 |
assert_response :success
|
|
102 |
json = ActiveSupport::JSON.decode(response.body)
|
|
103 |
assert_kind_of Hash, json['document']
|
|
104 |
assert_equal 1, json['document']['id']
|
|
105 |
assert_equal 'Test document', json['document']['title']
|
|
106 |
end
|
|
107 |
|
|
108 |
test "GET /documents/:id.xml with include=attachments should include attachments" do
|
|
109 |
get '/documents/1.xml?include=attachments'
|
|
110 |
|
|
111 |
assert_select 'document attachments[type=array]' do
|
|
112 |
assert_select 'attachment', 2
|
|
113 |
assert_select 'attachment id', :text => '2' do
|
|
114 |
assert_select '~ filename', :text => 'document.txt'
|
|
115 |
assert_select '~ content_url',
|
|
116 |
:text => 'http://www.example.com/attachments/download/2/document.txt'
|
|
117 |
end
|
|
118 |
end
|
|
119 |
end
|
|
120 |
|
|
121 |
test "GET /documents/:id.xml should include custom fields" do
|
|
122 |
field = DocumentCustomField.generate!(:name => 'Author', :field_format => 'string')
|
|
123 |
document = Document.find(1)
|
|
124 |
document.custom_field_values = {field.id => 'John Smith'}
|
|
125 |
document.save!
|
|
126 |
|
|
127 |
get '/documents/1.xml'
|
|
128 |
|
|
129 |
assert_response :success
|
|
130 |
assert_select 'document custom_fields[type=array]' do
|
|
131 |
assert_select "custom_field[id=\"#{field.id}\"][name=Author]" do
|
|
132 |
assert_select 'value', :text => 'John Smith'
|
|
133 |
end
|
|
134 |
end
|
|
135 |
end
|
|
136 |
|
|
137 |
test "GET /documents/:id.xml on a private project without credentials should return 401" do
|
|
138 |
Document.find(1).project.update_column :is_public, false
|
|
139 |
|
|
140 |
get '/documents/1.xml'
|
|
141 |
|
|
142 |
assert_response :unauthorized
|
|
143 |
end
|
|
144 |
|
|
145 |
test "POST /projects/:project_id/documents.xml should create a document with the attributes" do
|
|
146 |
payload = <<~XML
|
|
147 |
<?xml version="1.0" encoding="UTF-8" ?>
|
|
148 |
<document>
|
|
149 |
<title>API document</title>
|
|
150 |
<description>This is a document created by the API</description>
|
|
151 |
<category_id>2</category_id>
|
|
152 |
</document>
|
|
153 |
XML
|
|
154 |
assert_difference('Document.count') do
|
|
155 |
post(
|
|
156 |
'/projects/1/documents.xml',
|
|
157 |
:params => payload,
|
|
158 |
:headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith')))
|
|
159 |
end
|
|
160 |
assert_response :created
|
|
161 |
assert_equal 'application/xml', response.media_type
|
|
162 |
|
|
163 |
document = Document.order(:id => :desc).first
|
|
164 |
assert_equal 'API document', document.title
|
|
165 |
assert_equal 'This is a document created by the API', document.description
|
|
166 |
assert_equal DocumentCategory.find(2), document.category
|
|
167 |
assert_equal Project.find(1), document.project
|
|
168 |
assert_equal document_url(document), response.headers['Location']
|
|
169 |
assert_select 'document id', :text => document.id.to_s
|
|
170 |
end
|
|
171 |
|
|
172 |
test "POST /projects/:project_id/documents.json should create a document with the attributes" do
|
|
173 |
payload = <<~JSON
|
|
174 |
{
|
|
175 |
"document": {
|
|
176 |
"title": "API document",
|
|
177 |
"description": "This is a document created by the API",
|
|
178 |
"category_id": 2
|
|
179 |
}
|
|
180 |
}
|
|
181 |
JSON
|
|
182 |
assert_difference('Document.count') do
|
|
183 |
post(
|
|
184 |
'/projects/1/documents.json',
|
|
185 |
:params => payload,
|
|
186 |
:headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith')))
|
|
187 |
end
|
|
188 |
assert_response :created
|
|
189 |
|
|
190 |
document = Document.order(:id => :desc).first
|
|
191 |
assert_equal 'API document', document.title
|
|
192 |
assert_equal 'This is a document created by the API', document.description
|
|
193 |
|
|
194 |
json = ActiveSupport::JSON.decode(response.body)
|
|
195 |
assert_equal document.id, json['document']['id']
|
|
196 |
end
|
|
197 |
|
|
198 |
test "POST /projects/:project_id/documents.json with failure should return errors" do
|
|
199 |
assert_no_difference('Document.count') do
|
|
200 |
post(
|
|
201 |
'/projects/1/documents.json',
|
|
202 |
:params => {:document => {:title => '', :category_id => 1}},
|
|
203 |
:headers => credentials('jsmith'))
|
|
204 |
end
|
|
205 |
assert_response :unprocessable_content
|
|
206 |
json = ActiveSupport::JSON.decode(response.body)
|
|
207 |
assert json['errors'].include?("Title cannot be blank")
|
|
208 |
end
|
|
209 |
|
|
210 |
test "POST /projects/:project_id/documents.json with attachment should create a document with attachment" do
|
|
211 |
token = json_upload('test_create_with_attachment', credentials('jsmith'))
|
|
212 |
attachment = Attachment.find_by_token(token)
|
|
213 |
assert_difference 'Document.count' do
|
|
214 |
post(
|
|
215 |
'/projects/1/documents.json',
|
|
216 |
:params => {:document => {:title => 'API document with attachment',
|
|
217 |
:category_id => 1,
|
|
218 |
:uploads => [{:token => token, :filename => 'test.txt',
|
|
219 |
:content_type => 'text/plain'}]}},
|
|
220 |
:headers => credentials('jsmith'))
|
|
221 |
assert_response :created
|
|
222 |
end
|
|
223 |
document = Document.order(:id => :desc).first
|
|
224 |
assert_equal 'API document with attachment', document.title
|
|
225 |
assert_equal attachment, document.attachments.first
|
|
226 |
|
|
227 |
attachment.reload
|
|
228 |
assert_equal 'test.txt', attachment.filename
|
|
229 |
assert_equal 'text/plain', attachment.content_type
|
|
230 |
assert_equal 'test_create_with_attachment'.size, attachment.filesize
|
|
231 |
assert_equal 2, attachment.author_id
|
|
232 |
end
|
|
233 |
|
|
234 |
test "POST /projects/:project_id/documents.xml with attachment should create a document with attachment" do
|
|
235 |
token = xml_upload('test_create_with_attachment', credentials('jsmith'))
|
|
236 |
payload = <<~XML
|
|
237 |
<?xml version="1.0" encoding="UTF-8" ?>
|
|
238 |
<document>
|
|
239 |
<title>API document with attachment</title>
|
|
240 |
<category_id>1</category_id>
|
|
241 |
<uploads type="array">
|
|
242 |
<upload>
|
|
243 |
<token>#{token}</token>
|
|
244 |
<filename>test.txt</filename>
|
|
245 |
</upload>
|
|
246 |
</uploads>
|
|
247 |
</document>
|
|
248 |
XML
|
|
249 |
assert_difference 'Document.count' do
|
|
250 |
post(
|
|
251 |
'/projects/1/documents.xml',
|
|
252 |
:params => payload,
|
|
253 |
:headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith')))
|
|
254 |
assert_response :created
|
|
255 |
end
|
|
256 |
document = Document.order(:id => :desc).first
|
|
257 |
assert_equal ['test.txt'], document.attachments.map(&:filename)
|
|
258 |
end
|
|
259 |
|
|
260 |
test "POST /projects/:project_id/documents.xml without permission should return 403" do
|
|
261 |
Role.find(1).remove_permission! :add_documents
|
|
262 |
|
|
263 |
assert_no_difference 'Document.count' do
|
|
264 |
post(
|
|
265 |
'/projects/1/documents.xml',
|
|
266 |
:params => {:document => {:title => 'API document'}},
|
|
267 |
:headers => credentials('jsmith'))
|
|
268 |
end
|
|
269 |
assert_response :forbidden
|
|
270 |
end
|
|
271 |
|
|
272 |
test "PUT /documents/:id.xml should update the document" do
|
|
273 |
payload = <<~XML
|
|
274 |
<?xml version="1.0" encoding="UTF-8" ?>
|
|
275 |
<document>
|
|
276 |
<title>Updated title</title>
|
|
277 |
<description>Updated description</description>
|
|
278 |
<category_id>2</category_id>
|
|
279 |
</document>
|
|
280 |
XML
|
|
281 |
put(
|
|
282 |
'/documents/1.xml',
|
|
283 |
:params => payload,
|
|
284 |
:headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith')))
|
|
285 |
|
|
286 |
assert_response :no_content
|
|
287 |
assert_equal '', response.body
|
|
288 |
document = Document.find(1)
|
|
289 |
assert_equal 'Updated title', document.title
|
|
290 |
assert_equal 'Updated description', document.description
|
|
291 |
assert_equal DocumentCategory.find(2), document.category
|
|
292 |
end
|
|
293 |
|
|
294 |
test "PUT /documents/:id.xml with failure should return errors" do
|
|
295 |
put(
|
|
296 |
'/documents/1.xml',
|
|
297 |
:params => {:document => {:title => ''}},
|
|
298 |
:headers => credentials('jsmith'))
|
|
299 |
|
|
300 |
assert_response :unprocessable_content
|
|
301 |
assert_select 'errors error', :text => "Title cannot be blank"
|
|
302 |
end
|
|
303 |
|
|
304 |
test "PUT /documents/:id.json should update the document" do
|
|
305 |
put(
|
|
306 |
'/documents/1.json',
|
|
307 |
:params => {:document => {:title => 'Updated title'}},
|
|
308 |
:headers => credentials('jsmith'))
|
|
309 |
|
|
310 |
assert_response :no_content
|
|
311 |
assert_equal 'Updated title', Document.find(1).title
|
|
312 |
end
|
|
313 |
|
|
314 |
test "PUT /documents/:id.json with failure should return errors" do
|
|
315 |
put(
|
|
316 |
'/documents/1.json',
|
|
317 |
:params => {:document => {:title => ''}},
|
|
318 |
:headers => credentials('jsmith'))
|
|
319 |
|
|
320 |
assert_response :unprocessable_content
|
|
321 |
json = ActiveSupport::JSON.decode(response.body)
|
|
322 |
assert json['errors'].include?("Title cannot be blank")
|
|
323 |
end
|
|
324 |
|
|
325 |
test "PUT /documents/:id.json with attachment should add the attachment" do
|
|
326 |
token = json_upload('test_update_with_attachment', credentials('jsmith'))
|
|
327 |
|
|
328 |
put(
|
|
329 |
'/documents/2.json',
|
|
330 |
:params => {:document => {:uploads => [{:token => token, :filename => 'test.txt',
|
|
331 |
:content_type => 'text/plain'}]}},
|
|
332 |
:headers => credentials('jsmith'))
|
|
333 |
|
|
334 |
assert_response :no_content
|
|
335 |
assert_equal ['test.txt'], Document.find(2).attachments.map(&:filename)
|
|
336 |
end
|
|
337 |
|
|
338 |
test "PUT /documents/:id.xml with attachment should add the attachment" do
|
|
339 |
token = xml_upload('test_update_with_attachment', credentials('jsmith'))
|
|
340 |
payload = <<~XML
|
|
341 |
<?xml version="1.0" encoding="UTF-8" ?>
|
|
342 |
<document>
|
|
343 |
<uploads type="array">
|
|
344 |
<upload>
|
|
345 |
<token>#{token}</token>
|
|
346 |
<filename>test.txt</filename>
|
|
347 |
</upload>
|
|
348 |
</uploads>
|
|
349 |
</document>
|
|
350 |
XML
|
|
351 |
put(
|
|
352 |
'/documents/2.xml',
|
|
353 |
:params => payload,
|
|
354 |
:headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith')))
|
|
355 |
|
|
356 |
assert_response :no_content
|
|
357 |
assert_equal ['test.txt'], Document.find(2).attachments.map(&:filename)
|
|
358 |
end
|
|
359 |
|
|
360 |
test "PUT /documents/:id.xml without permission should return 403" do
|
|
361 |
Role.find(1).remove_permission! :edit_documents
|
|
362 |
|
|
363 |
put(
|
|
364 |
'/documents/1.xml',
|
|
365 |
:params => {:document => {:title => 'Updated title'}},
|
|
366 |
:headers => credentials('jsmith'))
|
|
367 |
|
|
368 |
assert_response :forbidden
|
|
369 |
end
|
|
370 |
|
|
371 |
test "DELETE /documents/:id.xml should delete the document" do
|
|
372 |
assert_difference('Document.count', -1) do
|
|
373 |
delete '/documents/1.xml', :headers => credentials('jsmith')
|
|
374 |
end
|
|
375 |
|
|
376 |
assert_response :no_content
|
|
377 |
assert_equal '', response.body
|
|
378 |
assert_nil Document.find_by_id(1)
|
|
379 |
end
|
|
380 |
|
|
381 |
test "DELETE /documents/:id.json should delete the document" do
|
|
382 |
assert_difference('Document.count', -1) do
|
|
383 |
delete '/documents/1.json', :headers => credentials('jsmith')
|
|
384 |
end
|
|
385 |
|
|
386 |
assert_response :no_content
|
|
387 |
assert_nil Document.find_by_id(1)
|
|
388 |
end
|
|
389 |
|
|
390 |
test "DELETE /documents/:id.xml without permission should return 403" do
|
|
391 |
Role.find(1).remove_permission! :delete_documents
|
|
392 |
|
|
393 |
assert_no_difference 'Document.count' do
|
|
394 |
delete '/documents/1.xml', :headers => credentials('jsmith')
|
|
395 |
end
|
|
396 |
|
|
397 |
assert_response :forbidden
|
|
398 |
end
|
|
399 |
end
|