Add test for FanOutOnWriteService

This commit is contained in:
Eugen Rochko 2016-11-06 15:56:34 +01:00
parent 7bb28bf780
commit 93212bc2c4
3 changed files with 45 additions and 2 deletions

View File

@ -15,10 +15,13 @@ class FanOutOnWriteService < BaseService
private private
def deliver_to_self(status) def deliver_to_self(status)
Rails.logger.debug "Delivering status #{status.id} to author"
FeedManager.instance.push(:home, status.account, status) FeedManager.instance.push(:home, status.account, status)
end end
def deliver_to_followers(status) def deliver_to_followers(status)
Rails.logger.debug "Delivering status #{status.id} to followers"
status.account.followers.find_each do |follower| status.account.followers.find_each do |follower|
next if !follower.local? || FeedManager.instance.filter?(:home, status, follower) next if !follower.local? || FeedManager.instance.filter?(:home, status, follower)
FeedManager.instance.push(:home, follower, status) FeedManager.instance.push(:home, follower, status)
@ -26,7 +29,9 @@ class FanOutOnWriteService < BaseService
end end
def deliver_to_mentioned(status) def deliver_to_mentioned(status)
status.mentions.find_each do |mention| Rails.logger.debug "Delivering status #{status.id} to mentioned accounts"
status.mentions.includes(:account).each do |mention|
mentioned_account = mention.account mentioned_account = mention.account
next if !mentioned_account.local? || mentioned_account.id == status.account_id || FeedManager.instance.filter?(:mentions, status, mentioned_account) next if !mentioned_account.local? || mentioned_account.id == status.account_id || FeedManager.instance.filter?(:mentions, status, mentioned_account)
FeedManager.instance.push(:mentions, mentioned_account, status) FeedManager.instance.push(:mentions, mentioned_account, status)
@ -34,12 +39,15 @@ class FanOutOnWriteService < BaseService
end end
def deliver_to_hashtags(status) def deliver_to_hashtags(status)
Rails.logger.debug "Delivering status #{status.id} to hashtags"
status.tags.find_each do |tag| status.tags.find_each do |tag|
FeedManager.instance.broadcast("hashtag:#{tag.name}", id: status.id) FeedManager.instance.broadcast("hashtag:#{tag.name}", id: status.id)
end end
end end
def deliver_to_public(status) def deliver_to_public(status)
Rails.logger.debug "Delivering status #{status.id} to public timeline"
FeedManager.instance.broadcast(:public, id: status.id) FeedManager.instance.broadcast(:public, id: status.id)
end end
end end

View File

@ -9,7 +9,7 @@ class FetchRemoteStatusService < BaseService
private private
def process_atom(url, body) def process_atom(url, body)
Rails.logger.debug 'Processing Atom for remote status' Rails.logger.debug "Processing Atom for remote status at #{url}"
xml = Nokogiri::XML(body) xml = Nokogiri::XML(body)
account = extract_author(url, xml) account = extract_author(url, xml)

View File

@ -1,5 +1,40 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe FanOutOnWriteService do RSpec.describe FanOutOnWriteService do
let(:author) { Fabricate(:account, username: 'tom') }
let(:status) { Fabricate(:status, text: 'Hello @alice #test', account: author) }
let(:alice) { Fabricate(:user, account: Fabricate(:account, username: 'alice')).account }
let(:follower) { Fabricate(:account, username: 'bob') }
subject { FanOutOnWriteService.new } subject { FanOutOnWriteService.new }
before do
alice
follower.follow!(author)
ProcessMentionsService.new.call(status)
ProcessHashtagsService.new.call(status)
subject.call(status)
end
it 'delivers status to home timeline' do
expect(Feed.new(:home, author).get(1).map(&:id)).to include status.id
end
it 'delivers status to local followers' do
expect(Feed.new(:home, follower).get(1).map(&:id)).to include status.id
end
it 'delivers status to mentioned users' do
expect(Feed.new(:mentions, alice).get(1).map(&:id)).to include status.id
end
it 'delivers status to hashtag' do
expect(Tag.find_by!(name: 'test').statuses.pluck(:id)).to include status.id
end
it 'delivers status to public timeline' do
expect(Status.as_public_timeline(alice).map(&:id)).to include status.id
end
end end