2014-02-04 21:53:09 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe PollPlugin::PollController, type: :controller do
|
|
|
|
let(:topic) { create_topic(title: "Poll: Chitoge vs Onodera") }
|
2014-04-12 13:23:21 -04:00
|
|
|
let!(:post) { create_post(topic: topic, raw: "Pick one.\n\n[poll]\n* Chitoge\n* Onodera\n[/poll]") }
|
2014-02-04 21:53:09 -05:00
|
|
|
let(:user1) { Fabricate(:user) }
|
|
|
|
let(:user2) { Fabricate(:user) }
|
2014-04-12 13:23:21 -04:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2014-04-12 13:23:21 -04:00
|
|
|
describe 'vote' do
|
|
|
|
it "returns 403 if no user is logged in" do
|
|
|
|
xhr :put, :vote, post_id: post.id, option: "Chitoge", use_route: :poll
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2014-04-12 13:23:21 -04:00
|
|
|
it "returns 400 if post_id or invalid option is not specified" do
|
|
|
|
log_in_user user1
|
|
|
|
xhr :put, :vote, use_route: :poll
|
|
|
|
response.status.should eq(400)
|
|
|
|
xhr :put, :vote, post_id: post.id, use_route: :poll
|
|
|
|
response.status.should eq(400)
|
|
|
|
xhr :put, :vote, option: "Chitoge", use_route: :poll
|
|
|
|
response.status.should eq(400)
|
|
|
|
xhr :put, :vote, post_id: post.id, option: "Tsugumi", use_route: :poll
|
|
|
|
response.status.should eq(400)
|
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2014-04-12 13:23:21 -04:00
|
|
|
it "returns 400 if post_id doesn't correspond to a poll post" do
|
|
|
|
log_in_user user1
|
|
|
|
post2 = create_post(topic: topic, raw: "Generic reply")
|
|
|
|
xhr :put, :vote, post_id: post2.id, option: "Chitoge", use_route: :poll
|
|
|
|
end
|
|
|
|
|
|
|
|
it "saves votes correctly" do
|
|
|
|
MessageBus.expects(:publish).times(3)
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2014-04-12 13:23:21 -04:00
|
|
|
log_in_user user1
|
|
|
|
xhr :put, :vote, post_id: post.id, option: "Chitoge", use_route: :poll
|
|
|
|
PollPlugin::Poll.new(post).get_vote(user1).should eq("Chitoge")
|
2014-03-24 00:05:39 -04:00
|
|
|
|
2014-04-12 13:23:21 -04:00
|
|
|
log_in_user user2
|
|
|
|
xhr :put, :vote, post_id: post.id, option: "Onodera", use_route: :poll
|
|
|
|
PollPlugin::Poll.new(post).get_vote(user2).should eq("Onodera")
|
|
|
|
|
|
|
|
PollPlugin::Poll.new(post).details["Chitoge"].should eq(1)
|
|
|
|
PollPlugin::Poll.new(post).details["Onodera"].should eq(1)
|
|
|
|
|
|
|
|
xhr :put, :vote, post_id: post.id, option: "Chitoge", use_route: :poll
|
|
|
|
PollPlugin::Poll.new(post).get_vote(user2).should eq("Chitoge")
|
|
|
|
|
|
|
|
PollPlugin::Poll.new(post).details["Chitoge"].should eq(2)
|
|
|
|
PollPlugin::Poll.new(post).details["Onodera"].should eq(0)
|
|
|
|
end
|
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2014-04-12 13:23:21 -04:00
|
|
|
describe 'toggle_close' do
|
|
|
|
it "returns 400 if post_id doesn't correspond to a poll post" do
|
|
|
|
log_in_user admin
|
|
|
|
post2 = create_post(topic: topic, raw: "Generic reply")
|
|
|
|
xhr :put, :toggle_close, post_id: post2.id, use_route: :poll
|
|
|
|
response.status.should eq(400)
|
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2014-04-12 13:23:21 -04:00
|
|
|
it "returns 400 if the topic is locked" do
|
|
|
|
log_in_user admin
|
|
|
|
topic.update_attributes closed: true
|
|
|
|
xhr :put, :toggle_close, post_id: post.id, use_route: :poll
|
|
|
|
response.status.should eq(400)
|
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2014-04-12 13:23:21 -04:00
|
|
|
it "raises Discourse::InvalidAccess is the user is not authorized" do
|
|
|
|
log_in_user user1
|
|
|
|
expect do
|
|
|
|
xhr :put, :toggle_close, post_id: post.id, use_route: :poll
|
|
|
|
end.to raise_error(Discourse::InvalidAccess)
|
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2014-04-12 13:23:21 -04:00
|
|
|
it "renames the topic" do
|
|
|
|
I18n.stubs(:t).with('poll.prefix').returns("Poll ")
|
|
|
|
I18n.stubs(:t).with('poll.closed_prefix').returns("Closed Poll ")
|
|
|
|
log_in_user admin
|
|
|
|
xhr :put, :toggle_close, post_id: post.id, use_route: :poll
|
|
|
|
response.status.should eq(200)
|
|
|
|
topic.reload.title.should == "Closed Poll : Chitoge vs Onodera"
|
|
|
|
xhr :put, :toggle_close, post_id: post.id, use_route: :poll
|
|
|
|
response.status.should eq(200)
|
|
|
|
topic.reload.title.should == "Poll : Chitoge vs Onodera"
|
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
end
|
|
|
|
end
|