##// END OF EJS Templates
Makes projects API return XML description when creating a project (#6874)....
Jean-Philippe Lang -
r4283:eaab2ede76c9
parent child
Show More
@@ -1,271 +1,271
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2009 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 class ProjectsController < ApplicationController
19 19 menu_item :overview
20 20 menu_item :roadmap, :only => :roadmap
21 21 menu_item :settings, :only => :settings
22 22
23 23 before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ]
24 24 before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy]
25 25 before_filter :authorize_global, :only => [:new, :create]
26 26 before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
27 27 accept_key_auth :index
28 28
29 29 after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller|
30 30 if controller.request.post?
31 31 controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
32 32 end
33 33 end
34 34
35 35 # TODO: convert to PUT only
36 36 verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
37 37
38 38 helper :sort
39 39 include SortHelper
40 40 helper :custom_fields
41 41 include CustomFieldsHelper
42 42 helper :issues
43 43 helper :queries
44 44 include QueriesHelper
45 45 helper :repositories
46 46 include RepositoriesHelper
47 47 include ProjectsHelper
48 48
49 49 # Lists visible projects
50 50 def index
51 51 respond_to do |format|
52 52 format.html {
53 53 @projects = Project.visible.find(:all, :order => 'lft')
54 54 }
55 55 format.xml {
56 56 @projects = Project.visible.find(:all, :order => 'lft')
57 57 }
58 58 format.atom {
59 59 projects = Project.visible.find(:all, :order => 'created_on DESC',
60 60 :limit => Setting.feeds_limit.to_i)
61 61 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
62 62 }
63 63 end
64 64 end
65 65
66 66 def new
67 67 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
68 68 @trackers = Tracker.all
69 69 @project = Project.new(params[:project])
70 70
71 71 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
72 72 @project.trackers = Tracker.all
73 73 @project.is_public = Setting.default_projects_public?
74 74 @project.enabled_module_names = Setting.default_projects_modules
75 75 end
76 76
77 77 def create
78 78 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
79 79 @trackers = Tracker.all
80 80 @project = Project.new(params[:project])
81 81
82 82 @project.enabled_module_names = params[:enabled_modules]
83 83 if validate_parent_id && @project.save
84 84 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
85 85 # Add current user as a project member if he is not admin
86 86 unless User.current.admin?
87 87 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
88 88 m = Member.new(:user => User.current, :roles => [r])
89 89 @project.members << m
90 90 end
91 91 respond_to do |format|
92 92 format.html {
93 93 flash[:notice] = l(:notice_successful_create)
94 94 redirect_to :controller => 'projects', :action => 'settings', :id => @project
95 95 }
96 format.xml { head :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
96 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
97 97 end
98 98 else
99 99 respond_to do |format|
100 100 format.html { render :action => 'new' }
101 101 format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
102 102 end
103 103 end
104 104
105 105 end
106 106
107 107 def copy
108 108 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
109 109 @trackers = Tracker.all
110 110 @root_projects = Project.find(:all,
111 111 :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
112 112 :order => 'name')
113 113 @source_project = Project.find(params[:id])
114 114 if request.get?
115 115 @project = Project.copy_from(@source_project)
116 116 if @project
117 117 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
118 118 else
119 119 redirect_to :controller => 'admin', :action => 'projects'
120 120 end
121 121 else
122 122 Mailer.with_deliveries(params[:notifications] == '1') do
123 123 @project = Project.new(params[:project])
124 124 @project.enabled_module_names = params[:enabled_modules]
125 125 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
126 126 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
127 127 flash[:notice] = l(:notice_successful_create)
128 128 redirect_to :controller => 'projects', :action => 'settings'
129 129 elsif !@project.new_record?
130 130 # Project was created
131 131 # But some objects were not copied due to validation failures
132 132 # (eg. issues from disabled trackers)
133 133 # TODO: inform about that
134 134 redirect_to :controller => 'projects', :action => 'settings'
135 135 end
136 136 end
137 137 end
138 138 rescue ActiveRecord::RecordNotFound
139 139 redirect_to :controller => 'admin', :action => 'projects'
140 140 end
141 141
142 142 # Show @project
143 143 def show
144 144 if params[:jump]
145 145 # try to redirect to the requested menu item
146 146 redirect_to_project_menu_item(@project, params[:jump]) && return
147 147 end
148 148
149 149 @users_by_role = @project.users_by_role
150 150 @subprojects = @project.children.visible
151 151 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
152 152 @trackers = @project.rolled_up_trackers
153 153
154 154 cond = @project.project_condition(Setting.display_subprojects_issues?)
155 155
156 156 @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
157 157 :include => [:project, :status, :tracker],
158 158 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
159 159 @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
160 160 :include => [:project, :status, :tracker],
161 161 :conditions => cond)
162 162
163 163 TimeEntry.visible_by(User.current) do
164 164 @total_hours = TimeEntry.sum(:hours,
165 165 :include => :project,
166 166 :conditions => cond).to_f
167 167 end
168 168 @key = User.current.rss_key
169 169
170 170 respond_to do |format|
171 171 format.html
172 172 format.xml
173 173 end
174 174 end
175 175
176 176 def settings
177 177 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
178 178 @issue_category ||= IssueCategory.new
179 179 @member ||= @project.members.new
180 180 @trackers = Tracker.all
181 181 @repository ||= @project.repository
182 182 @wiki ||= @project.wiki
183 183 end
184 184
185 185 def edit
186 186 end
187 187
188 188 def update
189 189 @project.attributes = params[:project]
190 190 if validate_parent_id && @project.save
191 191 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
192 192 respond_to do |format|
193 193 format.html {
194 194 flash[:notice] = l(:notice_successful_update)
195 195 redirect_to :action => 'settings', :id => @project
196 196 }
197 197 format.xml { head :ok }
198 198 end
199 199 else
200 200 respond_to do |format|
201 201 format.html {
202 202 settings
203 203 render :action => 'settings'
204 204 }
205 205 format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
206 206 end
207 207 end
208 208 end
209 209
210 210 def modules
211 211 @project.enabled_module_names = params[:enabled_modules]
212 212 flash[:notice] = l(:notice_successful_update)
213 213 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
214 214 end
215 215
216 216 def archive
217 217 if request.post?
218 218 unless @project.archive
219 219 flash[:error] = l(:error_can_not_archive_project)
220 220 end
221 221 end
222 222 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
223 223 end
224 224
225 225 def unarchive
226 226 @project.unarchive if request.post? && !@project.active?
227 227 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
228 228 end
229 229
230 230 # Delete @project
231 231 def destroy
232 232 @project_to_destroy = @project
233 233 if request.get?
234 234 # display confirmation view
235 235 else
236 236 if params[:format] == 'xml' || params[:confirm]
237 237 @project_to_destroy.destroy
238 238 respond_to do |format|
239 239 format.html { redirect_to :controller => 'admin', :action => 'projects' }
240 240 format.xml { head :ok }
241 241 end
242 242 end
243 243 end
244 244 # hide project in layout
245 245 @project = nil
246 246 end
247 247
248 248 private
249 249 def find_optional_project
250 250 return true unless params[:id]
251 251 @project = Project.find(params[:id])
252 252 authorize
253 253 rescue ActiveRecord::RecordNotFound
254 254 render_404
255 255 end
256 256
257 257 # Validates parent_id param according to user's permissions
258 258 # TODO: move it to Project model in a validation that depends on User.current
259 259 def validate_parent_id
260 260 return true if User.current.admin?
261 261 parent_id = params[:project] && params[:project][:parent_id]
262 262 if parent_id || @project.new_record?
263 263 parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
264 264 unless @project.allowed_parents.include?(parent)
265 265 @project.errors.add :parent_id, :invalid
266 266 return false
267 267 end
268 268 end
269 269 true
270 270 end
271 271 end
@@ -1,336 +1,340
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2010 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.dirname(__FILE__)}/../../test_helper"
19 19
20 20 class ApiTest::IssuesTest < ActionController::IntegrationTest
21 21 fixtures :projects,
22 22 :users,
23 23 :roles,
24 24 :members,
25 25 :member_roles,
26 26 :issues,
27 27 :issue_statuses,
28 28 :versions,
29 29 :trackers,
30 30 :projects_trackers,
31 31 :issue_categories,
32 32 :enabled_modules,
33 33 :enumerations,
34 34 :attachments,
35 35 :workflows,
36 36 :custom_fields,
37 37 :custom_values,
38 38 :custom_fields_projects,
39 39 :custom_fields_trackers,
40 40 :time_entries,
41 41 :journals,
42 42 :journal_details,
43 43 :queries
44 44
45 45 def setup
46 46 Setting.rest_api_enabled = '1'
47 47 end
48 48
49 49 # Use a private project to make sure auth is really working and not just
50 50 # only showing public issues.
51 51 context "/index.xml" do
52 52 should_allow_api_authentication(:get, "/projects/private-child/issues.xml")
53 53 end
54 54
55 55 context "/index.json" do
56 56 should_allow_api_authentication(:get, "/projects/private-child/issues.json")
57 57 end
58 58
59 59 context "/index.xml with filter" do
60 60 should_allow_api_authentication(:get, "/projects/private-child/issues.xml?status_id=5")
61 61
62 62 should "show only issues with the status_id" do
63 63 get '/issues.xml?status_id=5'
64 64 assert_tag :tag => 'issues',
65 65 :children => { :count => Issue.visible.count(:conditions => {:status_id => 5}),
66 66 :only => { :tag => 'issue' } }
67 67 end
68 68 end
69 69
70 70 context "/index.json with filter" do
71 71 should_allow_api_authentication(:get, "/projects/private-child/issues.json?status_id=5")
72 72
73 73 should "show only issues with the status_id" do
74 74 get '/issues.json?status_id=5'
75 75
76 76 json = ActiveSupport::JSON.decode(response.body)
77 77 status_ids_used = json.collect {|j| j['status_id'] }
78 78 assert_equal 3, status_ids_used.length
79 79 assert status_ids_used.all? {|id| id == 5 }
80 80 end
81 81
82 82 end
83 83
84 84 # Issue 6 is on a private project
85 85 context "/issues/6.xml" do
86 86 should_allow_api_authentication(:get, "/issues/6.xml")
87 87 end
88 88
89 89 context "/issues/6.json" do
90 90 should_allow_api_authentication(:get, "/issues/6.json")
91 91 end
92 92
93 93 context "POST /issues.xml" do
94 94 should_allow_api_authentication(:post,
95 95 '/issues.xml',
96 96 {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}},
97 97 {:success_code => :created})
98 98
99 99 should "create an issue with the attributes" do
100 100 assert_difference('Issue.count') do
101 101 post '/issues.xml', {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, :authorization => credentials('jsmith')
102 102 end
103 103
104 104 issue = Issue.first(:order => 'id DESC')
105 105 assert_equal 1, issue.project_id
106 106 assert_equal 2, issue.tracker_id
107 107 assert_equal 3, issue.status_id
108 108 assert_equal 'API test', issue.subject
109
110 assert_response :created
111 assert_equal 'application/xml', @response.content_type
112 assert_tag 'issue', :child => {:tag => 'id', :content => issue.id.to_s}
109 113 end
110 114 end
111 115
112 116 context "POST /issues.xml with failure" do
113 117 should_allow_api_authentication(:post,
114 118 '/issues.xml',
115 119 {:issue => {:project_id => 1}},
116 120 {:success_code => :unprocessable_entity})
117 121
118 122 should "have an errors tag" do
119 123 assert_no_difference('Issue.count') do
120 124 post '/issues.xml', {:issue => {:project_id => 1}}, :authorization => credentials('jsmith')
121 125 end
122 126
123 127 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
124 128 end
125 129 end
126 130
127 131 context "POST /issues.json" do
128 132 should_allow_api_authentication(:post,
129 133 '/issues.json',
130 134 {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}},
131 135 {:success_code => :created})
132 136
133 137 should "create an issue with the attributes" do
134 138 assert_difference('Issue.count') do
135 139 post '/issues.json', {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, :authorization => credentials('jsmith')
136 140 end
137 141
138 142 issue = Issue.first(:order => 'id DESC')
139 143 assert_equal 1, issue.project_id
140 144 assert_equal 2, issue.tracker_id
141 145 assert_equal 3, issue.status_id
142 146 assert_equal 'API test', issue.subject
143 147 end
144 148
145 149 end
146 150
147 151 context "POST /issues.json with failure" do
148 152 should_allow_api_authentication(:post,
149 153 '/issues.json',
150 154 {:issue => {:project_id => 1}},
151 155 {:success_code => :unprocessable_entity})
152 156
153 157 should "have an errors element" do
154 158 assert_no_difference('Issue.count') do
155 159 post '/issues.json', {:issue => {:project_id => 1}}, :authorization => credentials('jsmith')
156 160 end
157 161
158 162 json = ActiveSupport::JSON.decode(response.body)
159 163 assert_equal "can't be blank", json.first['subject']
160 164 end
161 165 end
162 166
163 167 # Issue 6 is on a private project
164 168 context "PUT /issues/6.xml" do
165 169 setup do
166 170 @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
167 171 @headers = { :authorization => credentials('jsmith') }
168 172 end
169 173
170 174 should_allow_api_authentication(:put,
171 175 '/issues/6.xml',
172 176 {:issue => {:subject => 'API update', :notes => 'A new note'}},
173 177 {:success_code => :ok})
174 178
175 179 should "not create a new issue" do
176 180 assert_no_difference('Issue.count') do
177 181 put '/issues/6.xml', @parameters, @headers
178 182 end
179 183 end
180 184
181 185 should "create a new journal" do
182 186 assert_difference('Journal.count') do
183 187 put '/issues/6.xml', @parameters, @headers
184 188 end
185 189 end
186 190
187 191 should "add the note to the journal" do
188 192 put '/issues/6.xml', @parameters, @headers
189 193
190 194 journal = Journal.last
191 195 assert_equal "A new note", journal.notes
192 196 end
193 197
194 198 should "update the issue" do
195 199 put '/issues/6.xml', @parameters, @headers
196 200
197 201 issue = Issue.find(6)
198 202 assert_equal "API update", issue.subject
199 203 end
200 204
201 205 end
202 206
203 207 context "PUT /issues/6.xml with failed update" do
204 208 setup do
205 209 @parameters = {:issue => {:subject => ''}}
206 210 @headers = { :authorization => credentials('jsmith') }
207 211 end
208 212
209 213 should_allow_api_authentication(:put,
210 214 '/issues/6.xml',
211 215 {:issue => {:subject => ''}}, # Missing subject should fail
212 216 {:success_code => :unprocessable_entity})
213 217
214 218 should "not create a new issue" do
215 219 assert_no_difference('Issue.count') do
216 220 put '/issues/6.xml', @parameters, @headers
217 221 end
218 222 end
219 223
220 224 should "not create a new journal" do
221 225 assert_no_difference('Journal.count') do
222 226 put '/issues/6.xml', @parameters, @headers
223 227 end
224 228 end
225 229
226 230 should "have an errors tag" do
227 231 put '/issues/6.xml', @parameters, @headers
228 232
229 233 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
230 234 end
231 235 end
232 236
233 237 context "PUT /issues/6.json" do
234 238 setup do
235 239 @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
236 240 @headers = { :authorization => credentials('jsmith') }
237 241 end
238 242
239 243 should_allow_api_authentication(:put,
240 244 '/issues/6.json',
241 245 {:issue => {:subject => 'API update', :notes => 'A new note'}},
242 246 {:success_code => :ok})
243 247
244 248 should "not create a new issue" do
245 249 assert_no_difference('Issue.count') do
246 250 put '/issues/6.json', @parameters, @headers
247 251 end
248 252 end
249 253
250 254 should "create a new journal" do
251 255 assert_difference('Journal.count') do
252 256 put '/issues/6.json', @parameters, @headers
253 257 end
254 258 end
255 259
256 260 should "add the note to the journal" do
257 261 put '/issues/6.json', @parameters, @headers
258 262
259 263 journal = Journal.last
260 264 assert_equal "A new note", journal.notes
261 265 end
262 266
263 267 should "update the issue" do
264 268 put '/issues/6.json', @parameters, @headers
265 269
266 270 issue = Issue.find(6)
267 271 assert_equal "API update", issue.subject
268 272 end
269 273
270 274 end
271 275
272 276 context "PUT /issues/6.json with failed update" do
273 277 setup do
274 278 @parameters = {:issue => {:subject => ''}}
275 279 @headers = { :authorization => credentials('jsmith') }
276 280 end
277 281
278 282 should_allow_api_authentication(:put,
279 283 '/issues/6.json',
280 284 {:issue => {:subject => ''}}, # Missing subject should fail
281 285 {:success_code => :unprocessable_entity})
282 286
283 287 should "not create a new issue" do
284 288 assert_no_difference('Issue.count') do
285 289 put '/issues/6.json', @parameters, @headers
286 290 end
287 291 end
288 292
289 293 should "not create a new journal" do
290 294 assert_no_difference('Journal.count') do
291 295 put '/issues/6.json', @parameters, @headers
292 296 end
293 297 end
294 298
295 299 should "have an errors attribute" do
296 300 put '/issues/6.json', @parameters, @headers
297 301
298 302 json = ActiveSupport::JSON.decode(response.body)
299 303 assert_equal "can't be blank", json.first['subject']
300 304 end
301 305 end
302 306
303 307 context "DELETE /issues/1.xml" do
304 308 should_allow_api_authentication(:delete,
305 309 '/issues/6.xml',
306 310 {},
307 311 {:success_code => :ok})
308 312
309 313 should "delete the issue" do
310 314 assert_difference('Issue.count',-1) do
311 315 delete '/issues/6.xml', {}, :authorization => credentials('jsmith')
312 316 end
313 317
314 318 assert_nil Issue.find_by_id(6)
315 319 end
316 320 end
317 321
318 322 context "DELETE /issues/1.json" do
319 323 should_allow_api_authentication(:delete,
320 324 '/issues/6.json',
321 325 {},
322 326 {:success_code => :ok})
323 327
324 328 should "delete the issue" do
325 329 assert_difference('Issue.count',-1) do
326 330 delete '/issues/6.json', {}, :authorization => credentials('jsmith')
327 331 end
328 332
329 333 assert_nil Issue.find_by_id(6)
330 334 end
331 335 end
332 336
333 337 def credentials(user, password=nil)
334 338 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
335 339 end
336 340 end
@@ -1,108 +1,111
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2010 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.dirname(__FILE__)}/../../test_helper"
19 19
20 20 class ApiTest::ProjectsTest < ActionController::IntegrationTest
21 21 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
22 22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
23 23 :attachments, :custom_fields, :custom_values, :time_entries
24 24
25 25 def setup
26 26 Setting.rest_api_enabled = '1'
27 27 end
28 28
29 29 def test_index
30 30 get '/projects.xml'
31 31 assert_response :success
32 32 assert_equal 'application/xml', @response.content_type
33 33 end
34 34
35 35 def test_show
36 36 get '/projects/1.xml'
37 37 assert_response :success
38 38 assert_equal 'application/xml', @response.content_type
39 39 assert_tag 'custom_field', :attributes => {:name => 'Development status'}, :content => 'Stable'
40 40 end
41 41
42 42 def test_show_should_not_display_hidden_custom_fields
43 43 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
44 44 get '/projects/1.xml'
45 45 assert_response :success
46 46 assert_equal 'application/xml', @response.content_type
47 47 assert_no_tag 'custom_field', :attributes => {:name => 'Development status'}
48 48 end
49 49
50 50 def test_create
51 51 attributes = {:name => 'API test', :identifier => 'api-test'}
52 52 assert_difference 'Project.count' do
53 53 post '/projects.xml', {:project => attributes}, :authorization => credentials('admin')
54 54 end
55 assert_response :created
56 assert_equal 'application/xml', @response.content_type
55
57 56 project = Project.first(:order => 'id DESC')
58 57 attributes.each do |attribute, value|
59 58 assert_equal value, project.send(attribute)
60 59 end
60
61 assert_response :created
62 assert_equal 'application/xml', @response.content_type
63 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
61 64 end
62 65
63 66 def test_create_failure
64 67 attributes = {:name => 'API test'}
65 68 assert_no_difference 'Project.count' do
66 69 post '/projects.xml', {:project => attributes}, :authorization => credentials('admin')
67 70 end
68 71 assert_response :unprocessable_entity
69 72 assert_equal 'application/xml', @response.content_type
70 73 assert_tag :errors, :child => {:tag => 'error', :content => "Identifier can't be blank"}
71 74 end
72 75
73 76 def test_update
74 77 attributes = {:name => 'API update'}
75 78 assert_no_difference 'Project.count' do
76 79 put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith')
77 80 end
78 81 assert_response :ok
79 82 assert_equal 'application/xml', @response.content_type
80 83 project = Project.find(1)
81 84 attributes.each do |attribute, value|
82 85 assert_equal value, project.send(attribute)
83 86 end
84 87 end
85 88
86 89 def test_update_failure
87 90 attributes = {:name => ''}
88 91 assert_no_difference 'Project.count' do
89 92 put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith')
90 93 end
91 94 assert_response :unprocessable_entity
92 95 assert_equal 'application/xml', @response.content_type
93 96 assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
94 97 end
95 98
96 99 def test_destroy
97 100 assert_difference 'Project.count', -1 do
98 101 delete '/projects/2.xml', {}, :authorization => credentials('admin')
99 102 end
100 103 assert_response :ok
101 104 assert_equal 'application/xml', @response.content_type
102 105 assert_nil Project.find_by_id(2)
103 106 end
104 107
105 108 def credentials(user, password=nil)
106 109 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
107 110 end
108 111 end
General Comments 0
You need to be logged in to leave comments. Login now