Преглед изворни кода

Adding more unit tests. Fixing Salmon slaps XML

master
Eugen Rochko пре 7 година
родитељ
комит
720ff55262
13 измењених фајлова са 363 додато и 58 уклоњено
  1. +47
    -39
      app/helpers/atom_builder_helper.rb
  2. +2
    -6
      app/services/favourite_service.rb
  3. +2
    -0
      app/services/fetch_atom_service.rb
  4. +2
    -6
      app/services/unfavourite_service.rb
  5. +1
    -1
      app/workers/after_remote_follow_request_worker.rb
  6. +1
    -1
      app/workers/after_remote_follow_worker.rb
  7. +49
    -0
      spec/services/authorize_follow_service_spec.rb
  8. +34
    -0
      spec/services/block_service_spec.rb
  9. +36
    -0
      spec/services/favourite_service_spec.rb
  10. +69
    -3
      spec/services/follow_service_spec.rb
  11. +49
    -0
      spec/services/reject_follow_service_spec.rb
  12. +36
    -0
      spec/services/unblock_service_spec.rb
  13. +35
    -2
      spec/services/unfollow_service_spec.rb

+ 47
- 39
app/helpers/atom_builder_helper.rb Прегледај датотеку

@@ -167,6 +167,52 @@ module AtomBuilderHelper
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)
unique_id xml, stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type
published_at xml, stream_entry.created_at
@@ -185,45 +231,7 @@ module AtomBuilderHelper

if stream_entry.targeted?
target(xml) do
simple_id xml, TagManager.instance.uri_for(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
include_target(xml, stream_entry.target)
end
end



+ 2
- 6
app/services/favourite_service.rb Прегледај датотеку

@@ -31,14 +31,10 @@ class FavouriteService < BaseService
end

object_type xml, :activity
verb xml, :favourite
verb xml, :favorite

target(xml) do
author(xml) do
include_author xml, favourite.status.account
end

include_entry xml, favourite.status.stream_entry
include_target xml, favourite.status
end
end
end.to_xml


+ 2
- 0
app/services/fetch_atom_service.rb Прегледај датотеку

@@ -2,6 +2,8 @@

class FetchAtomService < BaseService
def call(url)
return if url.blank?

response = http_client.head(url)

Rails.logger.debug "Remote status HEAD request returned code #{response.code}"


+ 2
- 6
app/services/unfavourite_service.rb Прегледај датотеку

@@ -22,14 +22,10 @@ class UnfavouriteService < BaseService
end

object_type xml, :activity
verb xml, :unfavourite
verb xml, :unfavorite

target(xml) do
author(xml) do
include_author xml, favourite.status.account
end

include_entry xml, favourite.status.stream_entry
include_target xml, favourite.status
end
end
end.to_xml


+ 1
- 1
app/workers/after_remote_follow_request_worker.rb Прегледај датотеку

@@ -9,7 +9,7 @@ class AfterRemoteFollowRequestWorker
follow_request = FollowRequest.find(follow_request_id)
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
FollowService.new.call(follow_request.account, updated_account.acct)


+ 1
- 1
app/workers/after_remote_follow_worker.rb Прегледај датотеку

@@ -9,7 +9,7 @@ class AfterRemoteFollowWorker
follow = Follow.find(follow_id)
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
FollowService.new.call(follow.account, updated_account.acct)


+ 49
- 0
spec/services/authorize_follow_service_spec.rb Прегледај датотеку

@@ -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

+ 34
- 0
spec/services/block_service_spec.rb Прегледај датотеку

@@ -1,5 +1,39 @@
require 'rails_helper'

RSpec.describe BlockService do
let(:sender) { Fabricate(:account, username: 'alice') }

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

+ 36
- 0
spec/services/favourite_service_spec.rb Прегледај датотеку

@@ -1,5 +1,41 @@
require 'rails_helper'

RSpec.describe FavouriteService do
let(:sender) { Fabricate(:account, username: 'alice') }

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

+ 69
- 3
spec/services/follow_service_spec.rb Прегледај датотеку

@@ -1,9 +1,75 @@
require 'rails_helper'

RSpec.describe FollowService do
let(:sender) { Fabricate(:account, username: 'alice') }

subject { FollowService.new }

it 'creates a following relation'
it 'creates local account for remote user'
it 'sends follow to the remote user'
context 'local account' do
describe 'locked account' do
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

+ 49
- 0
spec/services/reject_follow_service_spec.rb Прегледај датотеку

@@ -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

+ 36
- 0
spec/services/unblock_service_spec.rb Прегледај датотеку

@@ -1,5 +1,41 @@
require 'rails_helper'

RSpec.describe UnblockService do
let(:sender) { Fabricate(:account, username: 'alice') }

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

+ 35
- 2
spec/services/unfollow_service_spec.rb Прегледај датотеку

@@ -1,8 +1,41 @@
require 'rails_helper'

RSpec.describe UnfollowService do
let(:sender) { Fabricate(:account, username: 'alice') }

subject { UnfollowService.new }

it 'destroys the following relation'
it 'sends remote interaction for remote user'
describe 'local' do
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

Loading…
Откажи
Сачувај