diff options
author | Jenkins <jenkins@jenkins.ninjawedding.org> | 2018-02-11 04:10:16 +0000 |
---|---|---|
committer | Jenkins <jenkins@jenkins.ninjawedding.org> | 2018-02-11 04:10:16 +0000 |
commit | efc9ffcb80b995ef8c8d95ab1d8cb0af8efc094d (patch) | |
tree | 6ce57e5980ba0279355f97893d21852caace0232 /spec | |
parent | b6159bcb0a20f3aabfb4d17ad60842b7aa81925c (diff) | |
parent | 718802a05dfb3211d758513daf6070ffa22751dd (diff) |
Merge remote-tracking branch 'tootsuite/master' into glitchsoc/master
Diffstat (limited to 'spec')
-rw-r--r-- | spec/fixtures/requests/webfinger-hacker3.txt | 11 | ||||
-rw-r--r-- | spec/services/fetch_atom_service_spec.rb | 102 | ||||
-rw-r--r-- | spec/services/fetch_remote_account_service_spec.rb | 67 | ||||
-rw-r--r-- | spec/spec_helper.rb | 4 |
4 files changed, 146 insertions, 38 deletions
diff --git a/spec/fixtures/requests/webfinger-hacker3.txt b/spec/fixtures/requests/webfinger-hacker3.txt new file mode 100644 index 000000000..c59f518bd --- /dev/null +++ b/spec/fixtures/requests/webfinger-hacker3.txt @@ -0,0 +1,11 @@ +HTTP/1.1 200 OK +Server: nginx/1.6.2 +Date: Sun, 20 Mar 2016 11:13:16 GMT +Content-Type: application/jrd+json +Transfer-Encoding: chunked +Connection: keep-alive +Access-Control-Allow-Origin: * +Vary: Accept-Encoding,Cookie +Strict-Transport-Security: max-age=31536000; includeSubdomains; + +{"subject":"acct:localhost@kickass.zone","aliases":["https:\/\/kickass.zone\/user\/7477","https:\/\/kickass.zone\/gargron","https:\/\/kickass.zone\/index.php\/user\/7477","https:\/\/kickass.zone\/index.php\/gargron"],"links":[{"rel":"http:\/\/webfinger.net\/rel\/profile-page","type":"text\/html","href":"https:\/\/kickass.zone\/gargron"},{"rel":"http:\/\/gmpg.org\/xfn\/11","type":"text\/html","href":"https:\/\/kickass.zone\/gargron"},{"rel":"describedby","type":"application\/rdf+xml","href":"https:\/\/kickass.zone\/gargron\/foaf"},{"rel":"http:\/\/apinamespace.org\/atom","type":"application\/atomsvc+xml","href":"https:\/\/kickass.zone\/api\/statusnet\/app\/service\/gargron.xml"},{"rel":"http:\/\/apinamespace.org\/twitter","href":"https:\/\/kickass.zone\/api\/"},{"rel":"http:\/\/specs.openid.net\/auth\/2.0\/provider","href":"https:\/\/kickass.zone\/gargron"},{"rel":"http:\/\/schemas.google.com\/g\/2010#updates-from","type":"application\/atom+xml","href":"https:\/\/kickass.zone\/api\/statuses\/user_timeline\/7477.atom"},{"rel":"magic-public-key","href":"data:application\/magic-public-key,RSA.1ZBkHTavLvxH3FzlKv4O6WtlILKRFfNami3_Rcu8EuogtXSYiS-bB6hElZfUCSHbC4uLemOA34PEhz__CDMozax1iI_t8dzjDnh1x0iFSup7pSfW9iXk_WU3Dm74yWWW2jildY41vWgrEstuQ1dJ8vVFfSJ9T_tO4c-T9y8vDI8=.AQAB"},{"rel":"salmon","href":"https:\/\/kickass.zone\/main\/salmon\/user\/7477"},{"rel":"http:\/\/salmon-protocol.org\/ns\/salmon-replies","href":"https:\/\/kickass.zone\/main\/salmon\/user\/7477"},{"rel":"http:\/\/salmon-protocol.org\/ns\/salmon-mention","href":"https:\/\/kickass.zone\/main\/salmon\/user\/7477"},{"rel":"http:\/\/ostatus.org\/schema\/1.0\/subscribe","template":"https:\/\/kickass.zone\/main\/ostatussub?profile={uri}"}]} diff --git a/spec/services/fetch_atom_service_spec.rb b/spec/services/fetch_atom_service_spec.rb index d7f162b85..2bd127e92 100644 --- a/spec/services/fetch_atom_service_spec.rb +++ b/spec/services/fetch_atom_service_spec.rb @@ -1,62 +1,88 @@ require 'rails_helper' RSpec.describe FetchAtomService do - describe '#link_header' do - context 'Link is Array' do - target = FetchAtomService.new - target.instance_variable_set('@response', 'Link' => [ - '<http://example.com/>; rel="up"; meta="bar"', - '<http://example.com/foo>; rel="self"', - ]) + describe '#call' do + let(:url) { 'http://example.com' } + subject { FetchAtomService.new.call(url) } + + context 'url is blank' do + let(:url) { '' } + it { is_expected.to be_nil } + end - it 'set first link as link_header' do - expect(target.send(:link_header).links[0].href).to eq 'http://example.com/' + context 'request failed' do + before do + WebMock.stub_request(:get, url).to_return(status: 500, body: '', headers: {}) end + + it { is_expected.to be_nil } end - context 'Link is not Array' do - target = FetchAtomService.new - target.instance_variable_set('@response', 'Link' => '<http://example.com/foo>; rel="self", <http://example.com/>; rel = "up"') + context 'raise OpenSSL::SSL::SSLError' do + before do + allow(Request).to receive_message_chain(:new, :add_headers, :perform).and_raise(OpenSSL::SSL::SSLError) + end - it { expect(target.send(:link_header).links[0].href).to eq 'http://example.com/foo' } + it 'output log and return nil' do + expect_any_instance_of(ActiveSupport::Logger).to receive(:debug).with('SSL error: OpenSSL::SSL::SSLError') + is_expected.to be_nil + end end - end - describe '#perform_request' do - let(:url) { 'http://example.com' } - context 'Check method result' do + context 'raise HTTP::ConnectionError' do before do - WebMock.stub_request(:get, url).to_return(status: 200, body: '', headers: {}) - @target = FetchAtomService.new - @target.instance_variable_set('@url', url) + allow(Request).to receive_message_chain(:new, :add_headers, :perform).and_raise(HTTP::ConnectionError) end - it 'HTTP::Response instance is returned and set to @response' do - expect(@target.send(:perform_request).status.to_s).to eq '200 OK' - expect(@target.instance_variable_get('@response')).to be_instance_of HTTP::Response + it 'output log and return nil' do + expect_any_instance_of(ActiveSupport::Logger).to receive(:debug).with('HTTP ConnectionError: HTTP::ConnectionError') + is_expected.to be_nil end end - context 'check passed parameters to Request' do + context 'response success' do + let(:body) { '' } + let(:headers) { { 'Content-Type' => content_type } } + let(:json) { + { id: 1, + '@context': ActivityPub::TagManager::CONTEXT, + type: 'Note', + }.to_json + } + before do - @target = FetchAtomService.new - @target.instance_variable_set('@url', url) - @target.instance_variable_set('@unsupported_activity', unsupported_activity) - allow(Request).to receive(:new).with(:get, url) - expect(Request).to receive_message_chain(:new, :add_headers).with('Accept' => accept) - allow(Request).to receive_message_chain(:new, :add_headers, :perform).with(no_args) + WebMock.stub_request(:get, url).to_return(status: 200, body: body, headers: headers) end - context '@unsupported_activity is true' do - let(:unsupported_activity) { true } - let(:accept) { 'text/html' } - it { @target.send(:perform_request) } + context 'content type is application/atom+xml' do + let(:content_type) { 'application/atom+xml' } + + it { is_expected.to eq [url, {:prefetched_body=>""}, :ostatus] } + end + + context 'content_type is json' do + let(:content_type) { 'application/activity+json' } + let(:body) { json } + + it { is_expected.to eq [1, { prefetched_body: body, id: true }, :activitypub] } end - context '@unsupported_activity is false' do - let(:unsupported_activity) { false } - let(:accept) { 'application/activity+json, application/ld+json, application/atom+xml, text/html' } - it { @target.send(:perform_request) } + before do + WebMock.stub_request(:get, url).to_return(status: 200, body: body, headers: headers) + WebMock.stub_request(:get, 'http://example.com/foo').to_return(status: 200, body: json, headers: { 'Content-Type' => 'application/activity+json' }) + end + + context 'has link header' do + let(:headers) { { 'Link' => '<http://example.com/foo>; rel="alternate"; type="application/activity+json"', } } + + it { is_expected.to eq [1, { prefetched_body: json, id: true }, :activitypub] } + end + + context 'content type is text/html' do + let(:content_type) { 'text/html' } + let(:body) { '<html><head><link rel="alternate" href="http://example.com/foo" type="application/activity+json"/></head></html>' } + + it { is_expected.to eq [1, { prefetched_body: json, id: true }, :activitypub] } end end end diff --git a/spec/services/fetch_remote_account_service_spec.rb b/spec/services/fetch_remote_account_service_spec.rb index bb1877c7a..4388d4cf4 100644 --- a/spec/services/fetch_remote_account_service_spec.rb +++ b/spec/services/fetch_remote_account_service_spec.rb @@ -1,4 +1,71 @@ require 'rails_helper' RSpec.describe FetchRemoteAccountService do + let(:url) { 'https://example.com' } + let(:prefetched_body) { nil } + let(:protocol) { :ostatus } + subject { FetchRemoteAccountService.new.call(url, prefetched_body, protocol) } + + let(:actor) do + { + '@context': 'https://www.w3.org/ns/activitystreams', + id: 'https://example.com/alice', + type: 'Person', + preferredUsername: 'alice', + name: 'Alice', + summary: 'Foo bar', + inbox: 'http://example.com/alice/inbox', + } + end + + let(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice' }] } } + let(:xml) { File.read(File.join(Rails.root, 'spec', 'fixtures', 'xml', 'mastodon.atom')) } + + shared_examples 'return Account' do + it { is_expected.to be_an Account } + end + + context 'protocol is :activitypub' do + let(:prefetched_body) { Oj.dump(actor) } + let(:protocol) { :activitypub } + + before do + stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' }) + end + + include_examples 'return Account' + end + + context 'protocol is :ostatus' do + let(:prefetched_body) { xml } + let(:protocol) { :ostatus } + + before do + stub_request(:get, "https://kickass.zone/.well-known/webfinger?resource=acct:localhost@kickass.zone").to_return(request_fixture('webfinger-hacker3.txt')) + stub_request(:get, "https://kickass.zone/api/statuses/user_timeline/7477.atom").to_return(request_fixture('feed.txt')) + end + + include_examples 'return Account' + end + + context 'when prefetched_body is nil' do + context 'protocol is :activitypub' do + before do + stub_request(:get, url).to_return(status: 200, body: Oj.dump(actor), headers: { 'Content-Type' => 'application/activity+json' }) + stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' }) + end + + include_examples 'return Account' + end + + context 'protocol is :ostatus' do + before do + stub_request(:get, url).to_return(status: 200, body: xml, headers: { 'Content-Type' => 'application/atom+xml' }) + stub_request(:get, "https://kickass.zone/.well-known/webfinger?resource=acct:localhost@kickass.zone").to_return(request_fixture('webfinger-hacker3.txt')) + stub_request(:get, "https://kickass.zone/api/statuses/user_timeline/7477.atom").to_return(request_fixture('feed.txt')) + end + + include_examples 'return Account' + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index eecaec4ac..a0466dd4b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -25,6 +25,10 @@ RSpec.configure do |config| end end + config.before :suite do + Chewy.strategy(:bypass) + end + config.after :suite do gc_counter = 0 FileUtils.rm_rf(Dir["#{Rails.root}/spec/test_files/"]) |