From d299197392c62450e009a271e2a4986a0d191dd4 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 5 Apr 2019 09:00:48 +0800 Subject: [PATCH] DEV: Add missing test cases for `Topic.for_digest` and `Topic.secured`. --- spec/models/topic_spec.rb | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 65ecec8caa9..94b1b3e7dd0 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -1656,7 +1656,7 @@ describe Topic do end end - describe 'for_digest' do + describe '.for_digest' do let(:user) { Fabricate.build(:user) } context "no edit grace period" do @@ -1688,6 +1688,18 @@ describe Topic do expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to be_blank end + it "doesn't return topics that a user has muted" do + user = Fabricate(:user) + + Fabricate(:topic_user, + user: user, + topic: topic, + notification_level: TopicUser.notification_levels[:muted] + ) + + expect(Topic.for_digest(user, 1.year.ago)).to eq([]) + end + it "doesn't return topics from suppressed categories" do user = Fabricate(:user) category = Fabricate(:category) @@ -1784,21 +1796,24 @@ describe Topic do expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to eq([topic1]) end end - end - describe 'secured' do - it 'can remove secure groups' do + describe '.secured' do + it 'should return the right topics' do category = Fabricate(:category, read_restricted: true) - Fabricate(:topic, category: category, created_at: 1.day.ago) + topic = Fabricate(:topic, category: category, created_at: 1.day.ago) + group = Fabricate(:group) + user = Fabricate(:user) + group.add(user) + private_category = Fabricate(:private_category, group: group) - expect(Topic.secured(Guardian.new(nil)).count).to eq(0) - expect(Topic.secured(Guardian.new(Fabricate(:admin))).count).to eq(2) + expect(Topic.secured(Guardian.new(nil))).to eq([]) - # for_digest + expect(Topic.secured(Guardian.new(user))) + .to contain_exactly(private_category.topic) - expect(Topic.for_digest(Fabricate(:user), 1.year.ago).count).to eq(0) - expect(Topic.for_digest(Fabricate(:admin), 1.year.ago).count).to eq(1) + expect(Topic.secured(Guardian.new(Fabricate(:admin)))) + .to contain_exactly(category.topic, private_category.topic, topic) end end