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

receive_federation_messages_spec.rb « federation « integration « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b53bc97c1844812e7360b2a1c631dbdc452b18aa (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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# frozen_string_literal: true

require "integration/federation/federation_helper"
require "integration/federation/shared_receive_relayable"
require "integration/federation/shared_receive_retraction"
require "integration/federation/shared_receive_stream_items"

describe "Receive federation messages feature" do
  before do
    allow_callbacks(%i(queue_public_receive queue_private_receive receive_entity fetch_related_entity))
  end

  let(:sender) { remote_user_on_pod_b }
  let(:sender_id) { sender.diaspora_handle }

  context "with public receive" do
    let(:recipient) { nil }

    context "account deletion" do
      it "receives account deletion correctly" do
        post_message(generate_payload(DiasporaFederation::Entities::AccountDeletion.new(author: sender_id), sender))

        expect(AccountDeletion.exists?(person: sender.person)).to be_truthy
      end

      it "rejects account deletion with wrong author" do
        delete_id = Fabricate.sequence(:diaspora_id)
        expect {
          post_message(generate_payload(DiasporaFederation::Entities::AccountDeletion.new(author: delete_id), sender))
        }.not_to change(AccountDeletion, :count)
      end
    end

    context "account migration" do
      # In case when sender is unknown we should just ignore the migration
      # but this depends on https://github.com/diaspora/diaspora_federation/issues/72
      # which is low-priority, so we just discover the sender profile in this case.
      # But there won't be a spec for that.

      let(:entity) { create_account_migration_entity(sender_id, new_user) }

      def run_migration
        post_message(generate_payload(entity, sender))
      end

      context "with undiscovered new user profile" do
        before do
          allow_callbacks(%i[fetch_public_key])
          allow_private_key_fetch(new_user)
          expect_person_discovery(new_user)
        end

        let(:new_user) { create_undiscovered_user("example.org") }

        it "receives account migration correctly" do
          run_migration
          expect(AccountMigration.where(old_person: sender.person, new_person: new_user.person)).to exist
          expect(AccountMigration.find_by(old_person: sender.person, new_person: new_user.person)).to be_performed
        end

        it "doesn't run the same migration for the second time" do
          run_migration
          expect_any_instance_of(AccountMigration).not_to receive(:perform!)
          run_migration
          expect(AccountMigration.where(old_person: sender.person, new_person: new_user.person).count).to eq(1)
          expect(AccountMigration.find_by(old_person: sender.person, new_person: new_user.person)).to be_performed
        end

        it "doesn't accept second migration for the same sender" do
          run_migration
          expect {
            entity = create_account_migration_entity(sender_id, create_remote_user("example.org"))
            post_message(generate_payload(entity, sender))
          }.to raise_error(ActiveRecord::RecordInvalid)
        end

        it "doesn't accept second migration for the same new user profile" do
          run_migration
          expect {
            sender = create_remote_user("example.org")
            entity = create_account_migration_entity(sender.diaspora_handle, new_user)
            post_message(generate_payload(entity, sender))
          }.to raise_error(ActiveRecord::RecordInvalid)
        end

        context "when our pod was left" do
          let(:sender) { FactoryGirl.create(:user) }

          it "locks the old user account access" do
            run_migration
            expect(sender.reload.access_locked?).to be_truthy
          end
        end
      end

      context "with discovered profile" do
        let(:new_user) { create_remote_user("example.org") }

        it "updates person profile with data from entity" do
          new_user.profile.bio = "my updated biography"
          expect(entity.profile.bio).to eq("my updated biography")
          expect(new_user.profile.reload.bio).not_to eq("my updated biography")
          run_migration
          expect(new_user.profile.reload.bio).to eq("my updated biography")
        end
      end
    end

    context "reshare" do
      it "reshare of public post passes" do
        post = FactoryGirl.create(:status_message, author: alice.person, public: true)
        reshare = Fabricate(
          :reshare_entity, root_author: alice.diaspora_handle, root_guid: post.guid, author: sender_id)

        expect(Participation::Generator).to receive(:new).with(
          alice, instance_of(Reshare)
        ).and_return(double(create!: true))

        expect(Diaspora::Federation::Dispatcher).to receive(:build) do |_user, participation, _opts|
          expect(participation.target.guid).to eq(reshare.guid)
          instance_double(:dispatch)
        end

        post_message(generate_payload(reshare, sender))

        expect(Reshare.exists?(root_guid: post.guid)).to be_truthy
        expect(Reshare.where(root_guid: post.guid).last.diaspora_handle).to eq(sender_id)
      end

      it "reshare of private post fails" do
        post = FactoryGirl.create(:status_message, author: alice.person, public: false)
        reshare = Fabricate(
          :reshare_entity, root_author: alice.diaspora_handle, root_guid: post.guid, author: sender_id)
        expect {
          post_message(generate_payload(reshare, sender))
        }.to raise_error ActiveRecord::RecordInvalid, "Validation failed: Only posts which are public may be reshared."

        expect(Reshare.exists?(root_guid: post.guid)).to be_falsey
      end
    end

    it_behaves_like "messages which are indifferent about sharing fact"

    context "with sharing" do
      before do
        contact = alice.contacts.find_or_initialize_by(person_id: sender.person.id)
        contact.sharing = true
        contact.save
      end

      it_behaves_like "messages which are indifferent about sharing fact"
      it_behaves_like "messages which can't be send without sharing"
    end
  end

  context "with private receive" do
    let(:recipient) { alice }

    it "treats sharing request receive correctly" do
      entity = Fabricate(:contact_entity, author: sender_id, recipient: alice.diaspora_handle)

      expect(Workers::ReceiveLocal).to receive(:perform_async).and_call_original

      post_message(generate_payload(entity, sender, alice), alice)

      expect(alice.contacts.count).to eq(2)
      new_contact = alice.contacts.find {|c| c.person.diaspora_handle == sender_id }
      expect(new_contact).not_to be_nil
      expect(new_contact.sharing).to eq(true)

      expect(
        Notifications::StartedSharing.exists?(
          recipient_id: alice.id,
          target_type:  "Person",
          target_id:    sender.person.id
        )
      ).to be_truthy
    end

    context "with sharing" do
      before do
        contact = alice.contacts.find_or_initialize_by(person_id: sender.person.id)
        contact.sharing = true
        contact.save
      end

      it_behaves_like "messages which are indifferent about sharing fact"
      it_behaves_like "messages which can't be send without sharing"

      it "treats profile receive correctly" do
        entity = Fabricate(:profile_entity, author: sender_id)
        post_message(generate_payload(entity, sender, alice), alice)

        received_profile = sender.profile.reload

        expect(received_profile.first_name).to eq(entity.first_name)
        expect(received_profile.bio).to eq(entity.bio)
      end

      it "receives conversation correctly" do
        entity = Fabricate(
          :conversation_entity,
          author:       sender_id,
          participants: "#{sender_id};#{alice.diaspora_handle}"
        )
        post_message(generate_payload(entity, sender, alice), alice)

        expect(Conversation.exists?(guid: entity.guid)).to be_truthy
      end

      context "with message" do
        context "local" do
          let(:parent) {
            FactoryGirl.build(:conversation, author: alice.person).tap do |target|
              target.participants << remote_user_on_pod_b.person
              target.participants << remote_user_on_pod_c.person
              target.save
            end
          }
          let(:message) {
            Fabricate(
              :message_entity,
              conversation_guid: parent.guid,
              author:            sender_id,
              parent:            Diaspora::Federation::Entities.related_entity(parent)
            )
          }

          it "receives the message correctly" do
            expect(Workers::ReceiveLocal).to receive(:perform_async)
            post_message(generate_payload(message, sender, recipient), recipient)

            received_message = Message.find_by(guid: message.guid)
            expect(received_message).not_to be_nil
            expect(received_message.author.diaspora_handle).to eq(sender_id)
          end
        end

        context "remote" do
          let(:parent) {
            FactoryGirl.build(:conversation, author: remote_user_on_pod_b.person).tap do |target|
              target.participants << alice.person
              target.participants << remote_user_on_pod_c.person
              target.save
            end
          }
          let(:message) {
            Fabricate(
              :message_entity,
              conversation_guid: parent.guid,
              author:            remote_user_on_pod_c.diaspora_handle,
              parent:            Diaspora::Federation::Entities.related_entity(parent)
            )
          }

          it "receives the message correctly" do
            expect(Workers::ReceiveLocal).to receive(:perform_async)
            post_message(generate_payload(message, remote_user_on_pod_c, recipient), recipient)

            received_message = Message.find_by(guid: message.guid)
            expect(received_message).not_to be_nil
            expect(received_message.author.diaspora_handle).to eq(remote_user_on_pod_c.diaspora_handle)
          end
        end
      end
    end
  end
end