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