File: activity_serializer_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (138 lines) | stat: -rw-r--r-- 3,639 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

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

  let(:serializer) { implementer_class.new.represent(resource) }

  let(:resource) { build_stubbed(:release) }

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

      expose :type do |*|
        'Follow'
      end

      expose :actor do |*|
        'https://example.com/actor/alice'
      end

      expose :object do |*|
        'https://example.com/actor/bob'
      end
    end
  end

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

      expose :type do |*|
        'Question'
      end

      expose :actor do |*|
        'https://example.com/actor/alice'
      end

      expose :content do |*|
        "What's up?"
      end
    end
  end

  let(:entity_class) { transitive_entity_class }

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

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

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

    it 'has an actor' do
      expect(serializer['actor']).to eq 'https://example.com/actor/alice'
    end
  end

  before do
    implementer_class.entity entity_class
  end

  context 'with a valid represented entity' do
    it_behaves_like 'activity document'
  end

  context 'when the represented entity provides no identifier' do
    before do
      allow(entity_class).to receive(:represent).and_return({ type: 'Person', actor: 'http://something/' })
    end

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

  context 'when the represented entity provides no type' do
    before do
      allow(entity_class).to receive(:represent).and_return({
        id: 'http://something/',
        actor: 'http://something-else/'
      })
    end

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

  context 'when the represented entity provides no actor' do
    before do
      allow(entity_class).to receive(:represent).and_return({ id: 'http://something/', type: 'Person' })
    end

    it 'raises an exception' do
      expect { serializer }.to raise_error(ActivityPub::ActivitySerializer::MissingActorError)
    end
  end

  context 'when the represented entity provides no object' do
    let(:entity_class) { intransitive_entity_class }

    context 'when the caller provides the :intransitive option' do
      let(:serializer) { implementer_class.new.represent(resource, intransitive: true) }

      it_behaves_like 'activity document'
    end

    context 'when the caller does not provide the :intransitive option' do
      it 'raises an exception' do
        expect { serializer }.to raise_error(ActivityPub::ActivitySerializer::MissingObjectError)
      end
    end
  end

  context 'when the caller does provide the :intransitive option and an object' do
    let(:serializer) { implementer_class.new.represent(resource, intransitive: true) }

    it 'raises an exception' do
      expect { serializer }.to raise_error(ActivityPub::ActivitySerializer::IntransitiveWithObjectError)
    end
  end
end