2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-05 08:59:27 +08:00

Fix all the errors to get our tests green on Rails 5.1.

This commit is contained in:
Guo Xiang Tan 2017-08-31 12:06:56 +08:00
parent 898ee93547
commit 77d4c4d8dc
989 changed files with 5114 additions and 3117 deletions

View file

@ -1,4 +1,4 @@
class RenameTotalVotesToVoters < ActiveRecord::Migration
class RenameTotalVotesToVoters < ActiveRecord::Migration[4.2]
def up
PostCustomField.where(name: "polls").find_each do |pcf|

View file

@ -1,4 +1,4 @@
class MergePollsVotes < ActiveRecord::Migration
class MergePollsVotes < ActiveRecord::Migration[4.2]
def up
PostCustomField.where(name: "polls").order(:post_id).pluck(:post_id).each do |post_id|

View file

@ -1,4 +1,4 @@
class ClosePollsInClosedTopics < ActiveRecord::Migration
class ClosePollsInClosedTopics < ActiveRecord::Migration[4.2]
def up
PostCustomField.joins(post: :topic)

View file

@ -181,7 +181,7 @@ after_initialize do
class DiscoursePoll::PollsController < ::ApplicationController
requires_plugin PLUGIN_NAME
before_filter :ensure_logged_in, except: [:voters]
before_action :ensure_logged_in, except: [:voters]
def vote
post_id = params.require(:post_id)

View file

