Adding more unit tests. Fixing Salmon slaps XML

This commit is contained in:
Eugen Rochko 2017-02-12 17:28:15 +01:00
parent 446267d1bf
commit 720ff55262
13 changed files with 363 additions and 58 deletions

View File

@ -167,6 +167,52 @@ module AtomBuilderHelper
end end
end end
def include_target(xml, target)
simple_id xml, TagManager.instance.uri_for(target)
if target.object_type == :person
include_author xml, target
else
object_type xml, target.object_type
verb xml, target.verb
title xml, target.title
link_alternate xml, TagManager.instance.url_for(target)
end
# Statuses have content and author
return unless target.is_a?(Status)
rich_content xml, target
verb xml, target.verb
published_at xml, target.created_at
updated_at xml, target.updated_at
author(xml) do
include_author xml, target.account
end
if target.reply?
in_reply_to xml, TagManager.instance.uri_for(target.thread), TagManager.instance.url_for(target.thread)
end
link_visibility xml, target
target.mentions.each do |mention|
link_mention xml, mention.account
end
target.media_attachments.each do |media|
link_enclosure xml, media
end
target.tags.each do |tag|
category xml, tag.name
end
category(xml, 'nsfw') if target.sensitive?
privacy_scope(xml, target.visibility)
end
def include_entry(xml, stream_entry) def include_entry(xml, stream_entry)
unique_id xml, stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type unique_id xml, stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type
published_at xml, stream_entry.created_at published_at xml, stream_entry.created_at
@ -185,45 +231,7 @@ module AtomBuilderHelper
if stream_entry.targeted? if stream_entry.targeted?
target(xml) do target(xml) do
simple_id xml, TagManager.instance.uri_for(stream_entry.target) include_target(xml, stream_entry.target)
if stream_entry.target.object_type == :person
include_author xml, stream_entry.target
else
object_type xml, stream_entry.target.object_type
verb xml, stream_entry.target.verb
title xml, stream_entry.target.title
link_alternate xml, TagManager.instance.url_for(stream_entry.target)
end
# Statuses have content and author
if stream_entry.target.is_a?(Status)
rich_content xml, stream_entry.target
verb xml, stream_entry.target.verb
published_at xml, stream_entry.target.created_at
updated_at xml, stream_entry.target.updated_at
author(xml) do
include_author xml, stream_entry.target.account
end
link_visibility xml, stream_entry.target
stream_entry.target.mentions.each do |mention|
link_mention xml, mention.account
end
stream_entry.target.media_attachments.each do |media|
link_enclosure xml, media
end
stream_entry.target.tags.each do |tag|
category xml, tag.name
end
category(xml, 'nsfw') if stream_entry.target.sensitive?
privacy_scope(xml, stream_entry.target.visibility)
end
end end
end end

View File

@ -31,14 +31,10 @@ class FavouriteService < BaseService
end end
object_type xml, :activity object_type xml, :activity
verb xml, :favourite verb xml, :favorite
target(xml) do target(xml) do
author(xml) do include_target xml, favourite.status
include_author xml, favourite.status.account
end
include_entry xml, favourite.status.stream_entry
end end
end end
end.to_xml end.to_xml

View File

@ -2,6 +2,8 @@
class FetchAtomService < BaseService class FetchAtomService < BaseService
def call(url) def call(url)
return if url.blank?
response = http_client.head(url) response = http_client.head(url)
Rails.logger.debug "Remote status HEAD request returned code #{response.code}" Rails.logger.debug "Remote status HEAD request returned code #{response.code}"

View File

@ -22,14 +22,10 @@ class UnfavouriteService < BaseService
end end
object_type xml, :activity object_type xml, :activity
verb xml, :unfavourite verb xml, :unfavorite
target(xml) do target(xml) do
author(xml) do include_target xml, favourite.status
include_author xml, favourite.status.account
end
include_entry xml, favourite.status.stream_entry
end end
end end
end.to_xml end.to_xml

View File

@ -9,7 +9,7 @@ class AfterRemoteFollowRequestWorker
follow_request = FollowRequest.find(follow_request_id) follow_request = FollowRequest.find(follow_request_id)
updated_account = FetchRemoteAccountService.new.call(follow_request.target_account.remote_url) updated_account = FetchRemoteAccountService.new.call(follow_request.target_account.remote_url)
return if updated_account.locked? return if updated_account.nil? || updated_account.locked?
follow_request.destroy follow_request.destroy
FollowService.new.call(follow_request.account, updated_account.acct) FollowService.new.call(follow_request.account, updated_account.acct)

View File

