##// END OF EJS Templates
Use raw request content in tests to make sure that XML/JSON payload is properly parsed....
Jean-Philippe Lang -
r13410:3fe8197d7323
parent child
Show More
@@ -1,647 +1,665
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../../test_helper', __FILE__)
18 require File.expand_path('../../../test_helper', __FILE__)
19
19
20 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
20 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
21 fixtures :projects,
21 fixtures :projects,
22 :users,
22 :users,
23 :roles,
23 :roles,
24 :members,
24 :members,
25 :member_roles,
25 :member_roles,
26 :issues,
26 :issues,
27 :issue_statuses,
27 :issue_statuses,
28 :issue_relations,
28 :issue_relations,
29 :versions,
29 :versions,
30 :trackers,
30 :trackers,
31 :projects_trackers,
31 :projects_trackers,
32 :issue_categories,
32 :issue_categories,
33 :enabled_modules,
33 :enabled_modules,
34 :enumerations,
34 :enumerations,
35 :attachments,
35 :attachments,
36 :workflows,
36 :workflows,
37 :custom_fields,
37 :custom_fields,
38 :custom_values,
38 :custom_values,
39 :custom_fields_projects,
39 :custom_fields_projects,
40 :custom_fields_trackers,
40 :custom_fields_trackers,
41 :time_entries,
41 :time_entries,
42 :journals,
42 :journals,
43 :journal_details,
43 :journal_details,
44 :queries,
44 :queries,
45 :attachments
45 :attachments
46
46
47 test "GET /issues.xml should contain metadata" do
47 test "GET /issues.xml should contain metadata" do
48 get '/issues.xml'
48 get '/issues.xml'
49 assert_select 'issues[type=array][total_count=?][limit="25"][offset="0"]',
49 assert_select 'issues[type=array][total_count=?][limit="25"][offset="0"]',
50 assigns(:issue_count).to_s
50 assigns(:issue_count).to_s
51 end
51 end
52
52
53 test "GET /issues.xml with nometa param should not contain metadata" do
53 test "GET /issues.xml with nometa param should not contain metadata" do
54 get '/issues.xml?nometa=1'
54 get '/issues.xml?nometa=1'
55 assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
55 assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
56 end
56 end
57
57
58 test "GET /issues.xml with nometa header should not contain metadata" do
58 test "GET /issues.xml with nometa header should not contain metadata" do
59 get '/issues.xml', {}, {'X-Redmine-Nometa' => '1'}
59 get '/issues.xml', {}, {'X-Redmine-Nometa' => '1'}
60 assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
60 assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
61 end
61 end
62
62
63 test "GET /issues.xml with offset and limit" do
63 test "GET /issues.xml with offset and limit" do
64 get '/issues.xml?offset=2&limit=3'
64 get '/issues.xml?offset=2&limit=3'
65
65
66 assert_equal 3, assigns(:limit)
66 assert_equal 3, assigns(:limit)
67 assert_equal 2, assigns(:offset)
67 assert_equal 2, assigns(:offset)
68 assert_select 'issues issue', 3
68 assert_select 'issues issue', 3
69 end
69 end
70
70
71 test "GET /issues.xml with relations" do
71 test "GET /issues.xml with relations" do
72 get '/issues.xml?include=relations'
72 get '/issues.xml?include=relations'
73
73
74 assert_response :success
74 assert_response :success
75 assert_equal 'application/xml', @response.content_type
75 assert_equal 'application/xml', @response.content_type
76
76
77 assert_select 'issue id:content(3)' do
77 assert_select 'issue id:content(3)' do
78 assert_select '~ relations relation', 1
78 assert_select '~ relations relation', 1
79 assert_select '~ relations relation[id="2"][issue_id="2"][issue_to_id="3"][relation_type=relates]'
79 assert_select '~ relations relation[id="2"][issue_id="2"][issue_to_id="3"][relation_type=relates]'
80 end
80 end
81
81
82 assert_select 'issue id:content(1)' do
82 assert_select 'issue id:content(1)' do
83 assert_select '~ relations'
83 assert_select '~ relations'
84 assert_select '~ relations relation', 0
84 assert_select '~ relations relation', 0
85 end
85 end
86 end
86 end
87
87
88 test "GET /issues.xml with invalid query params" do
88 test "GET /issues.xml with invalid query params" do
89 get '/issues.xml', {:f => ['start_date'], :op => {:start_date => '='}}
89 get '/issues.xml', {:f => ['start_date'], :op => {:start_date => '='}}
90
90
91 assert_response :unprocessable_entity
91 assert_response :unprocessable_entity
92 assert_equal 'application/xml', @response.content_type
92 assert_equal 'application/xml', @response.content_type
93 assert_select 'errors error', :text => "Start date cannot be blank"
93 assert_select 'errors error', :text => "Start date cannot be blank"
94 end
94 end
95
95
96 test "GET /issues.xml with custom field filter" do
96 test "GET /issues.xml with custom field filter" do
97 get '/issues.xml',
97 get '/issues.xml',
98 {:set_filter => 1, :f => ['cf_1'], :op => {:cf_1 => '='}, :v => {:cf_1 => ['MySQL']}}
98 {:set_filter => 1, :f => ['cf_1'], :op => {:cf_1 => '='}, :v => {:cf_1 => ['MySQL']}}
99
99
100 expected_ids = Issue.visible.
100 expected_ids = Issue.visible.
101 joins(:custom_values).
101 joins(:custom_values).
102 where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
102 where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
103 assert expected_ids.any?
103 assert expected_ids.any?
104
104
105 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
105 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
106 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
106 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
107 end
107 end
108 end
108 end
109
109
110 test "GET /issues.xml with custom field filter (shorthand method)" do
110 test "GET /issues.xml with custom field filter (shorthand method)" do
111 get '/issues.xml', {:cf_1 => 'MySQL'}
111 get '/issues.xml', {:cf_1 => 'MySQL'}
112
112
113 expected_ids = Issue.visible.
113 expected_ids = Issue.visible.
114 joins(:custom_values).
114 joins(:custom_values).
115 where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
115 where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
116 assert expected_ids.any?
116 assert expected_ids.any?
117
117
118 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
118 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
119 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
119 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
120 end
120 end
121 end
121 end
122
122
123 def test_index_should_include_issue_attributes
123 def test_index_should_include_issue_attributes
124 get '/issues.xml'
124 get '/issues.xml'
125 assert_select 'issues>issue>is_private', :text => 'false'
125 assert_select 'issues>issue>is_private', :text => 'false'
126 end
126 end
127
127
128 def test_index_should_allow_timestamp_filtering
128 def test_index_should_allow_timestamp_filtering
129 Issue.delete_all
129 Issue.delete_all
130 Issue.generate!(:subject => '1').update_column(:updated_on, Time.parse("2014-01-02T10:25:00Z"))
130 Issue.generate!(:subject => '1').update_column(:updated_on, Time.parse("2014-01-02T10:25:00Z"))
131 Issue.generate!(:subject => '2').update_column(:updated_on, Time.parse("2014-01-02T12:13:00Z"))
131 Issue.generate!(:subject => '2').update_column(:updated_on, Time.parse("2014-01-02T12:13:00Z"))
132
132
133 get '/issues.xml',
133 get '/issues.xml',
134 {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '<='},
134 {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '<='},
135 :v => {:updated_on => ['2014-01-02T12:00:00Z']}}
135 :v => {:updated_on => ['2014-01-02T12:00:00Z']}}
136 assert_select 'issues>issue', :count => 1
136 assert_select 'issues>issue', :count => 1
137 assert_select 'issues>issue>subject', :text => '1'
137 assert_select 'issues>issue>subject', :text => '1'
138
138
139 get '/issues.xml',
139 get '/issues.xml',
140 {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '>='},
140 {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '>='},
141 :v => {:updated_on => ['2014-01-02T12:00:00Z']}}
141 :v => {:updated_on => ['2014-01-02T12:00:00Z']}}
142 assert_select 'issues>issue', :count => 1
142 assert_select 'issues>issue', :count => 1
143 assert_select 'issues>issue>subject', :text => '2'
143 assert_select 'issues>issue>subject', :text => '2'
144
144
145 get '/issues.xml',
145 get '/issues.xml',
146 {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '>='},
146 {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '>='},
147 :v => {:updated_on => ['2014-01-02T08:00:00Z']}}
147 :v => {:updated_on => ['2014-01-02T08:00:00Z']}}
148 assert_select 'issues>issue', :count => 2
148 assert_select 'issues>issue', :count => 2
149 end
149 end
150
150
151 test "GET /issues.xml with filter" do
151 test "GET /issues.xml with filter" do
152 get '/issues.xml?status_id=5'
152 get '/issues.xml?status_id=5'
153
153
154 expected_ids = Issue.visible.where(:status_id => 5).map(&:id)
154 expected_ids = Issue.visible.where(:status_id => 5).map(&:id)
155 assert expected_ids.any?
155 assert expected_ids.any?
156
156
157 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
157 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
158 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
158 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
159 end
159 end
160 end
160 end
161
161
162 test "GET /issues.json with filter" do
162 test "GET /issues.json with filter" do
163 get '/issues.json?status_id=5'
163 get '/issues.json?status_id=5'
164
164
165 json = ActiveSupport::JSON.decode(response.body)
165 json = ActiveSupport::JSON.decode(response.body)
166 status_ids_used = json['issues'].collect {|j| j['status']['id'] }
166 status_ids_used = json['issues'].collect {|j| j['status']['id'] }
167 assert_equal 3, status_ids_used.length
167 assert_equal 3, status_ids_used.length
168 assert status_ids_used.all? {|id| id == 5 }
168 assert status_ids_used.all? {|id| id == 5 }
169 end
169 end
170
170
171 test "GET /issues/:id.xml with journals" do
171 test "GET /issues/:id.xml with journals" do
172 get '/issues/1.xml?include=journals'
172 get '/issues/1.xml?include=journals'
173
173
174 assert_select 'issue journals[type=array]' do
174 assert_select 'issue journals[type=array]' do
175 assert_select 'journal[id="1"]' do
175 assert_select 'journal[id="1"]' do
176 assert_select 'details[type=array]' do
176 assert_select 'details[type=array]' do
177 assert_select 'detail[name=status_id]' do
177 assert_select 'detail[name=status_id]' do
178 assert_select 'old_value', :text => '1'
178 assert_select 'old_value', :text => '1'
179 assert_select 'new_value', :text => '2'
179 assert_select 'new_value', :text => '2'
180 end
180 end
181 end
181 end
182 end
182 end
183 end
183 end
184 end
184 end
185
185
186 test "GET /issues/:id.xml with custom fields" do
186 test "GET /issues/:id.xml with custom fields" do
187 get '/issues/3.xml'
187 get '/issues/3.xml'
188
188
189 assert_select 'issue custom_fields[type=array]' do
189 assert_select 'issue custom_fields[type=array]' do
190 assert_select 'custom_field[id="1"]' do
190 assert_select 'custom_field[id="1"]' do
191 assert_select 'value', :text => 'MySQL'
191 assert_select 'value', :text => 'MySQL'
192 end
192 end
193 end
193 end
194 assert_nothing_raised do
194 assert_nothing_raised do
195 Hash.from_xml(response.body).to_xml
195 Hash.from_xml(response.body).to_xml
196 end
196 end
197 end
197 end
198
198
199 test "GET /issues/:id.xml with multi custom fields" do
199 test "GET /issues/:id.xml with multi custom fields" do
200 field = CustomField.find(1)
200 field = CustomField.find(1)
201 field.update_attribute :multiple, true
201 field.update_attribute :multiple, true
202 issue = Issue.find(3)
202 issue = Issue.find(3)
203 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
203 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
204 issue.save!
204 issue.save!
205
205
206 get '/issues/3.xml'
206 get '/issues/3.xml'
207 assert_response :success
207 assert_response :success
208
208
209 assert_select 'issue custom_fields[type=array]' do
209 assert_select 'issue custom_fields[type=array]' do
210 assert_select 'custom_field[id="1"]' do
210 assert_select 'custom_field[id="1"]' do
211 assert_select 'value[type=array] value', 2
211 assert_select 'value[type=array] value', 2
212 end
212 end
213 end
213 end
214 xml = Hash.from_xml(response.body)
214 xml = Hash.from_xml(response.body)
215 custom_fields = xml['issue']['custom_fields']
215 custom_fields = xml['issue']['custom_fields']
216 assert_kind_of Array, custom_fields
216 assert_kind_of Array, custom_fields
217 field = custom_fields.detect {|f| f['id'] == '1'}
217 field = custom_fields.detect {|f| f['id'] == '1'}
218 assert_kind_of Hash, field
218 assert_kind_of Hash, field
219 assert_equal ['MySQL', 'Oracle'], field['value'].sort
219 assert_equal ['MySQL', 'Oracle'], field['value'].sort
220 end
220 end
221
221
222 test "GET /issues/:id.json with multi custom fields" do
222 test "GET /issues/:id.json with multi custom fields" do
223 field = CustomField.find(1)
223 field = CustomField.find(1)
224 field.update_attribute :multiple, true
224 field.update_attribute :multiple, true
225 issue = Issue.find(3)
225 issue = Issue.find(3)
226 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
226 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
227 issue.save!
227 issue.save!
228
228
229 get '/issues/3.json'
229 get '/issues/3.json'
230 assert_response :success
230 assert_response :success
231
231
232 json = ActiveSupport::JSON.decode(response.body)
232 json = ActiveSupport::JSON.decode(response.body)
233 custom_fields = json['issue']['custom_fields']
233 custom_fields = json['issue']['custom_fields']
234 assert_kind_of Array, custom_fields
234 assert_kind_of Array, custom_fields
235 field = custom_fields.detect {|f| f['id'] == 1}
235 field = custom_fields.detect {|f| f['id'] == 1}
236 assert_kind_of Hash, field
236 assert_kind_of Hash, field
237 assert_equal ['MySQL', 'Oracle'], field['value'].sort
237 assert_equal ['MySQL', 'Oracle'], field['value'].sort
238 end
238 end
239
239
240 test "GET /issues/:id.xml with empty value for multi custom field" do
240 test "GET /issues/:id.xml with empty value for multi custom field" do
241 field = CustomField.find(1)
241 field = CustomField.find(1)
242 field.update_attribute :multiple, true
242 field.update_attribute :multiple, true
243 issue = Issue.find(3)
243 issue = Issue.find(3)
244 issue.custom_field_values = {1 => ['']}
244 issue.custom_field_values = {1 => ['']}
245 issue.save!
245 issue.save!
246
246
247 get '/issues/3.xml'
247 get '/issues/3.xml'
248
248
249 assert_select 'issue custom_fields[type=array]' do
249 assert_select 'issue custom_fields[type=array]' do
250 assert_select 'custom_field[id="1"]' do
250 assert_select 'custom_field[id="1"]' do
251 assert_select 'value[type=array]:empty'
251 assert_select 'value[type=array]:empty'
252 end
252 end
253 end
253 end
254 xml = Hash.from_xml(response.body)
254 xml = Hash.from_xml(response.body)
255 custom_fields = xml['issue']['custom_fields']
255 custom_fields = xml['issue']['custom_fields']
256 assert_kind_of Array, custom_fields
256 assert_kind_of Array, custom_fields
257 field = custom_fields.detect {|f| f['id'] == '1'}
257 field = custom_fields.detect {|f| f['id'] == '1'}
258 assert_kind_of Hash, field
258 assert_kind_of Hash, field
259 assert_equal [], field['value']
259 assert_equal [], field['value']
260 end
260 end
261
261
262 test "GET /issues/:id.json with empty value for multi custom field" do
262 test "GET /issues/:id.json with empty value for multi custom field" do
263 field = CustomField.find(1)
263 field = CustomField.find(1)
264 field.update_attribute :multiple, true
264 field.update_attribute :multiple, true
265 issue = Issue.find(3)
265 issue = Issue.find(3)
266 issue.custom_field_values = {1 => ['']}
266 issue.custom_field_values = {1 => ['']}
267 issue.save!
267 issue.save!
268
268
269 get '/issues/3.json'
269 get '/issues/3.json'
270 assert_response :success
270 assert_response :success
271 json = ActiveSupport::JSON.decode(response.body)
271 json = ActiveSupport::JSON.decode(response.body)
272 custom_fields = json['issue']['custom_fields']
272 custom_fields = json['issue']['custom_fields']
273 assert_kind_of Array, custom_fields
273 assert_kind_of Array, custom_fields
274 field = custom_fields.detect {|f| f['id'] == 1}
274 field = custom_fields.detect {|f| f['id'] == 1}
275 assert_kind_of Hash, field
275 assert_kind_of Hash, field
276 assert_equal [], field['value'].sort
276 assert_equal [], field['value'].sort
277 end
277 end
278
278
279 test "GET /issues/:id.xml with attachments" do
279 test "GET /issues/:id.xml with attachments" do
280 get '/issues/3.xml?include=attachments'
280 get '/issues/3.xml?include=attachments'
281
281
282 assert_select 'issue attachments[type=array]' do
282 assert_select 'issue attachments[type=array]' do
283 assert_select 'attachment', 5
283 assert_select 'attachment', 5
284 assert_select 'attachment id:content(4)' do
284 assert_select 'attachment id:content(4)' do
285 assert_select '~ filename', :text => 'source.rb'
285 assert_select '~ filename', :text => 'source.rb'
286 assert_select '~ content_url', :text => 'http://www.example.com/attachments/download/4/source.rb'
286 assert_select '~ content_url', :text => 'http://www.example.com/attachments/download/4/source.rb'
287 end
287 end
288 end
288 end
289 end
289 end
290
290
291 test "GET /issues/:id.xml with subtasks" do
291 test "GET /issues/:id.xml with subtasks" do
292 issue = Issue.generate_with_descendants!(:project_id => 1)
292 issue = Issue.generate_with_descendants!(:project_id => 1)
293 get "/issues/#{issue.id}.xml?include=children"
293 get "/issues/#{issue.id}.xml?include=children"
294
294
295 assert_select 'issue children[type=array]' do
295 assert_select 'issue children[type=array]' do
296 assert_select 'issue', 2
296 assert_select 'issue', 2
297 assert_select 'issue children', 1
297 assert_select 'issue children', 1
298 end
298 end
299 end
299 end
300
300
301 test "GET /issues/:id.json with subtasks" do
301 test "GET /issues/:id.json with subtasks" do
302 issue = Issue.generate_with_descendants!(:project_id => 1)
302 issue = Issue.generate_with_descendants!(:project_id => 1)
303 get "/issues/#{issue.id}.json?include=children"
303 get "/issues/#{issue.id}.json?include=children"
304
304
305 json = ActiveSupport::JSON.decode(response.body)
305 json = ActiveSupport::JSON.decode(response.body)
306 assert_equal 2, json['issue']['children'].size
306 assert_equal 2, json['issue']['children'].size
307 assert_equal 1, json['issue']['children'].select {|child| child.key?('children')}.size
307 assert_equal 1, json['issue']['children'].select {|child| child.key?('children')}.size
308 end
308 end
309
309
310 def test_show_should_include_issue_attributes
310 def test_show_should_include_issue_attributes
311 get '/issues/1.xml'
311 get '/issues/1.xml'
312 assert_select 'issue>is_private', :text => 'false'
312 assert_select 'issue>is_private', :text => 'false'
313 end
313 end
314
314
315 test "GET /issues/:id.xml?include=watchers should include watchers" do
315 test "GET /issues/:id.xml?include=watchers should include watchers" do
316 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
316 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
317
317
318 get '/issues/1.xml?include=watchers', {}, credentials('jsmith')
318 get '/issues/1.xml?include=watchers', {}, credentials('jsmith')
319
319
320 assert_response :ok
320 assert_response :ok
321 assert_equal 'application/xml', response.content_type
321 assert_equal 'application/xml', response.content_type
322 assert_select 'issue' do
322 assert_select 'issue' do
323 assert_select 'watchers', Issue.find(1).watchers.count
323 assert_select 'watchers', Issue.find(1).watchers.count
324 assert_select 'watchers' do
324 assert_select 'watchers' do
325 assert_select 'user[id=3]'
325 assert_select 'user[id=3]'
326 end
326 end
327 end
327 end
328 end
328 end
329
329
330 test "POST /issues.xml should create an issue with the attributes" do
330 test "POST /issues.xml should create an issue with the attributes" do
331
332 payload = <<-XML
333 <?xml version="1.0" encoding="UTF-8" ?>
334 <issue>
335 <project_id>1</project_id>
336 <tracker_id>2</tracker_id>
337 <status_id>3</status_id>
338 <subject>API test</subject>
339 </issue>
340 XML
341
331 assert_difference('Issue.count') do
342 assert_difference('Issue.count') do
332 post '/issues.xml',
343 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
333 {:issue => {:project_id => 1, :subject => 'API test',
334 :tracker_id => 2, :status_id => 3}}, credentials('jsmith')
335 end
344 end
336 issue = Issue.order('id DESC').first
345 issue = Issue.order('id DESC').first
337 assert_equal 1, issue.project_id
346 assert_equal 1, issue.project_id
338 assert_equal 2, issue.tracker_id
347 assert_equal 2, issue.tracker_id
339 assert_equal 3, issue.status_id
348 assert_equal 3, issue.status_id
340 assert_equal 'API test', issue.subject
349 assert_equal 'API test', issue.subject
341
350
342 assert_response :created
351 assert_response :created
343 assert_equal 'application/xml', @response.content_type
352 assert_equal 'application/xml', @response.content_type
344 assert_select 'issue > id', :text => issue.id.to_s
353 assert_select 'issue > id', :text => issue.id.to_s
345 end
354 end
346
355
347 test "POST /issues.xml with watcher_user_ids should create issue with watchers" do
356 test "POST /issues.xml with watcher_user_ids should create issue with watchers" do
348 assert_difference('Issue.count') do
357 assert_difference('Issue.count') do
349 post '/issues.xml',
358 post '/issues.xml',
350 {:issue => {:project_id => 1, :subject => 'Watchers',
359 {:issue => {:project_id => 1, :subject => 'Watchers',
351 :tracker_id => 2, :status_id => 3, :watcher_user_ids => [3, 1]}}, credentials('jsmith')
360 :tracker_id => 2, :status_id => 3, :watcher_user_ids => [3, 1]}}, credentials('jsmith')
352 assert_response :created
361 assert_response :created
353 end
362 end
354 issue = Issue.order('id desc').first
363 issue = Issue.order('id desc').first
355 assert_equal 2, issue.watchers.size
364 assert_equal 2, issue.watchers.size
356 assert_equal [1, 3], issue.watcher_user_ids.sort
365 assert_equal [1, 3], issue.watcher_user_ids.sort
357 end
366 end
358
367
359 test "POST /issues.xml with failure should return errors" do
368 test "POST /issues.xml with failure should return errors" do
360 assert_no_difference('Issue.count') do
369 assert_no_difference('Issue.count') do
361 post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
370 post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
362 end
371 end
363
372
364 assert_select 'errors error', :text => "Subject cannot be blank"
373 assert_select 'errors error', :text => "Subject cannot be blank"
365 end
374 end
366
375
367 test "POST /issues.json should create an issue with the attributes" do
376 test "POST /issues.json should create an issue with the attributes" do
377
378 payload = <<-JSON
379 {
380 "issue": {
381 "project_id": "1",
382 "tracker_id": "2",
383 "status_id": "3",
384 "subject": "API test"
385 }
386 }
387 JSON
388
368 assert_difference('Issue.count') do
389 assert_difference('Issue.count') do
369 post '/issues.json',
390 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
370 {:issue => {:project_id => 1, :subject => 'API test',
371 :tracker_id => 2, :status_id => 3}},
372 credentials('jsmith')
373 end
391 end
374
392
375 issue = Issue.order('id DESC').first
393 issue = Issue.order('id DESC').first
376 assert_equal 1, issue.project_id
394 assert_equal 1, issue.project_id
377 assert_equal 2, issue.tracker_id
395 assert_equal 2, issue.tracker_id
378 assert_equal 3, issue.status_id
396 assert_equal 3, issue.status_id
379 assert_equal 'API test', issue.subject
397 assert_equal 'API test', issue.subject
380 end
398 end
381
399
382 test "POST /issues.json with failure should return errors" do
400 test "POST /issues.json with failure should return errors" do
383 assert_no_difference('Issue.count') do
401 assert_no_difference('Issue.count') do
384 post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
402 post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
385 end
403 end
386
404
387 json = ActiveSupport::JSON.decode(response.body)
405 json = ActiveSupport::JSON.decode(response.body)
388 assert json['errors'].include?("Subject cannot be blank")
406 assert json['errors'].include?("Subject cannot be blank")
389 end
407 end
390
408
391 test "PUT /issues/:id.xml" do
409 test "PUT /issues/:id.xml" do
392 assert_difference('Journal.count') do
410 assert_difference('Journal.count') do
393 put '/issues/6.xml',
411 put '/issues/6.xml',
394 {:issue => {:subject => 'API update', :notes => 'A new note'}},
412 {:issue => {:subject => 'API update', :notes => 'A new note'}},
395 credentials('jsmith')
413 credentials('jsmith')
396 end
414 end
397
415
398 issue = Issue.find(6)
416 issue = Issue.find(6)
399 assert_equal "API update", issue.subject
417 assert_equal "API update", issue.subject
400 journal = Journal.last
418 journal = Journal.last
401 assert_equal "A new note", journal.notes
419 assert_equal "A new note", journal.notes
402 end
420 end
403
421
404 test "PUT /issues/:id.xml with custom fields" do
422 test "PUT /issues/:id.xml with custom fields" do
405 put '/issues/3.xml',
423 put '/issues/3.xml',
406 {:issue => {:custom_fields => [
424 {:issue => {:custom_fields => [
407 {'id' => '1', 'value' => 'PostgreSQL' },
425 {'id' => '1', 'value' => 'PostgreSQL' },
408 {'id' => '2', 'value' => '150'}
426 {'id' => '2', 'value' => '150'}
409 ]}},
427 ]}},
410 credentials('jsmith')
428 credentials('jsmith')
411
429
412 issue = Issue.find(3)
430 issue = Issue.find(3)
413 assert_equal '150', issue.custom_value_for(2).value
431 assert_equal '150', issue.custom_value_for(2).value
414 assert_equal 'PostgreSQL', issue.custom_value_for(1).value
432 assert_equal 'PostgreSQL', issue.custom_value_for(1).value
415 end
433 end
416
434
417 test "PUT /issues/:id.xml with multi custom fields" do
435 test "PUT /issues/:id.xml with multi custom fields" do
418 field = CustomField.find(1)
436 field = CustomField.find(1)
419 field.update_attribute :multiple, true
437 field.update_attribute :multiple, true
420
438
421 put '/issues/3.xml',
439 put '/issues/3.xml',
422 {:issue => {:custom_fields => [
440 {:issue => {:custom_fields => [
423 {'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
441 {'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
424 {'id' => '2', 'value' => '150'}
442 {'id' => '2', 'value' => '150'}
425 ]}},
443 ]}},
426 credentials('jsmith')
444 credentials('jsmith')
427
445
428 issue = Issue.find(3)
446 issue = Issue.find(3)
429 assert_equal '150', issue.custom_value_for(2).value
447 assert_equal '150', issue.custom_value_for(2).value
430 assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
448 assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
431 end
449 end
432
450
433 test "PUT /issues/:id.xml with project change" do
451 test "PUT /issues/:id.xml with project change" do
434 put '/issues/3.xml',
452 put '/issues/3.xml',
435 {:issue => {:project_id => 2, :subject => 'Project changed'}},
453 {:issue => {:project_id => 2, :subject => 'Project changed'}},
436 credentials('jsmith')
454 credentials('jsmith')
437
455
438 issue = Issue.find(3)
456 issue = Issue.find(3)
439 assert_equal 2, issue.project_id
457 assert_equal 2, issue.project_id
440 assert_equal 'Project changed', issue.subject
458 assert_equal 'Project changed', issue.subject
441 end
459 end
442
460
443 test "PUT /issues/:id.xml with failed update" do
461 test "PUT /issues/:id.xml with failed update" do
444 put '/issues/6.xml', {:issue => {:subject => ''}}, credentials('jsmith')
462 put '/issues/6.xml', {:issue => {:subject => ''}}, credentials('jsmith')
445
463
446 assert_response :unprocessable_entity
464 assert_response :unprocessable_entity
447 assert_select 'errors error', :text => "Subject cannot be blank"
465 assert_select 'errors error', :text => "Subject cannot be blank"
448 end
466 end
449
467
450 test "PUT /issues/:id.json" do
468 test "PUT /issues/:id.json" do
451 assert_difference('Journal.count') do
469 assert_difference('Journal.count') do
452 put '/issues/6.json',
470 put '/issues/6.json',
453 {:issue => {:subject => 'API update', :notes => 'A new note'}},
471 {:issue => {:subject => 'API update', :notes => 'A new note'}},
454 credentials('jsmith')
472 credentials('jsmith')
455
473
456 assert_response :ok
474 assert_response :ok
457 assert_equal '', response.body
475 assert_equal '', response.body
458 end
476 end
459
477
460 issue = Issue.find(6)
478 issue = Issue.find(6)
461 assert_equal "API update", issue.subject
479 assert_equal "API update", issue.subject
462 journal = Journal.last
480 journal = Journal.last
463 assert_equal "A new note", journal.notes
481 assert_equal "A new note", journal.notes
464 end
482 end
465
483
466 test "PUT /issues/:id.json with failed update" do
484 test "PUT /issues/:id.json with failed update" do
467 put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')
485 put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')
468
486
469 assert_response :unprocessable_entity
487 assert_response :unprocessable_entity
470 json = ActiveSupport::JSON.decode(response.body)
488 json = ActiveSupport::JSON.decode(response.body)
471 assert json['errors'].include?("Subject cannot be blank")
489 assert json['errors'].include?("Subject cannot be blank")
472 end
490 end
473
491
474 test "DELETE /issues/:id.xml" do
492 test "DELETE /issues/:id.xml" do
475 assert_difference('Issue.count', -1) do
493 assert_difference('Issue.count', -1) do
476 delete '/issues/6.xml', {}, credentials('jsmith')
494 delete '/issues/6.xml', {}, credentials('jsmith')
477
495
478 assert_response :ok
496 assert_response :ok
479 assert_equal '', response.body
497 assert_equal '', response.body
480 end
498 end
481 assert_nil Issue.find_by_id(6)
499 assert_nil Issue.find_by_id(6)
482 end
500 end
483
501
484 test "DELETE /issues/:id.json" do
502 test "DELETE /issues/:id.json" do
485 assert_difference('Issue.count', -1) do
503 assert_difference('Issue.count', -1) do
486 delete '/issues/6.json', {}, credentials('jsmith')
504 delete '/issues/6.json', {}, credentials('jsmith')
487
505
488 assert_response :ok
506 assert_response :ok
489 assert_equal '', response.body
507 assert_equal '', response.body
490 end
508 end
491 assert_nil Issue.find_by_id(6)
509 assert_nil Issue.find_by_id(6)
492 end
510 end
493
511
494 test "POST /issues/:id/watchers.xml should add watcher" do
512 test "POST /issues/:id/watchers.xml should add watcher" do
495 assert_difference 'Watcher.count' do
513 assert_difference 'Watcher.count' do
496 post '/issues/1/watchers.xml', {:user_id => 3}, credentials('jsmith')
514 post '/issues/1/watchers.xml', {:user_id => 3}, credentials('jsmith')
497
515
498 assert_response :ok
516 assert_response :ok
499 assert_equal '', response.body
517 assert_equal '', response.body
500 end
518 end
501 watcher = Watcher.order('id desc').first
519 watcher = Watcher.order('id desc').first
502 assert_equal Issue.find(1), watcher.watchable
520 assert_equal Issue.find(1), watcher.watchable
503 assert_equal User.find(3), watcher.user
521 assert_equal User.find(3), watcher.user
504 end
522 end
505
523
506 test "DELETE /issues/:id/watchers/:user_id.xml should remove watcher" do
524 test "DELETE /issues/:id/watchers/:user_id.xml should remove watcher" do
507 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
525 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
508
526
509 assert_difference 'Watcher.count', -1 do
527 assert_difference 'Watcher.count', -1 do
510 delete '/issues/1/watchers/3.xml', {}, credentials('jsmith')
528 delete '/issues/1/watchers/3.xml', {}, credentials('jsmith')
511
529
512 assert_response :ok
530 assert_response :ok
513 assert_equal '', response.body
531 assert_equal '', response.body
514 end
532 end
515 assert_equal false, Issue.find(1).watched_by?(User.find(3))
533 assert_equal false, Issue.find(1).watched_by?(User.find(3))
516 end
534 end
517
535
518 def test_create_issue_with_uploaded_file
536 def test_create_issue_with_uploaded_file
519 set_tmp_attachments_directory
537 set_tmp_attachments_directory
520 # upload the file
538 # upload the file
521 assert_difference 'Attachment.count' do
539 assert_difference 'Attachment.count' do
522 post '/uploads.xml', 'test_create_with_upload',
540 post '/uploads.xml', 'test_create_with_upload',
523 {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
541 {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
524 assert_response :created
542 assert_response :created
525 end
543 end
526 xml = Hash.from_xml(response.body)
544 xml = Hash.from_xml(response.body)
527 token = xml['upload']['token']
545 token = xml['upload']['token']
528 attachment = Attachment.order('id DESC').first
546 attachment = Attachment.order('id DESC').first
529
547
530 # create the issue with the upload's token
548 # create the issue with the upload's token
531 assert_difference 'Issue.count' do
549 assert_difference 'Issue.count' do
532 post '/issues.xml',
550 post '/issues.xml',
533 {:issue => {:project_id => 1, :subject => 'Uploaded file',
551 {:issue => {:project_id => 1, :subject => 'Uploaded file',
534 :uploads => [{:token => token, :filename => 'test.txt',
552 :uploads => [{:token => token, :filename => 'test.txt',
535 :content_type => 'text/plain'}]}},
553 :content_type => 'text/plain'}]}},
536 credentials('jsmith')
554 credentials('jsmith')
537 assert_response :created
555 assert_response :created
538 end
556 end
539 issue = Issue.order('id DESC').first
557 issue = Issue.order('id DESC').first
540 assert_equal 1, issue.attachments.count
558 assert_equal 1, issue.attachments.count
541 assert_equal attachment, issue.attachments.first
559 assert_equal attachment, issue.attachments.first
542
560
543 attachment.reload
561 attachment.reload
544 assert_equal 'test.txt', attachment.filename
562 assert_equal 'test.txt', attachment.filename
545 assert_equal 'text/plain', attachment.content_type
563 assert_equal 'text/plain', attachment.content_type
546 assert_equal 'test_create_with_upload'.size, attachment.filesize
564 assert_equal 'test_create_with_upload'.size, attachment.filesize
547 assert_equal 2, attachment.author_id
565 assert_equal 2, attachment.author_id
548
566
549 # get the issue with its attachments
567 # get the issue with its attachments
550 get "/issues/#{issue.id}.xml", :include => 'attachments'
568 get "/issues/#{issue.id}.xml", :include => 'attachments'
551 assert_response :success
569 assert_response :success
552 xml = Hash.from_xml(response.body)
570 xml = Hash.from_xml(response.body)
553 attachments = xml['issue']['attachments']
571 attachments = xml['issue']['attachments']
554 assert_kind_of Array, attachments
572 assert_kind_of Array, attachments
555 assert_equal 1, attachments.size
573 assert_equal 1, attachments.size
556 url = attachments.first['content_url']
574 url = attachments.first['content_url']
557 assert_not_nil url
575 assert_not_nil url
558
576
559 # download the attachment
577 # download the attachment
560 get url
578 get url
561 assert_response :success
579 assert_response :success
562 end
580 end
563
581
564 def test_create_issue_with_multiple_uploaded_files_as_xml
582 def test_create_issue_with_multiple_uploaded_files_as_xml
565 token1 = xml_upload('File content 1', credentials('jsmith'))
583 token1 = xml_upload('File content 1', credentials('jsmith'))
566 token2 = xml_upload('File content 2', credentials('jsmith'))
584 token2 = xml_upload('File content 2', credentials('jsmith'))
567
585
568 payload = <<-XML
586 payload = <<-XML
569 <?xml version="1.0" encoding="UTF-8" ?>
587 <?xml version="1.0" encoding="UTF-8" ?>
570 <issue>
588 <issue>
571 <project_id>1</project_id>
589 <project_id>1</project_id>
572 <tracker_id>1</tracker_id>
590 <tracker_id>1</tracker_id>
573 <subject>Issue with multiple attachments</subject>
591 <subject>Issue with multiple attachments</subject>
574 <uploads type="array">
592 <uploads type="array">
575 <upload>
593 <upload>
576 <token>#{token1}</token>
594 <token>#{token1}</token>
577 <filename>test1.txt</filename>
595 <filename>test1.txt</filename>
578 </upload>
596 </upload>
579 <upload>
597 <upload>
580 <token>#{token2}</token>
598 <token>#{token2}</token>
581 <filename>test1.txt</filename>
599 <filename>test1.txt</filename>
582 </upload>
600 </upload>
583 </uploads>
601 </uploads>
584 </issue>
602 </issue>
585 XML
603 XML
586
604
587 assert_difference 'Issue.count' do
605 assert_difference 'Issue.count' do
588 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
606 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
589 assert_response :created
607 assert_response :created
590 end
608 end
591 issue = Issue.order('id DESC').first
609 issue = Issue.order('id DESC').first
592 assert_equal 2, issue.attachments.count
610 assert_equal 2, issue.attachments.count
593 end
611 end
594
612
595 def test_create_issue_with_multiple_uploaded_files_as_json
613 def test_create_issue_with_multiple_uploaded_files_as_json
596 token1 = json_upload('File content 1', credentials('jsmith'))
614 token1 = json_upload('File content 1', credentials('jsmith'))
597 token2 = json_upload('File content 2', credentials('jsmith'))
615 token2 = json_upload('File content 2', credentials('jsmith'))
598
616
599 payload = <<-JSON
617 payload = <<-JSON
600 {
618 {
601 "issue": {
619 "issue": {
602 "project_id": "1",
620 "project_id": "1",
603 "tracker_id": "1",
621 "tracker_id": "1",
604 "subject": "Issue with multiple attachments",
622 "subject": "Issue with multiple attachments",
605 "uploads": [
623 "uploads": [
606 {"token": "#{token1}", "filename": "test1.txt"},
624 {"token": "#{token1}", "filename": "test1.txt"},
607 {"token": "#{token2}", "filename": "test2.txt"}
625 {"token": "#{token2}", "filename": "test2.txt"}
608 ]
626 ]
609 }
627 }
610 }
628 }
611 JSON
629 JSON
612
630
613 assert_difference 'Issue.count' do
631 assert_difference 'Issue.count' do
614 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
632 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
615 assert_response :created
633 assert_response :created
616 end
634 end
617 issue = Issue.order('id DESC').first
635 issue = Issue.order('id DESC').first
618 assert_equal 2, issue.attachments.count
636 assert_equal 2, issue.attachments.count
619 end
637 end
620
638
621 def test_update_issue_with_uploaded_file
639 def test_update_issue_with_uploaded_file
622 set_tmp_attachments_directory
640 set_tmp_attachments_directory
623 # upload the file
641 # upload the file
624 assert_difference 'Attachment.count' do
642 assert_difference 'Attachment.count' do
625 post '/uploads.xml', 'test_upload_with_upload',
643 post '/uploads.xml', 'test_upload_with_upload',
626 {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
644 {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
627 assert_response :created
645 assert_response :created
628 end
646 end
629 xml = Hash.from_xml(response.body)
647 xml = Hash.from_xml(response.body)
630 token = xml['upload']['token']
648 token = xml['upload']['token']
631 attachment = Attachment.order('id DESC').first
649 attachment = Attachment.order('id DESC').first
632
650
633 # update the issue with the upload's token
651 # update the issue with the upload's token
634 assert_difference 'Journal.count' do
652 assert_difference 'Journal.count' do
635 put '/issues/1.xml',
653 put '/issues/1.xml',
636 {:issue => {:notes => 'Attachment added',
654 {:issue => {:notes => 'Attachment added',
637 :uploads => [{:token => token, :filename => 'test.txt',
655 :uploads => [{:token => token, :filename => 'test.txt',
638 :content_type => 'text/plain'}]}},
656 :content_type => 'text/plain'}]}},
639 credentials('jsmith')
657 credentials('jsmith')
640 assert_response :ok
658 assert_response :ok
641 assert_equal '', @response.body
659 assert_equal '', @response.body
642 end
660 end
643
661
644 issue = Issue.find(1)
662 issue = Issue.find(1)
645 assert_include attachment, issue.attachments
663 assert_include attachment, issue.attachments
646 end
664 end
647 end
665 end
General Comments 0
You need to be logged in to leave comments. Login now