discourse-topic-voting/spec/requests/votes_controller_spec.rb
Natalie Tay 3d3037729c
DEV: Scope topic voting tables to avoid confusion with post voting (#196)
Renaming discourse_voting to topic_voting since there are two forms of voting in Discourse - posts and topics.

This PR also moves a OnceOff into a post migration. The post migration will be executed, but should ideally be a no-op. This allows us to not have to maintain the OnceOff as it had to be modified before with a previous migration. I considered removing this file altogether, but I don't think there is anything negative from just converting it into a migration, and it might be useful in the unlikely scenario that a forum from the past has never ran the OnceOff before.
2024-07-17 20:26:40 +08:00

75 lines
2.7 KiB
Ruby

# frozen_string_literal: true
require "rails_helper"
describe DiscourseTopicVoting::VotesController do
let(:user) { Fabricate(:user) }
let(:category) { Fabricate(:category) }
let(:topic) { Fabricate(:topic, category_id: category.id) }
before do
DiscourseTopicVoting::CategorySetting.create!(category: category)
Category.reset_voting_cache
SiteSetting.topic_voting_show_who_voted = true
SiteSetting.topic_voting_enabled = true
sign_in(user)
end
it "does not allow voting if voting is not enabled" do
SiteSetting.topic_voting_enabled = false
post "/voting/vote.json", params: { topic_id: topic.id }
expect(response.status).to eq(404)
end
it "can correctly show deal with voting workflow" do
SiteSetting.public_send "topic_voting_tl#{user.trust_level}_vote_limit=", 2
post "/voting/vote.json", params: { topic_id: topic.id }
expect(response.status).to eq(200)
post "/voting/vote.json", params: { topic_id: topic.id }
expect(response.status).to eq(403)
expect(topic.reload.vote_count).to eq(1)
expect(user.reload.vote_count).to eq(1)
get "/voting/who.json", params: { topic_id: topic.id }
expect(response.status).to eq(200)
json = JSON.parse(response.body)
expect(json.length).to eq(1)
expect(json.first.keys.sort).to eq(%w[avatar_template id name username])
expect(json.first["id"]).to eq(user.id)
post "/voting/unvote.json", params: { topic_id: topic.id }
expect(response.status).to eq(200)
expect(topic.reload.vote_count).to eq(0)
expect(user.reload.vote_count).to eq(0)
end
it "triggers a topic_upvote webhook when voting" do
Fabricate(:topic_voting_web_hook)
post "/voting/vote.json", params: { topic_id: topic.id }
expect(response.status).to eq(200)
job_args = Jobs::EmitWebHookEvent.jobs[0]["args"].first
expect(job_args["event_name"]).to eq("topic_upvote")
payload = JSON.parse(job_args["payload"])
expect(payload["topic_id"]).to eq(topic.id)
expect(payload["topic_slug"]).to eq(topic.slug)
expect(payload["voter_id"]).to eq(user.id)
expect(payload["vote_count"]).to eq(1)
end
it "triggers a topic_unvote webhook when unvoting" do
Fabricate(:topic_voting_web_hook)
post "/voting/unvote.json", params: { topic_id: topic.id }
expect(response.status).to eq(200)
job_args = Jobs::EmitWebHookEvent.jobs[0]["args"].first
expect(job_args["event_name"]).to eq("topic_unvote")
payload = JSON.parse(job_args["payload"])
expect(payload["topic_id"]).to eq(topic.id)
expect(payload["topic_slug"]).to eq(topic.slug)
expect(payload["voter_id"]).to eq(user.id)
expect(payload["vote_count"]).to eq(0)
end
end