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

querying_spec.rb « user « models « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d010570d36476e8fa14b9e7d1774bb0cd44de58c (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

require 'spec_helper'

describe User do

  before do
    @alices_aspect = alice.aspects.where(:name => "generic").first
    @eves_aspect = eve.aspects.where(:name => "generic").first
    @bobs_aspect = bob.aspects.where(:name => "generic").first
  end

  describe "#visible_shareable_ids" do
    it "contains your public posts" do
      public_post = alice.post(:status_message, :text => "hi", :to => @alices_aspect.id, :public => true)
      alice.visible_shareable_ids(Post).should include(public_post.id)
    end

    it "contains your non-public posts" do
      private_post = alice.post(:status_message, :text => "hi", :to => @alices_aspect.id, :public => false)
      alice.visible_shareable_ids(Post).should include(private_post.id)
    end

    it "contains public posts from people you're following" do
      pending
      dogs = bob.aspects.create(:name => "dogs")
      bobs_public_post = Factory(:status_message, :text => "hello", :public => true, :author => bob.person)

      alice.visible_shareable_ids(Post).should include(bobs_public_post.id)
    end

    it "contains non-public posts from people who are following you" do
      bobs_post = bob.post(:status_message, :text => "hello", :to => @bobs_aspect.id)
      alice.visible_shareable_ids(Post).should include(bobs_post.id)
    end

    it "does not contain non-public posts from aspects you're not in" do
      dogs = bob.aspects.create(:name => "dogs")
      invisible_post = bob.post(:status_message, :text => "foobar", :to => dogs.id)
      alice.visible_shareable_ids(Post).should_not include(invisible_post.id)
    end

    it "does not contain pending posts" do
      pending_post = bob.post(:status_message, :text => "hey", :public => true, :to => @bobs_aspect.id, :pending => true)
      pending_post.should be_pending
      alice.visible_shareable_ids(Post).should_not include pending_post.id
    end

    it "does not contain pending photos" do
      pending_photo = bob.post(:photo, :pending => true, :user_file=> File.open(photo_fixture_name), :to => @bobs_aspect)
      alice.visible_shareable_ids(Photo).should_not include pending_photo.id
    end

    it "respects the :type option" do
      post = bob.post(:status_message, :text => "hey", :public => true, :to => @bobs_aspect.id, :pending => false)
      reshare = bob.post(:reshare, :pending => false, :root_guid => post.guid, :to => @bobs_aspect)
      alice.visible_shareable_ids(Post, :type => "Reshare").should include(reshare.id)
      alice.visible_shareable_ids(Post, :type => 'StatusMessage').should_not include(reshare.id)
    end

    it "does not contain duplicate posts" do
      bobs_other_aspect = bob.aspects.create(:name => "cat people")
      bob.add_contact_to_aspect(bob.contact_for(alice.person), bobs_other_aspect)
      bob.aspects_with_person(alice.person).should =~ [@bobs_aspect, bobs_other_aspect]

      bobs_post = bob.post(:status_message, :text => "hai to all my people", :to => [@bobs_aspect.id, bobs_other_aspect.id])

      alice.visible_shareable_ids(Post).length.should == 1
      alice.visible_shareable_ids(Post).should include(bobs_post.id)
    end

    describe 'hidden posts' do
      before do
        aspect_to_post = bob.aspects.where(:name => "generic").first
        @status = bob.post(:status_message, :text=> "hello", :to => aspect_to_post)
        @vis = @status.share_visibilities(Post).first
      end

      it "pulls back non hidden posts" do
        alice.visible_shareable_ids(Post).include?(@status.id).should be_true
      end

      it "does not pull back hidden posts" do
        @vis.update_attributes(:hidden => true)
        alice.visible_shareable_ids(Post).include?(@status.id).should be_false
      end
    end

    context "RedisCache" do
      before do
        AppConfig[:redis_cache] = true
        @opts = {:order => "created_at DESC", :order_field => "created_at", :all_aspects? => true}
      end

      after do
        AppConfig[:redis_cache] = nil
      end

      it "kicks off a job that will populate the latest 100 posts" do
        pending "we need a job for this - ensure_populated! is too costly to do synchronously for new users"
      end

      it 'does not get used if if all_aspects? option is not present' do
        RedisCache.should_not_receive(:new)
        alice.visible_shareable_ids(Post, @opts.merge({:all_aspects? => false}))
      end

      describe '#use_cache?' do
        before do
          cache = mock(:cache_exists? => true, :supported_order? => true, :ensure_populated! => mock, :post_ids => [])
          RedisCache.stub(:new).and_return(cache)
        end

        it 'returns true if redis cache is set' do
          AppConfig[:redis_cache] = true
          alice.send(:use_cache?, @opts).should be_true
        end

        it 'returns false if redis cache is set' do
          AppConfig[:redis_cache] = nil
          alice.send(:use_cache?, @opts).should be_false
        end
      end

      describe '#perform_db_query?' do
        before do
          @opts = {:limit => 15}
        end

        it 'returns true if cache is nil' do
          alice.send(:perform_db_query?, [1,2,3], nil, @opts).should be_true
        end

        it 'returns true if cache shareable_ids is blank' do
          cache = mock(:size => 100)
          alice.send(:perform_db_query?, [], cache, @opts).should be_true
        end

        it 'returns true if cache shareable_ids length is less than opts[:limit]' do
          cache = mock(:size => 100)
          alice.send(:perform_db_query?, [1,2,3], cache, @opts).should be_true
        end

        it 'returns false if cache size is less than opts[:limit]' do
          cache = mock(:size => 10)
          alice.send(:perform_db_query?, [1,2,3], cache, @opts).should be_false
        end
      end

      context 'populated cache' do
        before do
          @cache = mock(:cache_exists? => true, :size => 100, :ensure_populated! => mock)
          RedisCache.stub(:new).and_return(@cache)
        end

        it "reads from the cache" do
          @cache.should_receive(:post_ids).and_return([1,2,3])

          alice.visible_shareable_ids(Post, @opts.merge({:limit => 3})).should == [1,2,3]
        end

        it "queries if maxtime is later than the last cached post" do
          @cache.stub(:post_ids).and_return([])
          alice.should_receive(:visible_ids_from_sql)

          alice.visible_shareable_ids(Post, @opts)
        end

        it "does not get repopulated"
      end
    end
  end

  describe "#prep_opts" do
    it "defaults the opts" do
      time = Time.now
      Time.stub(:now).and_return(time)
      alice.send(:prep_opts, Post, {}).should == {
        :type => Stream::Base::TYPES_OF_POST_IN_STREAM, 
        :order => 'created_at DESC',
        :limit => 15,
        :hidden => false,
        :order_field => :created_at,
        :order_with_table => "posts.created_at DESC",
        :max_time => time + 1
      }
    end
  end

  describe "#visible_shareables" do
    it 'never contains posts from people not in your aspects' do
      Factory(:status_message, :public => true)
      bob.visible_shareables(Post).count.should == 0
    end
    context 'with many posts' do
      before do
        bob.move_contact(eve.person, @bobs_aspect, bob.aspects.create(:name => 'new aspect'))
        time_interval = 1000
        time_past = 1000000
        (1..25).each do |n|
          [alice, bob, eve].each do |u|
            aspect_to_post = u.aspects.where(:name => "generic").first
            post = u.post :status_message, :text => "#{u.username} - #{n}", :to => aspect_to_post.id
            post.created_at = (post.created_at-time_past) - time_interval
            post.updated_at = (post.updated_at-time_past) + time_interval
            post.save
            time_interval += 1000
          end
        end
      end

      it 'works' do # The set up takes a looong time, so to save time we do several tests in one
        bob.visible_shareables(Post).length.should == 15 #it returns 15 by default
        bob.visible_shareables(Post).map(&:id).should == bob.visible_shareables(Post, :by_members_of => bob.aspects.map { |a| a.id }).map(&:id) # it is the same when joining through aspects

        # checks the default sort order
        bob.visible_shareables(Post).sort_by { |p| p.created_at }.map { |p| p.id }.should == bob.visible_shareables(Post).map { |p| p.id }.reverse #it is sorted updated_at desc by default

        # It should respect the order option
        opts = {:order => 'created_at DESC'}
        bob.visible_shareables(Post, opts).first.created_at.should > bob.visible_shareables(Post, opts).last.created_at

        # It should respect the order option
        opts = {:order => 'updated_at DESC'}
        bob.visible_shareables(Post, opts).first.updated_at.should > bob.visible_shareables(Post, opts).last.updated_at
        
        # It should respect the limit option
        opts = {:limit => 40}
        bob.visible_shareables(Post, opts).length.should == 40
        bob.visible_shareables(Post, opts).map(&:id).should == bob.visible_shareables(Post, opts.merge(:by_members_of => bob.aspects.map { |a| a.id })).map(&:id)
        bob.visible_shareables(Post, opts).sort_by { |p| p.created_at }.map { |p| p.id }.should == bob.visible_shareables(Post, opts).map { |p| p.id }.reverse

        # It should paginate using a datetime timestamp
        last_time_of_last_page = bob.visible_shareables(Post).last.created_at
        opts = {:max_time => last_time_of_last_page}
        bob.visible_shareables(Post, opts).length.should == 15
        bob.visible_shareables(Post, opts).map { |p| p.id }.should == bob.visible_shareables(Post, opts.merge(:by_members_of => bob.aspects.map { |a| a.id })).map { |p| p.id }
        bob.visible_shareables(Post, opts).sort_by { |p| p.created_at}.map { |p| p.id }.should == bob.visible_shareables(Post, opts).map { |p| p.id }.reverse
        bob.visible_shareables(Post, opts).map { |p| p.id }.should == bob.visible_shareables(Post, :limit => 40)[15...30].map { |p| p.id } #pagination should return the right posts

        # It should paginate using an integer timestamp
        opts = {:max_time => last_time_of_last_page.to_i}
        bob.visible_shareables(Post, opts).length.should == 15
        bob.visible_shareables(Post, opts).map { |p| p.id }.should == bob.visible_shareables(Post, opts.merge(:by_members_of => bob.aspects.map { |a| a.id })).map { |p| p.id }
        bob.visible_shareables(Post, opts).sort_by { |p| p.created_at}.map { |p| p.id }.should == bob.visible_shareables(Post, opts).map { |p| p.id }.reverse
        bob.visible_shareables(Post, opts).map { |p| p.id }.should == bob.visible_shareables(Post, :limit => 40)[15...30].map { |p| p.id } #pagination should return the right posts
      end
    end
  end

  describe '#find_visible_shareable_by_id' do
    it "returns a post if you can see it" do
      bobs_post = bob.post(:status_message, :text => "hi", :to => @bobs_aspect.id, :public => false)
      alice.find_visible_shareable_by_id(Post, bobs_post.id).should == bobs_post
    end
    it "returns nil if you can't see that post" do
      dogs = bob.aspects.create(:name => "dogs")
      invisible_post = bob.post(:status_message, :text => "foobar", :to => dogs.id)
      alice.find_visible_shareable_by_id(Post, invisible_post.id).should be_nil
    end
  end

  context 'with two users' do
    describe '#people_in_aspects' do
      it 'returns people objects for a users contact in each aspect' do
        alice.people_in_aspects([@alices_aspect]).should == [bob.person]
      end

      it 'returns local/remote people objects for a users contact in each aspect' do
        local_user1 = Factory(:user)
        local_user2 = Factory(:user)
        remote_user = Factory(:user)

        asp1 = local_user1.aspects.create(:name => "lol")
        asp2 = local_user2.aspects.create(:name => "brb")
        asp3 = remote_user.aspects.create(:name => "ttyl")

        connect_users(alice, @alices_aspect, local_user1, asp1)
        connect_users(alice, @alices_aspect, local_user2, asp2)
        connect_users(alice, @alices_aspect, remote_user, asp3)

        local_person = remote_user.person
        local_person.owner_id = nil
        local_person.save
        local_person.reload

        alice.people_in_aspects([@alices_aspect]).count.should == 4
        alice.people_in_aspects([@alices_aspect], :type => 'remote').count.should == 1
        alice.people_in_aspects([@alices_aspect], :type => 'local').count.should == 3
      end

      it 'does not return people not connected to user on same pod' do
        3.times { Factory(:user) }
        alice.people_in_aspects([@alices_aspect]).count.should == 1
      end

      it "only returns non-pending contacts" do
        alice.people_in_aspects([@alices_aspect]).should == [bob.person]
      end

      it "returns an empty array when passed an aspect the user doesn't own" do
        alice.people_in_aspects([@eves_aspect]).should == []
      end
    end
  end

  context 'contact querying' do
    let(:person_one) { Factory.create :person }
    let(:person_two) { Factory.create :person }
    let(:person_three) { Factory.create :person }
    let(:aspect) { alice.aspects.create(:name => 'heroes') }

    describe '#contact_for_person_id' do
      it 'returns a contact' do
        contact = Contact.create(:user => alice, :person => person_one, :aspects => [aspect])
        alice.contacts << contact
        alice.contact_for_person_id(person_one.id).should be_true
      end

      it 'returns the correct contact' do
        contact = Contact.create(:user => alice, :person => person_one, :aspects => [aspect])
        alice.contacts << contact

        contact2 = Contact.create(:user => alice, :person => person_two, :aspects => [aspect])
        alice.contacts << contact2

        contact3 = Contact.create(:user => alice, :person => person_three, :aspects => [aspect])
        alice.contacts << contact3

        alice.contact_for_person_id(person_two.id).person.should == person_two
      end

      it 'returns nil for a non-contact' do
        alice.contact_for_person_id(person_one.id).should be_nil
      end

      it 'returns nil when someone else has contact with the target' do
        contact = Contact.create(:user => alice, :person => person_one, :aspects => [aspect])
        alice.contacts << contact
        eve.contact_for_person_id(person_one.id).should be_nil
      end
    end

    describe '#contact_for' do
      it 'takes a person_id and returns a contact' do
        alice.should_receive(:contact_for_person_id).with(person_one.id)
        alice.contact_for(person_one)
      end

      it 'returns nil if the input is nil' do
        alice.contact_for(nil).should be_nil
      end
    end

    describe '#aspects_with_person' do
      before do
        @connected_person = bob.person
      end

      it 'should return the aspects with given contact' do
        alice.aspects_with_person(@connected_person).should == [@alices_aspect]
      end

      it 'returns multiple aspects if the person is there' do
        aspect2 = alice.aspects.create(:name => 'second')
        contact = alice.contact_for(@connected_person)

        alice.add_contact_to_aspect(contact, aspect2)
        alice.aspects_with_person(@connected_person).to_set.should == alice.aspects.to_set
      end
    end
  end

  describe '#posts_from' do
    before do
      @user3 = Factory(:user)
      @aspect3 = @user3.aspects.create(:name => "bros")

      @public_message = @user3.post(:status_message, :text => "hey there", :to => 'all', :public => true)
      @private_message = @user3.post(:status_message, :text => "hey there", :to => @aspect3.id)
    end

    it 'displays public posts for a non-contact' do
      alice.posts_from(@user3.person).should include @public_message
    end

    it 'does not display private posts for a non-contact' do
      alice.posts_from(@user3.person).should_not include @private_message
    end

    it 'displays private and public posts for a non-contact after connecting' do
      connect_users(alice, @alices_aspect, @user3, @aspect3)
      new_message = @user3.post(:status_message, :text=> "hey there", :to => @aspect3.id)

      alice.reload

      alice.posts_from(@user3.person).should include @public_message
      alice.posts_from(@user3.person).should include new_message
    end

    it 'displays recent posts first' do
      msg3 = @user3.post(:status_message, :text => "hey there", :to => 'all', :public => true)
      msg4 = @user3.post(:status_message, :text => "hey there", :to => 'all', :public => true)
      msg3.created_at = Time.now+10
      msg3.save!
      msg4.created_at = Time.now+14
      msg4.save!

      alice.posts_from(@user3.person).map { |p| p.id }.should == [msg4, msg3, @public_message].map { |p| p.id }
    end
  end
end