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

public_spec.rb « fetcher « diaspora « lib « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f0e1f373861a7dfccec9da77e724b3dd82c816c9 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# frozen_string_literal: true

#   Copyright (c) 2010-2012, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

require "integration/federation/federation_helper"

# Tests fetching public posts of a person on a remote server
describe Diaspora::Fetcher::Public do
  let(:fixture) do
    File.read(Rails.root.join("spec/fixtures/public_posts.json"))
  end
  let(:fixture_data) do
    JSON.parse(fixture)
  end

  before do
    # the fixture is taken from an actual json request.
    # it contains 10 StatusMessages and 5 Reshares, all of them public
    # the guid of the person is "7445f9a0a6c28ebb"
    @fetcher = Diaspora::Fetcher::Public.new
    @person = FactoryBot.create(:person, guid:            "7445f9a0a6c28ebb",
                                         pod:             Pod.find_or_create_by(url: "https://remote-testpod.net"),
                                         diaspora_handle: "testuser@remote-testpod.net")

    stub_request(:get, %r{remote-testpod.net/people/.*/stream})
      .with(headers: {
              "Accept"          => "application/json",
              "Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
              "User-Agent"      => "diaspora-fetcher"
            }).to_return(body: fixture)
  end

  describe "#queue_for" do
    it "queues a new job" do
      @person.fetch_status = Diaspora::Fetcher::Public::Status_Initial

      expect(Workers::FetchPublicPosts).to receive(:perform_async).with(@person.diaspora_handle)

      Diaspora::Fetcher::Public.queue_for(@person)
    end

    it "queues no job if the status is not initial" do
      @person.fetch_status = Diaspora::Fetcher::Public::Status_Done

      expect(Workers::FetchPublicPosts).not_to receive(:perform_async).with(@person.diaspora_handle)

      Diaspora::Fetcher::Public.queue_for(@person)
    end
  end

  describe "#retrieve_posts" do
    before do
      person = @person
      @fetcher.instance_eval {
        @person = person
        retrieve_posts
      }
    end

    it "sets the operation status on the person" do
      @person.reload
      expect(@person.fetch_status).not_to eql(Diaspora::Fetcher::Public::Status_Initial)
      expect(@person.fetch_status).to eql(Diaspora::Fetcher::Public::Status_Fetched)
    end

    it "sets the @data variable to the parsed JSON data" do
      data = @fetcher.instance_eval {
        @data
      }
      expect(data).not_to be_nil
      expect(data.size).to eq(fixture_data.size)
    end
  end

  describe "#process_posts" do
    before do
      person = @person
      data = fixture_data

      @fetcher.instance_eval {
        @person = person
        @data = data
      }

      fixture_data.each do |post_data|
        post = if post_data["post_type"] == "StatusMessage"
                 FactoryBot.build(
                   :status_message,
                   guid:       post_data["guid"],
                   text:       post_data["text"],
                   created_at: post_data["created_at"],
                   public:     true,
                   author:     eve.person
                 )
               else
                 reshare = FactoryBot.build(
                   :reshare,
                   guid:       post_data["guid"],
                   created_at: post_data["created_at"],
                   public:     true,
                   author:     eve.person
                 )
                 reshare.root.save
                 reshare
               end
        payload = generate_payload(Diaspora::Federation::Entities.post(post), eve)

        stub_request(:get, "https://remote-testpod.net/fetch/post/#{post_data['guid']}")
          .to_return(status: 200, body: payload)
      end
    end

    it "creates 15 new posts in the database" do
      expect {
        @fetcher.instance_eval {
          process_posts
        }
      }.to change(Post, :count).by(15)
    end

    it "sets the operation status on the person" do
      @fetcher.instance_eval {
        process_posts
      }

      @person.reload
      expect(@person.fetch_status).not_to eql(Diaspora::Fetcher::Public::Status_Initial)
      expect(@person.fetch_status).to eql(Diaspora::Fetcher::Public::Status_Processed)
    end

    context "created post" do
      before do
        Timecop.freeze
        @now = DateTime.now.utc

        # save posts to db
        @fetcher.instance_eval {
          process_posts
        }
      end

      after do
        Timecop.return
      end

      it "applies the date from JSON to the record" do
        fixture_data.each do |post|
          date = ActiveSupport::TimeZone.new("UTC").parse(post["created_at"]).to_i

          entry = Post.find_by(guid: post["guid"])
          expect(entry.created_at.to_i).to eql(date)
        end
      end

      it "copied the text of status messages correctly" do
        fixture_data.select {|item| item["post_type"] == "StatusMessage" }.each do |post|
          entry = StatusMessage.find_by(guid: post["guid"])
          expect(entry.text).to eql(post["text"])
        end
      end

      it "applies now to interacted_at on the record" do
        fixture_data.each do |post|
          date = @now.to_i

          entry = Post.find_by(guid: post["guid"])
          expect(entry.interacted_at.to_i).to eql(date)
        end
      end
    end
  end

  context "private methods" do
    let(:public_fetcher) { Diaspora::Fetcher::Public.new }

    describe "#qualifies_for_fetching?" do
      it "raises an error if the person doesn't exist" do
        expect {
          public_fetcher.instance_eval {
            @person = Person.by_account_identifier "someone@unknown.com"
            qualifies_for_fetching?
          }
        }.to raise_error ActiveRecord::RecordNotFound
      end

      it "returns false if the person is unfetchable" do
        expect(public_fetcher.instance_eval {
          @person = FactoryBot.create(:person, fetch_status: Diaspora::Fetcher::Public::Status_Unfetchable)
          qualifies_for_fetching?
        }).to be false
      end

      it "returns false and sets the person unfetchable for a local account" do
        user = FactoryBot.create(:user)
        expect(public_fetcher.instance_eval {
          @person = user.person
          qualifies_for_fetching?
        }).to be false
        expect(user.person.fetch_status).to eql Diaspora::Fetcher::Public::Status_Unfetchable
      end

      it "returns false if the person is processing already (or has been processed)" do
        person = FactoryBot.create(:person)
        person.fetch_status = Diaspora::Fetcher::Public::Status_Fetched
        person.save
        expect(public_fetcher.instance_eval {
          @person = person
          qualifies_for_fetching?
        }).to be false
      end

      it "returns true, if the user is remote and hasn't been fetched" do
        person = FactoryBot.create(:person, {diaspora_handle: "neo@theone.net"})
        expect(public_fetcher.instance_eval {
          @person = person
          qualifies_for_fetching?
        }).to be true
      end
    end

    describe "#set_fetch_status" do
      it "sets the current status of fetching on the person" do
        person = @person
        public_fetcher.instance_eval {
          @person = person
          set_fetch_status Diaspora::Fetcher::Public::Status_Unfetchable
        }
        expect(@person.fetch_status).to eql Diaspora::Fetcher::Public::Status_Unfetchable

        public_fetcher.instance_eval {
          set_fetch_status Diaspora::Fetcher::Public::Status_Initial
        }
        expect(@person.fetch_status).to eql Diaspora::Fetcher::Public::Status_Initial
      end
    end

    describe "#validate" do
      it "calls all validation helper methods" do
        expect(public_fetcher).to receive(:check_existing).and_return(true)
        expect(public_fetcher).to receive(:check_author).and_return(true)
        expect(public_fetcher).to receive(:check_public).and_return(true)

        expect(public_fetcher.instance_eval { validate({}) }).to be true
      end
    end

    describe "#check_existing" do
      it "returns false if a post with the same guid exists" do
        post = {"guid" => FactoryBot.create(:status_message).guid}
        expect(public_fetcher.instance_eval { check_existing post }).to be false
      end

      it "returns true if the guid cannot be found" do
        post = {"guid" => SecureRandom.hex(8)}
        expect(public_fetcher.instance_eval { check_existing post }).to be true
      end
    end

    describe "#check_author" do
      let!(:some_person) { FactoryBot.create(:person) }

      before do
        person = some_person
        public_fetcher.instance_eval { @person = person }
      end

      it "returns false if the person doesn't match" do
        post = {"author" => {"guid" => SecureRandom.hex(8)}}
        expect(public_fetcher.instance_eval { check_author post }).to be false
      end

      it "returns true if the persons match" do
        post = {"author" => {"guid" => some_person.guid}}
        expect(public_fetcher.instance_eval { check_author post }).to be true
      end
    end

    describe "#check_public" do
      it "returns false if the post is not public" do
        post = {"public" => false}
        expect(public_fetcher.instance_eval { check_public post }).to be false
      end

      it "returns true if the post is public" do
        post = {"public" => true}
        expect(public_fetcher.instance_eval { check_public post }).to be true
      end
    end
  end
end