##// END OF EJS Templates
Added JSON support to the issues API. #1214...
Eric Davis -
r3652:345301284a9b
parent child
Show More
@@ -349,4 +349,11 class ApplicationController < ActionController::Base
349 349 render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
350 350 end
351 351
352 # Converts the errors on an ActiveRecord object into a common JSON format
353 def object_errors_to_json(object)
354 object.errors.collect do |attribute, error|
355 { attribute => error }
356 end.to_json
357 end
358
352 359 end
@@ -82,6 +82,7 class IssuesController < ApplicationController
82 82 respond_to do |format|
83 83 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
84 84 format.xml { render :layout => false }
85 format.json { render :text => @issues.to_json, :layout => false }
85 86 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
86 87 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
87 88 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
@@ -122,6 +123,7 class IssuesController < ApplicationController
122 123 respond_to do |format|
123 124 format.html { render :template => 'issues/show.rhtml' }
124 125 format.xml { render :layout => false }
126 format.json { render :text => @issue.to_json, :layout => false }
125 127 format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' }
126 128 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
127 129 end
@@ -146,12 +148,14 class IssuesController < ApplicationController
146 148 { :action => 'show', :id => @issue })
147 149 }
148 150 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) }
151 format.json { render :text => @issue.to_json, :status => :created, :location => url_for(:controller => 'issues', :action => 'show'), :layout => false }
149 152 end
150 153 return
151 154 else
152 155 respond_to do |format|
153 156 format.html { render :action => 'new' }
154 157 format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return }
158 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
155 159 end
156 160 end
157 161 end
@@ -181,6 +185,7 class IssuesController < ApplicationController
181 185 respond_to do |format|
182 186 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
183 187 format.xml { head :ok }
188 format.json { head :ok }
184 189 end
185 190 else
186 191 render_attachment_warning_if_needed(@issue)
@@ -190,6 +195,7 class IssuesController < ApplicationController
190 195 respond_to do |format|
191 196 format.html { render :action => 'edit' }
192 197 format.xml { render :xml => @issue.errors, :status => :unprocessable_entity }
198 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
193 199 end
194 200 end
195 201 end
@@ -305,7 +311,7 class IssuesController < ApplicationController
305 311 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
306 312 end
307 313 else
308 unless params[:format] == 'xml'
314 unless params[:format] == 'xml' || params[:format] == 'json'
309 315 # display the destroy form if it's a user request
310 316 return
311 317 end
@@ -315,6 +321,7 class IssuesController < ApplicationController
315 321 respond_to do |format|
316 322 format.html { redirect_to :action => 'index', :project_id => @project }
317 323 format.xml { head :ok }
324 format.json { head :ok }
318 325 end
319 326 end
320 327
@@ -54,7 +54,20 class IssuesApiTest < ActionController::IntegrationTest
54 54 should_respond_with :success
55 55 should_respond_with_content_type 'application/xml'
56 56 end
57
57
58 context "/index.json" do
59 setup do
60 get '/issues.json'
61 end
62
63 should_respond_with :success
64 should_respond_with_content_type 'application/json'
65
66 should 'return a valid JSON string' do
67 assert ActiveSupport::JSON.decode(response.body)
68 end
69 end
70
58 71 context "/index.xml with filter" do
59 72 setup do
60 73 get '/issues.xml?status_id=5'
@@ -69,6 +82,27 class IssuesApiTest < ActionController::IntegrationTest
69 82 end
70 83 end
71 84
85 context "/index.json with filter" do
86 setup do
87 get '/issues.json?status_id=5'
88 end
89
90 should_respond_with :success
91 should_respond_with_content_type 'application/json'
92
93 should 'return a valid JSON string' do
94 assert ActiveSupport::JSON.decode(response.body)
95 end
96
97 should "show only issues with the status_id" do
98 json = ActiveSupport::JSON.decode(response.body)
99 status_ids_used = json.collect {|j| j['status_id'] }
100 assert_equal 3, status_ids_used.length
101 assert status_ids_used.all? {|id| id == 5 }
102 end
103
104 end
105
72 106 context "/issues/1.xml" do
73 107 setup do
74 108 get '/issues/1.xml'
@@ -78,6 +112,19 class IssuesApiTest < ActionController::IntegrationTest
78 112 should_respond_with_content_type 'application/xml'
79 113 end
80 114
115 context "/issues/1.json" do
116 setup do
117 get '/issues/1.json'
118 end
119
120 should_respond_with :success
121 should_respond_with_content_type 'application/json'
122
123 should 'return a valid JSON string' do
124 assert ActiveSupport::JSON.decode(response.body)
125 end
126 end
127
81 128 context "POST /issues.xml" do
82 129 setup do
83 130 @issue_count = Issue.count
@@ -111,7 +158,42 class IssuesApiTest < ActionController::IntegrationTest
111 158 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
112 159 end
113 160 end
114
161
162 context "POST /issues.json" do
163 setup do
164 @issue_count = Issue.count
165 @attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}
166 post '/issues.json', {:issue => @attributes}, :authorization => credentials('jsmith')
167 end
168
169 should_respond_with :created
170 should_respond_with_content_type 'application/json'
171
172 should "create an issue with the attributes" do
173 assert_equal Issue.count, @issue_count + 1
174
175 issue = Issue.first(:order => 'id DESC')
176 @attributes.each do |attribute, value|
177 assert_equal value, issue.send(attribute)
178 end
179 end
180 end
181
182 context "POST /issues.json with failure" do
183 setup do
184 @attributes = {:project_id => 1}
185 post '/issues.json', {:issue => @attributes}, :authorization => credentials('jsmith')
186 end
187
188 should_respond_with :unprocessable_entity
189 should_respond_with_content_type 'application/json'
190
191 should "have an errors element" do
192 json = ActiveSupport::JSON.decode(response.body)
193 assert_equal "can't be blank", json.first['subject']
194 end
195 end
196
115 197 context "PUT /issues/1.xml" do
116 198 setup do
117 199 @issue_count = Issue.count
@@ -166,6 +248,61 class IssuesApiTest < ActionController::IntegrationTest
166 248 end
167 249 end
168 250
251 context "PUT /issues/1.json" do
252 setup do
253 @issue_count = Issue.count
254 @journal_count = Journal.count
255 @attributes = {:subject => 'API update'}
256
257 put '/issues/1.json', {:issue => @attributes}, :authorization => credentials('jsmith')
258 end
259
260 should_respond_with :ok
261 should_respond_with_content_type 'application/json'
262
263 should "not create a new issue" do
264 assert_equal Issue.count, @issue_count
265 end
266
267 should "create a new journal" do
268 assert_equal Journal.count, @journal_count + 1
269 end
270
271 should "update the issue" do
272 issue = Issue.find(1)
273 @attributes.each do |attribute, value|
274 assert_equal value, issue.send(attribute)
275 end
276 end
277
278 end
279
280 context "PUT /issues/1.json with failed update" do
281 setup do
282 @attributes = {:subject => ''}
283 @issue_count = Issue.count
284 @journal_count = Journal.count
285
286 put '/issues/1.json', {:issue => @attributes}, :authorization => credentials('jsmith')
287 end
288
289 should_respond_with :unprocessable_entity
290 should_respond_with_content_type 'application/json'
291
292 should "not create a new issue" do
293 assert_equal Issue.count, @issue_count
294 end
295
296 should "not create a new journal" do
297 assert_equal Journal.count, @journal_count
298 end
299
300 should "have an errors attribute" do
301 json = ActiveSupport::JSON.decode(response.body)
302 assert_equal "can't be blank", json.first['subject']
303 end
304 end
305
169 306 context "DELETE /issues/1.xml" do
170 307 setup do
171 308 @issue_count = Issue.count
@@ -180,7 +317,22 class IssuesApiTest < ActionController::IntegrationTest
180 317 assert_nil Issue.find_by_id(1)
181 318 end
182 319 end
183
320
321 context "DELETE /issues/1.json" do
322 setup do
323 @issue_count = Issue.count
324 delete '/issues/1.json', {}, :authorization => credentials('jsmith')
325 end
326
327 should_respond_with :ok
328 should_respond_with_content_type 'application/json'
329
330 should "delete the issue" do
331 assert_equal Issue.count, @issue_count -1
332 assert_nil Issue.find_by_id(1)
333 end
334 end
335
184 336 def credentials(user, password=nil)
185 337 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
186 338 end
General Comments 0
You need to be logged in to leave comments. Login now