##// END OF EJS Templates
Removed some test contexts....
Jean-Philippe Lang -
r13264:4989c9f6a845
parent child
Show More
@@ -29,6 +29,8 class Redmine::ApiTest::HttpBasicLoginTest < Redmine::ApiTest::Base
29 def setup
29 def setup
30 Setting.rest_api_enabled = '1'
30 Setting.rest_api_enabled = '1'
31 Setting.login_required = '1'
31 Setting.login_required = '1'
32 project = Project.find('onlinestore')
33 EnabledModule.create(:project => project, :name => 'news')
32 end
34 end
33
35
34 def teardown
36 def teardown
@@ -36,19 +38,6 class Redmine::ApiTest::HttpBasicLoginTest < Redmine::ApiTest::Base
36 Setting.login_required = '0'
38 Setting.login_required = '0'
37 end
39 end
38
40
39 # Using the NewsController because it's a simple API.
40 context "get /news" do
41 setup do
42 project = Project.find('onlinestore')
43 EnabledModule.create(:project => project, :name => 'news')
44 end
45
46 context "in :xml format" do
47 should_allow_http_basic_auth_with_username_and_password(:get, "/projects/onlinestore/news.xml")
41 should_allow_http_basic_auth_with_username_and_password(:get, "/projects/onlinestore/news.xml")
48 end
49
50 context "in :json format" do
51 should_allow_http_basic_auth_with_username_and_password(:get, "/projects/onlinestore/news.json")
42 should_allow_http_basic_auth_with_username_and_password(:get, "/projects/onlinestore/news.json")
52 end
43 end
53 end
54 end
@@ -36,15 +36,6 class Redmine::ApiTest::HttpBasicLoginWithApiTokenTest < Redmine::ApiTest::Base
36 Setting.login_required = '0'
36 Setting.login_required = '0'
37 end
37 end
38
38
39 # Using the NewsController because it's a simple API.
40 context "get /news" do
41
42 context "in :xml format" do
43 should_allow_http_basic_auth_with_key(:get, "/news.xml")
39 should_allow_http_basic_auth_with_key(:get, "/news.xml")
44 end
45
46 context "in :json format" do
47 should_allow_http_basic_auth_with_key(:get, "/news.json")
40 should_allow_http_basic_auth_with_key(:get, "/news.json")
48 end
41 end
49 end
50 end
@@ -48,45 +48,69 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
48 Setting.rest_api_enabled = '1'
48 Setting.rest_api_enabled = '1'
49 end
49 end
50
50
51 context "/issues" do
52 # Use a private project to make sure auth is really working and not just
51 # Use a private project to make sure auth is really working and not just
53 # only showing public issues.
52 # only showing public issues.
54 should_allow_api_authentication(:get, "/projects/private-child/issues.xml")
53 should_allow_api_authentication(:get, "/projects/private-child/issues.xml")
54 should_allow_api_authentication(:get, "/projects/private-child/issues.json")
55
55
56 should "contain metadata" do
56 should_allow_api_authentication(:get, "/issues/6.xml")
57 get '/issues.xml'
57 should_allow_api_authentication(:get, "/issues/6.json")
58
58
59 assert_select 'issues[type=array][total_count=?][limit="25"][offset="0"]', assigns(:issue_count).to_s
59 should_allow_api_authentication(
60 end
60 :post,
61 '/issues.xml',
62 {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}},
63 {:success_code => :created}
64 )
65 should_allow_api_authentication(:post,
66 '/issues.json',
67 {:issue => {:project_id => 1, :subject => 'API test',
68 :tracker_id => 2, :status_id => 3}},
69 {:success_code => :created})
61
70
62 context "with offset and limit" do
71 should_allow_api_authentication(:put,
63 should "use the params" do
72 '/issues/6.xml',
64 get '/issues.xml?offset=2&limit=3'
73 {:issue => {:subject => 'API update', :notes => 'A new note'}},
74 {:success_code => :ok})
75 should_allow_api_authentication(:put,
76 '/issues/6.json',
77 {:issue => {:subject => 'API update', :notes => 'A new note'}},
78 {:success_code => :ok})
65
79
66 assert_equal 3, assigns(:limit)
80 should_allow_api_authentication(:delete,
67 assert_equal 2, assigns(:offset)
81 '/issues/6.xml',
68 assert_select 'issues issue', 3
82 {},
69 end
83 {:success_code => :ok})
84 should_allow_api_authentication(:delete,
85 '/issues/6.json',
86 {},
87 {:success_code => :ok})
88
89 test "GET /issues.xml should contain metadata" do
90 get '/issues.xml'
91 assert_select 'issues[type=array][total_count=?][limit="25"][offset="0"]',
92 assigns(:issue_count).to_s
70 end
93 end
71
94
72 context "with nometa param" do
95 test "GET /issues.xml with nometa param should not contain metadata" do
73 should "not contain metadata" do
74 get '/issues.xml?nometa=1'
96 get '/issues.xml?nometa=1'
75
76 assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
97 assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
77 end
98 end
78 end
79
99
80 context "with nometa header" do
100 test "GET /issues.xml with nometa header should not contain metadata" do
81 should "not contain metadata" do
82 get '/issues.xml', {}, {'X-Redmine-Nometa' => '1'}
101 get '/issues.xml', {}, {'X-Redmine-Nometa' => '1'}
83
84 assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
102 assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
85 end
103 end
104
105 test "GET /issues.xml with offset and limit" do
106 get '/issues.xml?offset=2&limit=3'
107
108 assert_equal 3, assigns(:limit)
109 assert_equal 2, assigns(:offset)
110 assert_select 'issues issue', 3
86 end
111 end
87
112
88 context "with relations" do
113 test "GET /issues.xml with relations" do
89 should "display relations" do
90 get '/issues.xml?include=relations'
114 get '/issues.xml?include=relations'
91
115
92 assert_response :success
116 assert_response :success
@@ -102,46 +126,41 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
102 assert_select '~ relations relation', 0
126 assert_select '~ relations relation', 0
103 end
127 end
104 end
128 end
105 end
106
129
107 context "with invalid query params" do
130 test "GET /issues.xml with invalid query params" do
108 should "return errors" do
109 get '/issues.xml', {:f => ['start_date'], :op => {:start_date => '='}}
131 get '/issues.xml', {:f => ['start_date'], :op => {:start_date => '='}}
110
132
111 assert_response :unprocessable_entity
133 assert_response :unprocessable_entity
112 assert_equal 'application/xml', @response.content_type
134 assert_equal 'application/xml', @response.content_type
113 assert_select 'errors error', :text => "Start date can't be blank"
135 assert_select 'errors error', :text => "Start date can't be blank"
114 end
136 end
115 end
116
137
117 context "with custom field filter" do
138 test "GET /issues.xml with custom field filter" do
118 should "show only issues with the custom field value" do
119 get '/issues.xml',
139 get '/issues.xml',
120 {:set_filter => 1, :f => ['cf_1'], :op => {:cf_1 => '='},
140 {:set_filter => 1, :f => ['cf_1'], :op => {:cf_1 => '='}, :v => {:cf_1 => ['MySQL']}}
121 :v => {:cf_1 => ['MySQL']}}
141
122 expected_ids = Issue.visible.
142 expected_ids = Issue.visible.
123 joins(:custom_values).
143 joins(:custom_values).
124 where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
144 where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
145 assert expected_ids.any?
146
125 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
147 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
126 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
148 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
127 end
149 end
128 end
150 end
129 end
130
151
131 context "with custom field filter (shorthand method)" do
152 test "GET /issues.xml with custom field filter (shorthand method)" do
132 should "show only issues with the custom field value" do
133 get '/issues.xml', { :cf_1 => 'MySQL' }
153 get '/issues.xml', {:cf_1 => 'MySQL'}
134
154
135 expected_ids = Issue.visible.
155 expected_ids = Issue.visible.
136 joins(:custom_values).
156 joins(:custom_values).
137 where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
157 where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
158 assert expected_ids.any?
138
159
139 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
160 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
140 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
161 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
141 end
162 end
142 end
163 end
143 end
144 end
145
164
146 def test_index_should_include_issue_attributes
165 def test_index_should_include_issue_attributes
147 get '/issues.xml'
166 get '/issues.xml'
@@ -171,24 +190,18 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
171 assert_select 'issues>issue', :count => 2
190 assert_select 'issues>issue', :count => 2
172 end
191 end
173
192
174 context "/index.json" do
193 test "GET /issues.xml with filter" do
175 should_allow_api_authentication(:get, "/projects/private-child/issues.json")
176 end
177
178 context "/index.xml with filter" do
179 should "show only issues with the status_id" do
180 get '/issues.xml?status_id=5'
194 get '/issues.xml?status_id=5'
181
195
182 expected_ids = Issue.visible.where(:status_id => 5).map(&:id)
196 expected_ids = Issue.visible.where(:status_id => 5).map(&:id)
197 assert expected_ids.any?
183
198
184 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
199 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
185 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
200 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
186 end
201 end
187 end
202 end
188 end
189
203
190 context "/index.json with filter" do
204 test "GET /issues.json with filter" do
191 should "show only issues with the status_id" do
192 get '/issues.json?status_id=5'
205 get '/issues.json?status_id=5'
193
206
194 json = ActiveSupport::JSON.decode(response.body)
207 json = ActiveSupport::JSON.decode(response.body)
@@ -197,21 +210,7 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
197 assert status_ids_used.all? {|id| id == 5 }
210 assert status_ids_used.all? {|id| id == 5 }
198 end
211 end
199
212
200 end
213 test "GET /issues/:id.xml with journals" do
201
202 # Issue 6 is on a private project
203 context "/issues/6.xml" do
204 should_allow_api_authentication(:get, "/issues/6.xml")
205 end
206
207 context "/issues/6.json" do
208 should_allow_api_authentication(:get, "/issues/6.json")
209 end
210
211 context "GET /issues/:id" do
212 context "with journals" do
213 context ".xml" do
214 should "display journals" do
215 get '/issues/1.xml?include=journals'
214 get '/issues/1.xml?include=journals'
216
215
217 assert_select 'issue journals[type=array]' do
216 assert_select 'issue journals[type=array]' do
@@ -225,12 +224,8 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
225 end
224 end
226 end
225 end
227 end
226 end
228 end
229 end
230
227
231 context "with custom fields" do
228 test "GET /issues/:id.xml with custom fields" do
232 context ".xml" do
233 should "display custom fields" do
234 get '/issues/3.xml'
229 get '/issues/3.xml'
235
230
236 assert_select 'issue custom_fields[type=array]' do
231 assert_select 'issue custom_fields[type=array]' do
@@ -238,25 +233,18 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
238 assert_select 'value', :text => 'MySQL'
233 assert_select 'value', :text => 'MySQL'
239 end
234 end
240 end
235 end
241
242 assert_nothing_raised do
236 assert_nothing_raised do
243 Hash.from_xml(response.body).to_xml
237 Hash.from_xml(response.body).to_xml
244 end
238 end
245 end
239 end
246 end
247 end
248
240
249 context "with multi custom fields" do
241 test "GET /issues/:id.xml with multi custom fields" do
250 setup do
251 field = CustomField.find(1)
242 field = CustomField.find(1)
252 field.update_attribute :multiple, true
243 field.update_attribute :multiple, true
253 issue = Issue.find(3)
244 issue = Issue.find(3)
254 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
245 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
255 issue.save!
246 issue.save!
256 end
257
247
258 context ".xml" do
259 should "display custom fields" do
260 get '/issues/3.xml'
248 get '/issues/3.xml'
261 assert_response :success
249 assert_response :success
262
250
@@ -265,7 +253,6 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
265 assert_select 'value[type=array] value', 2
253 assert_select 'value[type=array] value', 2
266 end
254 end
267 end
255 end
268
269 xml = Hash.from_xml(response.body)
256 xml = Hash.from_xml(response.body)
270 custom_fields = xml['issue']['custom_fields']
257 custom_fields = xml['issue']['custom_fields']
271 assert_kind_of Array, custom_fields
258 assert_kind_of Array, custom_fields
@@ -273,12 +260,17 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
273 assert_kind_of Hash, field
260 assert_kind_of Hash, field
274 assert_equal ['MySQL', 'Oracle'], field['value'].sort
261 assert_equal ['MySQL', 'Oracle'], field['value'].sort
275 end
262 end
276 end
277
263
278 context ".json" do
264 test "GET /issues/:id.json with multi custom fields" do
279 should "display custom fields" do
265 field = CustomField.find(1)
266 field.update_attribute :multiple, true
267 issue = Issue.find(3)
268 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
269 issue.save!
270
280 get '/issues/3.json'
271 get '/issues/3.json'
281 assert_response :success
272 assert_response :success
273
282 json = ActiveSupport::JSON.decode(response.body)
274 json = ActiveSupport::JSON.decode(response.body)
283 custom_fields = json['issue']['custom_fields']
275 custom_fields = json['issue']['custom_fields']
284 assert_kind_of Array, custom_fields
276 assert_kind_of Array, custom_fields
@@ -286,20 +278,14 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
286 assert_kind_of Hash, field
278 assert_kind_of Hash, field
287 assert_equal ['MySQL', 'Oracle'], field['value'].sort
279 assert_equal ['MySQL', 'Oracle'], field['value'].sort
288 end
280 end
289 end
290 end
291
281
292 context "with empty value for multi custom field" do
282 test "GET /issues/:id.xml with empty value for multi custom field" do
293 setup do
294 field = CustomField.find(1)
283 field = CustomField.find(1)
295 field.update_attribute :multiple, true
284 field.update_attribute :multiple, true
296 issue = Issue.find(3)
285 issue = Issue.find(3)
297 issue.custom_field_values = {1 => ['']}
286 issue.custom_field_values = {1 => ['']}
298 issue.save!
287 issue.save!
299 end
300
288
301 context ".xml" do
302 should "display custom fields" do
303 get '/issues/3.xml'
289 get '/issues/3.xml'
304
290
305 assert_select 'issue custom_fields[type=array]' do
291 assert_select 'issue custom_fields[type=array]' do
@@ -307,7 +293,6 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
307 assert_select 'value[type=array]:empty'
293 assert_select 'value[type=array]:empty'
308 end
294 end
309 end
295 end
310
311 xml = Hash.from_xml(response.body)
296 xml = Hash.from_xml(response.body)
312 custom_fields = xml['issue']['custom_fields']
297 custom_fields = xml['issue']['custom_fields']
313 assert_kind_of Array, custom_fields
298 assert_kind_of Array, custom_fields
@@ -315,10 +300,14 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
315 assert_kind_of Hash, field
300 assert_kind_of Hash, field
316 assert_equal [], field['value']
301 assert_equal [], field['value']
317 end
302 end
318 end
319
303
320 context ".json" do
304 test "GET /issues/:id.json with empty value for multi custom field" do
321 should "display custom fields" do
305 field = CustomField.find(1)
306 field.update_attribute :multiple, true
307 issue = Issue.find(3)
308 issue.custom_field_values = {1 => ['']}
309 issue.save!
310
322 get '/issues/3.json'
311 get '/issues/3.json'
323 assert_response :success
312 assert_response :success
324 json = ActiveSupport::JSON.decode(response.body)
313 json = ActiveSupport::JSON.decode(response.body)
@@ -328,12 +317,8 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
328 assert_kind_of Hash, field
317 assert_kind_of Hash, field
329 assert_equal [], field['value'].sort
318 assert_equal [], field['value'].sort
330 end
319 end
331 end
332 end
333
320
334 context "with attachments" do
321 test "GET /issues/:id.xml with attachments" do
335 context ".xml" do
336 should "display attachments" do
337 get '/issues/3.xml?include=attachments'
322 get '/issues/3.xml?include=attachments'
338
323
339 assert_select 'issue attachments[type=array]' do
324 assert_select 'issue attachments[type=array]' do
@@ -344,61 +329,24 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
344 end
329 end
345 end
330 end
346 end
331 end
347 end
348 end
349
332
350 context "with subtasks" do
333 test "GET /issues/:id.xml with subtasks" do
351 setup do
334 issue = Issue.generate_with_descendants!(:project_id => 1)
352 @c1 = Issue.create!(
335 get "/issues/#{issue.id}.xml?include=children"
353 :status_id => 1, :subject => "child c1",
354 :tracker_id => 1, :project_id => 1, :author_id => 1,
355 :parent_issue_id => 1
356 )
357 @c2 = Issue.create!(
358 :status_id => 1, :subject => "child c2",
359 :tracker_id => 1, :project_id => 1, :author_id => 1,
360 :parent_issue_id => 1
361 )
362 @c3 = Issue.create!(
363 :status_id => 1, :subject => "child c3",
364 :tracker_id => 1, :project_id => 1, :author_id => 1,
365 :parent_issue_id => @c1.id
366 )
367 end
368
369 context ".xml" do
370 should "display children" do
371 get '/issues/1.xml?include=children'
372
336
373 assert_select 'issue children[type=array]' do
337 assert_select 'issue children[type=array]' do
374 assert_select 'issue', 2
338 assert_select 'issue', 2
375 assert_select 'issue[id=?]', @c1.id.to_s do
339 assert_select 'issue children', 1
376 assert_select 'subject', :text => 'child c1'
377 assert_select 'children' do
378 assert_select 'issue[id=?]', @c3.id.to_s
379 end
380 end
381 end
340 end
382 end
341 end
383
342
384 context ".json" do
343 test "GET /issues/:id.json with subtasks" do
385 should "display children" do
344 issue = Issue.generate_with_descendants!(:project_id => 1)
386 get '/issues/1.json?include=children'
345 get "/issues/#{issue.id}.json?include=children"
387
346
388 json = ActiveSupport::JSON.decode(response.body)
347 json = ActiveSupport::JSON.decode(response.body)
389 assert_equal([
348 assert_equal 2, json['issue']['children'].size
390 {
349 assert_equal 1, json['issue']['children'].select {|child| child.key?('children')}.size
391 'id' => @c1.id, 'subject' => 'child c1', 'tracker' => {'id' => 1, 'name' => 'Bug'},
392 'children' => [{'id' => @c3.id, 'subject' => 'child c3',
393 'tracker' => {'id' => 1, 'name' => 'Bug'} }]
394 },
395 { 'id' => @c2.id, 'subject' => 'child c2', 'tracker' => {'id' => 1, 'name' => 'Bug'} }
396 ],
397 json['issue']['children'])
398 end
399 end
400 end
401 end
402 end
350 end
403
351
404 def test_show_should_include_issue_attributes
352 def test_show_should_include_issue_attributes
@@ -421,14 +369,7 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
421 end
369 end
422 end
370 end
423
371
424 context "POST /issues.xml" do
372 test "POST /issues.xml should create an issue with the attributes" do
425 should_allow_api_authentication(
426 :post,
427 '/issues.xml',
428 {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}},
429 {:success_code => :created}
430 )
431 should "create an issue with the attributes" do
432 assert_difference('Issue.count') do
373 assert_difference('Issue.count') do
433 post '/issues.xml',
374 post '/issues.xml',
434 {:issue => {:project_id => 1, :subject => 'API test',
375 {:issue => {:project_id => 1, :subject => 'API test',
@@ -444,7 +385,6 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
444 assert_equal 'application/xml', @response.content_type
385 assert_equal 'application/xml', @response.content_type
445 assert_select 'issue > id', :text => issue.id.to_s
386 assert_select 'issue > id', :text => issue.id.to_s
446 end
387 end
447 end
448
388
449 test "POST /issues.xml with watcher_user_ids should create issue with watchers" do
389 test "POST /issues.xml with watcher_user_ids should create issue with watchers" do
450 assert_difference('Issue.count') do
390 assert_difference('Issue.count') do
@@ -458,24 +398,15 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
458 assert_equal [1, 3], issue.watcher_user_ids.sort
398 assert_equal [1, 3], issue.watcher_user_ids.sort
459 end
399 end
460
400
461 context "POST /issues.xml with failure" do
401 test "POST /issues.xml with failure should return errors" do
462 should "have an errors tag" do
463 assert_no_difference('Issue.count') do
402 assert_no_difference('Issue.count') do
464 post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
403 post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
465 end
404 end
466
405
467 assert_select 'errors error', :text => "Subject can't be blank"
406 assert_select 'errors error', :text => "Subject can't be blank"
468 end
407 end
469 end
470
471 context "POST /issues.json" do
472 should_allow_api_authentication(:post,
473 '/issues.json',
474 {:issue => {:project_id => 1, :subject => 'API test',
475 :tracker_id => 2, :status_id => 3}},
476 {:success_code => :created})
477
408
478 should "create an issue with the attributes" do
409 test "POST /issues.json should create an issue with the attributes" do
479 assert_difference('Issue.count') do
410 assert_difference('Issue.count') do
480 post '/issues.json',
411 post '/issues.json',
481 {:issue => {:project_id => 1, :subject => 'API test',
412 {:issue => {:project_id => 1, :subject => 'API test',
@@ -490,10 +421,7 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
490 assert_equal 'API test', issue.subject
421 assert_equal 'API test', issue.subject
491 end
422 end
492
423
493 end
424 test "POST /issues.json with failure should return errors" do
494
495 context "POST /issues.json with failure" do
496 should "have an errors element" do
497 assert_no_difference('Issue.count') do
425 assert_no_difference('Issue.count') do
498 post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
426 post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
499 end
427 end
@@ -501,204 +429,109 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
501 json = ActiveSupport::JSON.decode(response.body)
429 json = ActiveSupport::JSON.decode(response.body)
502 assert json['errors'].include?("Subject can't be blank")
430 assert json['errors'].include?("Subject can't be blank")
503 end
431 end
504 end
505
506 # Issue 6 is on a private project
507 context "PUT /issues/6.xml" do
508 setup do
509 @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
510 end
511
512 should_allow_api_authentication(:put,
513 '/issues/6.xml',
514 {:issue => {:subject => 'API update', :notes => 'A new note'}},
515 {:success_code => :ok})
516
517 should "not create a new issue" do
518 assert_no_difference('Issue.count') do
519 put '/issues/6.xml', @parameters, credentials('jsmith')
520 end
521 end
522
432
523 should "create a new journal" do
433 test "PUT /issues/:id.xml" do
524 assert_difference('Journal.count') do
434 assert_difference('Journal.count') do
525 put '/issues/6.xml', @parameters, credentials('jsmith')
435 put '/issues/6.xml',
526 end
436 {:issue => {:subject => 'API update', :notes => 'A new note'}},
527 end
437 credentials('jsmith')
528
529 should "add the note to the journal" do
530 put '/issues/6.xml', @parameters, credentials('jsmith')
531
532 journal = Journal.last
533 assert_equal "A new note", journal.notes
534 end
438 end
535
439
536 should "update the issue" do
537 put '/issues/6.xml', @parameters, credentials('jsmith')
538
539 issue = Issue.find(6)
440 issue = Issue.find(6)
540 assert_equal "API update", issue.subject
441 assert_equal "API update", issue.subject
442 journal = Journal.last
443 assert_equal "A new note", journal.notes
541 end
444 end
542
445
543 end
446 test "PUT /issues/:id.xml with custom fields" do
544
447 put '/issues/3.xml',
545 context "PUT /issues/3.xml with custom fields" do
448 {:issue => {:custom_fields => [
546 setup do
449 {'id' => '1', 'value' => 'PostgreSQL' },
547 @parameters = {
450 {'id' => '2', 'value' => '150'}
548 :issue => {:custom_fields => [{'id' => '1', 'value' => 'PostgreSQL' },
451 ]}},
549 {'id' => '2', 'value' => '150'}]}
452 credentials('jsmith')
550 }
551 end
552
553 should "update custom fields" do
554 assert_no_difference('Issue.count') do
555 put '/issues/3.xml', @parameters, credentials('jsmith')
556 end
557
453
558 issue = Issue.find(3)
454 issue = Issue.find(3)
559 assert_equal '150', issue.custom_value_for(2).value
455 assert_equal '150', issue.custom_value_for(2).value
560 assert_equal 'PostgreSQL', issue.custom_value_for(1).value
456 assert_equal 'PostgreSQL', issue.custom_value_for(1).value
561 end
457 end
562 end
563
458
564 context "PUT /issues/3.xml with multi custom fields" do
459 test "PUT /issues/:id.xml with multi custom fields" do
565 setup do
566 field = CustomField.find(1)
460 field = CustomField.find(1)
567 field.update_attribute :multiple, true
461 field.update_attribute :multiple, true
568 @parameters = {
569 :issue => {:custom_fields => [{'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
570 {'id' => '2', 'value' => '150'}]}
571 }
572 end
573
462
574 should "update custom fields" do
463 put '/issues/3.xml',
575 assert_no_difference('Issue.count') do
464 {:issue => {:custom_fields => [
576 put '/issues/3.xml', @parameters, credentials('jsmith')
465 {'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
577 end
466 {'id' => '2', 'value' => '150'}
467 ]}},
468 credentials('jsmith')
578
469
579 issue = Issue.find(3)
470 issue = Issue.find(3)
580 assert_equal '150', issue.custom_value_for(2).value
471 assert_equal '150', issue.custom_value_for(2).value
581 assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
472 assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
582 end
473 end
583 end
584
474
585 context "PUT /issues/3.xml with project change" do
475 test "PUT /issues/:id.xml with project change" do
586 setup do
476 put '/issues/3.xml',
587 @parameters = {:issue => {:project_id => 2, :subject => 'Project changed'}}
477 {:issue => {:project_id => 2, :subject => 'Project changed'}},
588 end
478 credentials('jsmith')
589
590 should "update project" do
591 assert_no_difference('Issue.count') do
592 put '/issues/3.xml', @parameters, credentials('jsmith')
593 end
594
479
595 issue = Issue.find(3)
480 issue = Issue.find(3)
596 assert_equal 2, issue.project_id
481 assert_equal 2, issue.project_id
597 assert_equal 'Project changed', issue.subject
482 assert_equal 'Project changed', issue.subject
598 end
483 end
599 end
600
601 context "PUT /issues/6.xml with failed update" do
602 setup do
603 @parameters = {:issue => {:subject => ''}}
604 end
605
606 should "not create a new issue" do
607 assert_no_difference('Issue.count') do
608 put '/issues/6.xml', @parameters, credentials('jsmith')
609 end
610 end
611
484
612 should "not create a new journal" do
485 test "PUT /issues/:id.xml with failed update" do
613 assert_no_difference('Journal.count') do
486 put '/issues/6.xml', {:issue => {:subject => ''}}, credentials('jsmith')
614 put '/issues/6.xml', @parameters, credentials('jsmith')
615 end
616 end
617
618 should "have an errors tag" do
619 put '/issues/6.xml', @parameters, credentials('jsmith')
620
487
488 assert_response :unprocessable_entity
621 assert_select 'errors error', :text => "Subject can't be blank"
489 assert_select 'errors error', :text => "Subject can't be blank"
622 end
490 end
623 end
624
625 context "PUT /issues/6.json" do
626 setup do
627 @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
628 end
629
630 should_allow_api_authentication(:put,
631 '/issues/6.json',
632 {:issue => {:subject => 'API update', :notes => 'A new note'}},
633 {:success_code => :ok})
634
491
635 should "update the issue" do
492 test "PUT /issues/:id.json" do
636 assert_no_difference('Issue.count') do
637 assert_difference('Journal.count') do
493 assert_difference('Journal.count') do
638 put '/issues/6.json', @parameters, credentials('jsmith')
494 put '/issues/6.json',
495 {:issue => {:subject => 'API update', :notes => 'A new note'}},
496 credentials('jsmith')
639
497
640 assert_response :ok
498 assert_response :ok
641 assert_equal '', response.body
499 assert_equal '', response.body
642 end
500 end
643 end
644
501
645 issue = Issue.find(6)
502 issue = Issue.find(6)
646 assert_equal "API update", issue.subject
503 assert_equal "API update", issue.subject
647 journal = Journal.last
504 journal = Journal.last
648 assert_equal "A new note", journal.notes
505 assert_equal "A new note", journal.notes
649 end
506 end
650 end
651
507
652 context "PUT /issues/6.json with failed update" do
508 test "PUT /issues/:id.json with failed update" do
653 should "return errors" do
654 assert_no_difference('Issue.count') do
655 assert_no_difference('Journal.count') do
656 put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')
509 put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')
657
510
658 assert_response :unprocessable_entity
511 assert_response :unprocessable_entity
659 end
660 end
661
662 json = ActiveSupport::JSON.decode(response.body)
512 json = ActiveSupport::JSON.decode(response.body)
663 assert json['errors'].include?("Subject can't be blank")
513 assert json['errors'].include?("Subject can't be blank")
664 end
514 end
665 end
666
667 context "DELETE /issues/1.xml" do
668 should_allow_api_authentication(:delete,
669 '/issues/6.xml',
670 {},
671 {:success_code => :ok})
672
515
673 should "delete the issue" do
516 test "DELETE /issues/:id.xml" do
674 assert_difference('Issue.count', -1) do
517 assert_difference('Issue.count', -1) do
675 delete '/issues/6.xml', {}, credentials('jsmith')
518 delete '/issues/6.xml', {}, credentials('jsmith')
676
519
677 assert_response :ok
520 assert_response :ok
678 assert_equal '', response.body
521 assert_equal '', response.body
679 end
522 end
680
681 assert_nil Issue.find_by_id(6)
523 assert_nil Issue.find_by_id(6)
682 end
524 end
683 end
684
525
685 context "DELETE /issues/1.json" do
526 test "DELETE /issues/:id.json" do
686 should_allow_api_authentication(:delete,
687 '/issues/6.json',
688 {},
689 {:success_code => :ok})
690
691 should "delete the issue" do
692 assert_difference('Issue.count', -1) do
527 assert_difference('Issue.count', -1) do
693 delete '/issues/6.json', {}, credentials('jsmith')
528 delete '/issues/6.json', {}, credentials('jsmith')
694
529
695 assert_response :ok
530 assert_response :ok
696 assert_equal '', response.body
531 assert_equal '', response.body
697 end
532 end
698
699 assert_nil Issue.find_by_id(6)
533 assert_nil Issue.find_by_id(6)
700 end
534 end
701 end
702
535
703 test "POST /issues/:id/watchers.xml should add watcher" do
536 test "POST /issues/:id/watchers.xml should add watcher" do
704 assert_difference 'Watcher.count' do
537 assert_difference 'Watcher.count' do
@@ -601,98 +601,63 class UserTest < ActiveSupport::TestCase
601 end
601 end
602
602
603 if ldap_configured?
603 if ldap_configured?
604 context "#try_to_login using LDAP" do
604 test "#try_to_login using LDAP with failed connection to the LDAP server" do
605 context "with failed connection to the LDAP server" do
605 auth_source = AuthSourceLdap.find(1)
606 should "return nil" do
607 @auth_source = AuthSourceLdap.find(1)
608 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
606 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
609
607
610 assert_equal nil, User.try_to_login('edavis', 'wrong')
608 assert_equal nil, User.try_to_login('edavis', 'wrong')
611 end
609 end
612 end
613
610
614 context "with an unsuccessful authentication" do
611 test "#try_to_login using LDAP" do
615 should "return nil" do
616 assert_equal nil, User.try_to_login('edavis', 'wrong')
612 assert_equal nil, User.try_to_login('edavis', 'wrong')
617 end
613 end
618 end
619
614
620 context "binding with user's account" do
615 test "#try_to_login using LDAP binding with user's account" do
621 setup do
616 auth_source = AuthSourceLdap.find(1)
622 @auth_source = AuthSourceLdap.find(1)
617 auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
623 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
618 auth_source.account_password = ''
624 @auth_source.account_password = ''
619 auth_source.save!
625 @auth_source.save!
626
620
627 @ldap_user = User.new(:mail => 'example1@redmine.org', :firstname => 'LDAP', :lastname => 'user', :auth_source_id => 1)
621 ldap_user = User.new(:mail => 'example1@redmine.org', :firstname => 'LDAP', :lastname => 'user', :auth_source_id => 1)
628 @ldap_user.login = 'example1'
622 ldap_user.login = 'example1'
629 @ldap_user.save!
623 ldap_user.save!
630 end
631
624
632 context "with a successful authentication" do
633 should "return the user" do
634 assert_equal @ldap_user, User.try_to_login('example1', '123456')
625 assert_equal @ldap_user, User.try_to_login('example1', '123456')
635 end
636 end
637
638 context "with an unsuccessful authentication" do
639 should "return nil" do
640 assert_nil User.try_to_login('example1', '11111')
626 assert_nil User.try_to_login('example1', '11111')
641 end
627 end
642 end
643 end
644
628
645 context "on the fly registration" do
629 test "#try_to_login using LDAP on the fly registration" do
646 setup do
630 AuthSourceLdap.find(1).update_attribute :onthefly_register, true
647 @auth_source = AuthSourceLdap.find(1)
648 @auth_source.update_attribute :onthefly_register, true
649 end
650
631
651 context "with a successful authentication" do
652 should "create a new user account if it doesn't exist" do
653 assert_difference('User.count') do
632 assert_difference('User.count') do
654 user = User.try_to_login('edavis', '123456')
633 assert User.try_to_login('edavis', '123456')
655 assert !user.admin?
656 end
634 end
657 end
658
659 should "retrieve existing user" do
660 user = User.try_to_login('edavis', '123456')
661 user.admin = true
662 user.save!
663
635
664 assert_no_difference('User.count') do
636 assert_no_difference('User.count') do
665 user = User.try_to_login('edavis', '123456')
637 assert User.try_to_login('edavis', '123456')
666 assert user.admin?
667 end
668 end
669 end
638 end
670
639
671 context "binding with user's account" do
640 assert_nil User.try_to_login('example1', '11111')
672 setup do
673 @auth_source = AuthSourceLdap.find(1)
674 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
675 @auth_source.account_password = ''
676 @auth_source.save!
677 end
641 end
678
642
679 context "with a successful authentication" do
643 test "#try_to_login using LDAP on the fly registration and binding with user's account" do
680 should "create a new user account if it doesn't exist" do
644 auth_source = AuthSourceLdap.find(1)
645 auth_source.update_attribute :onthefly_register, true
646 auth_source = AuthSourceLdap.find(1)
647 auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
648 auth_source.account_password = ''
649 auth_source.save!
650
681 assert_difference('User.count') do
651 assert_difference('User.count') do
682 user = User.try_to_login('example1', '123456')
652 assert User.try_to_login('example1', '123456')
683 assert_kind_of User, user
684 end
685 end
653 end
654
655 assert_no_difference('User.count') do
656 assert User.try_to_login('edavis', '123456')
686 end
657 end
687
658
688 context "with an unsuccessful authentication" do
689 should "return nil" do
690 assert_nil User.try_to_login('example1', '11111')
659 assert_nil User.try_to_login('example1', '11111')
691 end
660 end
692 end
693 end
694 end
695 end
696
661
697 else
662 else
698 puts "Skipping LDAP tests."
663 puts "Skipping LDAP tests."
General Comments 0
You need to be logged in to leave comments. Login now