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

shared_receive_stream_items.rb « federation « integration « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 347848e356f8469872adb5ebc435a4067322531d (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
# frozen_string_literal: true

# by "stream items" we mean everything that could appear in the stream - post, comment, like, poll, etc and therefore
# could be send either publicly or privately
shared_examples_for "messages which are indifferent about sharing fact" do
  let(:public) { recipient.nil? }

  it "treats status message receive correctly" do
    entity = Fabricate(:status_message_entity, author: sender_id, public: public)

    post_message(generate_payload(entity, sender, recipient), recipient)

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

  it "doesn't accept status message with wrong signature" do
    allow(sender).to receive(:encryption_key).and_return(OpenSSL::PKey::RSA.new(1024))
    entity = Fabricate(:status_message_entity, author: sender_id, public: public)

    post_message(generate_payload(entity, sender, recipient), recipient)

    expect(StatusMessage.exists?(guid: entity.guid)).to be_falsey
  end

  describe "with messages which require a status to operate on" do
    let(:local_parent) { FactoryGirl.create(:status_message, author: alice.person, public: public) }
    let(:remote_parent) { FactoryGirl.create(:status_message, author: remote_user_on_pod_b.person, public: public) }

    describe "notifications are sent where required" do
      it "for comment on local post" do
        entity = create_relayable_entity(:comment_entity, local_parent, remote_user_on_pod_b.diaspora_handle)
        post_message(generate_payload(entity, sender, recipient), recipient)

        expect(
          Notifications::CommentOnPost.exists?(
            recipient_id: alice.id,
            target_type:  "Post",
            target_id:    local_parent.id
          )
        ).to be_truthy
      end

      it "for like on local post" do
        entity = create_relayable_entity(:like_entity, local_parent, remote_user_on_pod_b.diaspora_handle)
        post_message(generate_payload(entity, sender, recipient), recipient)

        expect(
          Notifications::Liked.exists?(
            recipient_id: alice.id,
            target_type:  "Post",
            target_id:    local_parent.id
          )
        ).to be_truthy
      end
    end

    %w(comment like).each do |entity|
      context "with #{entity}" do
        let(:entity_name) { "#{entity}_entity".to_sym }
        let(:klass) { entity.camelize.constantize }

        it_behaves_like "it deals correctly with a relayable"
      end
    end

    context "with participations" do
      let(:entity) {
        Fabricate(
          :participation_entity,
          author:      sender_id,
          parent_guid: local_parent.guid,
          parent:      Diaspora::Federation::Entities.related_entity(local_parent)
        )
      }

      it "treats participation receive correctly" do
        expect(Workers::ReceiveLocal).to receive(:perform_async)
        post_message(generate_payload(entity, sender, recipient), recipient)

        received_entity = Participation.find_by(guid: entity.guid)
        expect(received_entity).not_to be_nil
        expect(received_entity.author.diaspora_handle).to eq(remote_user_on_pod_b.diaspora_handle)
      end

      it "rejects a participations for a remote parent" do
        expect(Workers::ReceiveLocal).not_to receive(:perform_async)
        entity = create_relayable_entity(:participation_entity, remote_parent, sender_id)

        post_message(generate_payload(entity, sender, recipient), recipient)

        expect(Participation.exists?(guid: entity.guid)).to be_falsey
      end
    end

    context "with poll_participation" do
      let(:local_parent) {
        FactoryGirl.create(
          :poll,
          status_message: FactoryGirl.create(:status_message, author: alice.person, public: public)
        )
      }
      let(:remote_parent) {
        FactoryGirl.create(
          :poll,
          status_message: FactoryGirl.create(:status_message, author: remote_user_on_pod_b.person, public: public)
        )
      }
      let(:entity_name) { :poll_participation_entity }
      let(:klass) { PollParticipation }

      it_behaves_like "it deals correctly with a relayable"
    end
  end
end

shared_examples_for "messages which can't be send without sharing" do
  # retractions shouldn't depend on sharing fact
  describe "retractions for non-relayable objects" do
    %w[status_message photo].each do |target|
      context "with #{target}" do
        let(:target_object) { FactoryGirl.create(target.to_sym, author: remote_user_on_pod_b.person) }

        it_behaves_like "it retracts non-relayable object"
      end
    end
  end

  describe "with messages which require a status to operate on" do
    let(:public) { recipient.nil? }
    let(:local_parent) { FactoryGirl.create(:status_message, author: alice.person, public: public) }
    let(:remote_parent) { FactoryGirl.create(:status_message, author: remote_user_on_pod_b.person, public: public) }

    # this one shouldn't depend on the sharing fact. this must be fixed
    describe "notifications are sent where required" do
      it "for comment on remote post where we participate" do
        alice.participate!(remote_parent)
        author_id = remote_user_on_pod_c.diaspora_handle
        entity = create_relayable_entity(:comment_entity, remote_parent, author_id)
        post_message(generate_payload(entity, sender, recipient), recipient)

        expect(
          Notifications::AlsoCommented.exists?(
            recipient_id: alice.id,
            target_type:  "Post",
            target_id:    remote_parent.id
          )
        ).to be_truthy
      end
    end

    describe "retractions for relayable objects" do
      before do
        allow(DiasporaFederation.callbacks).to receive(:trigger).with(
          :fetch_private_key, alice.diaspora_handle
        ) { alice.encryption_key }
      end

      context "with comment" do
        it_behaves_like "it retracts relayable object" do
          # case for to-upstream federation
          let(:target_object) {
            FactoryGirl.create(:comment, author: remote_user_on_pod_b.person, post: local_parent)
          }
        end

        it_behaves_like "it retracts relayable object" do
          # case for to-downsteam federation
          let(:target_object) {
            FactoryGirl.create(:comment, author: remote_user_on_pod_c.person, post: remote_parent)
          }
        end
      end

      context "with like" do
        it_behaves_like "it retracts relayable object" do
          # case for to-upstream federation
          let(:target_object) {
            FactoryGirl.create(:like, author: remote_user_on_pod_b.person, target: local_parent)
          }
        end

        it_behaves_like "it retracts relayable object" do
          # case for to-downsteam federation
          let(:target_object) {
            FactoryGirl.create(:like, author: remote_user_on_pod_c.person, target: remote_parent)
          }
        end
      end
    end
  end
end