##// END OF EJS Templates
Merged r14794 (#21136)....
Jean-Philippe Lang -
r14460:8935ccf736ba
parent child
Show More
@@ -1,75 +1,75
1 api.issue do
1 api.issue do
2 api.id @issue.id
2 api.id @issue.id
3 api.project(:id => @issue.project_id, :name => @issue.project.name) unless @issue.project.nil?
3 api.project(:id => @issue.project_id, :name => @issue.project.name) unless @issue.project.nil?
4 api.tracker(:id => @issue.tracker_id, :name => @issue.tracker.name) unless @issue.tracker.nil?
4 api.tracker(:id => @issue.tracker_id, :name => @issue.tracker.name) unless @issue.tracker.nil?
5 api.status(:id => @issue.status_id, :name => @issue.status.name) unless @issue.status.nil?
5 api.status(:id => @issue.status_id, :name => @issue.status.name) unless @issue.status.nil?
6 api.priority(:id => @issue.priority_id, :name => @issue.priority.name) unless @issue.priority.nil?
6 api.priority(:id => @issue.priority_id, :name => @issue.priority.name) unless @issue.priority.nil?
7 api.author(:id => @issue.author_id, :name => @issue.author.name) unless @issue.author.nil?
7 api.author(:id => @issue.author_id, :name => @issue.author.name) unless @issue.author.nil?
8 api.assigned_to(:id => @issue.assigned_to_id, :name => @issue.assigned_to.name) unless @issue.assigned_to.nil?
8 api.assigned_to(:id => @issue.assigned_to_id, :name => @issue.assigned_to.name) unless @issue.assigned_to.nil?
9 api.category(:id => @issue.category_id, :name => @issue.category.name) unless @issue.category.nil?
9 api.category(:id => @issue.category_id, :name => @issue.category.name) unless @issue.category.nil?
10 api.fixed_version(:id => @issue.fixed_version_id, :name => @issue.fixed_version.name) unless @issue.fixed_version.nil?
10 api.fixed_version(:id => @issue.fixed_version_id, :name => @issue.fixed_version.name) unless @issue.fixed_version.nil?
11 api.parent(:id => @issue.parent_id) unless @issue.parent.nil?
11 api.parent(:id => @issue.parent_id) unless @issue.parent.nil?
12
12
13 api.subject @issue.subject
13 api.subject @issue.subject
14 api.description @issue.description
14 api.description @issue.description
15 api.start_date @issue.start_date
15 api.start_date @issue.start_date
16 api.due_date @issue.due_date
16 api.due_date @issue.due_date
17 api.done_ratio @issue.done_ratio
17 api.done_ratio @issue.done_ratio
18 api.is_private @issue.is_private
18 api.is_private @issue.is_private
19 api.estimated_hours @issue.estimated_hours
19 api.estimated_hours @issue.estimated_hours
20 api.spent_hours(@issue.spent_hours) if User.current.allowed_to?(:view_time_entries, @project)
20 api.spent_hours(@issue.spent_hours) if User.current.allowed_to?(:view_time_entries, @project)
21
21
22 render_api_custom_values @issue.visible_custom_field_values, api
22 render_api_custom_values @issue.visible_custom_field_values, api
23
23
24 api.created_on @issue.created_on
24 api.created_on @issue.created_on
25 api.updated_on @issue.updated_on
25 api.updated_on @issue.updated_on
26 api.closed_on @issue.closed_on
26 api.closed_on @issue.closed_on
27
27
28 render_api_issue_children(@issue, api) if include_in_api_response?('children')
28 render_api_issue_children(@issue, api) if include_in_api_response?('children')
29
29
30 api.array :attachments do
30 api.array :attachments do
31 @issue.attachments.each do |attachment|
31 @issue.attachments.each do |attachment|
32 render_api_attachment(attachment, api)
32 render_api_attachment(attachment, api)
33 end
33 end
34 end if include_in_api_response?('attachments')
34 end if include_in_api_response?('attachments')
35
35
36 api.array :relations do
36 api.array :relations do
37 @relations.each do |relation|
37 @relations.each do |relation|
38 api.relation(:id => relation.id, :issue_id => relation.issue_from_id, :issue_to_id => relation.issue_to_id, :relation_type => relation.relation_type, :delay => relation.delay)
38 api.relation(:id => relation.id, :issue_id => relation.issue_from_id, :issue_to_id => relation.issue_to_id, :relation_type => relation.relation_type, :delay => relation.delay)
39 end
39 end
40 end if include_in_api_response?('relations') && @relations.present?
40 end if include_in_api_response?('relations') && @relations.present?
41
41
42 api.array :changesets do
42 api.array :changesets do
43 @issue.changesets.each do |changeset|
43 @changesets.each do |changeset|
44 api.changeset :revision => changeset.revision do
44 api.changeset :revision => changeset.revision do
45 api.user(:id => changeset.user_id, :name => changeset.user.name) unless changeset.user.nil?
45 api.user(:id => changeset.user_id, :name => changeset.user.name) unless changeset.user.nil?
46 api.comments changeset.comments
46 api.comments changeset.comments
47 api.committed_on changeset.committed_on
47 api.committed_on changeset.committed_on
48 end
48 end
49 end
49 end
50 end if include_in_api_response?('changesets') && User.current.allowed_to?(:view_changesets, @project)
50 end if include_in_api_response?('changesets')
51
51
52 api.array :journals do
52 api.array :journals do
53 @journals.each do |journal|
53 @journals.each do |journal|
54 api.journal :id => journal.id do
54 api.journal :id => journal.id do
55 api.user(:id => journal.user_id, :name => journal.user.name) unless journal.user.nil?
55 api.user(:id => journal.user_id, :name => journal.user.name) unless journal.user.nil?
56 api.notes journal.notes
56 api.notes journal.notes
57 api.created_on journal.created_on
57 api.created_on journal.created_on
58 api.array :details do
58 api.array :details do
59 journal.visible_details.each do |detail|
59 journal.visible_details.each do |detail|
60 api.detail :property => detail.property, :name => detail.prop_key do
60 api.detail :property => detail.property, :name => detail.prop_key do
61 api.old_value detail.old_value
61 api.old_value detail.old_value
62 api.new_value detail.value
62 api.new_value detail.value
63 end
63 end
64 end
64 end
65 end
65 end
66 end
66 end
67 end
67 end
68 end if include_in_api_response?('journals')
68 end if include_in_api_response?('journals')
69
69
70 api.array :watchers do
70 api.array :watchers do
71 @issue.watcher_users.each do |user|
71 @issue.watcher_users.each do |user|
72 api.user :id => user.id, :name => user.name
72 api.user :id => user.id, :name => user.name
73 end
73 end
74 end if include_in_api_response?('watchers') && User.current.allowed_to?(:view_issue_watchers, @issue.project)
74 end if include_in_api_response?('watchers') && User.current.allowed_to?(:view_issue_watchers, @issue.project)
75 end
75 end
@@ -1,693 +1,707
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 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
340 project = Project.generate!(:is_public => false)
341 repository = Repository::Subversion.create!(:project => project, :url => "svn://localhost")
342 Issue.find(1).changesets << Changeset.generate!(:repository => repository)
343 assert Issue.find(1).changesets.any?
344
345 get '/issues/1.xml?include=changesets', {}, credentials('jsmith')
346
347 # the user jsmith has no permission to view the associated changeset
348 assert_select 'issue changesets[type=array]' do
349 assert_select 'changeset', 0
350 end
351 end
352
339 test "POST /issues.xml should create an issue with the attributes" do
353 test "POST /issues.xml should create an issue with the attributes" do
340
354
341 payload = <<-XML
355 payload = <<-XML
342 <?xml version="1.0" encoding="UTF-8" ?>
356 <?xml version="1.0" encoding="UTF-8" ?>
343 <issue>
357 <issue>
344 <project_id>1</project_id>
358 <project_id>1</project_id>
345 <tracker_id>2</tracker_id>
359 <tracker_id>2</tracker_id>
346 <status_id>3</status_id>
360 <status_id>3</status_id>
347 <subject>API test</subject>
361 <subject>API test</subject>
348 </issue>
362 </issue>
349 XML
363 XML
350
364
351 assert_difference('Issue.count') do
365 assert_difference('Issue.count') do
352 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
366 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
353 end
367 end
354 issue = Issue.order('id DESC').first
368 issue = Issue.order('id DESC').first
355 assert_equal 1, issue.project_id
369 assert_equal 1, issue.project_id
356 assert_equal 2, issue.tracker_id
370 assert_equal 2, issue.tracker_id
357 assert_equal 3, issue.status_id
371 assert_equal 3, issue.status_id
358 assert_equal 'API test', issue.subject
372 assert_equal 'API test', issue.subject
359
373
360 assert_response :created
374 assert_response :created
361 assert_equal 'application/xml', @response.content_type
375 assert_equal 'application/xml', @response.content_type
362 assert_select 'issue > id', :text => issue.id.to_s
376 assert_select 'issue > id', :text => issue.id.to_s
363 end
377 end
364
378
365 test "POST /issues.xml with watcher_user_ids should create issue with watchers" do
379 test "POST /issues.xml with watcher_user_ids should create issue with watchers" do
366 assert_difference('Issue.count') do
380 assert_difference('Issue.count') do
367 post '/issues.xml',
381 post '/issues.xml',
368 {:issue => {:project_id => 1, :subject => 'Watchers',
382 {:issue => {:project_id => 1, :subject => 'Watchers',
369 :tracker_id => 2, :status_id => 3, :watcher_user_ids => [3, 1]}}, credentials('jsmith')
383 :tracker_id => 2, :status_id => 3, :watcher_user_ids => [3, 1]}}, credentials('jsmith')
370 assert_response :created
384 assert_response :created
371 end
385 end
372 issue = Issue.order('id desc').first
386 issue = Issue.order('id desc').first
373 assert_equal 2, issue.watchers.size
387 assert_equal 2, issue.watchers.size
374 assert_equal [1, 3], issue.watcher_user_ids.sort
388 assert_equal [1, 3], issue.watcher_user_ids.sort
375 end
389 end
376
390
377 test "POST /issues.xml with failure should return errors" do
391 test "POST /issues.xml with failure should return errors" do
378 assert_no_difference('Issue.count') do
392 assert_no_difference('Issue.count') do
379 post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
393 post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
380 end
394 end
381
395
382 assert_select 'errors error', :text => "Subject cannot be blank"
396 assert_select 'errors error', :text => "Subject cannot be blank"
383 end
397 end
384
398
385 test "POST /issues.json should create an issue with the attributes" do
399 test "POST /issues.json should create an issue with the attributes" do
386
400
387 payload = <<-JSON
401 payload = <<-JSON
388 {
402 {
389 "issue": {
403 "issue": {
390 "project_id": "1",
404 "project_id": "1",
391 "tracker_id": "2",
405 "tracker_id": "2",
392 "status_id": "3",
406 "status_id": "3",
393 "subject": "API test"
407 "subject": "API test"
394 }
408 }
395 }
409 }
396 JSON
410 JSON
397
411
398 assert_difference('Issue.count') do
412 assert_difference('Issue.count') do
399 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
413 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
400 end
414 end
401
415
402 issue = Issue.order('id DESC').first
416 issue = Issue.order('id DESC').first
403 assert_equal 1, issue.project_id
417 assert_equal 1, issue.project_id
404 assert_equal 2, issue.tracker_id
418 assert_equal 2, issue.tracker_id
405 assert_equal 3, issue.status_id
419 assert_equal 3, issue.status_id
406 assert_equal 'API test', issue.subject
420 assert_equal 'API test', issue.subject
407 end
421 end
408
422
409 test "POST /issues.json without tracker_id should accept custom fields" do
423 test "POST /issues.json without tracker_id should accept custom fields" do
410 field = IssueCustomField.generate!(
424 field = IssueCustomField.generate!(
411 :field_format => 'list',
425 :field_format => 'list',
412 :multiple => true,
426 :multiple => true,
413 :possible_values => ["V1", "V2", "V3"],
427 :possible_values => ["V1", "V2", "V3"],
414 :default_value => "V2",
428 :default_value => "V2",
415 :is_for_all => true,
429 :is_for_all => true,
416 :trackers => Tracker.all.to_a
430 :trackers => Tracker.all.to_a
417 )
431 )
418
432
419 payload = <<-JSON
433 payload = <<-JSON
420 {
434 {
421 "issue": {
435 "issue": {
422 "project_id": "1",
436 "project_id": "1",
423 "subject": "Multivalued custom field",
437 "subject": "Multivalued custom field",
424 "custom_field_values":{"#{field.id}":["V1","V3"]}
438 "custom_field_values":{"#{field.id}":["V1","V3"]}
425 }
439 }
426 }
440 }
427 JSON
441 JSON
428
442
429 assert_difference('Issue.count') do
443 assert_difference('Issue.count') do
430 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
444 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
431 end
445 end
432
446
433 assert_response :created
447 assert_response :created
434 issue = Issue.order('id DESC').first
448 issue = Issue.order('id DESC').first
435 assert_equal ["V1", "V3"], issue.custom_field_value(field).sort
449 assert_equal ["V1", "V3"], issue.custom_field_value(field).sort
436 end
450 end
437
451
438 test "POST /issues.json with failure should return errors" do
452 test "POST /issues.json with failure should return errors" do
439 assert_no_difference('Issue.count') do
453 assert_no_difference('Issue.count') do
440 post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
454 post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
441 end
455 end
442
456
443 json = ActiveSupport::JSON.decode(response.body)
457 json = ActiveSupport::JSON.decode(response.body)
444 assert json['errors'].include?("Subject cannot be blank")
458 assert json['errors'].include?("Subject cannot be blank")
445 end
459 end
446
460
447 test "POST /issues.json with invalid project_id should respond with 422" do
461 test "POST /issues.json with invalid project_id should respond with 422" do
448 post '/issues.json', {:issue => {:project_id => 999, :subject => "API"}}, credentials('jsmith')
462 post '/issues.json', {:issue => {:project_id => 999, :subject => "API"}}, credentials('jsmith')
449 assert_response 422
463 assert_response 422
450 end
464 end
451
465
452 test "PUT /issues/:id.xml" do
466 test "PUT /issues/:id.xml" do
453 assert_difference('Journal.count') do
467 assert_difference('Journal.count') do
454 put '/issues/6.xml',
468 put '/issues/6.xml',
455 {:issue => {:subject => 'API update', :notes => 'A new note'}},
469 {:issue => {:subject => 'API update', :notes => 'A new note'}},
456 credentials('jsmith')
470 credentials('jsmith')
457 end
471 end
458
472
459 issue = Issue.find(6)
473 issue = Issue.find(6)
460 assert_equal "API update", issue.subject
474 assert_equal "API update", issue.subject
461 journal = Journal.last
475 journal = Journal.last
462 assert_equal "A new note", journal.notes
476 assert_equal "A new note", journal.notes
463 end
477 end
464
478
465 test "PUT /issues/:id.xml with custom fields" do
479 test "PUT /issues/:id.xml with custom fields" do
466 put '/issues/3.xml',
480 put '/issues/3.xml',
467 {:issue => {:custom_fields => [
481 {:issue => {:custom_fields => [
468 {'id' => '1', 'value' => 'PostgreSQL' },
482 {'id' => '1', 'value' => 'PostgreSQL' },
469 {'id' => '2', 'value' => '150'}
483 {'id' => '2', 'value' => '150'}
470 ]}},
484 ]}},
471 credentials('jsmith')
485 credentials('jsmith')
472
486
473 issue = Issue.find(3)
487 issue = Issue.find(3)
474 assert_equal '150', issue.custom_value_for(2).value
488 assert_equal '150', issue.custom_value_for(2).value
475 assert_equal 'PostgreSQL', issue.custom_value_for(1).value
489 assert_equal 'PostgreSQL', issue.custom_value_for(1).value
476 end
490 end
477
491
478 test "PUT /issues/:id.xml with multi custom fields" do
492 test "PUT /issues/:id.xml with multi custom fields" do
479 field = CustomField.find(1)
493 field = CustomField.find(1)
480 field.update_attribute :multiple, true
494 field.update_attribute :multiple, true
481
495
482 put '/issues/3.xml',
496 put '/issues/3.xml',
483 {:issue => {:custom_fields => [
497 {:issue => {:custom_fields => [
484 {'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
498 {'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
485 {'id' => '2', 'value' => '150'}
499 {'id' => '2', 'value' => '150'}
486 ]}},
500 ]}},
487 credentials('jsmith')
501 credentials('jsmith')
488
502
489 issue = Issue.find(3)
503 issue = Issue.find(3)
490 assert_equal '150', issue.custom_value_for(2).value
504 assert_equal '150', issue.custom_value_for(2).value
491 assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
505 assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
492 end
506 end
493
507
494 test "PUT /issues/:id.xml with project change" do
508 test "PUT /issues/:id.xml with project change" do
495 put '/issues/3.xml',
509 put '/issues/3.xml',
496 {:issue => {:project_id => 2, :subject => 'Project changed'}},
510 {:issue => {:project_id => 2, :subject => 'Project changed'}},
497 credentials('jsmith')
511 credentials('jsmith')
498
512
499 issue = Issue.find(3)
513 issue = Issue.find(3)
500 assert_equal 2, issue.project_id
514 assert_equal 2, issue.project_id
501 assert_equal 'Project changed', issue.subject
515 assert_equal 'Project changed', issue.subject
502 end
516 end
503
517
504 test "PUT /issues/:id.xml with failed update" do
518 test "PUT /issues/:id.xml with failed update" do
505 put '/issues/6.xml', {:issue => {:subject => ''}}, credentials('jsmith')
519 put '/issues/6.xml', {:issue => {:subject => ''}}, credentials('jsmith')
506
520
507 assert_response :unprocessable_entity
521 assert_response :unprocessable_entity
508 assert_select 'errors error', :text => "Subject cannot be blank"
522 assert_select 'errors error', :text => "Subject cannot be blank"
509 end
523 end
510
524
511 test "PUT /issues/:id.json" do
525 test "PUT /issues/:id.json" do
512 assert_difference('Journal.count') do
526 assert_difference('Journal.count') do
513 put '/issues/6.json',
527 put '/issues/6.json',
514 {:issue => {:subject => 'API update', :notes => 'A new note'}},
528 {:issue => {:subject => 'API update', :notes => 'A new note'}},
515 credentials('jsmith')
529 credentials('jsmith')
516
530
517 assert_response :ok
531 assert_response :ok
518 assert_equal '', response.body
532 assert_equal '', response.body
519 end
533 end
520
534
521 issue = Issue.find(6)
535 issue = Issue.find(6)
522 assert_equal "API update", issue.subject
536 assert_equal "API update", issue.subject
523 journal = Journal.last
537 journal = Journal.last
524 assert_equal "A new note", journal.notes
538 assert_equal "A new note", journal.notes
525 end
539 end
526
540
527 test "PUT /issues/:id.json with failed update" do
541 test "PUT /issues/:id.json with failed update" do
528 put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')
542 put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')
529
543
530 assert_response :unprocessable_entity
544 assert_response :unprocessable_entity
531 json = ActiveSupport::JSON.decode(response.body)
545 json = ActiveSupport::JSON.decode(response.body)
532 assert json['errors'].include?("Subject cannot be blank")
546 assert json['errors'].include?("Subject cannot be blank")
533 end
547 end
534
548
535 test "DELETE /issues/:id.xml" do
549 test "DELETE /issues/:id.xml" do
536 assert_difference('Issue.count', -1) do
550 assert_difference('Issue.count', -1) do
537 delete '/issues/6.xml', {}, credentials('jsmith')
551 delete '/issues/6.xml', {}, credentials('jsmith')
538
552
539 assert_response :ok
553 assert_response :ok
540 assert_equal '', response.body
554 assert_equal '', response.body
541 end
555 end
542 assert_nil Issue.find_by_id(6)
556 assert_nil Issue.find_by_id(6)
543 end
557 end
544
558
545 test "DELETE /issues/:id.json" do
559 test "DELETE /issues/:id.json" do
546 assert_difference('Issue.count', -1) do
560 assert_difference('Issue.count', -1) do
547 delete '/issues/6.json', {}, credentials('jsmith')
561 delete '/issues/6.json', {}, credentials('jsmith')
548
562
549 assert_response :ok
563 assert_response :ok
550 assert_equal '', response.body
564 assert_equal '', response.body
551 end
565 end
552 assert_nil Issue.find_by_id(6)
566 assert_nil Issue.find_by_id(6)
553 end
567 end
554
568
555 test "POST /issues/:id/watchers.xml should add watcher" do
569 test "POST /issues/:id/watchers.xml should add watcher" do
556 assert_difference 'Watcher.count' do
570 assert_difference 'Watcher.count' do
557 post '/issues/1/watchers.xml', {:user_id => 3}, credentials('jsmith')
571 post '/issues/1/watchers.xml', {:user_id => 3}, credentials('jsmith')
558
572
559 assert_response :ok
573 assert_response :ok
560 assert_equal '', response.body
574 assert_equal '', response.body
561 end
575 end
562 watcher = Watcher.order('id desc').first
576 watcher = Watcher.order('id desc').first
563 assert_equal Issue.find(1), watcher.watchable
577 assert_equal Issue.find(1), watcher.watchable
564 assert_equal User.find(3), watcher.user
578 assert_equal User.find(3), watcher.user
565 end
579 end
566
580
567 test "DELETE /issues/:id/watchers/:user_id.xml should remove watcher" do
581 test "DELETE /issues/:id/watchers/:user_id.xml should remove watcher" do
568 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
582 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
569
583
570 assert_difference 'Watcher.count', -1 do
584 assert_difference 'Watcher.count', -1 do
571 delete '/issues/1/watchers/3.xml', {}, credentials('jsmith')
585 delete '/issues/1/watchers/3.xml', {}, credentials('jsmith')
572
586
573 assert_response :ok
587 assert_response :ok
574 assert_equal '', response.body
588 assert_equal '', response.body
575 end
589 end
576 assert_equal false, Issue.find(1).watched_by?(User.find(3))
590 assert_equal false, Issue.find(1).watched_by?(User.find(3))
577 end
591 end
578
592
579 def test_create_issue_with_uploaded_file
593 def test_create_issue_with_uploaded_file
580 token = xml_upload('test_create_with_upload', credentials('jsmith'))
594 token = xml_upload('test_create_with_upload', credentials('jsmith'))
581 attachment = Attachment.find_by_token(token)
595 attachment = Attachment.find_by_token(token)
582
596
583 # create the issue with the upload's token
597 # create the issue with the upload's token
584 assert_difference 'Issue.count' do
598 assert_difference 'Issue.count' do
585 post '/issues.xml',
599 post '/issues.xml',
586 {:issue => {:project_id => 1, :subject => 'Uploaded file',
600 {:issue => {:project_id => 1, :subject => 'Uploaded file',
587 :uploads => [{:token => token, :filename => 'test.txt',
601 :uploads => [{:token => token, :filename => 'test.txt',
588 :content_type => 'text/plain'}]}},
602 :content_type => 'text/plain'}]}},
589 credentials('jsmith')
603 credentials('jsmith')
590 assert_response :created
604 assert_response :created
591 end
605 end
592 issue = Issue.order('id DESC').first
606 issue = Issue.order('id DESC').first
593 assert_equal 1, issue.attachments.count
607 assert_equal 1, issue.attachments.count
594 assert_equal attachment, issue.attachments.first
608 assert_equal attachment, issue.attachments.first
595
609
596 attachment.reload
610 attachment.reload
597 assert_equal 'test.txt', attachment.filename
611 assert_equal 'test.txt', attachment.filename
598 assert_equal 'text/plain', attachment.content_type
612 assert_equal 'text/plain', attachment.content_type
599 assert_equal 'test_create_with_upload'.size, attachment.filesize
613 assert_equal 'test_create_with_upload'.size, attachment.filesize
600 assert_equal 2, attachment.author_id
614 assert_equal 2, attachment.author_id
601
615
602 # get the issue with its attachments
616 # get the issue with its attachments
603 get "/issues/#{issue.id}.xml", :include => 'attachments'
617 get "/issues/#{issue.id}.xml", :include => 'attachments'
604 assert_response :success
618 assert_response :success
605 xml = Hash.from_xml(response.body)
619 xml = Hash.from_xml(response.body)
606 attachments = xml['issue']['attachments']
620 attachments = xml['issue']['attachments']
607 assert_kind_of Array, attachments
621 assert_kind_of Array, attachments
608 assert_equal 1, attachments.size
622 assert_equal 1, attachments.size
609 url = attachments.first['content_url']
623 url = attachments.first['content_url']
610 assert_not_nil url
624 assert_not_nil url
611
625
612 # download the attachment
626 # download the attachment
613 get url
627 get url
614 assert_response :success
628 assert_response :success
615 assert_equal 'test_create_with_upload', response.body
629 assert_equal 'test_create_with_upload', response.body
616 end
630 end
617
631
618 def test_create_issue_with_multiple_uploaded_files_as_xml
632 def test_create_issue_with_multiple_uploaded_files_as_xml
619 token1 = xml_upload('File content 1', credentials('jsmith'))
633 token1 = xml_upload('File content 1', credentials('jsmith'))
620 token2 = xml_upload('File content 2', credentials('jsmith'))
634 token2 = xml_upload('File content 2', credentials('jsmith'))
621
635
622 payload = <<-XML
636 payload = <<-XML
623 <?xml version="1.0" encoding="UTF-8" ?>
637 <?xml version="1.0" encoding="UTF-8" ?>
624 <issue>
638 <issue>
625 <project_id>1</project_id>
639 <project_id>1</project_id>
626 <tracker_id>1</tracker_id>
640 <tracker_id>1</tracker_id>
627 <subject>Issue with multiple attachments</subject>
641 <subject>Issue with multiple attachments</subject>
628 <uploads type="array">
642 <uploads type="array">
629 <upload>
643 <upload>
630 <token>#{token1}</token>
644 <token>#{token1}</token>
631 <filename>test1.txt</filename>
645 <filename>test1.txt</filename>
632 </upload>
646 </upload>
633 <upload>
647 <upload>
634 <token>#{token2}</token>
648 <token>#{token2}</token>
635 <filename>test1.txt</filename>
649 <filename>test1.txt</filename>
636 </upload>
650 </upload>
637 </uploads>
651 </uploads>
638 </issue>
652 </issue>
639 XML
653 XML
640
654
641 assert_difference 'Issue.count' do
655 assert_difference 'Issue.count' do
642 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
656 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
643 assert_response :created
657 assert_response :created
644 end
658 end
645 issue = Issue.order('id DESC').first
659 issue = Issue.order('id DESC').first
646 assert_equal 2, issue.attachments.count
660 assert_equal 2, issue.attachments.count
647 end
661 end
648
662
649 def test_create_issue_with_multiple_uploaded_files_as_json
663 def test_create_issue_with_multiple_uploaded_files_as_json
650 token1 = json_upload('File content 1', credentials('jsmith'))
664 token1 = json_upload('File content 1', credentials('jsmith'))
651 token2 = json_upload('File content 2', credentials('jsmith'))
665 token2 = json_upload('File content 2', credentials('jsmith'))
652
666
653 payload = <<-JSON
667 payload = <<-JSON
654 {
668 {
655 "issue": {
669 "issue": {
656 "project_id": "1",
670 "project_id": "1",
657 "tracker_id": "1",
671 "tracker_id": "1",
658 "subject": "Issue with multiple attachments",
672 "subject": "Issue with multiple attachments",
659 "uploads": [
673 "uploads": [
660 {"token": "#{token1}", "filename": "test1.txt"},
674 {"token": "#{token1}", "filename": "test1.txt"},
661 {"token": "#{token2}", "filename": "test2.txt"}
675 {"token": "#{token2}", "filename": "test2.txt"}
662 ]
676 ]
663 }
677 }
664 }
678 }
665 JSON
679 JSON
666
680
667 assert_difference 'Issue.count' do
681 assert_difference 'Issue.count' do
668 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
682 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
669 assert_response :created
683 assert_response :created
670 end
684 end
671 issue = Issue.order('id DESC').first
685 issue = Issue.order('id DESC').first
672 assert_equal 2, issue.attachments.count
686 assert_equal 2, issue.attachments.count
673 end
687 end
674
688
675 def test_update_issue_with_uploaded_file
689 def test_update_issue_with_uploaded_file
676 token = xml_upload('test_upload_with_upload', credentials('jsmith'))
690 token = xml_upload('test_upload_with_upload', credentials('jsmith'))
677 attachment = Attachment.find_by_token(token)
691 attachment = Attachment.find_by_token(token)
678
692
679 # update the issue with the upload's token
693 # update the issue with the upload's token
680 assert_difference 'Journal.count' do
694 assert_difference 'Journal.count' do
681 put '/issues/1.xml',
695 put '/issues/1.xml',
682 {:issue => {:notes => 'Attachment added',
696 {:issue => {:notes => 'Attachment added',
683 :uploads => [{:token => token, :filename => 'test.txt',
697 :uploads => [{:token => token, :filename => 'test.txt',
684 :content_type => 'text/plain'}]}},
698 :content_type => 'text/plain'}]}},
685 credentials('jsmith')
699 credentials('jsmith')
686 assert_response :ok
700 assert_response :ok
687 assert_equal '', @response.body
701 assert_equal '', @response.body
688 end
702 end
689
703
690 issue = Issue.find(1)
704 issue = Issue.find(1)
691 assert_include attachment, issue.attachments
705 assert_include attachment, issue.attachments
692 end
706 end
693 end
707 end
General Comments 0
You need to be logged in to leave comments. Login now