Skip to content

Commit a80ccd3

Browse files
committed
fix api access to /scheduling_poll/show_by_issue and /scheduling_poll/update
1 parent 20c541a commit a80ccd3

File tree

2 files changed

+139
-17
lines changed

2 files changed

+139
-17
lines changed

app/controllers/scheduling_polls_controller.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class SchedulingPollsController < ApplicationController
66
before_action :ensure_allowed_to_view_scheduling_polls, :only => [:show, :show_by_issue]
77
before_action :ensure_allowed_to_vote_scheduling_polls, :only => [:edit, :update, :vote]
88

9-
accept_api_auth :create, :show, :vote
9+
accept_api_auth :create, :update, :show, :show_by_issue, :vote
1010

1111
def new
1212
@issue = Issue.find(params[:issue])
@@ -73,9 +73,15 @@ def update
7373

7474
if @poll.update(scheduling_poll_params)
7575
flash[:notice] = l(:notice_successful_update)
76-
redirect_to @poll
76+
respond_to do |format|
77+
format.html { redirect_to @poll }
78+
format.api { render_api_ok }
79+
end
7780
else
78-
render :edit
81+
respond_to do |format|
82+
format.html { render :edit }
83+
format.api { render_validation_errors(@poll) }
84+
end
7985
end
8086
end
8187

@@ -87,8 +93,10 @@ def show
8793
end
8894

8995
def show_by_issue
90-
#redirect_to @poll, :format => "xml"
91-
redirect_to :action => :show, :id => @poll.id, :format => params[:format]
96+
respond_to do |format|
97+
format.html { redirect_to @poll }
98+
format.api { render :action => :show }
99+
end
92100
end
93101

94102
def vote

test/functional/scheduling_polls_controller_test.rb

Lines changed: 126 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def teardown
6060
assert_not_nil flash[:notice]
6161
end
6262

63-
test "create.api" do
63+
test "create.json" do
6464
with_settings :rest_api_enabled => '1' do
6565
poll = SchedulingPoll.find_by(:issue => 1)
6666
assert_not_nil poll
@@ -70,11 +70,6 @@ def teardown
7070
assert_equal 'exist', json['status']
7171
assert_equal poll.id, json['poll']['id']
7272

73-
post :create, :scheduling_poll => {:issue_id => 1, :scheduling_poll_items_attributes => []}, :format => :xml, :key => User.current.api_key
74-
assert_response :success
75-
assert_equal 'application/xml', response.content_type
76-
assert_match /exist/, response.body
77-
7873
assert poll.destroy
7974
poll = nil
8075
post :create, :scheduling_poll => {:issue_id => 1, :scheduling_poll_items_attributes => [{:text => "text1", :position => 1}, {:text => "text2", :position => 2}, {:text => "", :position => 3}]}, :format => :json, :key => User.current.api_key
@@ -86,6 +81,17 @@ def teardown
8681
json = ActiveSupport::JSON.decode(response.body)
8782
assert_equal 'ok', json['status']
8883
assert_equal poll.id, json['poll']['id']
84+
end
85+
end
86+
87+
test "create.xml" do
88+
with_settings :rest_api_enabled => '1' do
89+
poll = SchedulingPoll.find_by(:issue => 1)
90+
assert_not_nil poll
91+
post :create, :scheduling_poll => {:issue_id => 1, :scheduling_poll_items_attributes => []}, :format => :xml, :key => User.current.api_key
92+
assert_response :success
93+
assert_equal 'application/xml', response.content_type
94+
assert_match /exist/, response.body
8995

9096
assert poll.destroy
9197
poll = nil
@@ -121,6 +127,26 @@ def teardown
121127
assert_not_nil flash[:notice]
122128
end
123129

