about summary refs log tree commit diff
path: root/spec/services/activitypub/fetch_featured_tags_collection_service_spec.rb
blob: 6ca22c9fc66e61a4698bf75c980949f77bb6e3cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require 'rails_helper'

RSpec.describe ActivityPub::FetchFeaturedTagsCollectionService, type: :service do
  let(:collection_url) { 'https://example.com/account/tags' }
  let(:actor) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/account') }

  let(:items) do
    [
      { type: 'Hashtag', href: 'https://example.com/account/tagged/foo', name: 'Foo' },
      { type: 'Hashtag', href: 'https://example.com/account/tagged/bar', name: 'bar' },
      { type: 'Hashtag', href: 'https://example.com/account/tagged/baz', name: 'baZ' },
    ]
  end

  let(:payload) do
    {
      '@context': 'https://www.w3.org/ns/activitystreams',
      type: 'Collection',
      id: collection_url,
      items: items,
    }.with_indifferent_access
  end

  subject { described_class.new }

  shared_examples 'sets featured tags' do
    before do
      subject.call(actor, collection_url)
    end

    it 'sets expected tags as pinned tags' do
      expect(actor.featured_tags.map(&:display_name)).to match_array ['Foo', 'bar', 'baZ']
    end
  end

  describe '#call' do
    context 'when the endpoint is a Collection' do
      before do
        stub_request(:get, collection_url).to_return(status: 200, body: Oj.dump(payload))
      end

      it_behaves_like 'sets featured tags'
    end

    context 'when the account already has featured tags' do
      before do
        stub_request(:get, collection_url).to_return(status: 200, body: Oj.dump(payload))

        actor.featured_tags.create!(name: 'FoO')
        actor.featured_tags.create!(name: 'baz')
        actor.featured_tags.create!(name: 'oh').update(name: nil)
      end

      it_behaves_like 'sets featured tags'
    end

    context 'when the endpoint is an OrderedCollection' do
      let(:payload) do
        {
          '@context': 'https://www.w3.org/ns/activitystreams',
          type: 'OrderedCollection',
          id: collection_url,
          orderedItems: items,
        }.with_indifferent_access
      end

      before do
        stub_request(:get, collection_url).to_return(status: 200, body: Oj.dump(payload))
      end

      it_behaves_like 'sets featured tags'
    end

    context 'when the endpoint is a paginated Collection' do
      let(:payload) do
        {
          '@context': 'https://www.w3.org/ns/activitystreams',
          type: 'Collection',
          id: collection_url,
          first: {
            type: 'CollectionPage',
            partOf: collection_url,
            items: items,
          }
        }.with_indifferent_access
      end

      before do
        stub_request(:get, collection_url).to_return(status: 200, body: Oj.dump(payload))
      end

      it_behaves_like 'sets featured tags'
    end
  end
end