Welcome to mirror list, hosted at ThFree Co, Russian Federation.

activity_streams_serializer_spec.rb « activity_pub « serializers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c74beba7a81265a7442cf3abdc85c6cb98687dce (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ActivityPub::ActivityStreamsSerializer, feature_category: :integrations do
  let(:implementer_class) do
    Class.new(described_class) do
      include WithPagination
    end
  end

  let(:entity_class) do
    Class.new(Grape::Entity) do
      expose :id do |*|
        'https://example.com/unique/url'
      end

      expose :type do |*|
        'Person'
      end

      expose :name do |*|
        'Alice'
      end
    end
  end

  shared_examples_for 'ActivityStreams document' do
    it 'belongs to the ActivityStreams namespace' do
      expect(subject['@context']).to eq 'https://www.w3.org/ns/activitystreams'
    end

    it 'has a unique identifier' do
      expect(subject).to have_key 'id'
    end

    it 'has a type' do
      expect(subject).to have_key 'type'
    end
  end

  before do
    implementer_class.entity entity_class
  end

  context 'when the serializer is not paginated' do
    let(:resource) { build_stubbed(:release) }
    let(:outbox_url) { 'https://example.com/unique/url/outbox' }

    context 'with a valid represented entity' do
      subject { implementer_class.new.represent(resource, outbox: outbox_url) }

      it_behaves_like 'ActivityStreams document'

      it 'exposes an outbox' do
        expect(subject['outbox']).to eq 'https://example.com/unique/url/outbox'
      end

      it 'includes serialized data' do
        expect(subject['name']).to eq 'Alice'
      end
    end

    context 'when the represented entity provides no identifier' do
      subject { implementer_class.new.represent(resource, outbox: outbox_url) }

      before do
        allow(entity_class).to receive(:represent).and_return({ type: 'Person' })
      end

      it 'raises an exception' do
        expect { subject }.to raise_error(ActivityPub::ActivityStreamsSerializer::MissingIdentifierError)
      end
    end

    context 'when the represented entity provides no type' do
      subject { implementer_class.new.represent(resource, outbox: outbox_url) }

      before do
        allow(entity_class).to receive(:represent).and_return({ id: 'https://example.com/unique/url' })
      end

      it 'raises an exception' do
        expect { subject }.to raise_error(ActivityPub::ActivityStreamsSerializer::MissingTypeError)
      end
    end

    context 'when the caller provides no outbox parameter' do
      subject { implementer_class.new.represent(resource) }

      it 'raises an exception' do
        expect { subject }.to raise_error(ActivityPub::ActivityStreamsSerializer::MissingOutboxError)
      end
    end
  end

  context 'when the serializer is paginated' do
    let(:resources) { build_stubbed_list(:release, 3) }
    let(:request) { ActionDispatch::Request.new(request_data) }
    let(:response) { ActionDispatch::Response.new }
    let(:url) { 'https://example.com/resource/url' }
    let(:decorated) { implementer_class.new.with_pagination(request, response) }

    before do
      allow(resources).to receive(:page).and_return(resources)
      allow(resources).to receive(:per).and_return(resources)
      allow(resources).to receive(:current_page).and_return(2)
      allow(resources).to receive(:total_pages).and_return(3)
      allow(resources).to receive(:total_count).and_return(10)
      allow(decorated.paginator).to receive(:paginate).and_return(resources)
    end

    context 'when no page parameter is provided' do
      subject { decorated.represent(resources) }

      let(:request_data) do
        { "rack.url_scheme" => "https", "HTTP_HOST" => "example.com", "PATH_INFO" => '/resource/url' }
      end

      it_behaves_like 'ActivityStreams document'

      it 'is an index document for the pagination' do
        expect(subject['type']).to eq 'OrderedCollection'
      end

      it 'contains the total amount of items' do
        expect(subject['totalItems']).to eq 10
      end

      it 'contains links to first and last page' do
        expect(subject['first']).to eq "#{url}?page=1"
        expect(subject['last']).to eq "#{url}?page=3"
      end
    end

    context 'when a page parameter is provided' do
      subject { decorated.represent(resources) }

      let(:request_data) do
        { 'rack.url_scheme' => 'https', 'HTTP_HOST' => 'example.com', 'PATH_INFO' => '/resource/url',
          'QUERY_STRING' => 'page=2&per_page=1' }
      end

      it_behaves_like 'ActivityStreams document'

      it 'is a page document' do
        expect(subject['type']).to eq 'OrderedCollectionPage'
      end

      it 'contains navigation links' do
        expect(subject['prev']).to be_present
        expect(subject['next']).to be_present
        expect(subject['partOf']).to be_present
      end
    end
  end
end