@ -9,7 +9,7 @@ class AfterRemoteFollowWorker
follow = Follow.find(follow_id) follow = Follow.find(follow_id)
updated_account = FetchRemoteAccountService.new.call(follow.target_account.remote_url) updated_account = FetchRemoteAccountService.new.call(follow.target_account.remote_url)
return unless updated_account.locked? return if updated_account.nil? || !updated_account.locked?
follow.destroy follow.destroy
FollowService.new.call(follow.account, updated_account.acct) FollowService.new.call(follow.account, updated_account.acct)

View File

@ -0,0 +1,49 @@
require 'rails_helper'
RSpec.describe AuthorizeFollowService do
let(:sender) { Fabricate(:account, username: 'alice') }
subject { AuthorizeFollowService.new }
describe 'local' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
before do
FollowRequest.create(account: bob, target_account: sender)
subject.call(bob, sender)
end
it 'removes follow request' do
expect(bob.requested?(sender)).to be false
end
it 'creates follow relation' do
expect(bob.following?(sender)).to be true
end
end
describe 'remote' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', domain: 'example.com', salmon_url: 'http://salmon.example.com')).account }
before do
FollowRequest.create(account: bob, target_account: sender)
stub_request(:post, "http://salmon.example.com/").to_return(:status => 200, :body => "", :headers => {})
subject.call(bob, sender)
end
it 'removes follow request' do
expect(bob.requested?(sender)).to be false
end
it 'creates follow relation' do
expect(bob.following?(sender)).to be true
end
it 'sends a follow request authorization salmon slap' do
expect(a_request(:post, "http://salmon.example.com/").with { |req|
xml = OStatus2::Salmon.new.unpack(req.body)
xml.match(TagManager::VERBS[:authorize])
}).to have_been_made.once
end
end
end

View File

@ -1,5 +1,39 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe BlockService do RSpec.describe BlockService do
let(:sender) { Fabricate(:account, username: 'alice') }
subject { BlockService.new } subject { BlockService.new }
describe 'local' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
before do
subject.call(sender, bob)
end
it 'creates a blocking relation' do
expect(sender.blocking?(bob)).to be true
end
end
describe 'remote' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', domain: 'example.com', salmon_url: 'http://salmon.example.com')).account }
before do
stub_request(:post, "http://salmon.example.com/").to_return(:status => 200, :body => "", :headers => {})
subject.call(sender, bob)
end
it 'creates a blocking relation' do
expect(sender.blocking?(bob)).to be true
end
it 'sends a block salmon slap' do
expect(a_request(:post, "http://salmon.example.com/").with { |req|
xml = OStatus2::Salmon.new.unpack(req.body)
xml.match(TagManager::VERBS[:block])
}).to have_been_made.once
end
end
end end

View File

@ -1,5 +1,41 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe FavouriteService do RSpec.describe FavouriteService do
let(:sender) { Fabricate(:account, username: 'alice') }
subject { FavouriteService.new } subject { FavouriteService.new }
describe 'local' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
let(:status) { Fabricate(:status, account: bob) }
before do
subject.call(sender, status)
end
it 'creates a favourite' do
expect(status.favourites.first).to_not be_nil
end
end
describe 'remote' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', domain: 'example.com', salmon_url: 'http://salmon.example.com')).account }
let(:status) { Fabricate(:status, account: bob, uri: 'tag:example.com:blahblah') }
before do
stub_request(:post, "http://salmon.example.com/").to_return(:status => 200, :body => "", :headers => {})
subject.call(sender, status)
end
it 'creates a favourite' do
expect(status.favourites.first).to_not be_nil
end
it 'sends a salmon slap' do
expect(a_request(:post, "http://salmon.example.com/").with { |req|
xml = OStatus2::Salmon.new.unpack(req.body)
xml.match(TagManager::VERBS[:favorite])
}).to have_been_made.once
end
end
end end

View File

