dereferencer_spec.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ActivityPub::Dereferencer do
  4. describe '#object' do
  5. subject { described_class.new(uri, permitted_origin: permitted_origin, signature_actor: signature_actor).object }
  6. let(:object) { { '@context': 'https://www.w3.org/ns/activitystreams', id: 'https://example.com/foo', type: 'Note', content: 'Hoge' } }
  7. let(:permitted_origin) { 'https://example.com' }
  8. let(:signature_actor) { nil }
  9. let(:uri) { nil }
  10. before do
  11. stub_request(:get, 'https://example.com/foo').to_return(body: Oj.dump(object), headers: { 'Content-Type' => 'application/activity+json' })
  12. end
  13. context 'with a URI' do
  14. let(:uri) { 'https://example.com/foo' }
  15. it 'returns object' do
  16. expect(subject.with_indifferent_access).to eq object.with_indifferent_access
  17. end
  18. context 'with signature account' do
  19. let(:signature_actor) { Fabricate(:account) }
  20. it 'makes signed request' do
  21. subject
  22. expect(a_request(:get, 'https://example.com/foo').with { |req| req.headers['Signature'].present? }).to have_been_made
  23. end
  24. end
  25. context 'with different origin' do
  26. let(:uri) { 'https://other-example.com/foo' }
  27. it 'does not make request' do
  28. subject
  29. expect(a_request(:get, 'https://other-example.com/foo')).to_not have_been_made
  30. end
  31. end
  32. end
  33. context 'with a bearcap' do
  34. let(:uri) { 'bear:?t=hoge&u=https://example.com/foo' }
  35. it 'makes request with Authorization header' do
  36. subject
  37. expect(a_request(:get, 'https://example.com/foo').with(headers: { 'Authorization' => 'Bearer hoge' })).to have_been_made
  38. end
  39. it 'returns object' do
  40. expect(subject.with_indifferent_access).to eq object.with_indifferent_access
  41. end
  42. context 'with signature account' do
  43. let(:signature_actor) { Fabricate(:account) }
  44. it 'makes signed request' do
  45. subject
  46. expect(a_request(:get, 'https://example.com/foo').with { |req| req.headers['Signature'].present? && req.headers['Authorization'] == 'Bearer hoge' }).to have_been_made
  47. end
  48. end
  49. context 'with different origin' do
  50. let(:uri) { 'bear:?t=hoge&u=https://other-example.com/foo' }
  51. it 'does not make request' do
  52. subject
  53. expect(a_request(:get, 'https://other-example.com/foo')).to_not have_been_made
  54. end
  55. end
  56. end
  57. end
  58. end