130+
test "update.json" do
131+
with_settings :rest_api_enabled => '1' do
132+
poll = SchedulingPoll.find_by(:issue => 1)
133+
assert_not_nil poll
134+
patch :update, :id => 1, :scheduling_poll => {:issue_id => 1, :scheduling_poll_items_attributes => [{:id => 1, :position => 1}, {:id => 2, :position => 2, :_destroy => 1}, {:id => 3, :position => 3, :_destroy => 0}, {:text => "text", :position => 4}, {:text => "", :position => 5}]}, :format => :json, :key => User.current.api_key
135+
assert_response :success
136+
assert_empty response.body
137+
end
138+
end
139+
140+
test "update.xml" do
141+
with_settings :rest_api_enabled => '1' do
142+
poll = SchedulingPoll.find_by(:issue => 1)
143+
assert_not_nil poll
144+
patch :update, :id => 1, :scheduling_poll => {:issue_id => 1, :scheduling_poll_items_attributes => [{:id => 1, :position => 1}, {:id => 2, :position => 2, :_destroy => 1}, {:id => 3, :position => 3, :_destroy => 0}, {:text => "text", :position => 4}, {:text => "", :position => 5}]}, :format => :xml, :key => User.current.api_key
145+
assert_response :success
146+
assert_empty response.body
147+
end
148+
end
149+
124150
test "show" do
125151
get :show, :id => 1
126152
assert_response :success
@@ -155,7 +181,7 @@ def teardown
155181

156182
get :show, :id => 1, :format => :xml, :key => User.current.api_key
157183
assert_response :success
158-
assert_equal 'application/xml', @response.content_type
184+
assert_equal 'application/xml', response.content_type
159185

160186
get :show, :id => 9999, :format => :json, :key => User.current.api_key # not-exist issue
161187
assert_response 404
@@ -165,6 +191,50 @@ def teardown
165191
end
166192
end
167193

194+
test "show_by_issue" do
195+
get :show_by_issue, :issue_id => 1
196+
assert_redirected_to :action => :show, :id => 1
197+
198+
get :show_by_issue, :issue_id => 9999 # not-exist issue
199+
assert_response 404
200+
end
201+
202+
test "show_by_issue.api" do
203+
with_settings :rest_api_enabled => '1' do
204+
get :show_by_issue, :issue_id => 1, :format => :json, :key => User.current.api_key
205+
assert_response :success
206+
json = ActiveSupport::JSON.decode(response.body)
207+
assert_kind_of Hash, json['scheduling_poll']
208+
assert_equal 1, json['scheduling_poll']['id']
209+
assert_equal 1, json['scheduling_poll']['issue']['id']
210+
assert_kind_of Array, json['scheduling_poll']['scheduling_poll_items']
211+
assert_equal 1, json['scheduling_poll']['scheduling_poll_items'][0]['id']
212+
assert_equal SchedulingPollItem.find(1).text, json['scheduling_poll']['scheduling_poll_items'][0]['text']
213+
assert_kind_of Array, json['scheduling_poll']['scheduling_poll_items'][0]['scheduling_votes']
214+
assert_equal 1, json['scheduling_poll']['scheduling_poll_items'][0]['scheduling_votes'][0]['user']['id']
215+
assert_equal [{
216+
'value' => 3,
217+
'text' => Setting.plugin_redmine_scheduling_poll["scheduling_vote_value_3"],
218+
}][0], json['scheduling_poll']['scheduling_poll_items'][0]['scheduling_votes'][0]['value']
219+
assert_equal 2, json['scheduling_poll']['scheduling_poll_items'][0]['scheduling_votes'][1]['user']['id']
220+
assert_equal [{
221+
'value' => 2,
222+
'text' => Setting.plugin_redmine_scheduling_poll["scheduling_vote_value_2"],
223+
}][0], json['scheduling_poll']['scheduling_poll_items'][0]['scheduling_votes'][1]['value']
224+
assert_response :success
225+
226+
get :show_by_issue, :issue_id => 1, :format => :xml, :key => User.current.api_key
227+
assert_response :success
228+
assert_equal 'application/xml', response.content_type
229+
230+
get :show_by_issue, :issue_id => 9999, :format => :json, :key => User.current.api_key # not-exist issue
231+
assert_response 404
232+
233+
get :show_by_issue, :issue_id => 9999, :format => :xml, :key => User.current.api_key # not-exist issue
234+
assert_response 404
235+
end
236+
end
237+
168238
test "vote" do
169239
assert_equal nil, SchedulingPollItem.find(4).vote_by_user(User.current)
170240
assert_equal nil, SchedulingPollItem.find(5).vote_by_user(User.current)
@@ -189,11 +259,55 @@ def teardown
189259
assert_response 500
190260
end
191261

