subclassing receivers, renamed perform to perform [bang]

This commit is contained in:
Ilya Zhitomirskiy 2011-09-30 16:45:01 -07:00
parent 4a8aea9e78
commit 7926ebfb53
6 changed files with 220 additions and 222 deletions

View file

@ -1,6 +1,9 @@
module Postzord # Copyright (c) 2010-2011, Diaspora Inc. This file is
module Receiver # licensed under the Affero General Public License version 3 or later. See
class LocalBatch # the COPYRIGHT file.
class Postzord::Receiver::LocalBatch < Postzord::Receiver
attr_reader :object, :recipient_user_ids, :users attr_reader :object, :recipient_user_ids, :users
def initialize(object, recipient_user_ids) def initialize(object, recipient_user_ids)
@ -14,6 +17,9 @@ module Postzord
receive_relayable receive_relayable
else else
create_post_visibilities create_post_visibilities
# if caching enabled, add to cache
end end
notify_mentioned_users if @object.respond_to?(:mentions) notify_mentioned_users if @object.respond_to?(:mentions)
@ -67,5 +73,3 @@ module Postzord
end end
end end
end end
end
end

View file

@ -1,13 +1,12 @@
# Copyright (c) 2010-2011, Diaspora Inc. This file is # Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See # licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file. # the COPYRIGHT file.
#
require File.join(Rails.root, 'lib/webfinger') require File.join(Rails.root, 'lib/webfinger')
require File.join(Rails.root, 'lib/diaspora/parser') require File.join(Rails.root, 'lib/diaspora/parser')
module Postzord class Postzord::Receiver::Private < Postzord::Receiver
module Receiver
class Private
def initialize(user, opts={}) def initialize(user, opts={})
@user = user @user = user
@user_person = @user.person @user_person = @user.person
@ -19,7 +18,7 @@ module Postzord
@object = opts[:object] @object = opts[:object]
end end
def perform def perform!
if @sender && self.salmon.verified_for_key?(@sender.public_key) if @sender && self.salmon.verified_for_key?(@sender.public_key)
parse_and_receive(salmon.parsed_data) parse_and_receive(salmon.parsed_data)
else else
@ -113,5 +112,3 @@ module Postzord
end end
end end
end end
end
end

View file

@ -2,9 +2,8 @@
# licensed under the Affero General Public License version 3 or later. See # licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file. # the COPYRIGHT file.
module Postzord class Postzord::Receiver::Public < Postzord::Receiver
module Receiver
class Public
attr_accessor :salmon, :author attr_accessor :salmon, :author
def initialize(xml) def initialize(xml)
@ -60,5 +59,3 @@ module Postzord
@object.respond_to?(:public) && !@object.public? @object.respond_to?(:public) && !@object.public?
end end
end end
end
end

View file

