fetch_featured_tags_collection_service_spec.rb 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. require 'rails_helper'
  2. RSpec.describe ActivityPub::FetchFeaturedTagsCollectionService, type: :service do
  3. let(:collection_url) { 'https://example.com/account/tags' }
  4. let(:actor) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/account') }
  5. let(:items) do
  6. [
  7. { type: 'Hashtag', href: 'https://example.com/account/tagged/foo', name: 'Foo' },
  8. { type: 'Hashtag', href: 'https://example.com/account/tagged/bar', name: 'bar' },
  9. { type: 'Hashtag', href: 'https://example.com/account/tagged/baz', name: 'baZ' },
  10. ]
  11. end
  12. let(:payload) do
  13. {
  14. '@context': 'https://www.w3.org/ns/activitystreams',
  15. type: 'Collection',
  16. id: collection_url,
  17. items: items,
  18. }.with_indifferent_access
  19. end
  20. subject { described_class.new }
  21. shared_examples 'sets featured tags' do
  22. before do
  23. subject.call(actor, collection_url)
  24. end
  25. it 'sets expected tags as pinned tags' do
  26. expect(actor.featured_tags.map(&:display_name)).to match_array ['Foo', 'bar', 'baZ']
  27. end
  28. end
  29. describe '#call' do
  30. context 'when the endpoint is a Collection' do
  31. before do
  32. stub_request(:get, collection_url).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  33. end
  34. it_behaves_like 'sets featured tags'
  35. end
  36. context 'when the account already has featured tags' do
  37. before do
  38. stub_request(:get, collection_url).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  39. actor.featured_tags.create!(name: 'FoO')
  40. actor.featured_tags.create!(name: 'baz')
  41. actor.featured_tags.create!(name: 'oh').update(name: nil)
  42. end
  43. it_behaves_like 'sets featured tags'
  44. end
  45. context 'when the endpoint is an OrderedCollection' do
  46. let(:payload) do
  47. {
  48. '@context': 'https://www.w3.org/ns/activitystreams',
  49. type: 'OrderedCollection',
  50. id: collection_url,
  51. orderedItems: items,
  52. }.with_indifferent_access
  53. end
  54. before do
  55. stub_request(:get, collection_url).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  56. end
  57. it_behaves_like 'sets featured tags'
  58. end
  59. context 'when the endpoint is a paginated Collection' do
  60. let(:payload) do
  61. {
  62. '@context': 'https://www.w3.org/ns/activitystreams',
  63. type: 'Collection',
  64. id: collection_url,
  65. first: {
  66. type: 'CollectionPage',
  67. partOf: collection_url,
  68. items: items,
  69. }
  70. }.with_indifferent_access
  71. end
  72. before do
  73. stub_request(:get, collection_url).to_return(status: 200, body: Oj.dump(payload), headers: { 'Content-Type': 'application/activity+json' })
  74. end
  75. it_behaves_like 'sets featured tags'
  76. end
  77. end
  78. end