A platform for community discussion. Free, open, simple.
FIX: must be able to post in a topic in order to vote on a poll
Régis Hanoldee35b72f228
diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rbindex 017ea72a81a..e1434343741 100644--- a/plugins/poll/plugin.rb+++ b/plugins/poll/plugin.rb@@ -47,6 +47,11 @@ after_initialize doraise StandardError.new I18n.t("poll.topic_must_be_open_to_vote")end+ # user must be allowed to post in topic+ unless Guardian.new(user).can_create_post?(post.topic)+ raise StandardError.new I18n.t("poll.user_cant_post_in_topic")+ end+polls = post.custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD]raise StandardError.new I18n.t("poll.no_polls_associated_with_this_post") if polls.blank?diff --git a/plugins/poll/spec/controllers/polls_controller_spec.rb b/plugins/poll/spec/controllers/polls_controller_spec.rbindex c9f353862e9..390d342f13b 100644--- a/plugins/poll/spec/controllers/polls_controller_spec.rb+++ b/plugins/poll/spec/controllers/polls_controller_spec.rb@@ -55,6 +55,7 @@ describe ::DiscoursePoll::PollsController doit "works even if topic is closed" dotopic.update_attribute(:closed, true)+put :vote, params: {post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]}, format: :json@@ -64,6 +65,7 @@ describe ::DiscoursePoll::PollsController doit "ensures topic is not archived" dotopic.update_attribute(:archived, true)+put :vote, params: {post_id: poll.id, poll_name: "poll", options: ["A"]}, format: :json@@ -75,6 +77,7 @@ describe ::DiscoursePoll::PollsController doit "ensures post is not trashed" dopoll.trash!+put :vote, params: {post_id: poll.id, poll_name: "poll", options: ["A"]}, format: :json@@ -84,6 +87,18 @@ describe ::DiscoursePoll::PollsController doexpect(json["errors"][0]).to eq(I18n.t("poll.post_is_deleted"))end+ it "ensures user can post in topic" do+ Guardian.any_instance.expects(:can_create_post?).returns(false)++ put :vote, params: {+ post_id: poll.id, poll_name: "poll", options: ["A"]+ }, format: :json++ expect(response).not_to be_success+ json = ::JSON.parse(response.body)+ expect(json["errors"][0]).to eq(I18n.t("poll.user_cant_post_in_topic"))+ end+it "ensures polls are associated with the post" doput :vote, params: {post_id: Fabricate(:post).id, poll_name: "foobar", options: ["A"]