192-
test "show_by_issue" do
193-
get :show_by_issue, :issue_id => 1
194-
assert_redirected_to :action => :show, :id => 1
262+
test "vote.json" do
263+
with_settings :rest_api_enabled => '1' do
264+
assert_equal nil, SchedulingPollItem.find(4).vote_by_user(User.current)
265+
assert_equal nil, SchedulingPollItem.find(5).vote_by_user(User.current)
266+
assert_equal nil, SchedulingPollItem.find(6).vote_by_user(User.current)
195267

196-
get :show_by_issue, :issue_id => 9999 # not-exist issue
197-
assert_response 404
268+
post :vote, :id => 2, :scheduling_vote => { '4' => '0', '5' => '1', '6' => '2' }, :vote_comment => '', :format => :json, :key => User.current.api_key
269+
assert_response :success
270+
assert_empty response.body
271+
assert_equal nil, SchedulingPollItem.find(4).vote_by_user(User.current)
272+
assert_equal 1, SchedulingPollItem.find(5).vote_value_by_user(User.current)
273+
assert_equal 2, SchedulingPollItem.find(6).vote_value_by_user(User.current)
274+
275+
post :vote, :id => 2, :scheduling_vote => { '4' => '2', '5' => '1', '6' => '0' }, :vote_comment => '**vote test msg**', :format => :json, :key => User.current.api_key
276+
assert_response :success
277+
assert_empty response.body
278+
assert_equal 2, SchedulingPollItem.find(4).vote_value_by_user(User.current)
279+
assert_equal 1, SchedulingPollItem.find(5).vote_value_by_user(User.current)
280+
assert_equal nil, SchedulingPollItem.find(6).vote_by_user(User.current)
281+
# Journal does not work in the test (i.e. empty)
282+
283+
post :vote, :id => 2, :scheduling_vote => { '4' => '2', '5' => '1', '6' => '0' }, :vote_comment => '', :format => :json, :key => User.current.api_key # no-change
284+
assert_response 422
285+
end
286+
end
287+
288+
test "vote.xml" do
289+
with_settings :rest_api_enabled => '1' do
290+
assert_equal nil, SchedulingPollItem.find(4).vote_by_user(User.current)
291+
assert_equal nil, SchedulingPollItem.find(5).vote_by_user(User.current)
292+
assert_equal nil, SchedulingPollItem.find(6).vote_by_user(User.current)
293+
294+
post :vote, :id => 2, :scheduling_vote => { '4' => '0', '5' => '1', '6' => '2' }, :vote_comment => '', :format => :xml, :key => User.current.api_key
295+
assert_response :success
296+
assert_empty response.body
297+
assert_equal nil, SchedulingPollItem.find(4).vote_by_user(User.current)
298+
assert_equal 1, SchedulingPollItem.find(5).vote_value_by_user(User.current)
299+
assert_equal 2, SchedulingPollItem.find(6).vote_value_by_user(User.current)
300+
301+
post :vote, :id => 2, :scheduling_vote => { '4' => '2', '5' => '1', '6' => '0' }, :vote_comment => '**vote test msg**', :format => :xml, :key => User.current.api_key
302+
assert_response :success
303+
assert_empty response.body
304+
assert_equal 2, SchedulingPollItem.find(4).vote_value_by_user(User.current)
305+
assert_equal 1, SchedulingPollItem.find(5).vote_value_by_user(User.current)
306+
assert_equal nil, SchedulingPollItem.find(6).vote_by_user(User.current)
307+
# Journal does not work in the test (i.e. empty)
308+
309+
post :vote, :id => 2, :scheduling_vote => { '4' => '2', '5' => '1', '6' => '0' }, :vote_comment => '', :format => :xml, :key => User.current.api_key # no-change
310+
assert_response 422
311+
end
198312
end
199313
end

0 commit comments

Comments
 (0)