discourse/plugins/poll/spec/controllers/polls_controller_spec.rb
Régis Hanol a737090442 - FEATURE: revamped poll plugin
- add User.staff scope
- inject MessageBus into Ember views (so it can be used by the poll plugin)
- REFACTOR: use more accurate is_first_post? method instead of post_number == 1
- FEATURE: add support for JSON-typed custom fields
- FEATURE: allow plugins to add validation
- FEATURE: add post_custom_fields to PostSerializer
- FEATURE: allow plugins to whitelist post_custom_fields
- FIX: don't bump when post did not save successfully
- FEATURE: polls are supported in any post
- FEATURE: allow for multiple polls in the same post
- FEATURE: multiple choice polls
- FEATURE: rating polls
- FEATURE: new dialect allowing users to preview polls in the composer
2015-04-23 19:33:29 +02:00

99 lines
3.5 KiB
Ruby

require "spec_helper"
describe ::DiscoursePoll::PollsController do
routes { ::DiscoursePoll::Engine.routes }
let!(:user) { log_in }
let(:topic) { Fabricate(:topic) }
let(:poll) { Fabricate(:post, topic_id: topic.id, user_id: user.id, raw: "[poll]\n- A\n- B\n[/poll]") }
describe "#vote" do
it "works" do
MessageBus.expects(:publish)
xhr :put, :vote, { post_id: poll.id, poll_name: "poll", options: ["A"] }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["poll"]["name"]).to eq("poll")
expect(json["poll"]["total_votes"]).to eq(1)
expect(json["vote"]).to eq(["A"])
end
it "supports vote changes" do
xhr :put, :vote, { post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"] }
expect(response).to be_success
xhr :put, :vote, { post_id: poll.id, poll_name: "poll", options: ["e89dec30bbd9bf50fabf6a05b4324edf"] }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["poll"]["total_votes"]).to eq(1)
expect(json["poll"]["options"][0]["votes"]).to eq(0)
expect(json["poll"]["options"][1]["votes"]).to eq(1)
end
it "ensures topic is not closed" do
topic.update_attribute(:closed, true)
xhr :put, :vote, { post_id: poll.id, poll_name: "poll", options: ["A"] }
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.topic_must_be_open_to_vote"))
end
it "ensures topic is not archived" do
topic.update_attribute(:archived, true)
xhr :put, :vote, { post_id: poll.id, poll_name: "poll", options: ["A"] }
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.topic_must_be_open_to_vote"))
end
it "ensures polls are associated with the post" do
xhr :put, :vote, { post_id: Fabricate(:post).id, poll_name: "foobar", options: ["A"] }
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.no_polls_associated_with_this_post"))
end
it "checks the name of the poll" do
xhr :put, :vote, { post_id: poll.id, poll_name: "foobar", options: ["A"] }
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.no_poll_with_this_name", name: "foobar"))
end
it "ensures poll is open" do
closed_poll = Fabricate(:post, raw: "[poll status=closed]\n- A\n- B\n[/poll]")
xhr :put, :vote, { post_id: closed_poll.id, poll_name: "poll", options: ["A"] }
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.poll_must_be_open_to_vote"))
end
end
describe "#toggle_status" do
it "works for OP" do
MessageBus.expects(:publish)
xhr :put, :toggle_status, { post_id: poll.id, poll_name: "poll", status: "closed" }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["poll"]["status"]).to eq("closed")
end
it "works for staff" do
log_in(:moderator)
MessageBus.expects(:publish)
xhr :put, :toggle_status, { post_id: poll.id, poll_name: "poll", status: "closed" }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["poll"]["status"]).to eq("closed")
end
end
end