##// END OF EJS Templates
Backported r16293 (#24875)....
Jean-Philippe Lang -
r15927:1efe3eb3a1f4
parent child
Show More
@@ -1,796 +1,827
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 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', :text => '3' do
77 assert_select 'issue id', :text => '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', :text => '1' do
82 assert_select 'issue id', :text => '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 journals should format timestamps in ISO 8601" do
186 test "GET /issues/:id.xml with journals should format timestamps in ISO 8601" do
187 get '/issues/1.xml?include=journals'
187 get '/issues/1.xml?include=journals'
188
188
189 iso_date = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/
189 iso_date = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/
190 assert_select 'issue>created_on', :text => iso_date
190 assert_select 'issue>created_on', :text => iso_date
191 assert_select 'issue>updated_on', :text => iso_date
191 assert_select 'issue>updated_on', :text => iso_date
192 assert_select 'issue journal>created_on', :text => iso_date
192 assert_select 'issue journal>created_on', :text => iso_date
193 end
193 end
194
194
195 test "GET /issues/:id.xml with custom fields" do
195 test "GET /issues/:id.xml with custom fields" do
196 get '/issues/3.xml'
196 get '/issues/3.xml'
197
197
198 assert_select 'issue custom_fields[type=array]' do
198 assert_select 'issue custom_fields[type=array]' do
199 assert_select 'custom_field[id="1"]' do
199 assert_select 'custom_field[id="1"]' do
200 assert_select 'value', :text => 'MySQL'
200 assert_select 'value', :text => 'MySQL'
201 end
201 end
202 end
202 end
203 assert_nothing_raised do
203 assert_nothing_raised do
204 Hash.from_xml(response.body).to_xml
204 Hash.from_xml(response.body).to_xml
205 end
205 end
206 end
206 end
207
207
208 test "GET /issues/:id.xml with multi custom fields" do
208 test "GET /issues/:id.xml with multi custom fields" do
209 field = CustomField.find(1)
209 field = CustomField.find(1)
210 field.update_attribute :multiple, true
210 field.update_attribute :multiple, true
211 issue = Issue.find(3)
211 issue = Issue.find(3)
212 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
212 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
213 issue.save!
213 issue.save!
214
214
215 get '/issues/3.xml'
215 get '/issues/3.xml'
216 assert_response :success
216 assert_response :success
217
217
218 assert_select 'issue custom_fields[type=array]' do
218 assert_select 'issue custom_fields[type=array]' do
219 assert_select 'custom_field[id="1"]' do
219 assert_select 'custom_field[id="1"]' do
220 assert_select 'value[type=array] value', 2
220 assert_select 'value[type=array] value', 2
221 end
221 end
222 end
222 end
223 xml = Hash.from_xml(response.body)
223 xml = Hash.from_xml(response.body)
224 custom_fields = xml['issue']['custom_fields']
224 custom_fields = xml['issue']['custom_fields']
225 assert_kind_of Array, custom_fields
225 assert_kind_of Array, custom_fields
226 field = custom_fields.detect {|f| f['id'] == '1'}
226 field = custom_fields.detect {|f| f['id'] == '1'}
227 assert_kind_of Hash, field
227 assert_kind_of Hash, field
228 assert_equal ['MySQL', 'Oracle'], field['value'].sort
228 assert_equal ['MySQL', 'Oracle'], field['value'].sort
229 end
229 end
230
230
231 test "GET /issues/:id.json with multi custom fields" do
231 test "GET /issues/:id.json with multi custom fields" do
232 field = CustomField.find(1)
232 field = CustomField.find(1)
233 field.update_attribute :multiple, true
233 field.update_attribute :multiple, true
234 issue = Issue.find(3)
234 issue = Issue.find(3)
235 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
235 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
236 issue.save!
236 issue.save!
237
237
238 get '/issues/3.json'
238 get '/issues/3.json'
239 assert_response :success
239 assert_response :success
240
240
241 json = ActiveSupport::JSON.decode(response.body)
241 json = ActiveSupport::JSON.decode(response.body)
242 custom_fields = json['issue']['custom_fields']
242 custom_fields = json['issue']['custom_fields']
243 assert_kind_of Array, custom_fields
243 assert_kind_of Array, custom_fields
244 field = custom_fields.detect {|f| f['id'] == 1}
244 field = custom_fields.detect {|f| f['id'] == 1}
245 assert_kind_of Hash, field
245 assert_kind_of Hash, field
246 assert_equal ['MySQL', 'Oracle'], field['value'].sort
246 assert_equal ['MySQL', 'Oracle'], field['value'].sort
247 end
247 end
248
248
249 test "GET /issues/:id.xml with empty value for multi custom field" do
249 test "GET /issues/:id.xml with empty value for multi custom field" do
250 field = CustomField.find(1)
250 field = CustomField.find(1)
251 field.update_attribute :multiple, true
251 field.update_attribute :multiple, true
252 issue = Issue.find(3)
252 issue = Issue.find(3)
253 issue.custom_field_values = {1 => ['']}
253 issue.custom_field_values = {1 => ['']}
254 issue.save!
254 issue.save!
255
255
256 get '/issues/3.xml'
256 get '/issues/3.xml'
257
257
258 assert_select 'issue custom_fields[type=array]' do
258 assert_select 'issue custom_fields[type=array]' do
259 assert_select 'custom_field[id="1"]' do
259 assert_select 'custom_field[id="1"]' do
260 assert_select 'value[type=array]:empty'
260 assert_select 'value[type=array]:empty'
261 end
261 end
262 end
262 end
263 xml = Hash.from_xml(response.body)
263 xml = Hash.from_xml(response.body)
264 custom_fields = xml['issue']['custom_fields']
264 custom_fields = xml['issue']['custom_fields']
265 assert_kind_of Array, custom_fields
265 assert_kind_of Array, custom_fields
266 field = custom_fields.detect {|f| f['id'] == '1'}
266 field = custom_fields.detect {|f| f['id'] == '1'}
267 assert_kind_of Hash, field
267 assert_kind_of Hash, field
268 assert_equal [], field['value']
268 assert_equal [], field['value']
269 end
269 end
270
270
271 test "GET /issues/:id.json with empty value for multi custom field" do
271 test "GET /issues/:id.json with empty value for multi custom field" do
272 field = CustomField.find(1)
272 field = CustomField.find(1)
273 field.update_attribute :multiple, true
273 field.update_attribute :multiple, true
274 issue = Issue.find(3)
274 issue = Issue.find(3)
275 issue.custom_field_values = {1 => ['']}
275 issue.custom_field_values = {1 => ['']}
276 issue.save!
276 issue.save!
277
277
278 get '/issues/3.json'
278 get '/issues/3.json'
279 assert_response :success
279 assert_response :success
280 json = ActiveSupport::JSON.decode(response.body)
280 json = ActiveSupport::JSON.decode(response.body)
281 custom_fields = json['issue']['custom_fields']
281 custom_fields = json['issue']['custom_fields']
282 assert_kind_of Array, custom_fields
282 assert_kind_of Array, custom_fields
283 field = custom_fields.detect {|f| f['id'] == 1}
283 field = custom_fields.detect {|f| f['id'] == 1}
284 assert_kind_of Hash, field
284 assert_kind_of Hash, field
285 assert_equal [], field['value'].sort
285 assert_equal [], field['value'].sort
286 end
286 end
287
287
288 test "GET /issues/:id.xml with attachments" do
288 test "GET /issues/:id.xml with attachments" do
289 get '/issues/3.xml?include=attachments'
289 get '/issues/3.xml?include=attachments'
290
290
291 assert_select 'issue attachments[type=array]' do
291 assert_select 'issue attachments[type=array]' do
292 assert_select 'attachment', 4
292 assert_select 'attachment', 4
293 assert_select 'attachment id', :text => '1' do
293 assert_select 'attachment id', :text => '1' do
294 assert_select '~ filename', :text => 'error281.txt'
294 assert_select '~ filename', :text => 'error281.txt'
295 assert_select '~ content_url', :text => 'http://www.example.com/attachments/download/1/error281.txt'
295 assert_select '~ content_url', :text => 'http://www.example.com/attachments/download/1/error281.txt'
296 end
296 end
297 end
297 end
298 end
298 end
299
299
300 test "GET /issues/:id.xml with subtasks" do
300 test "GET /issues/:id.xml with subtasks" do
301 issue = Issue.generate_with_descendants!(:project_id => 1)
301 issue = Issue.generate_with_descendants!(:project_id => 1)
302 get "/issues/#{issue.id}.xml?include=children"
302 get "/issues/#{issue.id}.xml?include=children"
303
303
304 assert_select 'issue id', :text => issue.id.to_s do
304 assert_select 'issue id', :text => issue.id.to_s do
305 assert_select '~ children[type=array] > issue', 2
305 assert_select '~ children[type=array] > issue', 2
306 assert_select '~ children[type=array] > issue > children', 1
306 assert_select '~ children[type=array] > issue > children', 1
307 end
307 end
308 end
308 end
309
309
310 test "GET /issues/:id.json with subtasks" do
310 test "GET /issues/:id.json with subtasks" do
311 issue = Issue.generate_with_descendants!(:project_id => 1)
311 issue = Issue.generate_with_descendants!(:project_id => 1)
312 get "/issues/#{issue.id}.json?include=children"
312 get "/issues/#{issue.id}.json?include=children"
313
313
314 json = ActiveSupport::JSON.decode(response.body)
314 json = ActiveSupport::JSON.decode(response.body)
315 assert_equal 2, json['issue']['children'].size
315 assert_equal 2, json['issue']['children'].size
316 assert_equal 1, json['issue']['children'].select {|child| child.key?('children')}.size
316 assert_equal 1, json['issue']['children'].select {|child| child.key?('children')}.size
317 end
317 end
318
318
319 def test_show_should_include_issue_attributes
319 def test_show_should_include_issue_attributes
320 get '/issues/1.xml'
320 get '/issues/1.xml'
321 assert_select 'issue>is_private', :text => 'false'
321 assert_select 'issue>is_private', :text => 'false'
322 end
322 end
323
323
324 test "GET /issues/:id.xml?include=watchers should include watchers" do
324 test "GET /issues/:id.xml?include=watchers should include watchers" do
325 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
325 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
326
326
327 get '/issues/1.xml?include=watchers', {}, credentials('jsmith')
327 get '/issues/1.xml?include=watchers', {}, credentials('jsmith')
328
328
329 assert_response :ok
329 assert_response :ok
330 assert_equal 'application/xml', response.content_type
330 assert_equal 'application/xml', response.content_type
331 assert_select 'issue' do
331 assert_select 'issue' do
332 assert_select 'watchers', Issue.find(1).watchers.count
332 assert_select 'watchers', Issue.find(1).watchers.count
333 assert_select 'watchers' do
333 assert_select 'watchers' do
334 assert_select 'user[id="3"]'
334 assert_select 'user[id="3"]'
335 end
335 end
336 end
336 end
337 end
337 end
338
338
339 test "GET /issues/:id.xml should not disclose associated changesets from projects the user has no access to" do
339 test "GET /issues/:id.xml should not disclose associated changesets from projects the user has no access to" do
340 project = Project.generate!(:is_public => false)
340 project = Project.generate!(:is_public => false)
341 repository = Repository::Subversion.create!(:project => project, :url => "svn://localhost")
341 repository = Repository::Subversion.create!(:project => project, :url => "svn://localhost")
342 Issue.find(1).changesets << Changeset.generate!(:repository => repository)
342 Issue.find(1).changesets << Changeset.generate!(:repository => repository)
343 assert Issue.find(1).changesets.any?
343 assert Issue.find(1).changesets.any?
344
344
345 get '/issues/1.xml?include=changesets', {}, credentials('jsmith')
345 get '/issues/1.xml?include=changesets', {}, credentials('jsmith')
346
346
347 # the user jsmith has no permission to view the associated changeset
347 # the user jsmith has no permission to view the associated changeset
348 assert_select 'issue changesets[type=array]' do
348 assert_select 'issue changesets[type=array]' do
349 assert_select 'changeset', 0
349 assert_select 'changeset', 0
350 end
350 end
351 end
351 end
352
352
353 test "GET /issues/:id.xml should contains visible spent_hours only" do
354 user = User.find_by_login('jsmith')
355 Role.find(1).update(:time_entries_visibility => 'own')
356 parent = Issue.find(3)
357 child = Issue.generate!(:parent_issue_id => parent.id)
358 TimeEntry.generate!(:user => user, :hours => 5.5, :issue_id => parent.id)
359 TimeEntry.generate!(:user => user, :hours => 2, :issue_id => child.id)
360 TimeEntry.generate!(:user => User.find(1), :hours => 100, :issue_id => child.id)
361 get '/issues/3.xml', {} , credentials(user.login)
362
363 assert_equal 'application/xml', response.content_type
364 assert_select 'issue' do
365 assert_select 'spent_hours', '5.5'
366 end
367 end
368
369 test "GET /issues/:id.json should contains visible spent_hours only" do
370 user = User.find_by_login('jsmith')
371 Role.find(1).update(:time_entries_visibility => 'own')
372 parent = Issue.find(3)
373 child = Issue.generate!(:parent_issue_id => parent.id)
374 TimeEntry.generate!(:user => user, :hours => 5.5, :issue_id => parent.id)
375 TimeEntry.generate!(:user => user, :hours => 2, :issue_id => child.id)
376 TimeEntry.generate!(:user => User.find(1), :hours => 100, :issue_id => child.id)
377 get '/issues/3.json', {} , credentials(user.login)
378
379 assert_equal 'application/json', response.content_type
380 json = ActiveSupport::JSON.decode(response.body)
381 assert_equal 5.5, json['issue']['spent_hours']
382 end
383
353 test "POST /issues.xml should create an issue with the attributes" do
384 test "POST /issues.xml should create an issue with the attributes" do
354
385
355 payload = <<-XML
386 payload = <<-XML
356 <?xml version="1.0" encoding="UTF-8" ?>
387 <?xml version="1.0" encoding="UTF-8" ?>
357 <issue>
388 <issue>
358 <project_id>1</project_id>
389 <project_id>1</project_id>
359 <tracker_id>2</tracker_id>
390 <tracker_id>2</tracker_id>
360 <status_id>3</status_id>
391 <status_id>3</status_id>
361 <subject>API test</subject>
392 <subject>API test</subject>
362 </issue>
393 </issue>
363 XML
394 XML
364
395
365 assert_difference('Issue.count') do
396 assert_difference('Issue.count') do
366 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
397 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
367 end
398 end
368 issue = Issue.order('id DESC').first
399 issue = Issue.order('id DESC').first
369 assert_equal 1, issue.project_id
400 assert_equal 1, issue.project_id
370 assert_equal 2, issue.tracker_id
401 assert_equal 2, issue.tracker_id
371 assert_equal 3, issue.status_id
402 assert_equal 3, issue.status_id
372 assert_equal 'API test', issue.subject
403 assert_equal 'API test', issue.subject
373
404
374 assert_response :created
405 assert_response :created
375 assert_equal 'application/xml', @response.content_type
406 assert_equal 'application/xml', @response.content_type
376 assert_select 'issue > id', :text => issue.id.to_s
407 assert_select 'issue > id', :text => issue.id.to_s
377 end
408 end
378
409
379 test "POST /issues.xml with watcher_user_ids should create issue with watchers" do
410 test "POST /issues.xml with watcher_user_ids should create issue with watchers" do
380 assert_difference('Issue.count') do
411 assert_difference('Issue.count') do
381 post '/issues.xml',
412 post '/issues.xml',
382 {:issue => {:project_id => 1, :subject => 'Watchers',
413 {:issue => {:project_id => 1, :subject => 'Watchers',
383 :tracker_id => 2, :status_id => 3, :watcher_user_ids => [3, 1]}}, credentials('jsmith')
414 :tracker_id => 2, :status_id => 3, :watcher_user_ids => [3, 1]}}, credentials('jsmith')
384 assert_response :created
415 assert_response :created
385 end
416 end
386 issue = Issue.order('id desc').first
417 issue = Issue.order('id desc').first
387 assert_equal 2, issue.watchers.size
418 assert_equal 2, issue.watchers.size
388 assert_equal [1, 3], issue.watcher_user_ids.sort
419 assert_equal [1, 3], issue.watcher_user_ids.sort
389 end
420 end
390
421
391 test "POST /issues.xml with failure should return errors" do
422 test "POST /issues.xml with failure should return errors" do
392 assert_no_difference('Issue.count') do
423 assert_no_difference('Issue.count') do
393 post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
424 post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
394 end
425 end
395
426
396 assert_select 'errors error', :text => "Subject cannot be blank"
427 assert_select 'errors error', :text => "Subject cannot be blank"
397 end
428 end
398
429
399 test "POST /issues.json should create an issue with the attributes" do
430 test "POST /issues.json should create an issue with the attributes" do
400
431
401 payload = <<-JSON
432 payload = <<-JSON
402 {
433 {
403 "issue": {
434 "issue": {
404 "project_id": "1",
435 "project_id": "1",
405 "tracker_id": "2",
436 "tracker_id": "2",
406 "status_id": "3",
437 "status_id": "3",
407 "subject": "API test"
438 "subject": "API test"
408 }
439 }
409 }
440 }
410 JSON
441 JSON
411
442
412 assert_difference('Issue.count') do
443 assert_difference('Issue.count') do
413 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
444 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
414 end
445 end
415
446
416 issue = Issue.order('id DESC').first
447 issue = Issue.order('id DESC').first
417 assert_equal 1, issue.project_id
448 assert_equal 1, issue.project_id
418 assert_equal 2, issue.tracker_id
449 assert_equal 2, issue.tracker_id
419 assert_equal 3, issue.status_id
450 assert_equal 3, issue.status_id
420 assert_equal 'API test', issue.subject
451 assert_equal 'API test', issue.subject
421 end
452 end
422
453
423 test "POST /issues.json without tracker_id should accept custom fields" do
454 test "POST /issues.json without tracker_id should accept custom fields" do
424 field = IssueCustomField.generate!(
455 field = IssueCustomField.generate!(
425 :field_format => 'list',
456 :field_format => 'list',
426 :multiple => true,
457 :multiple => true,
427 :possible_values => ["V1", "V2", "V3"],
458 :possible_values => ["V1", "V2", "V3"],
428 :default_value => "V2",
459 :default_value => "V2",
429 :is_for_all => true,
460 :is_for_all => true,
430 :trackers => Tracker.all.to_a
461 :trackers => Tracker.all.to_a
431 )
462 )
432
463
433 payload = <<-JSON
464 payload = <<-JSON
434 {
465 {
435 "issue": {
466 "issue": {
436 "project_id": "1",
467 "project_id": "1",
437 "subject": "Multivalued custom field",
468 "subject": "Multivalued custom field",
438 "custom_field_values":{"#{field.id}":["V1","V3"]}
469 "custom_field_values":{"#{field.id}":["V1","V3"]}
439 }
470 }
440 }
471 }
441 JSON
472 JSON
442
473
443 assert_difference('Issue.count') do
474 assert_difference('Issue.count') do
444 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
475 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
445 end
476 end
446
477
447 assert_response :created
478 assert_response :created
448 issue = Issue.order('id DESC').first
479 issue = Issue.order('id DESC').first
449 assert_equal ["V1", "V3"], issue.custom_field_value(field).sort
480 assert_equal ["V1", "V3"], issue.custom_field_value(field).sort
450 end
481 end
451
482
452 test "POST /issues.json with omitted custom field should set default value" do
483 test "POST /issues.json with omitted custom field should set default value" do
453 field = IssueCustomField.generate!(:default_value => "Default")
484 field = IssueCustomField.generate!(:default_value => "Default")
454
485
455 issue = new_record(Issue) do
486 issue = new_record(Issue) do
456 post '/issues.json',
487 post '/issues.json',
457 {:issue => {:project_id => 1, :subject => 'API', :custom_field_values => {}}},
488 {:issue => {:project_id => 1, :subject => 'API', :custom_field_values => {}}},
458 credentials('jsmith')
489 credentials('jsmith')
459 end
490 end
460 assert_equal "Default", issue.custom_field_value(field)
491 assert_equal "Default", issue.custom_field_value(field)
461 end
492 end
462
493
463 test "POST /issues.json with custom field set to blank should not set default value" do
494 test "POST /issues.json with custom field set to blank should not set default value" do
464 field = IssueCustomField.generate!(:default_value => "Default")
495 field = IssueCustomField.generate!(:default_value => "Default")
465
496
466 issue = new_record(Issue) do
497 issue = new_record(Issue) do
467 post '/issues.json',
498 post '/issues.json',
468 {:issue => {:project_id => 1, :subject => 'API', :custom_field_values => {field.id.to_s => ""}}},
499 {:issue => {:project_id => 1, :subject => 'API', :custom_field_values => {field.id.to_s => ""}}},
469 credentials('jsmith')
500 credentials('jsmith')
470 end
501 end
471 assert_equal "", issue.custom_field_value(field)
502 assert_equal "", issue.custom_field_value(field)
472 end
503 end
473
504
474 test "POST /issues.json with failure should return errors" do
505 test "POST /issues.json with failure should return errors" do
475 assert_no_difference('Issue.count') do
506 assert_no_difference('Issue.count') do
476 post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
507 post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
477 end
508 end
478
509
479 json = ActiveSupport::JSON.decode(response.body)
510 json = ActiveSupport::JSON.decode(response.body)
480 assert json['errors'].include?("Subject cannot be blank")
511 assert json['errors'].include?("Subject cannot be blank")
481 end
512 end
482
513
483 test "POST /issues.json with invalid project_id should respond with 422" do
514 test "POST /issues.json with invalid project_id should respond with 422" do
484 post '/issues.json', {:issue => {:project_id => 999, :subject => "API"}}, credentials('jsmith')
515 post '/issues.json', {:issue => {:project_id => 999, :subject => "API"}}, credentials('jsmith')
485 assert_response 422
516 assert_response 422
486 end
517 end
487
518
488 test "PUT /issues/:id.xml" do
519 test "PUT /issues/:id.xml" do
489 assert_difference('Journal.count') do
520 assert_difference('Journal.count') do
490 put '/issues/6.xml',
521 put '/issues/6.xml',
491 {:issue => {:subject => 'API update', :notes => 'A new note'}},
522 {:issue => {:subject => 'API update', :notes => 'A new note'}},
492 credentials('jsmith')
523 credentials('jsmith')
493 end
524 end
494
525
495 issue = Issue.find(6)
526 issue = Issue.find(6)
496 assert_equal "API update", issue.subject
527 assert_equal "API update", issue.subject
497 journal = Journal.last
528 journal = Journal.last
498 assert_equal "A new note", journal.notes
529 assert_equal "A new note", journal.notes
499 end
530 end
500
531
501 test "PUT /issues/:id.xml with custom fields" do
532 test "PUT /issues/:id.xml with custom fields" do
502 put '/issues/3.xml',
533 put '/issues/3.xml',
503 {:issue => {:custom_fields => [
534 {:issue => {:custom_fields => [
504 {'id' => '1', 'value' => 'PostgreSQL' },
535 {'id' => '1', 'value' => 'PostgreSQL' },
505 {'id' => '2', 'value' => '150'}
536 {'id' => '2', 'value' => '150'}
506 ]}},
537 ]}},
507 credentials('jsmith')
538 credentials('jsmith')
508
539
509 issue = Issue.find(3)
540 issue = Issue.find(3)
510 assert_equal '150', issue.custom_value_for(2).value
541 assert_equal '150', issue.custom_value_for(2).value
511 assert_equal 'PostgreSQL', issue.custom_value_for(1).value
542 assert_equal 'PostgreSQL', issue.custom_value_for(1).value
512 end
543 end
513
544
514 test "PUT /issues/:id.xml with multi custom fields" do
545 test "PUT /issues/:id.xml with multi custom fields" do
515 field = CustomField.find(1)
546 field = CustomField.find(1)
516 field.update_attribute :multiple, true
547 field.update_attribute :multiple, true
517
548
518 put '/issues/3.xml',
549 put '/issues/3.xml',
519 {:issue => {:custom_fields => [
550 {:issue => {:custom_fields => [
520 {'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
551 {'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
521 {'id' => '2', 'value' => '150'}
552 {'id' => '2', 'value' => '150'}
522 ]}},
553 ]}},
523 credentials('jsmith')
554 credentials('jsmith')
524
555
525 issue = Issue.find(3)
556 issue = Issue.find(3)
526 assert_equal '150', issue.custom_value_for(2).value
557 assert_equal '150', issue.custom_value_for(2).value
527 assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
558 assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
528 end
559 end
529
560
530 test "PUT /issues/:id.xml with project change" do
561 test "PUT /issues/:id.xml with project change" do
531 put '/issues/3.xml',
562 put '/issues/3.xml',
532 {:issue => {:project_id => 2, :subject => 'Project changed'}},
563 {:issue => {:project_id => 2, :subject => 'Project changed'}},
533 credentials('jsmith')
564 credentials('jsmith')
534
565
535 issue = Issue.find(3)
566 issue = Issue.find(3)
536 assert_equal 2, issue.project_id
567 assert_equal 2, issue.project_id
537 assert_equal 'Project changed', issue.subject
568 assert_equal 'Project changed', issue.subject
538 end
569 end
539
570
540 test "PUT /issues/:id.xml with notes only" do
571 test "PUT /issues/:id.xml with notes only" do
541 assert_difference('Journal.count') do
572 assert_difference('Journal.count') do
542 put '/issues/6.xml',
573 put '/issues/6.xml',
543 {:issue => {:notes => 'Notes only'}},
574 {:issue => {:notes => 'Notes only'}},
544 credentials('jsmith')
575 credentials('jsmith')
545 end
576 end
546
577
547 journal = Journal.last
578 journal = Journal.last
548 assert_equal "Notes only", journal.notes
579 assert_equal "Notes only", journal.notes
549 end
580 end
550
581
551 test "PUT /issues/:id.json with omitted custom field should not change blank value to default value" do
582 test "PUT /issues/:id.json with omitted custom field should not change blank value to default value" do
552 field = IssueCustomField.generate!(:default_value => "Default")
583 field = IssueCustomField.generate!(:default_value => "Default")
553 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => ""})
584 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => ""})
554 assert_equal "", issue.reload.custom_field_value(field)
585 assert_equal "", issue.reload.custom_field_value(field)
555
586
556 assert_difference('Journal.count') do
587 assert_difference('Journal.count') do
557 put "/issues/#{issue.id}.json",
588 put "/issues/#{issue.id}.json",
558 {:issue => {:custom_field_values => {}, :notes => 'API'}},
589 {:issue => {:custom_field_values => {}, :notes => 'API'}},
559 credentials('jsmith')
590 credentials('jsmith')
560 end
591 end
561
592
562 assert_equal "", issue.reload.custom_field_value(field)
593 assert_equal "", issue.reload.custom_field_value(field)
563 end
594 end
564
595
565 test "PUT /issues/:id.json with custom field set to blank should not change blank value to default value" do
596 test "PUT /issues/:id.json with custom field set to blank should not change blank value to default value" do
566 field = IssueCustomField.generate!(:default_value => "Default")
597 field = IssueCustomField.generate!(:default_value => "Default")
567 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => ""})
598 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => ""})
568 assert_equal "", issue.reload.custom_field_value(field)
599 assert_equal "", issue.reload.custom_field_value(field)
569
600
570 assert_difference('Journal.count') do
601 assert_difference('Journal.count') do
571 put "/issues/#{issue.id}.json",
602 put "/issues/#{issue.id}.json",
572 {:issue => {:custom_field_values => {field.id.to_s => ""}, :notes => 'API'}},
603 {:issue => {:custom_field_values => {field.id.to_s => ""}, :notes => 'API'}},
573 credentials('jsmith')
604 credentials('jsmith')
574 end
605 end
575
606
576 assert_equal "", issue.reload.custom_field_value(field)
607 assert_equal "", issue.reload.custom_field_value(field)
577 end
608 end
578
609
579 test "PUT /issues/:id.json with tracker change and omitted custom field specific to that tracker should set default value" do
610 test "PUT /issues/:id.json with tracker change and omitted custom field specific to that tracker should set default value" do
580 field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2])
611 field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2])
581 issue = Issue.generate!(:project_id => 1, :tracker_id => 1)
612 issue = Issue.generate!(:project_id => 1, :tracker_id => 1)
582
613
583 assert_difference('Journal.count') do
614 assert_difference('Journal.count') do
584 put "/issues/#{issue.id}.json",
615 put "/issues/#{issue.id}.json",
585 {:issue => {:tracker_id => 2, :custom_field_values => {}, :notes => 'API'}},
616 {:issue => {:tracker_id => 2, :custom_field_values => {}, :notes => 'API'}},
586 credentials('jsmith')
617 credentials('jsmith')
587 end
618 end
588
619
589 assert_equal 2, issue.reload.tracker_id
620 assert_equal 2, issue.reload.tracker_id
590 assert_equal "Default", issue.reload.custom_field_value(field)
621 assert_equal "Default", issue.reload.custom_field_value(field)
591 end
622 end
592
623
593 test "PUT /issues/:id.json with tracker change and custom field specific to that tracker set to blank should not set default value" do
624 test "PUT /issues/:id.json with tracker change and custom field specific to that tracker set to blank should not set default value" do
594 field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2])
625 field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2])
595 issue = Issue.generate!(:project_id => 1, :tracker_id => 1)
626 issue = Issue.generate!(:project_id => 1, :tracker_id => 1)
596
627
597 assert_difference('Journal.count') do
628 assert_difference('Journal.count') do
598 put "/issues/#{issue.id}.json",
629 put "/issues/#{issue.id}.json",
599 {:issue => {:tracker_id => 2, :custom_field_values => {field.id.to_s => ""}, :notes => 'API'}},
630 {:issue => {:tracker_id => 2, :custom_field_values => {field.id.to_s => ""}, :notes => 'API'}},
600 credentials('jsmith')
631 credentials('jsmith')
601 end
632 end
602
633
603 assert_equal 2, issue.reload.tracker_id
634 assert_equal 2, issue.reload.tracker_id
604 assert_equal "", issue.reload.custom_field_value(field)
635 assert_equal "", issue.reload.custom_field_value(field)
605 end
636 end
606
637
607 test "PUT /issues/:id.xml with failed update" do
638 test "PUT /issues/:id.xml with failed update" do
608 put '/issues/6.xml', {:issue => {:subject => ''}}, credentials('jsmith')
639 put '/issues/6.xml', {:issue => {:subject => ''}}, credentials('jsmith')
609
640
610 assert_response :unprocessable_entity
641 assert_response :unprocessable_entity
611 assert_select 'errors error', :text => "Subject cannot be blank"
642 assert_select 'errors error', :text => "Subject cannot be blank"
612 end
643 end
613
644
614 test "PUT /issues/:id.json" do
645 test "PUT /issues/:id.json" do
615 assert_difference('Journal.count') do
646 assert_difference('Journal.count') do
616 put '/issues/6.json',
647 put '/issues/6.json',
617 {:issue => {:subject => 'API update', :notes => 'A new note'}},
648 {:issue => {:subject => 'API update', :notes => 'A new note'}},
618 credentials('jsmith')
649 credentials('jsmith')
619
650
620 assert_response :ok
651 assert_response :ok
621 assert_equal '', response.body
652 assert_equal '', response.body
622 end
653 end
623
654
624 issue = Issue.find(6)
655 issue = Issue.find(6)
625 assert_equal "API update", issue.subject
656 assert_equal "API update", issue.subject
626 journal = Journal.last
657 journal = Journal.last
627 assert_equal "A new note", journal.notes
658 assert_equal "A new note", journal.notes
628 end
659 end
629
660
630 test "PUT /issues/:id.json with failed update" do
661 test "PUT /issues/:id.json with failed update" do
631 put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')
662 put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')
632
663
633 assert_response :unprocessable_entity
664 assert_response :unprocessable_entity
634 json = ActiveSupport::JSON.decode(response.body)
665 json = ActiveSupport::JSON.decode(response.body)
635 assert json['errors'].include?("Subject cannot be blank")
666 assert json['errors'].include?("Subject cannot be blank")
636 end
667 end
637
668
638 test "DELETE /issues/:id.xml" do
669 test "DELETE /issues/:id.xml" do
639 assert_difference('Issue.count', -1) do
670 assert_difference('Issue.count', -1) do
640 delete '/issues/6.xml', {}, credentials('jsmith')
671 delete '/issues/6.xml', {}, credentials('jsmith')
641
672
642 assert_response :ok
673 assert_response :ok
643 assert_equal '', response.body
674 assert_equal '', response.body
644 end
675 end
645 assert_nil Issue.find_by_id(6)
676 assert_nil Issue.find_by_id(6)
646 end
677 end
647
678
648 test "DELETE /issues/:id.json" do
679 test "DELETE /issues/:id.json" do
649 assert_difference('Issue.count', -1) do
680 assert_difference('Issue.count', -1) do
650 delete '/issues/6.json', {}, credentials('jsmith')
681 delete '/issues/6.json', {}, credentials('jsmith')
651
682
652 assert_response :ok
683 assert_response :ok
653 assert_equal '', response.body
684 assert_equal '', response.body
654 end
685 end
655 assert_nil Issue.find_by_id(6)
686 assert_nil Issue.find_by_id(6)
656 end
687 end
657
688
658 test "POST /issues/:id/watchers.xml should add watcher" do
689 test "POST /issues/:id/watchers.xml should add watcher" do
659 assert_difference 'Watcher.count' do
690 assert_difference 'Watcher.count' do
660 post '/issues/1/watchers.xml', {:user_id => 3}, credentials('jsmith')
691 post '/issues/1/watchers.xml', {:user_id => 3}, credentials('jsmith')
661
692
662 assert_response :ok
693 assert_response :ok
663 assert_equal '', response.body
694 assert_equal '', response.body
664 end
695 end
665 watcher = Watcher.order('id desc').first
696 watcher = Watcher.order('id desc').first
666 assert_equal Issue.find(1), watcher.watchable
697 assert_equal Issue.find(1), watcher.watchable
667 assert_equal User.find(3), watcher.user
698 assert_equal User.find(3), watcher.user
668 end
699 end
669
700
670 test "DELETE /issues/:id/watchers/:user_id.xml should remove watcher" do
701 test "DELETE /issues/:id/watchers/:user_id.xml should remove watcher" do
671 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
702 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
672
703
673 assert_difference 'Watcher.count', -1 do
704 assert_difference 'Watcher.count', -1 do
674 delete '/issues/1/watchers/3.xml', {}, credentials('jsmith')
705 delete '/issues/1/watchers/3.xml', {}, credentials('jsmith')
675
706
676 assert_response :ok
707 assert_response :ok
677 assert_equal '', response.body
708 assert_equal '', response.body
678 end
709 end
679 assert_equal false, Issue.find(1).watched_by?(User.find(3))
710 assert_equal false, Issue.find(1).watched_by?(User.find(3))
680 end
711 end
681
712
682 def test_create_issue_with_uploaded_file
713 def test_create_issue_with_uploaded_file
683 token = xml_upload('test_create_with_upload', credentials('jsmith'))
714 token = xml_upload('test_create_with_upload', credentials('jsmith'))
684 attachment = Attachment.find_by_token(token)
715 attachment = Attachment.find_by_token(token)
685
716
686 # create the issue with the upload's token
717 # create the issue with the upload's token
687 assert_difference 'Issue.count' do
718 assert_difference 'Issue.count' do
688 post '/issues.xml',
719 post '/issues.xml',
689 {:issue => {:project_id => 1, :subject => 'Uploaded file',
720 {:issue => {:project_id => 1, :subject => 'Uploaded file',
690 :uploads => [{:token => token, :filename => 'test.txt',
721 :uploads => [{:token => token, :filename => 'test.txt',
691 :content_type => 'text/plain'}]}},
722 :content_type => 'text/plain'}]}},
692 credentials('jsmith')
723 credentials('jsmith')
693 assert_response :created
724 assert_response :created
694 end
725 end
695 issue = Issue.order('id DESC').first
726 issue = Issue.order('id DESC').first
696 assert_equal 1, issue.attachments.count
727 assert_equal 1, issue.attachments.count
697 assert_equal attachment, issue.attachments.first
728 assert_equal attachment, issue.attachments.first
698
729
699 attachment.reload
730 attachment.reload
700 assert_equal 'test.txt', attachment.filename
731 assert_equal 'test.txt', attachment.filename
701 assert_equal 'text/plain', attachment.content_type
732 assert_equal 'text/plain', attachment.content_type
702 assert_equal 'test_create_with_upload'.size, attachment.filesize
733 assert_equal 'test_create_with_upload'.size, attachment.filesize
703 assert_equal 2, attachment.author_id
734 assert_equal 2, attachment.author_id
704
735
705 # get the issue with its attachments
736 # get the issue with its attachments
706 get "/issues/#{issue.id}.xml", :include => 'attachments'
737 get "/issues/#{issue.id}.xml", :include => 'attachments'
707 assert_response :success
738 assert_response :success
708 xml = Hash.from_xml(response.body)
739 xml = Hash.from_xml(response.body)
709 attachments = xml['issue']['attachments']
740 attachments = xml['issue']['attachments']
710 assert_kind_of Array, attachments
741 assert_kind_of Array, attachments
711 assert_equal 1, attachments.size
742 assert_equal 1, attachments.size
712 url = attachments.first['content_url']
743 url = attachments.first['content_url']
713 assert_not_nil url
744 assert_not_nil url
714
745
715 # download the attachment
746 # download the attachment
716 get url
747 get url
717 assert_response :success
748 assert_response :success
718 assert_equal 'test_create_with_upload', response.body
749 assert_equal 'test_create_with_upload', response.body
719 end
750 end
720
751
721 def test_create_issue_with_multiple_uploaded_files_as_xml
752 def test_create_issue_with_multiple_uploaded_files_as_xml
722 token1 = xml_upload('File content 1', credentials('jsmith'))
753 token1 = xml_upload('File content 1', credentials('jsmith'))
723 token2 = xml_upload('File content 2', credentials('jsmith'))
754 token2 = xml_upload('File content 2', credentials('jsmith'))
724
755
725 payload = <<-XML
756 payload = <<-XML
726 <?xml version="1.0" encoding="UTF-8" ?>
757 <?xml version="1.0" encoding="UTF-8" ?>
727 <issue>
758 <issue>
728 <project_id>1</project_id>
759 <project_id>1</project_id>
729 <tracker_id>1</tracker_id>
760 <tracker_id>1</tracker_id>
730 <subject>Issue with multiple attachments</subject>
761 <subject>Issue with multiple attachments</subject>
731 <uploads type="array">
762 <uploads type="array">
732 <upload>
763 <upload>
733 <token>#{token1}</token>
764 <token>#{token1}</token>
734 <filename>test1.txt</filename>
765 <filename>test1.txt</filename>
735 </upload>
766 </upload>
736 <upload>
767 <upload>
737 <token>#{token2}</token>
768 <token>#{token2}</token>
738 <filename>test1.txt</filename>
769 <filename>test1.txt</filename>
739 </upload>
770 </upload>
740 </uploads>
771 </uploads>
741 </issue>
772 </issue>
742 XML
773 XML
743
774
744 assert_difference 'Issue.count' do
775 assert_difference 'Issue.count' do
745 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
776 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
746 assert_response :created
777 assert_response :created
747 end
778 end
748 issue = Issue.order('id DESC').first
779 issue = Issue.order('id DESC').first
749 assert_equal 2, issue.attachments.count
780 assert_equal 2, issue.attachments.count
750 end
781 end
751
782
752 def test_create_issue_with_multiple_uploaded_files_as_json
783 def test_create_issue_with_multiple_uploaded_files_as_json
753 token1 = json_upload('File content 1', credentials('jsmith'))
784 token1 = json_upload('File content 1', credentials('jsmith'))
754 token2 = json_upload('File content 2', credentials('jsmith'))
785 token2 = json_upload('File content 2', credentials('jsmith'))
755
786
756 payload = <<-JSON
787 payload = <<-JSON
757 {
788 {
758 "issue": {
789 "issue": {
759 "project_id": "1",
790 "project_id": "1",
760 "tracker_id": "1",
791 "tracker_id": "1",
761 "subject": "Issue with multiple attachments",
792 "subject": "Issue with multiple attachments",
762 "uploads": [
793 "uploads": [
763 {"token": "#{token1}", "filename": "test1.txt"},
794 {"token": "#{token1}", "filename": "test1.txt"},
764 {"token": "#{token2}", "filename": "test2.txt"}
795 {"token": "#{token2}", "filename": "test2.txt"}
765 ]
796 ]
766 }
797 }
767 }
798 }
768 JSON
799 JSON
769
800
770 assert_difference 'Issue.count' do
801 assert_difference 'Issue.count' do
771 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
802 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
772 assert_response :created
803 assert_response :created
773 end
804 end
774 issue = Issue.order('id DESC').first
805 issue = Issue.order('id DESC').first
775 assert_equal 2, issue.attachments.count
806 assert_equal 2, issue.attachments.count
776 end
807 end
777
808
778 def test_update_issue_with_uploaded_file
809 def test_update_issue_with_uploaded_file
779 token = xml_upload('test_upload_with_upload', credentials('jsmith'))
810 token = xml_upload('test_upload_with_upload', credentials('jsmith'))
780 attachment = Attachment.find_by_token(token)
811 attachment = Attachment.find_by_token(token)
781
812
782 # update the issue with the upload's token
813 # update the issue with the upload's token
783 assert_difference 'Journal.count' do
814 assert_difference 'Journal.count' do
784 put '/issues/1.xml',
815 put '/issues/1.xml',
785 {:issue => {:notes => 'Attachment added',
816 {:issue => {:notes => 'Attachment added',
786 :uploads => [{:token => token, :filename => 'test.txt',
817 :uploads => [{:token => token, :filename => 'test.txt',
787 :content_type => 'text/plain'}]}},
818 :content_type => 'text/plain'}]}},
788 credentials('jsmith')
819 credentials('jsmith')
789 assert_response :ok
820 assert_response :ok
790 assert_equal '', @response.body
821 assert_equal '', @response.body
791 end
822 end
792
823
793 issue = Issue.find(1)
824 issue = Issue.find(1)
794 assert_include attachment, issue.attachments
825 assert_include attachment, issue.attachments
795 end
826 end
796 end
827 end
General Comments 0
You need to be logged in to leave comments. Login now