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

dispatch_spec.rb « postzord « lib « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d1e4fae54de1653904f2776fd06b7022faecc941 (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
#   Copyright (c) 2010, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

require 'spec_helper'

require File.join(Rails.root, 'lib/postzord')
require File.join(Rails.root, 'lib/postzord/dispatch')

describe Postzord::Dispatch do
  before do
    @sm = Factory(:status_message, :public => true, :author => alice.person)
    @subscribers = []
    5.times{@subscribers << Factory(:person)}
    @sm.stub!(:subscribers)
    @xml = @sm.to_diaspora_xml
  end

  describe '.initialize' do
    it 'takes an sender(User) and object (responds_to #subscibers) and sets then to @sender and @object' do
      zord = Postzord::Dispatch.new(alice, @sm)
      zord.instance_variable_get(:@sender).should == alice
      zord.instance_variable_get(:@object).should == @sm
    end

    it 'sets @subscribers from object' do
      @sm.should_receive(:subscribers).and_return(@subscribers)
      zord = Postzord::Dispatch.new(alice, @sm)
      zord.instance_variable_get(:@subscribers).should == @subscribers
    end

    it 'sets the @sender_person object' do
      zord = Postzord::Dispatch.new(alice, @sm)
      zord.instance_variable_get(:@sender_person).should == alice.person
    end

    it 'raises and gives you a helpful message if the object can not federate' do
      proc{ Postzord::Dispatch.new(alice, [])
      }.should raise_error /Diaspora::Webhooks/
    end
  end

  it 'creates a salmon base object' do
    zord = Postzord::Dispatch.new(alice, @sm)
    zord.salmon.should_not be nil
  end

  context 'instance methods' do
    before do
      @subscribers << bob.person
      @remote_people, @local_people = @subscribers.partition{ |person| person.owner_id.nil? }
      @sm.stub!(:subscribers).and_return @subscribers
      @zord =  Postzord::Dispatch.new(alice, @sm)
    end

    describe '#post' do
      before do
        @zord.stub!(:socket_and_notify_users)
      end
      it 'calls Array#partition on subscribers' do
        @subscribers.should_receive(:partition).and_return([@remote_people, @local_people])
        @zord.post
      end

      it 'calls #deliver_to_local with local people' do
        @zord.should_receive(:deliver_to_local).with(@local_people)
        @zord.post
      end

      it 'calls #deliver_to_remote with remote people' do
        @zord.should_receive(:deliver_to_remote).with(@remote_people)
        @zord.post
      end
    end

    context "comments" do
      before do
        @local_luke, @local_leia, @remote_raphael = set_up_friends
      end

      context "local luke's post is commented on by" do
        before do
          @post = @local_luke.post(:status_message, :text => "hello", :to => @local_luke.aspects.first)
        end
        context "local leia" do
          before do
            @comment = @local_leia.build_comment "yo", :on => @post
            @comment.save
          end
          context "local leia's mailman" do
            before do
              @mailman = Postzord::Dispatch.new(@local_leia, @comment)
            end
            it 'calls deliver_to_local with local_luke' do
              @mailman.should_receive(:deliver_to_local).with([@local_luke.person])
              @mailman.post
            end
            it 'calls deliver_to_remote with nobody' do
              @mailman.should_receive(:deliver_to_remote).with([])
              @mailman.post
            end
            it 'does not call socket_to_users' do
              @mailman.should_not_receive(:socket_to_users)
              @mailman.post
            end
            it 'does not call notify_users' do
              @mailman.should_not_receive(:notify_users)
              @mailman.post
            end
          end
          context "local luke's mailman" do
            before do
              @mailman = Postzord::Dispatch.new(@local_luke, @comment)
            end
            it 'does not call deliver_to_local' do
              @mailman.should_not_receive(:deliver_to_local)
              @mailman.post
            end
            it 'calls deliver_to_remote with remote raphael' do
              @mailman.should_receive(:deliver_to_remote).with([@remote_raphael])
              @mailman.post
            end
            it 'calls socket_to_users' do
              @mailman.should_receive(:socket_to_users).with([@local_leia, @local_luke])
              @mailman.post
            end
            it 'calls notify_users' do
              @mailman.should_receive(:notify_users).with([@local_leia])
              @mailman.post
            end
          end

        end
        context "remote raphael" do
          before do
            @comment = Factory.build(:comment, :author => @remote_raphael, :post => @post)
            @comment.save
            @mailman = Postzord::Dispatch.new(@local_luke, @comment)
          end
          it 'does not call deliver_to_local' do
            @mailman.should_not_receive(:deliver_to_local)
            @mailman.post
          end
          it 'calls deliver_to_remote with remote_raphael' do
            @mailman.should_receive(:deliver_to_remote).with([@remote_raphael])
            @mailman.post
          end
          it 'calls socket_to_users' do
            @mailman.should_receive(:socket_to_users).with([@local_leia])
            @mailman.post
          end
          it 'calls notify_users' do
            @mailman.should_receive(:notify_users).with([@local_leia])
            @mailman.post
          end
        end
        context "local luke" do
          before do
            @comment = @local_luke.build_comment "yo", :on => @post
            @comment.save
            @mailman = Postzord::Dispatch.new(@local_luke, @comment)
          end
          it 'does not call deliver_to_local' do
            @mailman.should_not_receive(:deliver_to_local)
            @mailman.post
          end
          it 'calls deliver_to_remote with remote_raphael' do
            @mailman.should_receive(:deliver_to_remote).with([@remote_raphael])
            @mailman.post
          end
          it 'calls socket_to_users' do
            @mailman.should_receive(:socket_to_users).with([@local_leia, @local_luke])
            @mailman.post
          end
          it 'calls notify_users' do
            @mailman.should_receive(:notify_users).with([@local_leia])
            @mailman.post
          end
        end
      end

      context "remote raphael's post is commented on by local luke" do
        before do
          @post = Factory(:status_message, :author => @remote_raphael)
          @comment = @local_luke.build_comment "yo", :on => @post
          @comment.save
          @mailman = Postzord::Dispatch.new(@local_luke, @comment)
        end
        it 'calls deliver_to_remote with remote_raphael' do
          @mailman.should_receive(:deliver_to_remote).with([@remote_raphael])
          @mailman.post
        end
        it 'calls deliver_to_local with nobody' do
          @mailman.should_receive(:deliver_to_local).with([])
          @mailman.post
        end
        it 'does not call socket_to_users' do
          @mailman.should_not_receive(:socket_to_users)
          @mailman.post
        end
        it 'does not call notify_users' do
          @mailman.should_not_receive(:notify_users)
          @mailman.post
        end
      end
    end

    describe '#deliver_to_remote' do
      before do
        @remote_people = []
        @remote_people << alice.person
        @mailman = Postzord::Dispatch.new(alice, @sm)
        @hydra = mock()
        Typhoeus::Hydra.stub!(:new).and_return(@hydra)
      end

      it 'should queue an HttpPost job for each remote person' do
        Resque.should_receive(:enqueue).with(Job::HttpMulti, alice.id, anything, @remote_people.map{|p| p.id}).once
        @mailman.send(:deliver_to_remote, @remote_people)
      end

      it 'calls salmon_for each remote person' do
       salmon = @mailman.salmon
       Salmon::SalmonSlap.stub(:create).and_return(salmon)
       salmon.should_receive(:xml_for).with(alice.person).and_return('what')
       @hydra.stub!(:queue)
       @hydra.stub!(:run)
       fantasy_resque do
         @mailman.send(:deliver_to_remote, @remote_people)
       end
      end
    end

    describe '#deliver_to_local' do
      before do
        @mailman = Postzord::Dispatch.new(alice, @sm)
      end

      it 'queues a batch receive' do
        local_people = []
        local_people << alice.person
        Resque.should_receive(:enqueue).with(Job::ReceiveLocalBatch, @sm.id, [alice.id]).once
        @mailman.send(:deliver_to_local, local_people)
      end

      it 'returns if people are empty' do
        Resque.should_not_receive(:enqueue)
        @mailman.send(:deliver_to_local, [])
      end

      it 'returns if the object is a profile' do
        @mailman.instance_variable_set(:@object, Profile.new)
        Resque.should_not_receive(:enqueue)
        @mailman.send(:deliver_to_local, [1])
      end
    end

    describe '#deliver_to_services' do
      before do
        alice.aspects.create(:name => "whatever")
        @service = Services::Facebook.new(:access_token => "yeah")
        alice.services << @service
      end

      it 'queues a job to notify the hub' do
        Resque.stub!(:enqueue).with(Job::PostToService, anything, anything, anything)
        Resque.should_receive(:enqueue).with(Job::PublishToHub, alice.public_url)
        @zord.send(:deliver_to_services, nil, [])
      end

      it 'does not push to hub for non-public posts' do
       @sm     = Factory(:status_message)
       mailman = Postzord::Dispatch.new(alice, @sm)

       mailman.should_not_receive(:deliver_to_hub)
       mailman.post(:url => "http://joindiaspora.com/p/123")
      end

      it 'only pushes to specified services' do
       @s1 = Factory.create(:service, :user_id => alice.id)
       alice.services << @s1
       @s2 = Factory.create(:service, :user_id => alice.id)
       alice.services << @s2
       mailman = Postzord::Dispatch.new(alice, Factory(:status_message))

       Resque.stub!(:enqueue).with(Job::PublishToHub, anything)
       Resque.stub!(:enqueue).with(Job::HttpMulti, anything, anything, anything)
       Resque.should_receive(:enqueue).with(Job::PostToService, @s1.id, anything, anything)
       mailman.post(:url => "http://joindiaspora.com/p/123", :services => [@s1])
      end

      it 'does not push to services if none are specified' do
       mailman = Postzord::Dispatch.new(alice, Factory(:status_message))

       Resque.stub!(:enqueue).with(Job::PublishToHub, anything)
       Resque.should_not_receive(:enqueue).with(Job::PostToService, anything, anything, anything)
       mailman.post(:url => "http://joindiaspora.com/p/123")
      end
    end

    describe '#socket_and_notify_users' do
      it 'should call object#socket_to_user for each local user' do
        sc = mock()
        SocketsController.should_receive(:new).and_return(sc)
        sc.should_receive(:outgoing).with(bob, @zord.instance_variable_get(:@object), :aspect_ids => bob.contact_for(alice.person).aspect_memberships.map{|a| a.aspect_id})
        @zord.send(:socket_and_notify_users, [bob])
      end

      it 'only tries to socket when the object responds to #socket_to_user' do
        f = Request.new
        f.stub!(:subscribers)
        f.stub!(:to_diaspora_xml)
        users = [bob]
        z = Postzord::Dispatch.new(alice, f)
        z.instance_variable_get(:@object).should_receive(:socket_to_user).once
        z.send(:socket_to_users, users)
      end

      it 'queues Job::NotifyLocalUsers jobs' do
        @zord.instance_variable_get(:@object).should_receive(:socket_to_user).and_return(false)
        Resque.should_receive(:enqueue).with(Job::NotifyLocalUsers, [bob.id], @sm.class.to_s, @sm.id, @sm.author.id)
        @zord.send(:socket_and_notify_users, [bob])
      end
    end
  end
end