Adding test for ProcessFeedService

This commit is contained in:
Eugen Rochko 2016-10-10 16:03:38 +02:00
parent 3306a5d524
commit 81065bc06c
5 changed files with 306 additions and 7 deletions

View File

@ -175,7 +175,7 @@ class ProcessFeedService < BaseService
end
def content(xml)
xml.at_xpath('./xmlns:content').content
xml.at_xpath('./xmlns:content').try(:content)
end
def thread_id(xml)

BIN
spec/fixtures/requests/attachment1.txt vendored Normal file

Binary file not shown.

BIN
spec/fixtures/requests/attachment2.txt vendored Normal file

Binary file not shown.

261
spec/fixtures/xml/mastodon.atom vendored Normal file
View File

@ -0,0 +1,261 @@
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:media="http://purl.org/syndication/atommedia">
<id>http://kickass.zone/users/localhost.atom</id>
<title>::1</title>
<updated>2016-10-10T13:29:56Z</updated>
<logo>http://kickass.zone/system/accounts/avatars/000/000/001/medium/eris.png</logo>
<author>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<uri>http://kickass.zone/users/localhost</uri>
<name>localhost</name>
<email>localhost@kickass.zone</email>
<link rel="alternate" type="text/html" href="http://kickass.zone/users/localhost"/>
<link rel="avatar" type="image/png" media:width="300" media:height="300" href="http://kickass.zone/system/accounts/avatars/000/000/001/large/eris.png"/>
<link rel="avatar" type="image/png" media:width="96" media:height="96" href="http://kickass.zone/system/accounts/avatars/000/000/001/medium/eris.png"/>
<link rel="avatar" type="image/png" media:width="48" media:height="48" href="http://kickass.zone/system/accounts/avatars/000/000/001/small/eris.png"/>
<poco:preferredUsername>localhost</poco:preferredUsername>
<poco:displayName>::1</poco:displayName>
</author>
<link rel="alternate" type="text/html" href="http://kickass.zone/users/localhost"/>
<link rel="self" type="application/atom+xml" href="http://kickass.zone/users/localhost.atom"/>
<link rel="hub" href="https://pubsubhubbub.superfeedr.com"/>
<link rel="salmon" href="http://kickass.zone/api/salmon/1"/>
<entry>
<id>tag:kickass.zone,2016-10-10:objectId=7:objectType=Follow</id>
<published>2016-10-10T13:29:56Z</published>
<updated>2016-10-10T13:29:56Z</updated>
<title>localhost started following kat@mastodon.social</title>
<content type="html">localhost started following kat@mastodon.social</content>
<activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
<link rel="self" type="application/atom+xml" href="http://kickass.zone/users/localhost/updates/12.atom"/>
<link rel="alternate" type="text/html" href="http://kickass.zone/users/localhost/updates/12"/>
<activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
<activity:object>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<uri>https://mastodon.social/users/kat</uri>
<name>kat</name>
<email>kat@mastodon.social</email>
<summary>#trans #queer</summary>
<link rel="alternate" type="text/html" href="https://mastodon.social/users/kat"/>
<link rel="avatar" type="image/jpeg" media:width="300" media:height="300" href="http://kickass.zone/system/accounts/avatars/000/000/016/large/kat-20150403T124737-b2mbt44.jpg"/>
<link rel="avatar" type="image/jpeg" media:width="96" media:height="96" href="http://kickass.zone/system/accounts/avatars/000/000/016/medium/kat-20150403T124737-b2mbt44.jpg"/>
<link rel="avatar" type="image/jpeg" media:width="48" media:height="48" href="http://kickass.zone/system/accounts/avatars/000/000/016/small/kat-20150403T124737-b2mbt44.jpg"/>
<poco:preferredUsername>kat</poco:preferredUsername>
<poco:displayName>Kat</poco:displayName>
<poco:note>#trans #queer</poco:note>
</activity:object>
</entry>
<entry>
<id>tag:kickass.zone,2016-10-10:objectId=3:objectType=Favourite</id>
<published>2016-10-10T13:29:26Z</published>
<updated>2016-10-10T13:29:26Z</updated>
<title>localhost favourited a status by kat@mastodon.social</title>
<content type="html">localhost favourited a status by kat@mastodon.social</content>
<activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb>
<link rel="self" type="application/atom+xml" href="http://kickass.zone/users/localhost/updates/11.atom"/>
<link rel="alternate" type="text/html" href="http://kickass.zone/users/localhost/updates/11"/>
<activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
<thr:in-reply-to ref="tag:mastodon.social,2016-10-10:objectId=22833:objectType=Status" href="https://mastodon.social/users/kat/updates/16543" type="text/html"/>
<activity:object>
<activity:object-type>http://activitystrea.ms/schema/1.0/comment</activity:object-type>
<id>tag:mastodon.social,2016-10-10:objectId=22833:objectType=Status</id>
<title>@localhost oooh more mastodons &#x2764;</title>
<link rel="alternate" type="text/html" href="https://mastodon.social/users/kat/updates/16543"/>
<content type="html">&lt;p&gt;&lt;a href="http://kickass.zone/users/localhost"&gt;@localhost&lt;/a&gt; oooh more mastodons &#x2764;&lt;/p&gt;</content>
<activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
<published>2016-10-10T13:23:35Z</published>
<updated>2016-10-10T13:23:35Z</updated>
<author>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<uri>https://mastodon.social/users/kat</uri>
<name>kat</name>
<email>kat@mastodon.social</email>
<summary>#trans #queer</summary>
<link rel="alternate" type="text/html" href="https://mastodon.social/users/kat"/>
<link rel="avatar" type="image/jpeg" media:width="300" media:height="300" href="http://kickass.zone/system/accounts/avatars/000/000/016/large/kat-20150403T124737-b2mbt44.jpg"/>
<link rel="avatar" type="image/jpeg" media:width="96" media:height="96" href="http://kickass.zone/system/accounts/avatars/000/000/016/medium/kat-20150403T124737-b2mbt44.jpg"/>
<link rel="avatar" type="image/jpeg" media:width="48" media:height="48" href="http://kickass.zone/system/accounts/avatars/000/000/016/small/kat-20150403T124737-b2mbt44.jpg"/>
<poco:preferredUsername>kat</poco:preferredUsername>
<poco:displayName>Kat</poco:displayName>
<poco:note>#trans #queer</poco:note>
</author>
<link rel="mentioned" href="http://kickass.zone/users/localhost"/>
</activity:object>
</entry>
<entry>
<id>tag:kickass.zone,2016-10-10:objectId=2:objectType=Favourite</id>
<published>2016-10-10T13:13:15Z</published>
<updated>2016-10-10T13:13:15Z</updated>
<title>localhost favourited a status by Gargron@mastodon.social</title>
<content type="html">localhost favourited a status by Gargron@mastodon.social</content>
<activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb>
<link rel="self" type="application/atom+xml" href="http://kickass.zone/users/localhost/updates/10.atom"/>
<link rel="alternate" type="text/html" href="http://kickass.zone/users/localhost/updates/10"/>
<activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
<thr:in-reply-to ref="tag:mastodon.social,2016-10-10:objectId=22825:objectType=Status" href="https://mastodon.social/users/Gargron/updates/16538" type="text/html"/>
<activity:object>
<activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
<id>tag:mastodon.social,2016-10-10:objectId=22825:objectType=Status</id>
<title>Deployed some fixes</title>
<link rel="alternate" type="text/html" href="https://mastodon.social/users/Gargron/updates/16538"/>
<content type="html">&lt;p&gt;Deployed some fixes&lt;/p&gt;</content>
<activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
<published>2016-10-10T13:10:37Z</published>
<updated>2016-10-10T13:10:37Z</updated>
<author>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<uri>https://mastodon.social/users/Gargron</uri>
<name>Gargron</name>
<email>Gargron@mastodon.social</email>
<summary>Developer of Mastodon, a GNU social alternative: https://github.com/Gargron/mastodon</summary>
<link rel="alternate" type="text/html" href="https://mastodon.social/users/Gargron"/>
<link rel="avatar" type="image/png" media:width="300" media:height="300" href="http://kickass.zone/system/accounts/avatars/000/000/003/large/4375_eugencommish.png"/>
<link rel="avatar" type="image/png" media:width="96" media:height="96" href="http://kickass.zone/system/accounts/avatars/000/000/003/medium/4375_eugencommish.png"/>
<link rel="avatar" type="image/png" media:width="48" media:height="48" href="http://kickass.zone/system/accounts/avatars/000/000/003/small/4375_eugencommish.png"/>
<poco:preferredUsername>Gargron</poco:preferredUsername>
<poco:displayName>Eugen</poco:displayName>
<poco:note>Developer of Mastodon, a GNU social alternative: https://github.com/Gargron/mastodon</poco:note>
</author>
</activity:object>
</entry>
<entry>
<id>tag:kickass.zone,2016-10-10:objectId=17:objectType=Status</id>
<published>2016-10-10T00:41:31Z</published>
<updated>2016-10-10T00:41:31Z</updated>
<title>Social media needs MOAR cats! http://kickass.zone/media/3</title>
<content type="html">&lt;p&gt;Social media needs MOAR cats! &lt;a rel="nofollow noopener" href="http://kickass.zone/media/3"&gt;http://kickass.zone/media/3&lt;/a&gt;&lt;/p&gt;</content>
<activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
<link rel="self" type="application/atom+xml" href="http://kickass.zone/users/localhost/updates/9.atom"/>
<link rel="alternate" type="text/html" href="http://kickass.zone/users/localhost/updates/9"/>
<activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
<link rel="enclosure" href="http://kickass.zone/system/media_attachments/files/000/000/003/original/gizmo.jpg?1476060065" type="image/jpeg" length="108841"/>
</entry>
<entry>
<id>tag:kickass.zone,2016-10-10:objectId=14:objectType=Status</id>
<published>2016-10-10T00:38:39Z</published>
<updated>2016-10-10T00:38:39Z</updated>
<title>http://kickass.zone/media/2</title>
<content type="html">&lt;p&gt;&lt;a rel="nofollow noopener" href="http://kickass.zone/media/2"&gt;http://kickass.zone/media/2&lt;/a&gt;&lt;/p&gt;</content>
<activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
<link rel="self" type="application/atom+xml" href="http://kickass.zone/users/localhost/updates/8.atom"/>
<link rel="alternate" type="text/html" href="http://kickass.zone/users/localhost/updates/8"/>
<activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
<link rel="enclosure" href="http://kickass.zone/system/media_attachments/files/000/000/002/original/morpheus_linux.jpg?1476059910" type="image/jpeg" length="191816"/>
</entry>
<entry>
<id>tag:kickass.zone,2016-10-10:objectId=12:objectType=Status</id>
<published>2016-10-10T00:37:49Z</published>
<updated>2016-10-10T00:37:49Z</updated>
<title/>
<activity:verb>http://activitystrea.ms/schema/1.0/delete</activity:verb>
<link rel="self" type="application/atom+xml" href="http://kickass.zone/users/localhost/updates/7.atom"/>
<link rel="alternate" type="text/html" href="http://kickass.zone/users/localhost/updates/7"/>
<activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
</entry>
<entry>
<id>tag:kickass.zone,2016-10-10:objectId=4:objectType=Follow</id>
<published>2016-10-10T00:23:07Z</published>
<updated>2016-10-10T00:23:07Z</updated>
<title>localhost started following bignimbus@mastodon.social</title>
<content type="html">localhost started following bignimbus@mastodon.social</content>
<activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
<link rel="self" type="application/atom+xml" href="http://kickass.zone/users/localhost/updates/6.atom"/>
<link rel="alternate" type="text/html" href="http://kickass.zone/users/localhost/updates/6"/>
<activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
<activity:object>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<uri>https://mastodon.social/users/bignimbus</uri>
<name>bignimbus</name>
<email>bignimbus@mastodon.social</email>
<summary>jdauriemma.com</summary>
<link rel="alternate" type="text/html" href="https://mastodon.social/users/bignimbus"/>
<link rel="avatar" type="image/png" media:width="300" media:height="300" href="http://kickass.zone/system/accounts/avatars/000/000/004/large/jeff_avatar.png"/>
<link rel="avatar" type="image/png" media:width="96" media:height="96" href="http://kickass.zone/system/accounts/avatars/000/000/004/medium/jeff_avatar.png"/>
<link rel="avatar" type="image/png" media:width="48" media:height="48" href="http://kickass.zone/system/accounts/avatars/000/000/004/small/jeff_avatar.png"/>
<poco:preferredUsername>bignimbus</poco:preferredUsername>
<poco:displayName>Jeff Auriemma</poco:displayName>
<poco:note>jdauriemma.com</poco:note>
</activity:object>
</entry>
<entry>
<id>tag:kickass.zone,2016-10-10:objectId=2:objectType=Follow</id>
<published>2016-10-10T00:14:18Z</published>
<updated>2016-10-10T00:14:18Z</updated>
<title>localhost started following Gargron@mastodon.social</title>
<content type="html">localhost started following Gargron@mastodon.social</content>
<activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
<link rel="self" type="application/atom+xml" href="http://kickass.zone/users/localhost/updates/5.atom"/>
<link rel="alternate" type="text/html" href="http://kickass.zone/users/localhost/updates/5"/>
<activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
<activity:object>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<uri>https://mastodon.social/users/Gargron</uri>
<name>Gargron</name>
<email>Gargron@mastodon.social</email>
<summary>Developer of Mastodon, a GNU social alternative: https://github.com/Gargron/mastodon</summary>
<link rel="alternate" type="text/html" href="https://mastodon.social/users/Gargron"/>
<link rel="avatar" type="image/png" media:width="300" media:height="300" href="http://kickass.zone/system/accounts/avatars/000/000/003/large/4375_eugencommish.png"/>
<link rel="avatar" type="image/png" media:width="96" media:height="96" href="http://kickass.zone/system/accounts/avatars/000/000/003/medium/4375_eugencommish.png"/>
<link rel="avatar" type="image/png" media:width="48" media:height="48" href="http://kickass.zone/system/accounts/avatars/000/000/003/small/4375_eugencommish.png"/>
<poco:preferredUsername>Gargron</poco:preferredUsername>
<poco:displayName>Eugen</poco:displayName>
<poco:note>Developer of Mastodon, a GNU social alternative: https://github.com/Gargron/mastodon</poco:note>
</activity:object>
</entry>
<entry>
<id>tag:kickass.zone,2016-10-10:objectId=1:objectType=Follow</id>
<published>2016-10-10T00:09:09Z</published>
<updated>2016-10-10T00:09:09Z</updated>
<title>localhost started following abc@mastodon.social</title>
<content type="html">localhost started following abc@mastodon.social</content>
<activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
<link rel="self" type="application/atom+xml" href="http://kickass.zone/users/localhost/updates/4.atom"/>
<link rel="alternate" type="text/html" href="http://kickass.zone/users/localhost/updates/4"/>
<activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
<activity:object>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<uri>https://mastodon.social/users/abc</uri>
<name>abc</name>
<email>abc@mastodon.social</email>
<link rel="alternate" type="text/html" href="https://mastodon.social/users/abc"/>
<link rel="avatar" type="image/jpeg" media:width="300" media:height="300" href="http://kickass.zone/system/accounts/avatars/000/000/002/large/cbm64_80x80.jpg"/>
<link rel="avatar" type="image/jpeg" media:width="96" media:height="96" href="http://kickass.zone/system/accounts/avatars/000/000/002/medium/cbm64_80x80.jpg"/>
<link rel="avatar" type="image/jpeg" media:width="48" media:height="48" href="http://kickass.zone/system/accounts/avatars/000/000/002/small/cbm64_80x80.jpg"/>
<poco:preferredUsername>abc</poco:preferredUsername>
<poco:displayName>abc</poco:displayName>
</activity:object>
</entry>
<entry>
<id>tag:kickass.zone,2016-10-10:objectId=3:objectType=Status</id>
<published>2016-10-10T00:02:47Z</published>
<updated>2016-10-10T00:02:47Z</updated>
<title/>
<activity:verb>http://activitystrea.ms/schema/1.0/delete</activity:verb>
<link rel="self" type="application/atom+xml" href="http://kickass.zone/users/localhost/updates/3.atom"/>
<link rel="alternate" type="text/html" href="http://kickass.zone/users/localhost/updates/3"/>
<activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
</entry>
<entry>
<id>tag:kickass.zone,2016-10-10:objectId=2:objectType=Status</id>
<published>2016-10-10T00:02:18Z</published>
<updated>2016-10-10T00:02:18Z</updated>
<title>Yes, that was the obligatory first post. :)</title>
<content type="html">&lt;p&gt;Yes, that was the obligatory first post. :)&lt;/p&gt;</content>
<activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
<link rel="self" type="application/atom+xml" href="http://kickass.zone/users/localhost/updates/2.atom"/>
<link rel="alternate" type="text/html" href="http://kickass.zone/users/localhost/updates/2"/>
<activity:object-type>http://activitystrea.ms/schema/1.0/comment</activity:object-type>
<thr:in-reply-to ref="tag:kickass.zone,2016-10-10:objectId=1:objectType=Status" href="http://kickass.zone/users/localhost/updates/1" type="text/html"/>
</entry>
<entry>
<id>tag:kickass.zone,2016-10-10:objectId=1:objectType=Status</id>
<published>2016-10-10T00:01:56Z</published>
<updated>2016-10-10T00:01:56Z</updated>
<title>Hello, world!</title>
<content type="html">&lt;p&gt;Hello, world!&lt;/p&gt;</content>
<activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
<link rel="self" type="application/atom+xml" href="http://kickass.zone/users/localhost/updates/1.atom"/>
<link rel="alternate" type="text/html" href="http://kickass.zone/users/localhost/updates/1"/>
<activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
</entry>
</feed>

