DEV: Backfill tests before rename (#131)
This commit is contained in:
parent
9fab0ee3b3
commit
1d81f8f3ed
56
plugin.rb
56
plugin.rb
|
@ -39,35 +39,12 @@ after_initialize do
|
|||
Topic.class_eval { prepend DiscourseTopicVoting::TopicExtension }
|
||||
User.class_eval { prepend DiscourseTopicVoting::UserExtension }
|
||||
|
||||
require_dependency 'post_serializer'
|
||||
class ::PostSerializer
|
||||
attributes :can_vote
|
||||
add_to_serializer(:post, :can_vote, false) { object.topic&.can_vote? }
|
||||
add_to_serializer(:post, :include_can_vote?) { SiteSetting.voting_enabled && object.post_number == 1 }
|
||||
|
||||
def include_can_vote?
|
||||
object.post_number == 1
|
||||
end
|
||||
|
||||
def can_vote
|
||||
object.topic&.can_vote?
|
||||
end
|
||||
end
|
||||
|
||||
require_dependency 'topic_view_serializer'
|
||||
class ::TopicViewSerializer
|
||||
attributes :can_vote, :vote_count, :user_voted
|
||||
|
||||
def can_vote
|
||||
object.topic.can_vote?
|
||||
end
|
||||
|
||||
def vote_count
|
||||
object.topic.vote_count
|
||||
end
|
||||
|
||||
def user_voted
|
||||
scope.user ? object.topic.user_voted?(scope.user) : false
|
||||
end
|
||||
end
|
||||
add_to_serializer(:topic_view, :can_vote) { object.topic.can_vote? }
|
||||
add_to_serializer(:topic_view, :vote_count) { object.topic.vote_count }
|
||||
add_to_serializer(:topic_view, :user_voted) { scope.user ? object.topic.user_voted?(scope.user) : false }
|
||||
|
||||
if TopicQuery.respond_to?(:results_filter_callbacks)
|
||||
TopicQuery.results_filter_callbacks << ->(_type, result, user, options) {
|
||||
|
@ -102,9 +79,7 @@ after_initialize do
|
|||
add_to_serializer(:topic_list_item, :include_vote_count?) { object.can_vote? }
|
||||
add_to_serializer(:topic_list_item, :include_can_vote?) { SiteSetting.voting_enabled && object.regular? }
|
||||
add_to_serializer(:topic_list_item, :include_user_voted?) { object.can_vote? }
|
||||
# this always evaluates to true because
|
||||
# include_can_vote? returns false if voting is not enabled
|
||||
add_to_serializer(:basic_category, :can_vote, false) { SiteSetting.voting_enabled }
|
||||
add_to_serializer(:basic_category, :can_vote, false) { true }
|
||||
add_to_serializer(:basic_category, :include_can_vote?) { Category.can_vote?(object.id) }
|
||||
|
||||
register_search_advanced_filter(/^min_vote_count:(\d+)$/) do |posts, match|
|
||||
|
@ -169,22 +144,9 @@ after_initialize do
|
|||
end
|
||||
end
|
||||
|
||||
require_dependency 'current_user_serializer'
|
||||
class ::CurrentUserSerializer
|
||||
attributes :votes_exceeded, :vote_count, :votes_left
|
||||
|
||||
def votes_exceeded
|
||||
object.reached_voting_limit?
|
||||
end
|
||||
|
||||
def vote_count
|
||||
object.vote_count
|
||||
end
|
||||
|
||||
def votes_left
|
||||
[object.vote_limit - object.vote_count, 0].max
|
||||
end
|
||||
end
|
||||
add_to_serializer(:current_user, :votes_exceeded) { object.reached_voting_limit? }
|
||||
add_to_serializer(:current_user, :votes_count) { object.vote_count }
|
||||
add_to_serializer(:current_user, :votes_left) { [object.vote_limit - object.vote_count, 0].max }
|
||||
|
||||
require_dependency 'list_controller'
|
||||
class ::ListController
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
# 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) { Fabricate(: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.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.voting_enabled = true
|
||||
SiteSetting.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.voting_enabled = true
|
||||
SiteSetting.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.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
|
|
@ -9,13 +9,14 @@ describe TopicViewSerializer do
|
|||
let(:topic_view) { TopicView.new(topic, user) }
|
||||
let(:guardian) { Guardian.new(user) }
|
||||
|
||||
it 'returns false when voting disabled' do
|
||||
describe 'can_vote' do
|
||||
it 'returns nil when voting disabled' do
|
||||
SiteSetting.voting_enabled = false
|
||||
DiscourseTopicVoting::CategorySetting.create!(category: category)
|
||||
|
||||
json = TopicViewSerializer.new(topic_view, scope: guardian, root: false).as_json
|
||||
|
||||
expect(json[:can_vote]).to eq(false)
|
||||
expect(json[:can_vote]).to eq(nil)
|
||||
end
|
||||
|
||||
it 'returns false when topic not in category' do
|
||||
|
@ -40,4 +41,27 @@ describe TopicViewSerializer do
|
|||
|
||||
expect(json[:can_vote]).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'vote_count' do
|
||||
it 'returns the topic vote counts' do
|
||||
Fabricate(:topic_voting_vote_count, topic: topic, votes_count: 3)
|
||||
json = TopicViewSerializer.new(topic_view, scope: guardian, root: false).as_json
|
||||
|
||||
expect(json[:vote_count]).to eq(3)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'user_voted' do
|
||||
it 'returns true if the user has voted on the topic' do
|
||||
json = TopicViewSerializer.new(topic_view, scope: guardian, root: false).as_json
|
||||
|
||||
expect(json[:user_voted]).to eq(false)
|
||||
|
||||
Fabricate(:topic_voting_votes, topic: topic, user: user)
|
||||
json = TopicViewSerializer.new(topic_view, scope: guardian, root: false).as_json
|
||||
|
||||
expect(json[:user_voted]).to eq(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue