discourse/plugins/poll/spec/controllers/posts_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

137 lines
4.6 KiB
Ruby

require "spec_helper"
describe PostsController do
let!(:user) { log_in }
let!(:title) { "Testing Poll Plugin" }
describe "polls" do
it "works" do
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
expect(json["polls"]["poll"]).to be
end
it "works on any post" do
post = Fabricate(:post)
xhr :post, :create, { topic_id: post.topic.id, raw: "[poll]\n- A\n- B\n[/poll]" }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
expect(json["polls"]["poll"]).to be
end
it "should have different options" do
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- A[/poll]" }
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_different_options"))
end
it "should have at least 2 options" do
xhr :post, :create, { title: title, raw: "[poll]\n- A[/poll]" }
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_at_least_2_options"))
end
describe "edit window" do
describe "within the first 5 minutes" do
let(:post_id) do
Timecop.freeze(3.minutes.ago) do
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }
::JSON.parse(response.body)["id"]
end
end
it "can be changed" do
xhr :put, :update, { id: post_id, post: { raw: "[poll]\n- A\n- B\n- C\n[/poll]" } }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["polls"]["poll"]["options"][2]["html"]).to eq("C")
end
end
describe "after the first 5 minutes" do
let(:post_id) do
Timecop.freeze(6.minutes.ago) do
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }
::JSON.parse(response.body)["id"]
end
end
let(:new_raw) { "[poll]\n- A\n- C[/poll]" }
it "cannot be changed by OP" do
xhr :put, :update, { id: post_id, post: { raw: new_raw } }
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.cannot_change_polls_after_5_minutes"))
end
it "can be edited by staff" do
log_in_user(Fabricate(:moderator))
xhr :put, :update, { id: post_id, post: { raw: new_raw } }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
end
end
end
end
describe "named polls" do
it "should have different options" do
xhr :post, :create, { title: title, raw: "[poll name=foo]\n- A\n- A[/poll]" }
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.named_poll_must_have_different_options", name: "foo"))
end
it "should have at least 2 options" do
xhr :post, :create, { title: title, raw: "[poll name=foo]\n- A[/poll]" }
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.named_poll_must_have_at_least_2_options", name: "foo"))
end
end
describe "multiple polls" do
it "works" do
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]" }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
expect(json["polls"]["poll"]).to be
expect(json["polls"]["foo"]).to be
end
it "should have a name" do
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]\n[poll]\n- A\n- B\n[/poll]" }
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.multiple_polls_without_name"))
end
it "should have unique name" do
xhr :post, :create, { title: title, raw: "[poll name=foo]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]" }
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.multiple_polls_with_same_name", name: "foo"))
end
end
end