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