##// END OF EJS Templates
Adds a routing test for deleting attachment via the API....
Jean-Philippe Lang -
r15480:30fc03ac3259
parent child
Show More
@@ -1,159 +1,160
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 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 Redmine::ApiTest::ApiRoutingTest < Redmine::ApiTest::Routing
21 21
22 22 def test_attachments
23 23 should_route 'GET /attachments/1' => 'attachments#show', :id => '1'
24 24 should_route 'PATCH /attachments/1' => 'attachments#update', :id => '1'
25 should_route 'DELETE /attachments/1' => 'attachments#destroy', :id => '1'
25 26 should_route 'POST /uploads' => 'attachments#upload'
26 27 end
27 28
28 29 def test_custom_fields
29 30 should_route 'GET /custom_fields' => 'custom_fields#index'
30 31 end
31 32
32 33 def test_enumerations
33 34 should_route 'GET /enumerations/issue_priorities' => 'enumerations#index', :type => 'issue_priorities'
34 35 end
35 36
36 37 def test_groups
37 38 should_route 'GET /groups' => 'groups#index'
38 39 should_route 'POST /groups' => 'groups#create'
39 40
40 41 should_route 'GET /groups/1' => 'groups#show', :id => '1'
41 42 should_route 'PUT /groups/1' => 'groups#update', :id => '1'
42 43 should_route 'DELETE /groups/1' => 'groups#destroy', :id => '1'
43 44 end
44 45
45 46 def test_group_users
46 47 should_route 'POST /groups/567/users' => 'groups#add_users', :id => '567'
47 48 should_route 'DELETE /groups/567/users/12' => 'groups#remove_user', :id => '567', :user_id => '12'
48 49 end
49 50
50 51 def test_issue_categories
51 52 should_route 'GET /projects/foo/issue_categories' => 'issue_categories#index', :project_id => 'foo'
52 53 should_route 'POST /projects/foo/issue_categories' => 'issue_categories#create', :project_id => 'foo'
53 54
54 55 should_route 'GET /issue_categories/1' => 'issue_categories#show', :id => '1'
55 56 should_route 'PUT /issue_categories/1' => 'issue_categories#update', :id => '1'
56 57 should_route 'DELETE /issue_categories/1' => 'issue_categories#destroy', :id => '1'
57 58 end
58 59
59 60 def test_issue_relations
60 61 should_route 'GET /issues/1/relations' => 'issue_relations#index', :issue_id => '1'
61 62 should_route 'POST /issues/1/relations' => 'issue_relations#create', :issue_id => '1'
62 63
63 64 should_route 'GET /relations/23' => 'issue_relations#show', :id => '23'
64 65 should_route 'DELETE /relations/23' => 'issue_relations#destroy', :id => '23'
65 66 end
66 67
67 68 def test_issue_statuses
68 69 should_route 'GET /issue_statuses' => 'issue_statuses#index'
69 70 end
70 71
71 72 def test_issues
72 73 should_route 'GET /issues' => 'issues#index'
73 74 should_route 'POST /issues' => 'issues#create'
74 75
75 76 should_route 'GET /issues/64' => 'issues#show', :id => '64'
76 77 should_route 'PUT /issues/64' => 'issues#update', :id => '64'
77 78 should_route 'DELETE /issues/64' => 'issues#destroy', :id => '64'
78 79 end
79 80
80 81 def test_issue_watchers
81 82 should_route 'POST /issues/12/watchers' => 'watchers#create', :object_type => 'issue', :object_id => '12'
82 83 should_route 'DELETE /issues/12/watchers/3' => 'watchers#destroy', :object_type => 'issue', :object_id => '12', :user_id => '3'
83 84 end
84 85
85 86 def test_memberships
86 87 should_route 'GET /projects/5234/memberships' => 'members#index', :project_id => '5234'
87 88 should_route 'POST /projects/5234/memberships' => 'members#create', :project_id => '5234'
88 89
89 90 should_route 'GET /memberships/5234' => 'members#show', :id => '5234'
90 91 should_route 'PUT /memberships/5234' => 'members#update', :id => '5234'
91 92 should_route 'DELETE /memberships/5234' => 'members#destroy', :id => '5234'
92 93 end
93 94
94 95 def test_news
95 96 should_route 'GET /news' => 'news#index'
96 97 should_route 'GET /projects/567/news' => 'news#index', :project_id => '567'
97 98 end
98 99
99 100 def test_projects
100 101 should_route 'GET /projects' => 'projects#index'
101 102 should_route 'POST /projects' => 'projects#create'
102 103
103 104 should_route 'GET /projects/1' => 'projects#show', :id => '1'
104 105 should_route 'PUT /projects/1' => 'projects#update', :id => '1'
105 106 should_route 'DELETE /projects/1' => 'projects#destroy', :id => '1'
106 107 end
107 108
108 109 def test_queries
109 110 should_route 'GET /queries' => 'queries#index'
110 111 end
111 112
112 113 def test_roles
113 114 should_route 'GET /roles' => 'roles#index'
114 115 should_route 'GET /roles/2' => 'roles#show', :id => '2'
115 116 end
116 117
117 118 def test_time_entries
118 119 should_route 'GET /time_entries' => 'timelog#index'
119 120 should_route 'POST /time_entries' => 'timelog#create'
120 121
121 122 should_route 'GET /time_entries/1' => 'timelog#show', :id => '1'
122 123 should_route 'PUT /time_entries/1' => 'timelog#update', :id => '1'
123 124 should_route 'DELETE /time_entries/1' => 'timelog#destroy', :id => '1'
124 125 end
125 126
126 127 def test_trackers
127 128 should_route 'GET /trackers' => 'trackers#index'
128 129 end
129 130
130 131 def test_users
131 132 should_route 'GET /users' => 'users#index'
132 133 should_route 'POST /users' => 'users#create'
133 134
134 135 should_route 'GET /users/44' => 'users#show', :id => '44'
135 136 should_route 'GET /users/current' => 'users#show', :id => 'current'
136 137 should_route 'PUT /users/44' => 'users#update', :id => '44'
137 138 should_route 'DELETE /users/44' => 'users#destroy', :id => '44'
138 139 end
139 140
140 141 def test_versions
141 142 should_route 'GET /projects/foo/versions' => 'versions#index', :project_id => 'foo'
142 143 should_route 'POST /projects/foo/versions' => 'versions#create', :project_id => 'foo'
143 144
144 145 should_route 'GET /versions/1' => 'versions#show', :id => '1'
145 146 should_route 'PUT /versions/1' => 'versions#update', :id => '1'
146 147 should_route 'DELETE /versions/1' => 'versions#destroy', :id => '1'
147 148 end
148 149
149 150 def test_wiki
150 151 should_route 'GET /projects/567/wiki/index' => 'wiki#index', :project_id => '567'
151 152
152 153 should_route 'GET /projects/567/wiki/my_page' => 'wiki#show', :project_id => '567', :id => 'my_page'
153 154 should_route 'GET /projects/567/wiki/my_page' => 'wiki#show', :project_id => '567', :id => 'my_page'
154 155 should_route 'GET /projects/1/wiki/my_page/2' => 'wiki#show', :project_id => '1', :id => 'my_page', :version => '2'
155 156
156 157 should_route 'PUT /projects/567/wiki/my_page' => 'wiki#update', :project_id => '567', :id => 'my_page'
157 158 should_route 'DELETE /projects/567/wiki/my_page' => 'wiki#destroy', :project_id => '567', :id => 'my_page'
158 159 end
159 160 end
@@ -1,200 +1,200
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 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 Redmine::ApiTest::AttachmentsTest < Redmine::ApiTest::Base
21 21 fixtures :projects, :trackers, :issue_statuses, :issues,
22 22 :enumerations, :users, :issue_categories,
23 23 :projects_trackers,
24 24 :roles,
25 25 :member_roles,
26 26 :members,
27 27 :enabled_modules,
28 28 :attachments
29 29
30 30 def setup
31 31 super
32 32 set_fixtures_attachments_directory
33 33 end
34 34
35 35 def teardown
36 36 super
37 37 set_tmp_attachments_directory
38 38 end
39 39
40 40 test "GET /attachments/:id.xml should return the attachment" do
41 41 get '/attachments/7.xml', {}, credentials('jsmith')
42 42 assert_response :success
43 43 assert_equal 'application/xml', @response.content_type
44 44 assert_select 'attachment id', :text => '7' do
45 45 assert_select '~ filename', :text => 'archive.zip'
46 46 assert_select '~ content_url', :text => 'http://www.example.com/attachments/download/7/archive.zip'
47 47 end
48 48 end
49 49
50 50 test "GET /attachments/:id.xml for image should include thumbnail_url" do
51 51 get '/attachments/16.xml', {}, credentials('jsmith')
52 52 assert_response :success
53 53 assert_equal 'application/xml', @response.content_type
54 54 assert_select 'attachment id:contains(16)' do
55 55 assert_select '~ thumbnail_url', :text => 'http://www.example.com/attachments/thumbnail/16'
56 56 end
57 57 end
58 58
59 59 test "GET /attachments/:id.xml should deny access without credentials" do
60 60 get '/attachments/7.xml'
61 61 assert_response 401
62 62 set_tmp_attachments_directory
63 63 end
64 64
65 65 test "GET /attachments/download/:id/:filename should return the attachment content" do
66 66 get '/attachments/download/7/archive.zip', {}, credentials('jsmith')
67 67 assert_response :success
68 68 assert_equal 'application/zip', @response.content_type
69 69 set_tmp_attachments_directory
70 70 end
71 71
72 72 test "GET /attachments/download/:id/:filename should deny access without credentials" do
73 73 get '/attachments/download/7/archive.zip'
74 74 assert_response 302
75 75 set_tmp_attachments_directory
76 76 end
77 77
78 78 test "GET /attachments/thumbnail/:id should return the thumbnail" do
79 79 skip unless convert_installed?
80 80 get '/attachments/thumbnail/16', {}, credentials('jsmith')
81 81 assert_response :success
82 82 end
83 83
84 test "Destroy /attachments/:id.xml should return ok and deleted Attachment" do
84 test "DELETE /attachments/:id.xml should return ok and delete Attachment" do
85 85 assert_difference 'Attachment.count', -1 do
86 86 delete '/attachments/7.xml', {}, credentials('jsmith')
87 87 assert_response :ok
88 88 assert_equal '', response.body
89 89 end
90 90 assert_nil Attachment.find_by_id(7)
91 91 end
92 92
93 test "Destroy /attachments/:id.json should return ok and deleted Attachment" do
93 test "DELETE /attachments/:id.json should return ok and delete Attachment" do
94 94 assert_difference 'Attachment.count', -1 do
95 95 delete '/attachments/7.json', {}, credentials('jsmith')
96 96 assert_response :ok
97 97 assert_equal '', response.body
98 98 end
99 99 assert_nil Attachment.find_by_id(7)
100 100 end
101 101
102 102 test "PATCH /attachments/:id.json should update the attachment" do
103 103 patch '/attachments/7.json',
104 104 {:attachment => {:filename => 'renamed.zip', :description => 'updated'}},
105 105 credentials('jsmith')
106 106
107 107 assert_response :ok
108 108 assert_equal 'application/json', response.content_type
109 109 attachment = Attachment.find(7)
110 110 assert_equal 'renamed.zip', attachment.filename
111 111 assert_equal 'updated', attachment.description
112 112 end
113 113
114 114 test "PATCH /attachments/:id.json with failure should return the errors" do
115 115 patch '/attachments/7.json',
116 116 {:attachment => {:filename => '', :description => 'updated'}},
117 117 credentials('jsmith')
118 118
119 119 assert_response 422
120 120 assert_equal 'application/json', response.content_type
121 121 json = ActiveSupport::JSON.decode(response.body)
122 122 assert_include "File cannot be blank", json['errors']
123 123 end
124 124
125 125 test "POST /uploads.xml should return the token" do
126 126 set_tmp_attachments_directory
127 127 assert_difference 'Attachment.count' do
128 128 post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
129 129 assert_response :created
130 130 assert_equal 'application/xml', response.content_type
131 131 end
132 132
133 133 xml = Hash.from_xml(response.body)
134 134 assert_kind_of Hash, xml['upload']
135 135 token = xml['upload']['token']
136 136 assert_not_nil token
137 137 attachment_id = xml['upload']['id']
138 138 assert_not_nil attachment_id
139 139
140 140 attachment = Attachment.order('id DESC').first
141 141 assert_equal token, attachment.token
142 142 assert_equal attachment_id, attachment.id.to_s
143 143 assert_nil attachment.container
144 144 assert_equal 2, attachment.author_id
145 145 assert_equal 'File content'.size, attachment.filesize
146 146 assert attachment.content_type.blank?
147 147 assert attachment.filename.present?
148 148 assert_match /\d+_[0-9a-z]+/, attachment.diskfile
149 149 assert File.exist?(attachment.diskfile)
150 150 assert_equal 'File content', File.read(attachment.diskfile)
151 151 end
152 152
153 153 test "POST /uploads.json should return the token" do
154 154 set_tmp_attachments_directory
155 155 assert_difference 'Attachment.count' do
156 156 post '/uploads.json', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
157 157 assert_response :created
158 158 assert_equal 'application/json', response.content_type
159 159 end
160 160
161 161 json = ActiveSupport::JSON.decode(response.body)
162 162 assert_kind_of Hash, json['upload']
163 163 token = json['upload']['token']
164 164 assert_not_nil token
165 165
166 166 attachment = Attachment.order('id DESC').first
167 167 assert_equal token, attachment.token
168 168 end
169 169
170 170 test "POST /uploads.xml should accept :filename param as the attachment filename" do
171 171 set_tmp_attachments_directory
172 172 assert_difference 'Attachment.count' do
173 173 post '/uploads.xml?filename=test.txt', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
174 174 assert_response :created
175 175 end
176 176
177 177 attachment = Attachment.order('id DESC').first
178 178 assert_equal 'test.txt', attachment.filename
179 179 assert_match /_test\.txt$/, attachment.diskfile
180 180 end
181 181
182 182 test "POST /uploads.xml should not accept other content types" do
183 183 set_tmp_attachments_directory
184 184 assert_no_difference 'Attachment.count' do
185 185 post '/uploads.xml', 'PNG DATA', {"CONTENT_TYPE" => 'image/png'}.merge(credentials('jsmith'))
186 186 assert_response 406
187 187 end
188 188 end
189 189
190 190 test "POST /uploads.xml should return errors if file is too big" do
191 191 set_tmp_attachments_directory
192 192 with_settings :attachment_max_size => 1 do
193 193 assert_no_difference 'Attachment.count' do
194 194 post '/uploads.xml', ('x' * 2048), {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
195 195 assert_response 422
196 196 assert_select 'error', :text => /exceeds the maximum allowed file size/
197 197 end
198 198 end
199 199 end
200 200 end
General Comments 0
You need to be logged in to leave comments. Login now