comparison test/integration/api_test/projects_test.rb @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents 8661b858af72
children 433d4f72a19b
comparison
equal deleted inserted replaced
908:c6c2cbd0afee 909:cbb26bc654de
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2010 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 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 ApiTest::ProjectsTest < ActionController::IntegrationTest 20 class ApiTest::ProjectsTest < ActionController::IntegrationTest
21 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details, 21 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages, 22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
23 :attachments, :custom_fields, :custom_values, :time_entries 23 :attachments, :custom_fields, :custom_values, :time_entries, :issue_categories
24 24
25 def setup 25 def setup
26 Setting.rest_api_enabled = '1' 26 Setting.rest_api_enabled = '1'
27 end 27 set_tmp_attachments_directory
28 28 end
29
29 context "GET /projects" do 30 context "GET /projects" do
30 context ".xml" do 31 context ".xml" do
31 should "return projects" do 32 should "return projects" do
32 get '/projects.xml' 33 get '/projects.xml'
33 assert_response :success 34 assert_response :success
34 assert_equal 'application/xml', @response.content_type 35 assert_equal 'application/xml', @response.content_type
35 36
36 assert_tag :tag => 'projects', 37 assert_tag :tag => 'projects',
37 :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}} 38 :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}}
38 end 39 end
39 end 40 end
40 41
41 context ".json" do 42 context ".json" do
42 should "return projects" do 43 should "return projects" do
43 get '/projects.json' 44 get '/projects.json'
44 assert_response :success 45 assert_response :success
45 assert_equal 'application/json', @response.content_type 46 assert_equal 'application/json', @response.content_type
46 47
47 json = ActiveSupport::JSON.decode(response.body) 48 json = ActiveSupport::JSON.decode(response.body)
48 assert_kind_of Hash, json 49 assert_kind_of Hash, json
49 assert_kind_of Array, json['projects'] 50 assert_kind_of Array, json['projects']
50 assert_kind_of Hash, json['projects'].first 51 assert_kind_of Hash, json['projects'].first
51 assert json['projects'].first.has_key?('id') 52 assert json['projects'].first.has_key?('id')
52 end 53 end
53 end 54 end
54 end 55 end
55 56
56 context "GET /projects/:id" do 57 context "GET /projects/:id" do
57 context ".xml" do 58 context ".xml" do
58 # TODO: A private project is needed because should_allow_api_authentication 59 # TODO: A private project is needed because should_allow_api_authentication
59 # actually tests that authentication is *required*, not just allowed 60 # actually tests that authentication is *required*, not just allowed
60 should_allow_api_authentication(:get, "/projects/2.xml") 61 should_allow_api_authentication(:get, "/projects/2.xml")
61 62
62 should "return requested project" do 63 should "return requested project" do
63 get '/projects/1.xml' 64 get '/projects/1.xml'
64 assert_response :success 65 assert_response :success
65 assert_equal 'application/xml', @response.content_type 66 assert_equal 'application/xml', @response.content_type
66 67
67 assert_tag :tag => 'project', 68 assert_tag :tag => 'project',
68 :child => {:tag => 'id', :content => '1'} 69 :child => {:tag => 'id', :content => '1'}
69 assert_tag :tag => 'custom_field', 70 assert_tag :tag => 'custom_field',
70 :attributes => {:name => 'Development status'}, :content => 'Stable' 71 :attributes => {:name => 'Development status'}, :content => 'Stable'
71 end 72
72 73 assert_no_tag 'trackers'
74 assert_no_tag 'issue_categories'
75 end
76
73 context "with hidden custom fields" do 77 context "with hidden custom fields" do
74 setup do 78 setup do
75 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false 79 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
76 end 80 end
77 81
78 should "not display hidden custom fields" do 82 should "not display hidden custom fields" do
79 get '/projects/1.xml' 83 get '/projects/1.xml'
80 assert_response :success 84 assert_response :success
81 assert_equal 'application/xml', @response.content_type 85 assert_equal 'application/xml', @response.content_type
82 86
83 assert_no_tag 'custom_field', 87 assert_no_tag 'custom_field',
84 :attributes => {:name => 'Development status'} 88 :attributes => {:name => 'Development status'}
85 end 89 end
86 end 90 end
91
92 should "return categories with include=issue_categories" do
93 get '/projects/1.xml?include=issue_categories'
94 assert_response :success
95 assert_equal 'application/xml', @response.content_type
96
97 assert_tag 'issue_categories',
98 :attributes => {:type => 'array'},
99 :child => {
100 :tag => 'issue_category',
101 :attributes => {
102 :id => '2',
103 :name => 'Recipes'
104 }
105 }
106 end
107
108 should "return trackers with include=trackers" do
109 get '/projects/1.xml?include=trackers'
110 assert_response :success
111 assert_equal 'application/xml', @response.content_type
112
113 assert_tag 'trackers',
114 :attributes => {:type => 'array'},
115 :child => {
116 :tag => 'tracker',
117 :attributes => {
118 :id => '2',
119 :name => 'Feature request'
120 }
121 }
122 end
87 end 123 end
88 124
89 context ".json" do 125 context ".json" do
90 should_allow_api_authentication(:get, "/projects/2.json") 126 should_allow_api_authentication(:get, "/projects/2.json")
91 127
92 should "return requested project" do 128 should "return requested project" do
93 get '/projects/1.json' 129 get '/projects/1.json'
94 130
95 json = ActiveSupport::JSON.decode(response.body) 131 json = ActiveSupport::JSON.decode(response.body)
96 assert_kind_of Hash, json 132 assert_kind_of Hash, json
97 assert_kind_of Hash, json['project'] 133 assert_kind_of Hash, json['project']
98 assert_equal 1, json['project']['id'] 134 assert_equal 1, json['project']['id']
99 end 135 end
100 end 136 end
101 end 137 end
102 138
103 context "POST /projects" do 139 context "POST /projects" do
104 context "with valid parameters" do 140 context "with valid parameters" do
105 setup do 141 setup do
106 Setting.default_projects_modules = ['issue_tracking', 'repository'] 142 Setting.default_projects_modules = ['issue_tracking', 'repository']
107 @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}} 143 @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}}
108 end 144 end
109 145
110 context ".xml" do 146 context ".xml" do
111 should_allow_api_authentication(:post, 147 should_allow_api_authentication(:post,
112 '/projects.xml', 148 '/projects.xml',
113 {:project => {:name => 'API test', :identifier => 'api-test'}}, 149 {:project => {:name => 'API test', :identifier => 'api-test'}},
114 {:success_code => :created}) 150 {:success_code => :created})
115 151
116 152
117 should "create a project with the attributes" do 153 should "create a project with the attributes" do
118 assert_difference('Project.count') do 154 assert_difference('Project.count') do
119 post '/projects.xml', @parameters, :authorization => credentials('admin') 155 post '/projects.xml', @parameters, :authorization => credentials('admin')
120 end 156 end
121 157
122 project = Project.first(:order => 'id DESC') 158 project = Project.first(:order => 'id DESC')
123 assert_equal 'API test', project.name 159 assert_equal 'API test', project.name
124 assert_equal 'api-test', project.identifier 160 assert_equal 'api-test', project.identifier
125 assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort 161 assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort
126 assert_equal Tracker.all.size, project.trackers.size 162 assert_equal Tracker.all.size, project.trackers.size
127 163
128 assert_response :created 164 assert_response :created
129 assert_equal 'application/xml', @response.content_type 165 assert_equal 'application/xml', @response.content_type
130 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s} 166 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
131 end 167 end
132 168
133 should "accept enabled_module_names attribute" do 169 should "accept enabled_module_names attribute" do
134 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}) 170 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
135 171
136 assert_difference('Project.count') do 172 assert_difference('Project.count') do
137 post '/projects.xml', @parameters, :authorization => credentials('admin') 173 post '/projects.xml', @parameters, :authorization => credentials('admin')
138 end 174 end
139 175
140 project = Project.first(:order => 'id DESC') 176 project = Project.first(:order => 'id DESC')
141 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort 177 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
142 end 178 end
143 179
144 should "accept tracker_ids attribute" do 180 should "accept tracker_ids attribute" do
145 @parameters[:project].merge!({:tracker_ids => [1, 3]}) 181 @parameters[:project].merge!({:tracker_ids => [1, 3]})
146 182
147 assert_difference('Project.count') do 183 assert_difference('Project.count') do
148 post '/projects.xml', @parameters, :authorization => credentials('admin') 184 post '/projects.xml', @parameters, :authorization => credentials('admin')
149 end 185 end
150 186
151 project = Project.first(:order => 'id DESC') 187 project = Project.first(:order => 'id DESC')
152 assert_equal [1, 3], project.trackers.map(&:id).sort 188 assert_equal [1, 3], project.trackers.map(&:id).sort
153 end 189 end
154 end 190 end
155 end 191 end
156 192
157 context "with invalid parameters" do 193 context "with invalid parameters" do
158 setup do 194 setup do
159 @parameters = {:project => {:name => 'API test'}} 195 @parameters = {:project => {:name => 'API test'}}
160 end 196 end
161 197
162 context ".xml" do 198 context ".xml" do
163 should "return errors" do 199 should "return errors" do
164 assert_no_difference('Project.count') do 200 assert_no_difference('Project.count') do
165 post '/projects.xml', @parameters, :authorization => credentials('admin') 201 post '/projects.xml', @parameters, :authorization => credentials('admin')
166 end 202 end
167 203
168 assert_response :unprocessable_entity 204 assert_response :unprocessable_entity
169 assert_equal 'application/xml', @response.content_type 205 assert_equal 'application/xml', @response.content_type
170 assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"} 206 assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"}
171 end 207 end
172 end 208 end
173 end 209 end
174 end 210 end
175 211
176 context "PUT /projects/:id" do 212 context "PUT /projects/:id" do
177 context "with valid parameters" do 213 context "with valid parameters" do
178 setup do 214 setup do
179 @parameters = {:project => {:name => 'API update'}} 215 @parameters = {:project => {:name => 'API update'}}
180 end 216 end
181 217
182 context ".xml" do 218 context ".xml" do
183 should_allow_api_authentication(:put, 219 should_allow_api_authentication(:put,
184 '/projects/2.xml', 220 '/projects/2.xml',
185 {:project => {:name => 'API update'}}, 221 {:project => {:name => 'API update'}},
186 {:success_code => :ok}) 222 {:success_code => :ok})
187 223
188 should "update the project" do 224 should "update the project" do
189 assert_no_difference 'Project.count' do 225 assert_no_difference 'Project.count' do
190 put '/projects/2.xml', @parameters, :authorization => credentials('jsmith') 226 put '/projects/2.xml', @parameters, :authorization => credentials('jsmith')
191 end 227 end
192 assert_response :ok 228 assert_response :ok
193 assert_equal 'application/xml', @response.content_type 229 assert_equal 'application/xml', @response.content_type
194 project = Project.find(2) 230 project = Project.find(2)
195 assert_equal 'API update', project.name 231 assert_equal 'API update', project.name
196 end 232 end
197 233
198 should "accept enabled_module_names attribute" do 234 should "accept enabled_module_names attribute" do
199 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}) 235 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
200 236
201 assert_no_difference 'Project.count' do 237 assert_no_difference 'Project.count' do
202 put '/projects/2.xml', @parameters, :authorization => credentials('admin') 238 put '/projects/2.xml', @parameters, :authorization => credentials('admin')
203 end 239 end
204 assert_response :ok 240 assert_response :ok
205 project = Project.find(2) 241 project = Project.find(2)
206 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort 242 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
207 end 243 end
208 244
209 should "accept tracker_ids attribute" do 245 should "accept tracker_ids attribute" do
210 @parameters[:project].merge!({:tracker_ids => [1, 3]}) 246 @parameters[:project].merge!({:tracker_ids => [1, 3]})
211 247
212 assert_no_difference 'Project.count' do 248 assert_no_difference 'Project.count' do
213 put '/projects/2.xml', @parameters, :authorization => credentials('admin') 249 put '/projects/2.xml', @parameters, :authorization => credentials('admin')
214 end 250 end
215 assert_response :ok 251 assert_response :ok
216 project = Project.find(2) 252 project = Project.find(2)
217 assert_equal [1, 3], project.trackers.map(&:id).sort 253 assert_equal [1, 3], project.trackers.map(&:id).sort
218 end 254 end
219 end 255 end
220 end 256 end
221 257
222 context "with invalid parameters" do 258 context "with invalid parameters" do
223 setup do 259 setup do
224 @parameters = {:project => {:name => ''}} 260 @parameters = {:project => {:name => ''}}
225 end 261 end
226 262
227 context ".xml" do 263 context ".xml" do
228 should "return errors" do 264 should "return errors" do
229 assert_no_difference('Project.count') do 265 assert_no_difference('Project.count') do
230 put '/projects/2.xml', @parameters, :authorization => credentials('admin') 266 put '/projects/2.xml', @parameters, :authorization => credentials('admin')
231 end 267 end
232 268
233 assert_response :unprocessable_entity 269 assert_response :unprocessable_entity
234 assert_equal 'application/xml', @response.content_type 270 assert_equal 'application/xml', @response.content_type
235 assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"} 271 assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
236 end 272 end
237 end 273 end
238 end 274 end
239 end 275 end
240 276
241 context "DELETE /projects/:id" do 277 context "DELETE /projects/:id" do
242 context ".xml" do 278 context ".xml" do
243 should_allow_api_authentication(:delete, 279 should_allow_api_authentication(:delete,
244 '/projects/2.xml', 280 '/projects/2.xml',
245 {}, 281 {},
246 {:success_code => :ok}) 282 {:success_code => :ok})
247 283
248 should "delete the project" do 284 should "delete the project" do
249 assert_difference('Project.count',-1) do 285 assert_difference('Project.count',-1) do
250 delete '/projects/2.xml', {}, :authorization => credentials('admin') 286 delete '/projects/2.xml', {}, :authorization => credentials('admin')
251 end 287 end
252 assert_response :ok 288 assert_response :ok
253 assert_nil Project.find_by_id(2) 289 assert_nil Project.find_by_id(2)
254 end 290 end
255 end 291 end
256 end 292 end
257 293
258 def credentials(user, password=nil) 294 def credentials(user, password=nil)
259 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) 295 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
260 end 296 end
261 end 297 end