Refactoring: Move 'vote' action to Votes Controllers
As far as possible I think the code is clearer if we use CRUD actions rather than custom actions. This will make it easier to add the action to remove votes in the next commit. Note that we are adding this line as we need to validate it that a vote can be created on a debate by the current user: ```authorize! :create, Vote.new(voter: current_user, votable: @debate)``` We have done it this way and not with the following code as you might expect, as this way two votes are created instead of one. ```load_and_authorize_resource through: :debate, through_association: :votes_for``` This line tries to load the resource @debate and through the association "votes_for" it tries to create a new vote associated to that debate. Therefore a vote is created when trying to authorise the resource and then another one in the create action, when calling @debate.vote_by (which is called by @debate.register_vote).
This commit is contained in:
43
spec/controllers/debates/votes_controller_spec.rb
Normal file
43
spec/controllers/debates/votes_controller_spec.rb
Normal file
@@ -0,0 +1,43 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Debates::VotesController do
|
||||
describe "POST create" do
|
||||
it "does not authorize unauthenticated users" do
|
||||
debate = create(:debate)
|
||||
|
||||
post :create, xhr: true, params: { debate_id: debate.id, value: "yes" }
|
||||
|
||||
expect(response).to be_unauthorized
|
||||
end
|
||||
|
||||
it "redirects unauthenticated users without JavaScript to the sign in page" do
|
||||
debate = create(:debate)
|
||||
|
||||
post :create, params: { debate_id: debate.id, value: "yes" }
|
||||
|
||||
expect(response).to redirect_to new_user_session_path
|
||||
end
|
||||
|
||||
describe "Vote with too many anonymous votes" do
|
||||
it "allows vote if user is allowed" do
|
||||
Setting["max_ratio_anon_votes_on_debates"] = 100
|
||||
debate = create(:debate)
|
||||
sign_in create(:user)
|
||||
|
||||
expect do
|
||||
post :create, xhr: true, params: { debate_id: debate.id, value: "yes" }
|
||||
end.to change { debate.reload.votes_for.size }.by(1)
|
||||
end
|
||||
|
||||
it "does not allow voting if user is not allowed" do
|
||||
Setting["max_ratio_anon_votes_on_debates"] = 0
|
||||
debate = create(:debate, cached_votes_total: 1000)
|
||||
sign_in create(:user)
|
||||
|
||||
expect do
|
||||
post :create, xhr: true, params: { debate_id: debate.id, value: "yes" }
|
||||
end.not_to change { debate.reload.votes_for.size }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -37,28 +37,6 @@ describe DebatesController do
|
||||
end
|
||||
end
|
||||
|
||||
describe "Vote with too many anonymous votes" do
|
||||
it "allows vote if user is allowed" do
|
||||
Setting["max_ratio_anon_votes_on_debates"] = 100
|
||||
debate = create(:debate)
|
||||
sign_in create(:user)
|
||||
|
||||
expect do
|
||||
post :vote, xhr: true, params: { id: debate.id, value: "yes" }
|
||||
end.to change { debate.reload.votes_for.size }.by(1)
|
||||
end
|
||||
|
||||
it "does not allow vote if user is not allowed" do
|
||||
Setting["max_ratio_anon_votes_on_debates"] = 0
|
||||
debate = create(:debate, cached_votes_total: 1000)
|
||||
sign_in create(:user)
|
||||
|
||||
expect do
|
||||
post :vote, xhr: true, params: { id: debate.id, value: "yes" }
|
||||
end.not_to change { debate.reload.votes_for.size }
|
||||
end
|
||||
end
|
||||
|
||||
describe "PUT mark_featured" do
|
||||
it "ignores query parameters" do
|
||||
debate = create(:debate)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Legislation::Proposals::VotesController do
|
||||
let(:legislation_process) { create(:legislation_process) }
|
||||
let(:proposal) { create(:legislation_proposal, process: legislation_process) }
|
||||
|
||||
describe "POST create" do
|
||||
let(:vote_params) do
|
||||
{ process_id: legislation_process.id, legislation_proposal_id: proposal.id, value: "yes" }
|
||||
end
|
||||
|
||||
it "does not authorize unauthenticated users" do
|
||||
post :create, xhr: true, params: vote_params
|
||||
|
||||
expect(response).to be_unauthorized
|
||||
end
|
||||
|
||||
it "redirects unauthenticated users without JavaScript to the sign in page" do
|
||||
post :create, params: vote_params
|
||||
|
||||
expect(response).to redirect_to new_user_session_path
|
||||
end
|
||||
|
||||
it "allows vote if user is level_two_or_three_verified" do
|
||||
sign_in create(:user, :level_two)
|
||||
|
||||
expect do
|
||||
post :create, xhr: true, params: vote_params
|
||||
end.to change { proposal.reload.votes_for.size }.by(1)
|
||||
end
|
||||
|
||||
it "does not allow voting if user is not level_two_or_three_verified" do
|
||||
sign_in create(:user)
|
||||
|
||||
expect do
|
||||
post :create, xhr: true, params: vote_params
|
||||
end.not_to change { proposal.reload.votes_for.size }
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user