@ -14,7 +14,9 @@ describe ::DiscoursePoll::PollsController do
it "works" do
MessageBus.expects(:publish)
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
put :vote, params: {
post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
@ -24,7 +26,9 @@ describe ::DiscoursePoll::PollsController do
end
it "requires at least 1 valid option" do
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["A", "B"]
put :vote, params: {
post_id: poll.id, poll_name: "poll", options: ["A", "B"]
}, format: :json
expect(response).not_to be_success
json = ::JSON.parse(response.body)
@ -32,10 +36,16 @@ describe ::DiscoursePoll::PollsController do
end
it "supports vote changes" do
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
put :vote, params: {
post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
}, format: :json
expect(response).to be_success
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["e89dec30bbd9bf50fabf6a05b4324edf"]
put :vote, params: {
post_id: poll.id, poll_name: "poll", options: ["e89dec30bbd9bf50fabf6a05b4324edf"]
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["poll"]["voters"]).to eq(1)
@ -45,13 +55,19 @@ describe ::DiscoursePoll::PollsController do
it "works even if topic is closed" do
topic.update_attribute(:closed, true)
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
put :vote, params: {
post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
}, format: :json
expect(response).to be_success
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"]
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.topic_must_be_open_to_vote"))
@ -59,21 +75,30 @@ describe ::DiscoursePoll::PollsController do
it "ensures post is not trashed" do
poll.trash!
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["A"]
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.post_is_deleted"))
end
it "ensures polls are associated with the post" do
xhr :put, :vote, post_id: Fabricate(:post).id, poll_name: "foobar", options: ["A"]
put :vote, params: {
post_id: Fabricate(:post).id, poll_name: "foobar", options: ["A"]
}, format: :json
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"]
put :vote, params: {
post_id: poll.id, poll_name: "foobar", options: ["A"]
}, format: :json
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"))
@ -81,7 +106,11 @@ describe ::DiscoursePoll::PollsController do
it "ensures poll is open" do
closed_poll = create_post(raw: "[poll status=closed]\n- A\n- B\n[/poll]")
xhr :put, :vote, post_id: closed_poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
put :vote, params: {
post_id: closed_poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
}, format: :json
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"))
@ -91,7 +120,10 @@ describe ::DiscoursePoll::PollsController do
default_poll = poll.custom_fields["polls"]["poll"]
add_anonymous_votes(poll, default_poll, 17, "5c24fc1df56d764b550ceae1b9319125" => 11, "e89dec30bbd9bf50fabf6a05b4324edf" => 6)
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
put :vote, params: {
post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
@ -105,7 +137,9 @@ describe ::DiscoursePoll::PollsController do
body = { post_id: public_poll.id, poll_name: "poll" }
message = MessageBus.track_publish do
xhr :put, :vote, body.merge(options: ["5c24fc1df56d764b550ceae1b9319125"])
put :vote,
params: body.merge(options: ["5c24fc1df56d764b550ceae1b9319125"]),
format: :json
end.first
expect(response).to be_success
@ -119,7 +153,10 @@ describe ::DiscoursePoll::PollsController do
expect(message.data[:post_id].to_i).to eq(public_poll.id)
expect(message.data[:user][:id].to_i).to eq(user.id)
xhr :put, :vote, body.merge(options: ["e89dec30bbd9bf50fabf6a05b4324edf"])
put :vote,
params: body.merge(options: ["e89dec30bbd9bf50fabf6a05b4324edf"]),
format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
@ -132,7 +169,10 @@ describe ::DiscoursePoll::PollsController do
another_user = Fabricate(:user)
log_in_user(another_user)
xhr :put, :vote, body.merge(options: ["e89dec30bbd9bf50fabf6a05b4324edf", "5c24fc1df56d764b550ceae1b9319125"])
put :vote,
params: body.merge(options: ["e89dec30bbd9bf50fabf6a05b4324edf", "5c24fc1df56d764b550ceae1b9319125"]),
format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
@ -149,7 +189,10 @@ describe ::DiscoursePoll::PollsController do
it "works for OP" do
MessageBus.expects(:publish)
xhr :put, :toggle_status, post_id: poll.id, poll_name: "poll", status: "closed"
put :toggle_status, params: {
post_id: poll.id, poll_name: "poll", status: "closed"
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["poll"]["status"]).to eq("closed")
@ -159,7 +202,10 @@ describe ::DiscoursePoll::PollsController do
log_in(:moderator)
MessageBus.expects(:publish)
xhr :put, :toggle_status, post_id: poll.id, poll_name: "poll", status: "closed"
put :toggle_status, params: {
post_id: poll.id, poll_name: "poll", status: "closed"
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["poll"]["status"]).to eq("closed")
@ -167,7 +213,11 @@ describe ::DiscoursePoll::PollsController do
it "ensures post is not trashed" do
poll.trash!
xhr :put, :toggle_status, post_id: poll.id, poll_name: "poll", status: "closed"
put :toggle_status, params: {
post_id: poll.id, poll_name: "poll", status: "closed"
}, format: :json
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.post_is_deleted"))
@ -183,18 +233,37 @@ describe ::DiscoursePoll::PollsController do
second = "e89dec30bbd9bf50fabf6a05b4324edf"
user1 = log_in
xhr :put, :vote, post_id: multi_poll.id, poll_name: "poll", options: [first]
put :vote, params: {
post_id: multi_poll.id, poll_name: "poll", options: [first]
}, format: :json
expect(response).to be_success
user2 = log_in
xhr :put, :vote, post_id: multi_poll.id, poll_name: "poll", options: [first]
put :vote, params: {
post_id: multi_poll.id, poll_name: "poll", options: [first]
}, format: :json
expect(response).to be_success
user3 = log_in
xhr :put, :vote,
put :vote, params: {
post_id: multi_poll.id,
poll_name: "poll",
options: [first, second]
}, format: :json
expect(response).to be_success
get :voters, params: {
poll_name: 'poll', post_id: multi_poll.id, voter_limit: 2
}, format: :json
expect(response).to be_success
xhr :get, :voters, poll_name: 'poll', post_id: multi_poll.id, voter_limit: 2
json = JSON.parse(response.body)
# no user3 cause voter_limit is 2

View file

@ -12,7 +12,10 @@ describe PostsController do
describe "polls" do
it "works" do
xhr :post, :create, title: title, raw: "[poll]\n- A\n- B\n[/poll]"
post :create, params: {
title: title, raw: "[poll]\n- A\n- B\n[/poll]"
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
@ -20,8 +23,12 @@ describe PostsController do
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]"
post_1 = Fabricate(:post)
post :create, params: {
topic_id: post_1.topic.id, raw: "[poll]\n- A\n- B\n[/poll]"
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
@ -29,14 +36,20 @@ describe PostsController do
end
it "should have different options" do
xhr :post, :create, title: title, raw: "[poll]\n- A\n- A\n[/poll]"
post :create, params: {
title: title, raw: "[poll]\n- A\n- A\n[/poll]"
}, format: :json
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\n[/poll]"
post :create, params: {
title: title, raw: "[poll]\n- A\n[/poll]"
}, format: :json
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"))
@ -47,7 +60,9 @@ describe PostsController do
(SiteSetting.poll_maximum_options + 1).times { |n| raw << "\n- #{n}" }
raw << "\n[/poll]"
xhr :post, :create, title: title, raw: raw
post :create, params: {
title: title, raw: raw
}, format: :json
expect(response).not_to be_success
json = ::JSON.parse(response.body)
@ -55,14 +70,20 @@ describe PostsController do
end
it "should have valid parameters" do
xhr :post, :create, title: title, raw: "[poll type=multiple min=5]\n- A\n- B\n[/poll]"
post :create, params: {
title: title, raw: "[poll type=multiple min=5]\n- A\n- B\n[/poll]"
}, format: :json
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters"))
end
it "prevents self-xss" do
xhr :post, :create, title: title, raw: "[poll name=<script>alert('xss')</script>]\n- A\n- B\n[/poll]"
post :create, params: {
title: title, raw: "[poll name=<script>alert('xss')</script>]\n- A\n- B\n[/poll]"
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
@ -71,7 +92,10 @@ describe PostsController do
end
it "also works whe there is a link starting with '[poll'" do
xhr :post, :create, title: title, raw: "[Polls are awesome](/foobar)\n[poll]\n- A\n- B\n[/poll]"
post :create, params: {
title: title, raw: "[Polls are awesome](/foobar)\n[poll]\n- A\n- B\n[/poll]"
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
@ -79,7 +103,10 @@ describe PostsController do
end
it "prevents pollception" do
xhr :post, :create, title: title, raw: "[poll name=1]\n- A\n[poll name=2]\n- B\n- C\n[/poll]\n- D\n[/poll]"
post :create, params: {
title: title, raw: "[poll name=1]\n- A\n[poll name=2]\n- B\n- C\n[/poll]\n- D\n[/poll]"
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
@ -93,13 +120,19 @@ describe PostsController do
let(:post_id) do
freeze_time(4.minutes.ago) do
xhr :post, :create, title: title, raw: "[poll]\n- A\n- B\n[/poll]"
post :create, params: {
title: title, raw: "[poll]\n- A\n- B\n[/poll]"
}, format: :json
::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]" }
put :update, params: {
id: post_id, post: { raw: "[poll]\n- A\n- B\n- C\n[/poll]" }
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["polls"]["poll"]["options"][2]["html"]).to eq("C")
@ -107,7 +140,11 @@ describe PostsController do
it "resets the votes" do
DiscoursePoll::Poll.vote(post_id, "poll", ["5c24fc1df56d764b550ceae1b9319125"], user)
xhr :put, :update, id: post_id, post: { raw: "[poll]\n- A\n- B\n- C\n[/poll]" }
put :update, params: {
id: post_id, post: { raw: "[poll]\n- A\n- B\n- C\n[/poll]" }
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["polls_votes"]).to_not be
@ -123,7 +160,10 @@ describe PostsController do
let(:post_id) do
freeze_time(6.minutes.ago) do
xhr :post, :create, title: title, raw: poll
post :create, params: {
title: title, raw: poll
}, format: :json
::JSON.parse(response.body)["id"]
end
end
@ -137,7 +177,10 @@ describe PostsController do
describe "with no vote" do
it "OP can change the options" do
xhr :put, :update, id: post_id, post: { raw: new_option }
put :update, params: {
id: post_id, post: { raw: new_option }
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
@ -145,14 +188,18 @@ describe PostsController do
it "staff can change the options" do
log_in_user(Fabricate(:moderator))
xhr :put, :update, id: post_id, post: { raw: new_option }
put :update, params: {
id: post_id, post: { raw: new_option }
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
end
it "support changes on the post" do
xhr :put, :update, id: post_id, post: { raw: updated }
put :update, params: { id: post_id, post: { raw: updated } }, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["cooked"]).to match("before")
@ -167,7 +214,10 @@ describe PostsController do
end
it "OP cannot change the options" do
xhr :put, :update, id: post_id, post: { raw: new_option }
put :update, params: {
id: post_id, post: { raw: new_option }
}, format: :json
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t(
@ -178,7 +228,11 @@ describe PostsController do
it "staff can change the options and votes are merged" do
log_in_user(Fabricate(:moderator))
xhr :put, :update, id: post_id, post: { raw: new_option }
put :update, params: {
id: post_id, post: { raw: new_option }
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
@ -193,7 +247,11 @@ describe PostsController do
add_anonymous_votes(post, default_poll, 7, "5c24fc1df56d764b550ceae1b9319125" => 7)
log_in_user(Fabricate(:moderator))
xhr :put, :update, id: post_id, post: { raw: new_option }
put :update, params: {
id: post_id, post: { raw: new_option }
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
@ -204,7 +262,7 @@ describe PostsController do
end
it "support changes on the post" do
xhr :put, :update, id: post_id, post: { raw: updated }
put :update, params: { id: post_id, post: { raw: updated } }, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["cooked"]).to match("before")
@ -221,14 +279,20 @@ describe PostsController do
describe "named polls" do
it "should have different options" do
xhr :post, :create, title: title, raw: "[poll name=""foo""]\n- A\n- A\n[/poll]"
post :create, params: {
title: title, raw: "[poll name=""foo""]\n- A\n- A\n[/poll]"
}, format: :json
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\n[/poll]"
post :create, params: {
title: title, raw: "[poll name='foo']\n- A\n[/poll]"
}, format: :json
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"))
@ -239,7 +303,10 @@ describe PostsController do
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]"
post :create, params: {
title: title, raw: "[poll]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]"
}, format: :json
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
@ -248,14 +315,20 @@ describe PostsController do
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]"
post :create, params: {
title: title, raw: "[poll]\n- A\n- B\n[/poll]\n[poll]\n- A\n- B\n[/poll]"
}, format: :json
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]"
post :create, params: {
title: title, raw: "[poll name=foo]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]"
}, format: :json
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"))

View file

@ -13,8 +13,10 @@ describe "DiscoursePoll endpoints" do
user
)
get "/polls/voters.json", post_id: post.id,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME
get "/polls/voters.json", params: {
post_id: post.id,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME
}
expect(response.status).to eq(200)
@ -34,9 +36,11 @@ describe "DiscoursePoll endpoints" do
user
)
get "/polls/voters.json", post_id: post.id,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
option_id: 'e89dec30bbd9bf50fabf6a05b4324edf'
get "/polls/voters.json", params: {
post_id: post.id,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
option_id: 'e89dec30bbd9bf50fabf6a05b4324edf'
}
expect(response.status).to eq(200)
@ -53,7 +57,7 @@ describe "DiscoursePoll endpoints" do
describe 'when post_id is blank' do
it 'should raise the right error' do
expect { get "/polls/voters.json", poll_name: DiscoursePoll::DEFAULT_POLL_NAME }
expect { get "/polls/voters.json", params: { poll_name: DiscoursePoll::DEFAULT_POLL_NAME } }
.to raise_error(ActionController::ParameterMissing)
end
end
@ -61,15 +65,17 @@ describe "DiscoursePoll endpoints" do
describe 'when post_id is not valid' do
it 'should raise the right error' do
expect do
get "/polls/voters.json", post_id: -1,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME
get "/polls/voters.json", params: {
post_id: -1,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME
}
end.to raise_error(Discourse::InvalidParameters, 'post_id is invalid')
end
end
describe 'when poll_name is blank' do
it 'should raise the right error' do
expect { get "/polls/voters.json", post_id: post.id }
expect { get "/polls/voters.json", params: { post_id: post.id } }
.to raise_error(ActionController::ParameterMissing)
end
end
@ -77,7 +83,7 @@ describe "DiscoursePoll endpoints" do
describe 'when poll_name is not valid' do
it 'should raise the right error' do
expect do
get "/polls/voters.json", post_id: post.id, poll_name: 'wrongpoll'
get "/polls/voters.json", params: { post_id: post.id, poll_name: 'wrongpoll' }
end.to raise_error(Discourse::InvalidParameters, 'poll_name is invalid')
end
end
@ -95,8 +101,10 @@ describe "DiscoursePoll endpoints" do
user
)
get "/polls/voters.json", post_id: post.id,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME
get "/polls/voters.json", params: {
post_id: post.id,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME
}
expect(response.status).to eq(200)