fetch_featured_collection_service_spec.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. require 'rails_helper'
  2. RSpec.describe ActivityPub::FetchFeaturedCollectionService, type: :service do
  3. let(:actor) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/account', featured_collection_url: 'https://example.com/account/pinned') }
  4. let!(:known_status) { Fabricate(:status, account: actor, uri: 'https://example.com/account/pinned/1') }
  5. let(:status_json_1) do
  6. {
  7. '@context': 'https://www.w3.org/ns/activitystreams',
  8. type: 'Note',
  9. id: 'https://example.com/account/pinned/1',
  10. content: 'foo',
  11. attributedTo: actor.uri,
  12. to: 'https://www.w3.org/ns/activitystreams#Public',
  13. }
  14. end
  15. let(:status_json_2) do
  16. {
  17. '@context': 'https://www.w3.org/ns/activitystreams',
  18. type: 'Note',
  19. id: 'https://example.com/account/pinned/2',
  20. content: 'foo',
  21. attributedTo: actor.uri,
  22. to: 'https://www.w3.org/ns/activitystreams#Public',
  23. }
  24. end
  25. let(:status_json_4) do
  26. {
  27. '@context': 'https://www.w3.org/ns/activitystreams',
  28. type: 'Note',
  29. id: 'https://example.com/account/pinned/4',
  30. content: 'foo',
  31. attributedTo: actor.uri,
  32. to: 'https://www.w3.org/ns/activitystreams#Public',
  33. }
  34. end
  35. let(:items) do
  36. [
  37. 'https://example.com/account/pinned/1', # known
  38. status_json_2, # unknown inlined
  39. 'https://example.com/account/pinned/3', # unknown unreachable
  40. 'https://example.com/account/pinned/4', # unknown reachable
  41. ]
  42. end
  43. let(:payload) do
  44. {
  45. '@context': 'https://www.w3.org/ns/activitystreams',
  46. type: 'Collection',
  47. id: actor.featured_collection_url,
  48. items: items,
  49. }.with_indifferent_access
  50. end
  51. subject { described_class.new }
  52. shared_examples 'sets pinned posts' do
  53. before do
  54. stub_request(:get, 'https://example.com/account/pinned/1').to_return(status: 200, body: Oj.dump(status_json_1), headers: { 'Content-Type': 'application/activity+json' })
  55. stub_request(:get, 'https://example.com/account/pinned/2').to_return(status: 200, body: Oj.dump(status_json_2), headers: { 'Content-Type': 'application/activity+json' })
  56. stub_request(:get, 'https://example.com/account/pinned/3').to_return(status: 404)
  57. stub_request(:get, 'https://example.com/account/pinned/4').to_return(status: 200, body: Oj.dump(status_json_4), headers: { 'Content-Type': 'application/activity+json' })
  58. subject.call(actor)
  59. end
  60. it 'sets expected posts as pinned posts' do
  61. expect(actor.pinned_statuses.pluck(:uri)).to match_array ['https://example.com/account/pinned/1', 'https://example.com/account/pinned/2', 'https://example.com/account/pinned/4']
  62. end
  63. end
  64. describe '#call' do
  65. context 'when the endpoint is a Collection' do
  66. before do
  67. stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  68. end
  69. it_behaves_like 'sets pinned posts'
  70. end
  71. context 'when the endpoint is an OrderedCollection' do
  72. let(:payload) do
  73. {
  74. '@context': 'https://www.w3.org/ns/activitystreams',
  75. type: 'OrderedCollection',
  76. id: actor.featured_collection_url,
  77. orderedItems: items,
  78. }.with_indifferent_access
  79. end
  80. before do
  81. stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  82. end
  83. it_behaves_like 'sets pinned posts'
  84. end
  85. context 'when the endpoint is a paginated Collection' do
  86. let(:payload) do
  87. {
  88. '@context': 'https://www.w3.org/ns/activitystreams',
  89. type: 'Collection',
  90. id: actor.featured_collection_url,
  91. first: {
  92. type: 'CollectionPage',
  93. partOf: actor.featured_collection_url,
  94. items: items,
  95. }
  96. }.with_indifferent_access
  97. end
  98. before do
  99. stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  100. end
  101. it_behaves_like 'sets pinned posts'
  102. end
  103. end
  104. end