Mercurial > hg > soundsoftware-site
comparison test/integration/issues_api_test.rb @ 0:513646585e45
* Import Redmine trunk SVN rev 3859
author | Chris Cannam |
---|---|
date | Fri, 23 Jul 2010 15:52:44 +0100 |
parents | |
children | 40f7cfd4df19 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:513646585e45 |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2010 Jean-Philippe Lang | |
3 # | |
4 # This program is free software; you can redistribute it and/or | |
5 # modify it under the terms of the GNU General Public License | |
6 # as published by the Free Software Foundation; either version 2 | |
7 # of the License, or (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
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 | |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | |
18 require "#{File.dirname(__FILE__)}/../test_helper" | |
19 | |
20 class IssuesApiTest < ActionController::IntegrationTest | |
21 fixtures :projects, | |
22 :users, | |
23 :roles, | |
24 :members, | |
25 :member_roles, | |
26 :issues, | |
27 :issue_statuses, | |
28 :versions, | |
29 :trackers, | |
30 :projects_trackers, | |
31 :issue_categories, | |
32 :enabled_modules, | |
33 :enumerations, | |
34 :attachments, | |
35 :workflows, | |
36 :custom_fields, | |
37 :custom_values, | |
38 :custom_fields_projects, | |
39 :custom_fields_trackers, | |
40 :time_entries, | |
41 :journals, | |
42 :journal_details, | |
43 :queries | |
44 | |
45 def setup | |
46 Setting.rest_api_enabled = '1' | |
47 end | |
48 | |
49 context "/index.xml" do | |
50 setup do | |
51 get '/issues.xml' | |
52 end | |
53 | |
54 should_respond_with :success | |
55 should_respond_with_content_type 'application/xml' | |
56 end | |
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 | |
71 context "/index.xml with filter" do | |
72 setup do | |
73 get '/issues.xml?status_id=5' | |
74 end | |
75 | |
76 should_respond_with :success | |
77 should_respond_with_content_type 'application/xml' | |
78 should "show only issues with the status_id" do | |
79 assert_tag :tag => 'issues', | |
80 :children => { :count => Issue.visible.count(:conditions => {:status_id => 5}), | |
81 :only => { :tag => 'issue' } } | |
82 end | |
83 end | |
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 | |
106 context "/issues/1.xml" do | |
107 setup do | |
108 get '/issues/1.xml' | |
109 end | |
110 | |
111 should_respond_with :success | |
112 should_respond_with_content_type 'application/xml' | |
113 end | |
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 | |
128 context "POST /issues.xml" do | |
129 setup do | |
130 @issue_count = Issue.count | |
131 @attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3} | |
132 post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith') | |
133 end | |
134 | |
135 should_respond_with :created | |
136 should_respond_with_content_type 'application/xml' | |
137 | |
138 should "create an issue with the attributes" do | |
139 assert_equal Issue.count, @issue_count + 1 | |
140 | |
141 issue = Issue.first(:order => 'id DESC') | |
142 @attributes.each do |attribute, value| | |
143 assert_equal value, issue.send(attribute) | |
144 end | |
145 end | |
146 end | |
147 | |
148 context "POST /issues.xml with failure" do | |
149 setup do | |
150 @attributes = {:project_id => 1} | |
151 post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith') | |
152 end | |
153 | |
154 should_respond_with :unprocessable_entity | |
155 should_respond_with_content_type 'application/xml' | |
156 | |
157 should "have an errors tag" do | |
158 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"} | |
159 end | |
160 end | |
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 | |
197 context "PUT /issues/1.xml" do | |
198 setup do | |
199 @issue_count = Issue.count | |
200 @journal_count = Journal.count | |
201 @attributes = {:subject => 'API update'} | |
202 | |
203 put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith') | |
204 end | |
205 | |
206 should_respond_with :ok | |
207 should_respond_with_content_type 'application/xml' | |
208 | |
209 should "not create a new issue" do | |
210 assert_equal Issue.count, @issue_count | |
211 end | |
212 | |
213 should "create a new journal" do | |
214 assert_equal Journal.count, @journal_count + 1 | |
215 end | |
216 | |
217 should "update the issue" do | |
218 issue = Issue.find(1) | |
219 @attributes.each do |attribute, value| | |
220 assert_equal value, issue.send(attribute) | |
221 end | |
222 end | |
223 | |
224 end | |
225 | |
226 context "PUT /issues/1.xml with failed update" do | |
227 setup do | |
228 @attributes = {:subject => ''} | |
229 @issue_count = Issue.count | |
230 @journal_count = Journal.count | |
231 | |
232 put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith') | |
233 end | |
234 | |
235 should_respond_with :unprocessable_entity | |
236 should_respond_with_content_type 'application/xml' | |
237 | |
238 should "not create a new issue" do | |
239 assert_equal Issue.count, @issue_count | |
240 end | |
241 | |
242 should "not create a new journal" do | |
243 assert_equal Journal.count, @journal_count | |
244 end | |
245 | |
246 should "have an errors tag" do | |
247 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"} | |
248 end | |
249 end | |
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 | |
306 context "DELETE /issues/1.xml" do | |
307 setup do | |
308 @issue_count = Issue.count | |
309 delete '/issues/1.xml', {}, :authorization => credentials('jsmith') | |
310 end | |
311 | |
312 should_respond_with :ok | |
313 should_respond_with_content_type 'application/xml' | |
314 | |
315 should "delete the issue" do | |
316 assert_equal Issue.count, @issue_count -1 | |
317 assert_nil Issue.find_by_id(1) | |
318 end | |
319 end | |
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 | |
336 def credentials(user, password=nil) | |
337 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) | |
338 end | |
339 end |