Mercurial > hg > soundsoftware-site
comparison test/integration/api_test/.svn/text-base/issues_test.rb.svn-base @ 37:94944d00e43c
* Update to SVN trunk rev 4411
author | Chris Cannam <chris.cannam@soundsoftware.ac.uk> |
---|---|
date | Fri, 19 Nov 2010 13:24:41 +0000 |
parents | |
children | af80e5618e9b |
comparison
equal
deleted
inserted
replaced
22:40f7cfd4df19 | 37:94944d00e43c |
---|---|
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 ApiTest::IssuesTest < 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 # Use a private project to make sure auth is really working and not just | |
50 # only showing public issues. | |
51 context "/index.xml" do | |
52 should_allow_api_authentication(:get, "/projects/private-child/issues.xml") | |
53 end | |
54 | |
55 context "/index.json" do | |
56 should_allow_api_authentication(:get, "/projects/private-child/issues.json") | |
57 end | |
58 | |
59 context "/index.xml with filter" do | |
60 should_allow_api_authentication(:get, "/projects/private-child/issues.xml?status_id=5") | |
61 | |
62 should "show only issues with the status_id" do | |
63 get '/issues.xml?status_id=5' | |
64 assert_tag :tag => 'issues', | |
65 :children => { :count => Issue.visible.count(:conditions => {:status_id => 5}), | |
66 :only => { :tag => 'issue' } } | |
67 end | |
68 end | |
69 | |
70 context "/index.json with filter" do | |
71 should_allow_api_authentication(:get, "/projects/private-child/issues.json?status_id=5") | |
72 | |
73 should "show only issues with the status_id" do | |
74 get '/issues.json?status_id=5' | |
75 | |
76 json = ActiveSupport::JSON.decode(response.body) | |
77 status_ids_used = json.collect {|j| j['status_id'] } | |
78 assert_equal 3, status_ids_used.length | |
79 assert status_ids_used.all? {|id| id == 5 } | |
80 end | |
81 | |
82 end | |
83 | |
84 # Issue 6 is on a private project | |
85 context "/issues/6.xml" do | |
86 should_allow_api_authentication(:get, "/issues/6.xml") | |
87 end | |
88 | |
89 context "/issues/6.json" do | |
90 should_allow_api_authentication(:get, "/issues/6.json") | |
91 end | |
92 | |
93 context "POST /issues.xml" do | |
94 should_allow_api_authentication(:post, | |
95 '/issues.xml', | |
96 {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, | |
97 {:success_code => :created}) | |
98 | |
99 should "create an issue with the attributes" do | |
100 assert_difference('Issue.count') do | |
101 post '/issues.xml', {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, :authorization => credentials('jsmith') | |
102 end | |
103 | |
104 issue = Issue.first(:order => 'id DESC') | |
105 assert_equal 1, issue.project_id | |
106 assert_equal 2, issue.tracker_id | |
107 assert_equal 3, issue.status_id | |
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} | |
113 end | |
114 end | |
115 | |
116 context "POST /issues.xml with failure" do | |
117 should_allow_api_authentication(:post, | |
118 '/issues.xml', | |
119 {:issue => {:project_id => 1}}, | |
120 {:success_code => :unprocessable_entity}) | |
121 | |
122 should "have an errors tag" do | |
123 assert_no_difference('Issue.count') do | |
124 post '/issues.xml', {:issue => {:project_id => 1}}, :authorization => credentials('jsmith') | |
125 end | |
126 | |
127 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"} | |
128 end | |
129 end | |
130 | |
131 context "POST /issues.json" do | |
132 should_allow_api_authentication(:post, | |
133 '/issues.json', | |
134 {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, | |
135 {:success_code => :created}) | |
136 | |
137 should "create an issue with the attributes" do | |
138 assert_difference('Issue.count') do | |
139 post '/issues.json', {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, :authorization => credentials('jsmith') | |
140 end | |
141 | |
142 issue = Issue.first(:order => 'id DESC') | |
143 assert_equal 1, issue.project_id | |
144 assert_equal 2, issue.tracker_id | |
145 assert_equal 3, issue.status_id | |
146 assert_equal 'API test', issue.subject | |
147 end | |
148 | |
149 end | |
150 | |
151 context "POST /issues.json with failure" do | |
152 should_allow_api_authentication(:post, | |
153 '/issues.json', | |
154 {:issue => {:project_id => 1}}, | |
155 {:success_code => :unprocessable_entity}) | |
156 | |
157 should "have an errors element" do | |
158 assert_no_difference('Issue.count') do | |
159 post '/issues.json', {:issue => {:project_id => 1}}, :authorization => credentials('jsmith') | |
160 end | |
161 | |
162 json = ActiveSupport::JSON.decode(response.body) | |
163 assert_equal "can't be blank", json.first['subject'] | |
164 end | |
165 end | |
166 | |
167 # Issue 6 is on a private project | |
168 context "PUT /issues/6.xml" do | |
169 setup do | |
170 @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}} | |
171 @headers = { :authorization => credentials('jsmith') } | |
172 end | |
173 | |
174 should_allow_api_authentication(:put, | |
175 '/issues/6.xml', | |
176 {:issue => {:subject => 'API update', :notes => 'A new note'}}, | |
177 {:success_code => :ok}) | |
178 | |
179 should "not create a new issue" do | |
180 assert_no_difference('Issue.count') do | |
181 put '/issues/6.xml', @parameters, @headers | |
182 end | |
183 end | |
184 | |
185 should "create a new journal" do | |
186 assert_difference('Journal.count') do | |
187 put '/issues/6.xml', @parameters, @headers | |
188 end | |
189 end | |
190 | |
191 should "add the note to the journal" do | |
192 put '/issues/6.xml', @parameters, @headers | |
193 | |
194 journal = Journal.last | |
195 assert_equal "A new note", journal.notes | |
196 end | |
197 | |
198 should "update the issue" do | |
199 put '/issues/6.xml', @parameters, @headers | |
200 | |
201 issue = Issue.find(6) | |
202 assert_equal "API update", issue.subject | |
203 end | |
204 | |
205 end | |
206 | |
207 context "PUT /issues/6.xml with failed update" do | |
208 setup do | |
209 @parameters = {:issue => {:subject => ''}} | |
210 @headers = { :authorization => credentials('jsmith') } | |
211 end | |
212 | |
213 should_allow_api_authentication(:put, | |
214 '/issues/6.xml', | |
215 {:issue => {:subject => ''}}, # Missing subject should fail | |
216 {:success_code => :unprocessable_entity}) | |
217 | |
218 should "not create a new issue" do | |
219 assert_no_difference('Issue.count') do | |
220 put '/issues/6.xml', @parameters, @headers | |
221 end | |
222 end | |
223 | |
224 should "not create a new journal" do | |
225 assert_no_difference('Journal.count') do | |
226 put '/issues/6.xml', @parameters, @headers | |
227 end | |
228 end | |
229 | |
230 should "have an errors tag" do | |
231 put '/issues/6.xml', @parameters, @headers | |
232 | |
233 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"} | |
234 end | |
235 end | |
236 | |
237 context "PUT /issues/6.json" do | |
238 setup do | |
239 @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}} | |
240 @headers = { :authorization => credentials('jsmith') } | |
241 end | |
242 | |
243 should_allow_api_authentication(:put, | |
244 '/issues/6.json', | |
245 {:issue => {:subject => 'API update', :notes => 'A new note'}}, | |
246 {:success_code => :ok}) | |
247 | |
248 should "not create a new issue" do | |
249 assert_no_difference('Issue.count') do | |
250 put '/issues/6.json', @parameters, @headers | |
251 end | |
252 end | |
253 | |
254 should "create a new journal" do | |
255 assert_difference('Journal.count') do | |
256 put '/issues/6.json', @parameters, @headers | |
257 end | |
258 end | |
259 | |
260 should "add the note to the journal" do | |
261 put '/issues/6.json', @parameters, @headers | |
262 | |
263 journal = Journal.last | |
264 assert_equal "A new note", journal.notes | |
265 end | |
266 | |
267 should "update the issue" do | |
268 put '/issues/6.json', @parameters, @headers | |
269 | |
270 issue = Issue.find(6) | |
271 assert_equal "API update", issue.subject | |
272 end | |
273 | |
274 end | |
275 | |
276 context "PUT /issues/6.json with failed update" do | |
277 setup do | |
278 @parameters = {:issue => {:subject => ''}} | |
279 @headers = { :authorization => credentials('jsmith') } | |
280 end | |
281 | |
282 should_allow_api_authentication(:put, | |
283 '/issues/6.json', | |
284 {:issue => {:subject => ''}}, # Missing subject should fail | |
285 {:success_code => :unprocessable_entity}) | |
286 | |
287 should "not create a new issue" do | |
288 assert_no_difference('Issue.count') do | |
289 put '/issues/6.json', @parameters, @headers | |
290 end | |
291 end | |
292 | |
293 should "not create a new journal" do | |
294 assert_no_difference('Journal.count') do | |
295 put '/issues/6.json', @parameters, @headers | |
296 end | |
297 end | |
298 | |
299 should "have an errors attribute" do | |
300 put '/issues/6.json', @parameters, @headers | |
301 | |
302 json = ActiveSupport::JSON.decode(response.body) | |
303 assert_equal "can't be blank", json.first['subject'] | |
304 end | |
305 end | |
306 | |
307 context "DELETE /issues/1.xml" do | |
308 should_allow_api_authentication(:delete, | |
309 '/issues/6.xml', | |
310 {}, | |
311 {:success_code => :ok}) | |
312 | |
313 should "delete the issue" do | |
314 assert_difference('Issue.count',-1) do | |
315 delete '/issues/6.xml', {}, :authorization => credentials('jsmith') | |
316 end | |
317 | |
318 assert_nil Issue.find_by_id(6) | |
319 end | |
320 end | |
321 | |
322 context "DELETE /issues/1.json" do | |
323 should_allow_api_authentication(:delete, | |
324 '/issues/6.json', | |
325 {}, | |
326 {:success_code => :ok}) | |
327 | |
328 should "delete the issue" do | |
329 assert_difference('Issue.count',-1) do | |
330 delete '/issues/6.json', {}, :authorization => credentials('jsmith') | |
331 end | |
332 | |
333 assert_nil Issue.find_by_id(6) | |
334 end | |
335 end | |
336 | |
337 def credentials(user, password=nil) | |
338 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) | |
339 end | |
340 end |