@ -22,7 +22,7 @@ describe "attack vectors" do
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
expect { expect {
zord.perform zord.perform!
}.should raise_error /not a valid object/ }.should raise_error /not a valid object/
bob.visible_posts.include?(post_from_non_contact).should be_false bob.visible_posts.include?(post_from_non_contact).should be_false
@ -39,7 +39,7 @@ describe "attack vectors" do
salmon_xml = bob.salmon(original_message).xml_for(alice.person) salmon_xml = bob.salmon(original_message).xml_for(alice.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
expect { expect {
zord.perform zord.perform!
}.should raise_error /not a valid object/ }.should raise_error /not a valid object/
alice.reload.visible_posts.should_not include(StatusMessage.find(original_message.id)) alice.reload.visible_posts.should_not include(StatusMessage.find(original_message.id))
@ -53,12 +53,12 @@ describe "attack vectors" do
salmon_xml = eve.salmon(original_message).xml_for(bob.person) salmon_xml = eve.salmon(original_message).xml_for(bob.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
zord.perform zord.perform!
malicious_message = Factory.build(:status_message, :id => original_message.id, :text => 'BAD!!!', :author => alice.person) malicious_message = Factory.build(:status_message, :id => original_message.id, :text => 'BAD!!!', :author => alice.person)
salmon_xml = alice.salmon(malicious_message).xml_for(bob.person) salmon_xml = alice.salmon(malicious_message).xml_for(bob.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
zord.perform zord.perform!
original_message.reload.text.should == "store this!" original_message.reload.text.should == "store this!"
end end
@ -68,14 +68,14 @@ describe "attack vectors" do
salmon_xml = eve.salmon(original_message).xml_for(bob.person) salmon_xml = eve.salmon(original_message).xml_for(bob.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
zord.perform zord.perform!
lambda { lambda {
malicious_message = Factory.build( :status_message, :id => original_message.id, :text => 'BAD!!!', :author => eve.person) malicious_message = Factory.build( :status_message, :id => original_message.id, :text => 'BAD!!!', :author => eve.person)
salmon_xml2 = alice.salmon(malicious_message).xml_for(bob.person) salmon_xml2 = alice.salmon(malicious_message).xml_for(bob.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
zord.perform zord.perform!
}.should_not change{ }.should_not change{
bob.reload.visible_posts.count bob.reload.visible_posts.count
@ -97,7 +97,7 @@ describe "attack vectors" do
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
expect { expect {
zord.perform zord.perform!
}.should raise_error /not a valid object/ }.should raise_error /not a valid object/
eve.reload.profile.first_name.should == first_name eve.reload.profile.first_name.should == first_name
@ -109,7 +109,7 @@ describe "attack vectors" do
salmon_xml = eve.salmon(original_message).xml_for(bob.person) salmon_xml = eve.salmon(original_message).xml_for(bob.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
zord.perform zord.perform!
bob.visible_posts.count.should == 1 bob.visible_posts.count.should == 1
StatusMessage.count.should == 1 StatusMessage.count.should == 1
@ -121,7 +121,7 @@ describe "attack vectors" do
salmon_xml = alice.salmon(ret).xml_for(bob.person) salmon_xml = alice.salmon(ret).xml_for(bob.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
zord.perform zord.perform!
StatusMessage.count.should == 1 StatusMessage.count.should == 1
bob.visible_posts.count.should == 1 bob.visible_posts.count.should == 1
@ -143,7 +143,7 @@ describe "attack vectors" do
proc { proc {
salmon_xml = alice.salmon(ret).xml_for(bob.person) salmon_xml = alice.salmon(ret).xml_for(bob.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
zord.perform zord.perform!
}.should_not raise_error }.should_not raise_error
end end
@ -152,7 +152,7 @@ describe "attack vectors" do
salmon_xml = eve.salmon(original_message).xml_for(bob.person) salmon_xml = eve.salmon(original_message).xml_for(bob.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
zord.perform zord.perform!
bob.visible_posts.count.should == 1 bob.visible_posts.count.should == 1
@ -164,7 +164,7 @@ describe "attack vectors" do
salmon_xml = alice.salmon(ret).xml_for(bob.person) salmon_xml = alice.salmon(ret).xml_for(bob.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
expect { expect {
zord.perform zord.perform!
}.should raise_error /not a valid object/ }.should raise_error /not a valid object/
bob.reload.visible_posts.count.should == 1 bob.reload.visible_posts.count.should == 1
@ -180,7 +180,7 @@ describe "attack vectors" do
salmon_xml = alice.salmon(ret).xml_for(bob.person) salmon_xml = alice.salmon(ret).xml_for(bob.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
zord.perform zord.perform!
}.should_not change{bob.reload.contacts.count} }.should_not change{bob.reload.contacts.count}
end end
@ -196,7 +196,7 @@ describe "attack vectors" do
salmon_xml = alice.salmon(ret).xml_for(bob.person) salmon_xml = alice.salmon(ret).xml_for(bob.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
expect { expect {
zord.perform zord.perform!
}.should raise_error /not a valid object/ }.should raise_error /not a valid object/
bob.reload.contacts.count.should == 2 bob.reload.contacts.count.should == 2
@ -207,7 +207,7 @@ describe "attack vectors" do
salmon_xml = eve.salmon(original_message).xml_for(bob.person) salmon_xml = eve.salmon(original_message).xml_for(bob.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
zord.perform zord.perform!
original_message.diaspora_handle = alice.diaspora_handle original_message.diaspora_handle = alice.diaspora_handle
original_message.text= "bad bad bad" original_message.text= "bad bad bad"
@ -215,7 +215,7 @@ describe "attack vectors" do
salmon_xml = alice.salmon(original_message).xml_for(bob.person) salmon_xml = alice.salmon(original_message).xml_for(bob.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
zord.perform zord.perform!
original_message.reload.text.should == "store this!" original_message.reload.text.should == "store this!"
end end

View file

@ -334,7 +334,7 @@ describe 'a user receives a post' do
salmon_xml = salmon.xml_for(bob.person) salmon_xml = salmon.xml_for(bob.person)
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
zord.perform zord.perform!
bob.visible_posts.include?(post).should be_true bob.visible_posts.include?(post).should be_true
end end

View file

@ -47,7 +47,7 @@ describe Postzord::Receiver::Private do
end end
end end
describe '#perform' do describe '#perform!' do
before do before do
@zord = Postzord::Receiver::Private.new(@user, :salmon_xml => @salmon_xml) @zord = Postzord::Receiver::Private.new(@user, :salmon_xml => @salmon_xml)
@salmon = @zord.instance_variable_get(:@salmon) @salmon = @zord.instance_variable_get(:@salmon)
@ -56,25 +56,25 @@ describe Postzord::Receiver::Private do
context 'returns nil' do context 'returns nil' do
it 'if the salmon author does not exist' do it 'if the salmon author does not exist' do
@zord.instance_variable_set(:@sender, nil) @zord.instance_variable_set(:@sender, nil)
@zord.perform.should be_nil @zord.perform!.should be_nil
end end
it 'if the author does not match the signature' do it 'if the author does not match the signature' do
@zord.instance_variable_set(:@sender, Factory(:person)) @zord.instance_variable_set(:@sender, Factory(:person))
@zord.perform.should be_nil @zord.perform!.should be_nil
end end
end end
context 'returns the sent object' do context 'returns the sent object' do
it 'returns the received object on success' do it 'returns the received object on success' do
object = @zord.perform object = @zord.perform!
object.should respond_to(:to_diaspora_xml) object.should respond_to(:to_diaspora_xml)
end end
end end
it 'parses the salmon object' do it 'parses the salmon object' do
Diaspora::Parser.should_receive(:from_xml).with(@salmon.parsed_data).and_return(@original_post) Diaspora::Parser.should_receive(:from_xml).with(@salmon.parsed_data).and_return(@original_post)
@zord.perform @zord.perform!
end end
end end