|
1 |
# Redmine - project management software
|
|
2 |
# Copyright (C) 2015 Savoir-Faire Linux Inc.
|
|
3 |
# Author: Lucile Quirion <lucile.quirion@savoirfairelinux.com>
|
|
4 |
#
|
|
5 |
# This program is free software; you can redistribute it and/or
|
|
6 |
# modify it under the terms of the GNU General Public License
|
|
7 |
# as published by the Free Software Foundation; either version 2
|
|
8 |
# of the License, or (at your option) any later version.
|
|
9 |
#
|
|
10 |
# This program is distributed in the hope that it will be useful,
|
|
11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
# GNU General Public License for more details.
|
|
14 |
#
|
|
15 |
# You should have received a copy of the GNU General Public License
|
|
16 |
# along with this program; if not, write to the Free Software
|
|
17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
18 |
|
|
19 |
require File.expand_path('../../../test_helper', __FILE__)
|
|
20 |
|
|
21 |
class Redmine::ApiTest::FilesTest < Redmine::ApiTest::Base
|
|
22 |
fixtures :projects,
|
|
23 |
:users,
|
|
24 |
:members,
|
|
25 |
:roles,
|
|
26 |
:member_roles,
|
|
27 |
:enabled_modules,
|
|
28 |
:attachments,
|
|
29 |
:versions
|
|
30 |
|
|
31 |
test "GET /projects/:project_id/files.xml should return the list of uploaded files" do
|
|
32 |
get '/projects/1/files.xml', {}, credentials('jsmith')
|
|
33 |
assert_response :success
|
|
34 |
assert_select 'files file id', :text => '8'
|
|
35 |
end
|
|
36 |
|
|
37 |
test "POST /projects/:project_id/files.json should create a file" do
|
|
38 |
set_tmp_attachments_directory
|
|
39 |
post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
|
|
40 |
token = Attachment.last.token
|
|
41 |
payload = <<-JSON
|
|
42 |
{ "attachments":
|
|
43 |
{ "file_1":
|
|
44 |
{"token": "#{token}" }
|
|
45 |
}
|
|
46 |
}
|
|
47 |
JSON
|
|
48 |
post '/projects/1/files.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
|
|
49 |
assert_response :success
|
|
50 |
assert_equal 1, Attachment.last.container_id
|
|
51 |
assert_equal "Project", Attachment.last.container_type
|
|
52 |
end
|
|
53 |
|
|
54 |
test "POST /projects/:project_id/files.xml should create a file" do
|
|
55 |
set_tmp_attachments_directory
|
|
56 |
post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
|
|
57 |
token = Attachment.last.token
|
|
58 |
payload = <<-XML
|
|
59 |
<attachments>
|
|
60 |
<file_1>
|
|
61 |
<token>#{token}</token>
|
|
62 |
</file_1>
|
|
63 |
</attachments>
|
|
64 |
XML
|
|
65 |
post '/projects/1/files.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
|
|
66 |
assert_response :success
|
|
67 |
assert_equal 1, Attachment.last.container_id
|
|
68 |
assert_equal "Project", Attachment.last.container_type
|
|
69 |
end
|
|
70 |
|
|
71 |
test "POST /projects/:project_id/files.json should accept :filename, :description, :content_type as parameters" do
|
|
72 |
set_tmp_attachments_directory
|
|
73 |
post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
|
|
74 |
token = Attachment.last.token
|
|
75 |
payload = <<-JSON
|
|
76 |
{ "attachments":
|
|
77 |
{ "file_1":
|
|
78 |
{ "filename": "New filename",
|
|
79 |
"description": "New description",
|
|
80 |
"content_type": "application/txt",
|
|
81 |
"token": "#{token}"
|
|
82 |
}
|
|
83 |
}
|
|
84 |
}
|
|
85 |
JSON
|
|
86 |
post '/projects/1/files.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
|
|
87 |
assert_response :success
|
|
88 |
assert_equal "New filename", Attachment.last.filename
|
|
89 |
assert_equal "New description", Attachment.last.description
|
|
90 |
assert_equal "application/txt", Attachment.last.content_type
|
|
91 |
end
|
|
92 |
|
|
93 |
test "POST /projects/:project_id/versions/:version_id/files.json should attach an attachment to a version" do
|
|
94 |
set_tmp_attachments_directory
|
|
95 |
post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
|
|
96 |
token = Attachment.last.token
|
|
97 |
payload = <<-JSON
|
|
98 |
{ "attachments":
|
|
99 |
{ "1":
|
|
100 |
{ "token": "#{token}" }
|
|
101 |
}
|
|
102 |
}
|
|
103 |
JSON
|
|
104 |
post '/projects/1/versions/3/files.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
|
|
105 |
assert_equal 3, Attachment.last.container_id
|
|
106 |
assert_equal "Version", Attachment.last.container_type
|
|
107 |
end
|
|
108 |
|
|
109 |
test "POST /projects/:project_id/files.json should accept :version_id to attach the file to a version" do
|
|
110 |
set_tmp_attachments_directory
|
|
111 |
post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
|
|
112 |
token = Attachment.last.token
|
|
113 |
payload = <<-JSON
|
|
114 |
{ "version_id": 3,
|
|
115 |
"attachments":
|
|
116 |
{ "1":
|
|
117 |
{ "filename": "New filename",
|
|
118 |
"description": "New description",
|
|
119 |
"token": "#{token}"
|
|
120 |
}
|
|
121 |
}
|
|
122 |
}
|
|
123 |
JSON
|
|
124 |
post '/projects/1/files.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
|
|
125 |
assert_equal 3, Attachment.last.container_id
|
|
126 |
assert_equal "Version", Attachment.last.container_type
|
|
127 |
end
|
|
128 |
end
|