about summary refs log tree commit diff
path: root/spec/lib/activitypub/activity/create_spec.rb
blob: fcb044ebcbfd9d19de9bffc6612e62b3b7cf3091 (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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
require 'rails_helper'

RSpec.describe ActivityPub::Activity::Create do
  let(:sender) { Fabricate(:account, followers_url: 'http://example.com/followers') }

  let(:json) do
    {
      '@context': 'https://www.w3.org/ns/activitystreams',
      id: 'foo',
      type: 'Create',
      actor: ActivityPub::TagManager.instance.uri_for(sender),
      object: object_json,
    }.with_indifferent_access
  end

  subject { described_class.new(json, sender) }

  before do
    stub_request(:get, 'http://example.com/attachment.png').to_return(request_fixture('avatar.txt'))
  end

  describe '#perform' do
    before do
      subject.perform
    end

    context 'standalone' do
      let(:object_json) do
        {
          id: 'bar',
          type: 'Note',
          content: 'Lorem ipsum',
        }
      end

      it 'creates status' do
        status = sender.statuses.first

        expect(status).to_not be_nil
        expect(status.text).to eq 'Lorem ipsum'
      end

      it 'missing to/cc defaults to direct privacy' do
        status = sender.statuses.first

        expect(status).to_not be_nil
        expect(status.visibility).to eq 'direct'
      end
    end

    context 'public' do
      let(:object_json) do
        {
          id: 'bar',
          type: 'Note',
          content: 'Lorem ipsum',
          to: 'https://www.w3.org/ns/activitystreams#Public',
        }
      end

      it 'creates status' do
        status = sender.statuses.first

        expect(status).to_not be_nil
        expect(status.visibility).to eq 'public'
      end
    end

    context 'unlisted' do
      let(:object_json) do
        {
          id: 'bar',
          type: 'Note',
          content: 'Lorem ipsum',
          cc: 'https://www.w3.org/ns/activitystreams#Public',
        }
      end

      it 'creates status' do
        status = sender.statuses.first

        expect(status).to_not be_nil
        expect(status.visibility).to eq 'unlisted'
      end
    end

    context 'private' do
      let(:object_json) do
        {
          id: 'bar',
          type: 'Note',
          content: 'Lorem ipsum',
          to: 'http://example.com/followers',
        }
      end

      it 'creates status' do
        status = sender.statuses.first

        expect(status).to_not be_nil
        expect(status.visibility).to eq 'private'
      end
    end

    context 'direct' do
      let(:recipient) { Fabricate(:account) }

      let(:object_json) do
        {
          id: 'bar',
          type: 'Note',
          content: 'Lorem ipsum',
          to: ActivityPub::TagManager.instance.uri_for(recipient),
        }
      end

      it 'creates status' do
        status = sender.statuses.first

        expect(status).to_not be_nil
        expect(status.visibility).to eq 'direct'
      end
    end

    context 'as a reply' do
      let(:original_status) { Fabricate(:status) }

      let(:object_json) do
        {
          id: 'bar',
          type: 'Note',
          content: 'Lorem ipsum',
          inReplyTo: ActivityPub::TagManager.instance.uri_for(original_status),
        }
      end

      it 'creates status' do
        status = sender.statuses.first

        expect(status).to_not be_nil
        expect(status.thread).to eq original_status
        expect(status.reply?).to be true
        expect(status.in_reply_to_account).to eq original_status.account
        expect(status.conversation).to eq original_status.conversation
      end
    end

    context 'with mentions' do
      let(:recipient) { Fabricate(:account) }

      let(:object_json) do
        {
          id: 'bar',
          type: 'Note',
          content: 'Lorem ipsum',
          tag: [
            {
              type: 'Mention',
              href: ActivityPub::TagManager.instance.uri_for(recipient),
            },
          ],
        }
      end

      it 'creates status' do
        status = sender.statuses.first

        expect(status).to_not be_nil
        expect(status.mentions.map(&:account)).to include(recipient)
      end
    end

    context 'with media attachments' do
      let(:object_json) do
        {
          id: 'bar',
          type: 'Note',
          content: 'Lorem ipsum',
          attachment: [
            {
              type: 'Document',
              mime_type: 'image/png',
              url: 'http://example.com/attachment.png',
            },
          ],
        }
      end

      it 'creates status' do
        status = sender.statuses.first

        expect(status).to_not be_nil
        expect(status.media_attachments.map(&:remote_url)).to include('http://example.com/attachment.png')
      end
    end

    context 'with hashtags' do
      let(:object_json) do
        {
          id: 'bar',
          type: 'Note',
          content: 'Lorem ipsum',
          tag: [
            {
              type: 'Hashtag',
              href: 'http://example.com/blah',
              name: '#test',
            },
          ],
        }
      end

      it 'creates status' do
        status = sender.statuses.first

        expect(status).to_not be_nil
        expect(status.tags.map(&:name)).to include('test')
      end
    end
  end
end