View File

@ -1,12 +1,50 @@
require 'rails_helper'
RSpec.describe ProcessFeedService do
let(:body) { File.read(File.join(Rails.root, 'spec', 'fixtures', 'xml', 'mastodon.atom')) }
let(:account) { Fabricate(:account, username: 'localhost', domain: 'kickass.zone') }
subject { ProcessFeedService.new }
it 'updates remote user\'s account information'
it 'creates posts'
it 'creates reblogs'
it 'creates replies'
it 'creates reblogged replies'
it 'deletes removed posts'
before do
stub_request(:get, "http://kickass.zone/system/accounts/avatars/000/000/001/large/eris.png").to_return(request_fixture('avatar.txt'))
stub_request(:get, "http://kickass.zone/system/media_attachments/files/000/000/002/original/morpheus_linux.jpg?1476059910").to_return(request_fixture('attachment1.txt'))
stub_request(:get, "http://kickass.zone/system/media_attachments/files/000/000/003/original/gizmo.jpg?1476060065").to_return(request_fixture('attachment2.txt'))
subject.call(body, account)
end
it 'updates remote user\'s account information' do
expect(account.display_name).to eq '::1'
expect(account).to have_attached_file(:avatar)
end
it 'creates posts' do
expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=1:objectType=Status')).to_not be_nil
expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=2:objectType=Status')).to_not be_nil
end
it 'ignores delete statuses unless they existed before' do
expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=3:objectType=Status')).to be_nil
expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=12:objectType=Status')).to be_nil
end
it 'does not create statuses for follows' do
expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=1:objectType=Follow')).to be_nil
expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=2:objectType=Follow')).to be_nil
expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=4:objectType=Follow')).to be_nil
expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=7:objectType=Follow')).to be_nil
end
it 'does not create statuses for favourites' do
expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=2:objectType=Favourite')).to be_nil
expect(Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=3:objectType=Favourite')).to be_nil
end
it 'creates posts with media' do
status = Status.find_by(uri: 'tag:kickass.zone,2016-10-10:objectId=14:objectType=Status')
expect(status).to_not be_nil
expect(status.media_attachments.first).to have_attached_file(:file)
end
end