Merge pull request #3011 from chancancode/dont_use_route

Don't `use_route`
This commit is contained in:
Régis Hanol 2014-12-01 11:16:01 +01:00
commit 5f445826c5
1 changed files with 16 additions and 14 deletions

View File

@ -1,6 +1,8 @@
require 'spec_helper' require 'spec_helper'
describe PollPlugin::PollController, type: :controller do describe PollPlugin::PollController, type: :controller do
routes { PollPlugin::Engine.routes }
let(:topic) { create_topic(title: "Poll: Chitoge vs Onodera") } let(:topic) { create_topic(title: "Poll: Chitoge vs Onodera") }
let!(:post) { create_post(topic: topic, raw: "Pick one.\n\n[poll]\n* Chitoge\n* Onodera\n[/poll]") } let!(:post) { create_post(topic: topic, raw: "Pick one.\n\n[poll]\n* Chitoge\n* Onodera\n[/poll]") }
let(:user1) { Fabricate(:user) } let(:user1) { Fabricate(:user) }
@ -9,43 +11,43 @@ describe PollPlugin::PollController, type: :controller do
describe 'vote' do describe 'vote' do
it "returns 403 if no user is logged in" do it "returns 403 if no user is logged in" do
xhr :put, :vote, post_id: post.id, option: "Chitoge", use_route: :poll xhr :put, :vote, post_id: post.id, option: "Chitoge"
response.should be_forbidden response.should be_forbidden
end end
it "returns 400 if post_id or invalid option is not specified" do it "returns 400 if post_id or invalid option is not specified" do
log_in_user user1 log_in_user user1
xhr :put, :vote, use_route: :poll xhr :put, :vote
response.status.should eq(400) response.status.should eq(400)
xhr :put, :vote, post_id: post.id, use_route: :poll xhr :put, :vote, post_id: post.id
response.status.should eq(400) response.status.should eq(400)
xhr :put, :vote, option: "Chitoge", use_route: :poll xhr :put, :vote, option: "Chitoge"
response.status.should eq(400) response.status.should eq(400)
xhr :put, :vote, post_id: post.id, option: "Tsugumi", use_route: :poll xhr :put, :vote, post_id: post.id, option: "Tsugumi"
response.status.should eq(400) response.status.should eq(400)
end end
it "returns 400 if post_id doesn't correspond to a poll post" do it "returns 400 if post_id doesn't correspond to a poll post" do
log_in_user user1 log_in_user user1
post2 = create_post(topic: topic, raw: "Generic reply") post2 = create_post(topic: topic, raw: "Generic reply")
xhr :put, :vote, post_id: post2.id, option: "Chitoge", use_route: :poll xhr :put, :vote, post_id: post2.id, option: "Chitoge"
end end
it "saves votes correctly" do it "saves votes correctly" do
MessageBus.expects(:publish).times(3) MessageBus.expects(:publish).times(3)
log_in_user user1 log_in_user user1
xhr :put, :vote, post_id: post.id, option: "Chitoge", use_route: :poll xhr :put, :vote, post_id: post.id, option: "Chitoge"
PollPlugin::Poll.new(post).get_vote(user1).should eq("Chitoge") PollPlugin::Poll.new(post).get_vote(user1).should eq("Chitoge")
log_in_user user2 log_in_user user2
xhr :put, :vote, post_id: post.id, option: "Onodera", use_route: :poll xhr :put, :vote, post_id: post.id, option: "Onodera"
PollPlugin::Poll.new(post).get_vote(user2).should eq("Onodera") PollPlugin::Poll.new(post).get_vote(user2).should eq("Onodera")
PollPlugin::Poll.new(post).details["Chitoge"].should eq(1) PollPlugin::Poll.new(post).details["Chitoge"].should eq(1)
PollPlugin::Poll.new(post).details["Onodera"].should eq(1) PollPlugin::Poll.new(post).details["Onodera"].should eq(1)
xhr :put, :vote, post_id: post.id, option: "Chitoge", use_route: :poll xhr :put, :vote, post_id: post.id, option: "Chitoge"
PollPlugin::Poll.new(post).get_vote(user2).should eq("Chitoge") PollPlugin::Poll.new(post).get_vote(user2).should eq("Chitoge")
PollPlugin::Poll.new(post).details["Chitoge"].should eq(2) PollPlugin::Poll.new(post).details["Chitoge"].should eq(2)
@ -57,21 +59,21 @@ describe PollPlugin::PollController, type: :controller do
it "returns 400 if post_id doesn't correspond to a poll post" do it "returns 400 if post_id doesn't correspond to a poll post" do
log_in_user admin log_in_user admin
post2 = create_post(topic: topic, raw: "Generic reply") post2 = create_post(topic: topic, raw: "Generic reply")
xhr :put, :toggle_close, post_id: post2.id, use_route: :poll xhr :put, :toggle_close, post_id: post2.id
response.status.should eq(400) response.status.should eq(400)
end end
it "returns 400 if the topic is locked" do it "returns 400 if the topic is locked" do
log_in_user admin log_in_user admin
topic.update_attributes closed: true topic.update_attributes closed: true
xhr :put, :toggle_close, post_id: post.id, use_route: :poll xhr :put, :toggle_close, post_id: post.id
response.status.should eq(400) response.status.should eq(400)
end end
it "raises Discourse::InvalidAccess is the user is not authorized" do it "raises Discourse::InvalidAccess is the user is not authorized" do
log_in_user user1 log_in_user user1
expect do expect do
xhr :put, :toggle_close, post_id: post.id, use_route: :poll xhr :put, :toggle_close, post_id: post.id
end.to raise_error(Discourse::InvalidAccess) end.to raise_error(Discourse::InvalidAccess)
end end
@ -79,10 +81,10 @@ describe PollPlugin::PollController, type: :controller do
I18n.stubs(:t).with('poll.prefix').returns("Poll ") I18n.stubs(:t).with('poll.prefix').returns("Poll ")
I18n.stubs(:t).with('poll.closed_prefix').returns("Closed Poll ") I18n.stubs(:t).with('poll.closed_prefix').returns("Closed Poll ")
log_in_user admin log_in_user admin
xhr :put, :toggle_close, post_id: post.id, use_route: :poll xhr :put, :toggle_close, post_id: post.id
response.status.should eq(200) response.status.should eq(200)
topic.reload.title.should == "Closed Poll : Chitoge vs Onodera" topic.reload.title.should == "Closed Poll : Chitoge vs Onodera"
xhr :put, :toggle_close, post_id: post.id, use_route: :poll xhr :put, :toggle_close, post_id: post.id
response.status.should eq(200) response.status.should eq(200)
topic.reload.title.should == "Poll : Chitoge vs Onodera" topic.reload.title.should == "Poll : Chitoge vs Onodera"
end end