discourse-topic-voting/spec/serializers/current_user_serializer_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

65 lines
2.3 KiB
Ruby

# frozen_string_literal: true
RSpec.describe CurrentUserSerializer do
fab!(:user1) { Fabricate(:user, trust_level: 3) }
let(:user2) { Fabricate(:user) }
fab!(:guardian) { Guardian.new(user1) }
fab!(:category)
fab!(:topic1) { Fabricate(:topic, category_id: category.id) }
let(:topic2) { Fabricate(:topic, category_id: category.id) }
let(:topic3) { Fabricate(:topic, category_id: category.id) }
let(:topic4) { Fabricate(:topic, category_id: category.id) }
it "does not return attributes related to voting if disabled" do
SiteSetting.topic_voting_enabled = false
json = described_class.new(user1, scope: guardian, root: false).as_json
expect(json[:votes_exceeded]).to eq(nil)
expect(json[:vote_count]).to eq(nil)
expect(json[:votes_left]).to eq(nil)
end
describe "votes_exceeded" do
it "returns false when within voting limits" do
SiteSetting.topic_voting_enabled = true
SiteSetting.topic_voting_tl3_vote_limit = 1
Fabricate(:topic_voting_votes, user: user2, topic: topic1)
json = described_class.new(user1, scope: guardian, root: false).as_json
expect(json[:votes_exceeded]).to eq(false)
end
it "returns true when hit voting limits" do
SiteSetting.topic_voting_enabled = true
SiteSetting.topic_voting_tl3_vote_limit = 1
Fabricate(:topic_voting_votes, user: user1, topic: topic1)
json = described_class.new(user1, scope: guardian, root: false).as_json
expect(json[:votes_exceeded]).to eq(true)
end
end
describe "votes_left" do
it "returns the number of votes the user has left" do
SiteSetting.topic_voting_tl3_vote_limit = 3
json = described_class.new(user1, scope: guardian, root: false).as_json
expect(json[:votes_left]).to eq(3)
Fabricate(:topic_voting_votes, user: user1, topic: topic1)
Fabricate(:topic_voting_votes, user: user1, topic: topic2)
Fabricate(:topic_voting_votes, user: user1, topic: topic3)
json = described_class.new(user1, scope: guardian, root: false).as_json
expect(json[:votes_left]).to eq(0)
Fabricate(:topic_voting_votes, user: user1, topic: topic4)
json = described_class.new(user1, scope: guardian, root: false).as_json
expect(json[:votes_left]).to eq(0)
end
end
end