DEV: An endpoint to check if the current user voted in a poll. (#13648)

The endpoint the existence of the poll and if the current user can see it. It
will facilitate using a poll programmatically, especially if we'd like to create an external poll through a theme component.
This commit is contained in:
Roman Rizzi
2021-07-06 14:46:34 -03:00
committed by GitHub
parent 95b5794331
commit 7925a76d93
2 changed files with 56 additions and 0 deletions
@@ -385,4 +385,48 @@ describe ::DiscoursePoll::PollsController do
end
describe '#current_user_voted' do
let(:logged_user) { Fabricate(:user) }
let(:post_with_poll) { Fabricate(:post, raw: "[poll]\n- A\n- B\n[/poll]") }
before { log_in_user(logged_user) }
it 'returns true if the logged user already voted' do
poll = post_with_poll.polls.last
PollVote.create!(poll: poll, user: logged_user)
get :current_user_voted, params: { id: poll.id }, format: :json
parsed_body = JSON.parse(response.body)
expect(response.status).to eq(200)
expect(parsed_body['voted']).to eq(true)
end
it 'returns a 404 if there is no poll' do
unknown_poll_id = 999999
get :current_user_voted, params: { id: unknown_poll_id }, format: :json
expect(response.status).to eq(404)
end
it "returns a 404 if the user doesn't have access to the poll" do
pm_with_poll = Fabricate(:private_message_post, raw: "[poll]\n- A\n- B\n[/poll]")
poll = pm_with_poll.polls.last
get :current_user_voted, params: { id: poll.id }, format: :json
expect(response.status).to eq(404)
end
it "returns false if the user didn't vote yet" do
poll = post_with_poll.polls.last
get :current_user_voted, params: { id: poll.id }, format: :json
parsed_body = JSON.parse(response.body)
expect(response.status).to eq(200)
expect(parsed_body['voted']).to eq(false)
end
end
end