2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-09 21:33:17 +08:00
|
|
|
require "rails_helper"
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe "DiscoursePoll endpoints" do
|
2017-01-27 17:09:33 +08:00
|
|
|
describe "fetch voters for a poll" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
2021-11-19 09:50:08 +08:00
|
|
|
fab!(:post) { Fabricate(:post, raw: "[poll public=true]\n- A\n- B\n[/poll]") }
|
|
|
|
|
|
|
|
fab!(:post_with_multiple_poll) { Fabricate(:post, raw: <<~SQL) }
|
|
|
|
[poll type=multiple public=true min=1 max=2]
|
|
|
|
- A
|
|
|
|
- B
|
|
|
|
- C
|
|
|
|
[/poll]
|
|
|
|
SQL
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
let(:option_a) { "5c24fc1df56d764b550ceae1b9319125" }
|
|
|
|
let(:option_b) { "e89dec30bbd9bf50fabf6a05b4324edf" }
|
2016-06-09 21:33:17 +08:00
|
|
|
|
|
|
|
it "should return the right response" do
|
2017-01-27 17:09:33 +08:00
|
|
|
DiscoursePoll::Poll.vote(user, post.id, DiscoursePoll::DEFAULT_POLL_NAME, [option_a])
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get "/polls/voters.json",
|
|
|
|
params: {
|
|
|
|
post_id: post.id,
|
|
|
|
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
|
|
|
|
}
|
2016-06-09 21:33:17 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
poll = response.parsed_body["voters"]
|
2018-11-19 21:50:00 +08:00
|
|
|
option = poll[option_a]
|
2017-01-27 17:09:33 +08:00
|
|
|
|
|
|
|
expect(option.length).to eq(1)
|
|
|
|
expect(option.first["id"]).to eq(user.id)
|
|
|
|
expect(option.first["username"]).to eq(user.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the right response for a single option" do
|
|
|
|
DiscoursePoll::Poll.vote(
|
2021-10-05 16:38:49 +08:00
|
|
|
user,
|
2021-11-19 09:50:08 +08:00
|
|
|
post_with_multiple_poll.id,
|
2017-01-27 17:09:33 +08:00
|
|
|
DiscoursePoll::DEFAULT_POLL_NAME,
|
2021-10-05 16:38:49 +08:00
|
|
|
[option_a, option_b],
|
2017-01-27 17:09:33 +08:00
|
|
|
)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get "/polls/voters.json",
|
|
|
|
params: {
|
2021-11-19 09:50:08 +08:00
|
|
|
post_id: post_with_multiple_poll.id,
|
2017-08-31 12:06:56 +08:00
|
|
|
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
|
2018-11-19 21:50:00 +08:00
|
|
|
option_id: option_b,
|
2017-08-31 12:06:56 +08:00
|
|
|
}
|
2017-01-27 17:09:33 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
poll = response.parsed_body["voters"]
|
2017-01-27 17:09:33 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
expect(poll[option_a]).to eq(nil)
|
2017-01-27 17:09:33 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
option = poll[option_b]
|
2017-01-27 17:09:33 +08:00
|
|
|
|
|
|
|
expect(option.length).to eq(1)
|
|
|
|
expect(option.first["id"]).to eq(user.id)
|
|
|
|
expect(option.first["username"]).to eq(user.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when post_id is blank" do
|
|
|
|
it "should raise the right error" do
|
2018-01-12 11:42:05 +08:00
|
|
|
get "/polls/voters.json", params: { poll_name: DiscoursePoll::DEFAULT_POLL_NAME }
|
|
|
|
expect(response.status).to eq(400)
|
2017-01-27 17:09:33 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when post_id is not valid" do
|
|
|
|
it "should raise the right error" do
|
2018-01-12 11:42:05 +08:00
|
|
|
get "/polls/voters.json",
|
|
|
|
params: {
|
|
|
|
post_id: -1,
|
|
|
|
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
|
|
|
|
}
|
2021-10-05 16:38:49 +08:00
|
|
|
expect(response.status).to eq(400)
|
2019-11-26 01:51:01 +08:00
|
|
|
expect(response.body).to include("post_id")
|
2017-01-27 17:09:33 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when poll_name is blank" do
|
|
|
|
it "should raise the right error" do
|
2018-01-12 11:42:05 +08:00
|
|
|
get "/polls/voters.json", params: { post_id: post.id }
|
|
|
|
expect(response.status).to eq(400)
|
2017-01-27 17:09:33 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when poll_name is not valid" do
|
|
|
|
it "should raise the right error" do
|
2018-01-12 11:42:05 +08:00
|
|
|
get "/polls/voters.json", params: { post_id: post.id, poll_name: "wrongpoll" }
|
2021-10-05 16:38:49 +08:00
|
|
|
expect(response.status).to eq(400)
|
2019-11-26 01:51:01 +08:00
|
|
|
expect(response.body).to include("poll_name")
|
2017-01-27 17:09:33 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with number poll" do
|
2017-07-19 02:31:22 +08:00
|
|
|
let(:post) do
|
|
|
|
Fabricate(:post, raw: "[poll type=number min=1 max=20 step=1 public=true]\n[/poll]")
|
2023-01-07 04:42:16 +08:00
|
|
|
end
|
2017-01-27 17:09:33 +08:00
|
|
|
|
|
|
|
it "should return the right response" do
|
|
|
|
post
|
|
|
|
|
|
|
|
DiscoursePoll::Poll.vote(
|
2021-10-05 16:38:49 +08:00
|
|
|
user,
|
2017-01-27 17:09:33 +08:00
|
|
|
post.id,
|
|
|
|
DiscoursePoll::DEFAULT_POLL_NAME,
|
2021-10-05 16:38:49 +08:00
|
|
|
["4d8a15e3cc35750f016ce15a43937620"],
|
2017-01-27 17:09:33 +08:00
|
|
|
)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get "/polls/voters.json",
|
|
|
|
params: {
|
|
|
|
post_id: post.id,
|
|
|
|
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
|
|
|
|
}
|
2017-01-27 17:09:33 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
poll = response.parsed_body["voters"]
|
2016-06-09 21:33:17 +08:00
|
|
|
|
2017-01-27 17:09:33 +08:00
|
|
|
expect(poll.first["id"]).to eq(user.id)
|
|
|
|
expect(poll.first["username"]).to eq(user.username)
|
|
|
|
end
|
2016-06-09 21:33:17 +08:00
|
|
|
end
|
|
|
|
end
|
2019-11-26 01:51:01 +08:00
|
|
|
|
|
|
|
describe "#grouped_poll_results" do
|
|
|
|
fab!(:user1) { Fabricate(:user) }
|
|
|
|
fab!(:user2) { Fabricate(:user) }
|
|
|
|
fab!(:user3) { Fabricate(:user) }
|
|
|
|
fab!(:user4) { Fabricate(:user) }
|
2021-11-19 09:50:08 +08:00
|
|
|
|
|
|
|
fab!(:post) { Fabricate(:post, raw: <<~SQL) }
|
|
|
|
[poll type=multiple public=true min=1 max=2]
|
|
|
|
- A
|
|
|
|
- B
|
|
|
|
[/poll]
|
|
|
|
SQL
|
|
|
|
|
2019-11-26 01:51:01 +08:00
|
|
|
let(:option_a) { "5c24fc1df56d764b550ceae1b9319125" }
|
|
|
|
let(:option_b) { "e89dec30bbd9bf50fabf6a05b4324edf" }
|
|
|
|
|
|
|
|
before do
|
2023-10-01 23:10:35 +08:00
|
|
|
sign_in(user1)
|
2019-11-26 01:51:01 +08:00
|
|
|
user_votes = { user_0: option_a, user_1: option_a, user_2: option_b }
|
2021-11-19 09:50:08 +08:00
|
|
|
|
2019-11-26 01:51:01 +08:00
|
|
|
[user1, user2, user3].each_with_index do |user, index|
|
|
|
|
DiscoursePoll::Poll.vote(
|
2021-10-05 16:38:49 +08:00
|
|
|
user,
|
2019-11-26 01:51:01 +08:00
|
|
|
post.id,
|
|
|
|
DiscoursePoll::DEFAULT_POLL_NAME,
|
2021-10-05 16:38:49 +08:00
|
|
|
[user_votes["user_#{index}".to_sym]],
|
2019-11-26 01:51:01 +08:00
|
|
|
)
|
|
|
|
UserCustomField.create(user_id: user.id, name: "something", value: "value#{index}")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add another user to one of the fields to prove it groups users properly
|
|
|
|
DiscoursePoll::Poll.vote(
|
2021-10-05 16:38:49 +08:00
|
|
|
user4,
|
2019-11-26 01:51:01 +08:00
|
|
|
post.id,
|
|
|
|
DiscoursePoll::DEFAULT_POLL_NAME,
|
2021-10-05 16:38:49 +08:00
|
|
|
[option_a, option_b],
|
2019-11-26 01:51:01 +08:00
|
|
|
)
|
|
|
|
UserCustomField.create(user_id: user4.id, name: "something", value: "value1")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns grouped poll results based on user field" do
|
|
|
|
SiteSetting.poll_groupable_user_fields = "something"
|
|
|
|
|
|
|
|
get "/polls/grouped_poll_results.json",
|
|
|
|
params: {
|
|
|
|
post_id: post.id,
|
|
|
|
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
|
|
|
|
user_field_name: "something",
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
expect(response.parsed_body.deep_symbolize_keys).to eq(
|
2019-11-26 01:51:01 +08:00
|
|
|
grouped_results: [
|
|
|
|
{
|
|
|
|
group: "Value0",
|
|
|
|
options: [
|
|
|
|
{ digest: option_a, html: "A", votes: 1 },
|
|
|
|
{ digest: option_b, html: "B", votes: 0 },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
group: "Value1",
|
|
|
|
options: [
|
|
|
|
{ digest: option_a, html: "A", votes: 2 },
|
|
|
|
{ digest: option_b, html: "B", votes: 1 },
|
2023-01-07 04:42:16 +08:00
|
|
|
],
|
2019-11-26 01:51:01 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
group: "Value2",
|
|
|
|
options: [
|
|
|
|
{ digest: option_a, html: "A", votes: 0 },
|
|
|
|
{ digest: option_b, html: "B", votes: 1 },
|
|
|
|
],
|
2023-01-07 04:42:16 +08:00
|
|
|
},
|
|
|
|
],
|
2019-11-26 01:51:01 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an error when poll_groupable_user_fields is empty" do
|
|
|
|
SiteSetting.poll_groupable_user_fields = ""
|
|
|
|
get "/polls/grouped_poll_results.json",
|
|
|
|
params: {
|
|
|
|
post_id: post.id,
|
|
|
|
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
|
|
|
|
user_field_name: "something",
|
|
|
|
}
|
|
|
|
|
2021-10-05 16:38:49 +08:00
|
|
|
expect(response.status).to eq(400)
|
2019-11-26 01:51:01 +08:00
|
|
|
expect(response.body).to include("user_field_name")
|
|
|
|
end
|
2023-10-01 23:10:35 +08:00
|
|
|
|
|
|
|
context "when topic is in a private category" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:admin)
|
|
|
|
fab!(:group)
|
2023-10-01 23:10:35 +08:00
|
|
|
fab!(:private_category) { Fabricate(:private_category, group: group) }
|
|
|
|
fab!(:private_topic) { Fabricate(:topic, category: private_category) }
|
|
|
|
fab!(:private_post) { Fabricate(:post, topic: private_topic, raw: <<~SQL) }
|
|
|
|
[poll type=multiple public=true min=1 max=2]
|
|
|
|
- A
|
|
|
|
- B
|
|
|
|
[/poll]
|
|
|
|
SQL
|
|
|
|
let(:groupable_user_field) { "anything" }
|
|
|
|
let(:expected_results) do
|
|
|
|
{
|
|
|
|
grouped_results: [
|
|
|
|
{
|
|
|
|
group: "Value0",
|
|
|
|
options: [
|
|
|
|
{ digest: option_a, html: "A", votes: 1 },
|
|
|
|
{ digest: option_b, html: "B", votes: 0 },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
group: "Value1",
|
|
|
|
options: [
|
|
|
|
{ digest: option_a, html: "A", votes: 2 },
|
|
|
|
{ digest: option_b, html: "B", votes: 1 },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
group: "Value2",
|
|
|
|
options: [
|
|
|
|
{ digest: option_a, html: "A", votes: 0 },
|
|
|
|
{ digest: option_b, html: "B", votes: 1 },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
user_votes = { user_0: option_a, user_1: option_a, user_2: option_b }
|
|
|
|
SiteSetting.poll_groupable_user_fields = groupable_user_field
|
|
|
|
|
|
|
|
[user1, user2, user3].each_with_index do |user, index|
|
|
|
|
group.add(user)
|
|
|
|
DiscoursePoll::Poll.vote(
|
|
|
|
user,
|
|
|
|
private_post.id,
|
|
|
|
DiscoursePoll::DEFAULT_POLL_NAME,
|
|
|
|
[user_votes["user_#{index}".to_sym]],
|
|
|
|
)
|
|
|
|
UserCustomField.create(
|
|
|
|
user_id: user.id,
|
|
|
|
name: groupable_user_field,
|
|
|
|
value: "value#{index}",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add another user to one of the fields to prove it groups users properly
|
|
|
|
group.add(user4)
|
|
|
|
DiscoursePoll::Poll.vote(
|
|
|
|
user4,
|
|
|
|
private_post.id,
|
|
|
|
DiscoursePoll::DEFAULT_POLL_NAME,
|
|
|
|
[option_a, option_b],
|
|
|
|
)
|
|
|
|
UserCustomField.create(user_id: user4.id, name: groupable_user_field, value: "value1")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns grouped poll results for admin based on user field" do
|
|
|
|
sign_in(admin)
|
|
|
|
|
|
|
|
get "/polls/grouped_poll_results.json",
|
|
|
|
params: {
|
|
|
|
post_id: private_post.id,
|
|
|
|
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
|
|
|
|
user_field_name: groupable_user_field,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response).to have_http_status :success
|
|
|
|
expect(response.parsed_body.deep_symbolize_keys).to eq(expected_results)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns grouped poll results for user within private group based on user field" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
group.add(user)
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
get "/polls/grouped_poll_results.json",
|
|
|
|
params: {
|
|
|
|
post_id: private_post.id,
|
|
|
|
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
|
|
|
|
user_field_name: groupable_user_field,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response).to have_http_status :success
|
|
|
|
expect(response.parsed_body.deep_symbolize_keys).to eq(expected_results)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an error when user does not have access to topic category" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
get "/polls/grouped_poll_results.json",
|
|
|
|
params: {
|
|
|
|
post_id: private_post.id,
|
|
|
|
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
|
|
|
|
user_field_name: groupable_user_field,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response).to have_http_status :unprocessable_entity
|
|
|
|
expect(response.parsed_body["errors"][0]).to eq(I18n.t("poll.user_cant_post_in_topic"))
|
|
|
|
end
|
|
|
|
end
|
2019-11-26 01:51:01 +08:00
|
|
|
end
|
2016-06-09 21:33:17 +08:00
|
|
|
end
|