@ -1,9 +1,75 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe FollowService do RSpec.describe FollowService do
let(:sender) { Fabricate(:account, username: 'alice') }
subject { FollowService.new } subject { FollowService.new }
it 'creates a following relation' context 'local account' do
it 'creates local account for remote user' describe 'locked account' do
it 'sends follow to the remote user' let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, locked: true, username: 'bob')).account }
before do
subject.call(sender, bob.acct)
end
it 'creates a follow request' do
expect(FollowRequest.find_by(account: sender, target_account: bob)).to_not be_nil
end
end
describe 'unlocked account' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
before do
subject.call(sender, bob.acct)
end
it 'creates a following relation' do
expect(sender.following?(bob)).to be true
end
end
end
context 'remote account' do
describe 'locked account' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, locked: true, username: 'bob', domain: 'example.com', salmon_url: 'http://salmon.example.com')).account }
before do
stub_request(:post, "http://salmon.example.com/").to_return(:status => 200, :body => "", :headers => {})
subject.call(sender, bob.acct)
end
it 'creates a follow request' do
expect(FollowRequest.find_by(account: sender, target_account: bob)).to_not be_nil
end
it 'sends a follow request salmon slap' do
expect(a_request(:post, "http://salmon.example.com/").with { |req|
xml = OStatus2::Salmon.new.unpack(req.body)
xml.match(TagManager::VERBS[:request_friend])
}).to have_been_made.once
end
end
describe 'unlocked account' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', domain: 'example.com', salmon_url: 'http://salmon.example.com')).account }
before do
stub_request(:post, "http://salmon.example.com/").to_return(:status => 200, :body => "", :headers => {})
subject.call(sender, bob.acct)
end
it 'creates a following relation' do
expect(sender.following?(bob)).to be true
end
it 'sends a follow salmon slap' do
expect(a_request(:post, "http://salmon.example.com/").with { |req|
xml = OStatus2::Salmon.new.unpack(req.body)
xml.match(TagManager::VERBS[:follow])
}).to have_been_made.once
end
end
end
end end

View File

@ -0,0 +1,49 @@
require 'rails_helper'
RSpec.describe RejectFollowService do
let(:sender) { Fabricate(:account, username: 'alice') }
subject { RejectFollowService.new }
describe 'local' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
before do
FollowRequest.create(account: bob, target_account: sender)
subject.call(bob, sender)
end
it 'removes follow request' do
expect(bob.requested?(sender)).to be false
end
it 'does not create follow relation' do
expect(bob.following?(sender)).to be false
end
end
describe 'remote' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', domain: 'example.com', salmon_url: 'http://salmon.example.com')).account }
before do
FollowRequest.create(account: bob, target_account: sender)
stub_request(:post, "http://salmon.example.com/").to_return(:status => 200, :body => "", :headers => {})
subject.call(bob, sender)
end
it 'removes follow request' do
expect(bob.requested?(sender)).to be false
end
it 'does not create follow relation' do
expect(bob.following?(sender)).to be false
end
it 'sends a follow request rejection salmon slap' do
expect(a_request(:post, "http://salmon.example.com/").with { |req|
xml = OStatus2::Salmon.new.unpack(req.body)
xml.match(TagManager::VERBS[:reject])
}).to have_been_made.once
end
end
end

View File

@ -1,5 +1,41 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe UnblockService do RSpec.describe UnblockService do
let(:sender) { Fabricate(:account, username: 'alice') }
subject { UnblockService.new } subject { UnblockService.new }
describe 'local' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
before do
sender.block!(bob)
subject.call(sender, bob)
end
it 'destroys the blocking relation' do
expect(sender.blocking?(bob)).to be false
end
end
describe 'remote' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', domain: 'example.com', salmon_url: 'http://salmon.example.com')).account }
before do
sender.block!(bob)
stub_request(:post, "http://salmon.example.com/").to_return(:status => 200, :body => "", :headers => {})
subject.call(sender, bob)
end
it 'destroys the blocking relation' do
expect(sender.following?(bob)).to be false
end
it 'sends an unblock salmon slap' do
expect(a_request(:post, "http://salmon.example.com/").with { |req|
xml = OStatus2::Salmon.new.unpack(req.body)
xml.match(TagManager::VERBS[:unblock])
}).to have_been_made.once
end
end
end end

View File

@ -1,8 +1,41 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe UnfollowService do RSpec.describe UnfollowService do
let(:sender) { Fabricate(:account, username: 'alice') }
subject { UnfollowService.new } subject { UnfollowService.new }
it 'destroys the following relation' describe 'local' do
it 'sends remote interaction for remote user' let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
before do
sender.follow!(bob)
subject.call(sender, bob)
end
it 'destroys the following relation' do
expect(sender.following?(bob)).to be false
end
end
describe 'remote' do
let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', domain: 'example.com', salmon_url: 'http://salmon.example.com')).account }
before do
sender.follow!(bob)
stub_request(:post, "http://salmon.example.com/").to_return(:status => 200, :body => "", :headers => {})
subject.call(sender, bob)
end
it 'destroys the following relation' do
expect(sender.following?(bob)).to be false
end
it 'sends an unfollow salmon slap' do
expect(a_request(:post, "http://salmon.example.com/").with { |req|
xml = OStatus2::Salmon.new.unpack(req.body)
xml.match(TagManager::VERBS[:unfollow])
}).to have_been_made.once
end
end
end end