Skip to content
Extraits de code Groupes Projets
Valider e16b9af7 rédigé par Benjamin Neff's avatar Benjamin Neff Validation de Dennis Schubert
Parcourir les fichiers

make public and private receiver work similar ...

parent ed10bea2
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -14,12 +14,19 @@ class Postzord::Receiver ...@@ -14,12 +14,19 @@ class Postzord::Receiver
self.receive! self.receive!
end end
private
def author_does_not_match_xml_author? def author_does_not_match_xml_author?
if (@author.diaspora_handle != xml_author) return false unless @author.diaspora_handle != xml_author
logger.warn "event=receive status=abort reason='author in xml does not match retrieved person' " \ logger.error "event=receive status=abort reason='author in xml does not match retrieved person' " \
"payload_type=#{@object.class} sender=#{@author.diaspora_handle}" "type=#{@object.class} sender=#{@author.diaspora_handle}"
return true true
end
end end
end
def relayable_without_parent?
return false unless @object.respond_to?(:relayable?) && @object.parent.nil?
logger.error "event=receive status=abort reason='no corresponding post' type=#{@object.class} " \
"sender=#{@author.diaspora_handle}"
true
end
end
...@@ -9,30 +9,28 @@ class Postzord::Receiver::Private < Postzord::Receiver ...@@ -9,30 +9,28 @@ class Postzord::Receiver::Private < Postzord::Receiver
@user_person = @user.person @user_person = @user.person
@salmon_xml = opts[:salmon_xml] @salmon_xml = opts[:salmon_xml]
@sender = opts[:person] || Webfinger.new(self.salmon.author_id).fetch @author = opts[:person] || Webfinger.new(salmon.author_id).fetch
@author = @sender
@object = opts[:object] @object = opts[:object]
end end
def receive! def receive!
if @sender && self.salmon.verified_for_key?(@sender.public_key) if @author && salmon.verified_for_key?(@author.public_key)
parse_and_receive(salmon.parsed_data) parse_and_receive(salmon.parsed_data)
else else
logger.error "event=receive status=abort reason='not_verified for key' " \ logger.error "event=receive status=abort reason='not_verified for key' " \
"recipient=#{@user.diaspora_handle} sender=#{@salmon.author_id}" "recipient=#{@user.diaspora_handle} sender=#{@salmon.author_id}"
end end
rescue => e rescue => e
logger.error "failed to receive #{@object.class} from sender:#{@sender.id} for user:#{@user.id}: #{e.message}\n" \ logger.error "failed to receive #{@object.class} from sender:#{@author.id} for user:#{@user.id}: #{e.message}\n" \
"#{@object.inspect}" "#{@object.inspect}"
raise e raise e
end end
def parse_and_receive(xml) def parse_and_receive(xml)
@object ||= Diaspora::Parser.from_xml(xml) @object ||= Diaspora::Parser.from_xml(xml)
return if @object.nil?
logger.info "user:#{@user.id} starting private receive from person:#{@sender.guid}" logger.info "user:#{@user.id} starting private receive from person:#{@author.guid}"
validate_object validate_object
set_author! set_author!
...@@ -43,27 +41,17 @@ class Postzord::Receiver::Private < Postzord::Receiver ...@@ -43,27 +41,17 @@ class Postzord::Receiver::Private < Postzord::Receiver
def receive_object def receive_object
obj = @object.receive(@user, @author) obj = @object.receive(@user, @author)
Notification.notify(@user, obj, @author) if obj.respond_to?(:notification_type) Notification.notify(@user, obj, @author) if obj.respond_to?(:notification_type)
logger.info "user:#{@user.id} successfully received #{@object.class} from person #{@sender.guid}" \ logger.info "user:#{@user.id} successfully received #{@object.class} from person #{@author.guid}" \
"#{": #{@object.guid}" if @object.respond_to?(:guid)}" "#{": #{@object.guid}" if @object.respond_to?(:guid)}"
logger.debug "received: #{@object.inspect}" logger.debug "received: #{@object.inspect}"
end end
protected protected
def salmon def salmon
@salmon ||= Salmon::EncryptedSlap.from_xml(@salmon_xml, @user) @salmon ||= Salmon::EncryptedSlap.from_xml(@salmon_xml, @user)
end end
def validate_object
raise Diaspora::ContactRequiredUnlessRequest if contact_required_unless_request
raise Diaspora::RelayableObjectWithoutParent if relayable_without_parent?
assign_sender_handle_if_request
raise Diaspora::AuthorXMLAuthorMismatch if author_does_not_match_xml_author?
@object
end
def xml_author def xml_author
if @object.respond_to?(:relayable?) if @object.respond_to?(:relayable?)
#if A and B are friends, and A sends B a comment from C, we delegate the validation to the owner of the post being commented on #if A and B are friends, and A sends B a comment from C, we delegate the validation to the owner of the post being commented on
...@@ -84,19 +72,22 @@ class Postzord::Receiver::Private < Postzord::Receiver ...@@ -84,19 +72,22 @@ class Postzord::Receiver::Private < Postzord::Receiver
private private
#validations # validations
def relayable_without_parent?
if @object.respond_to?(:relayable?) && @object.parent.nil? def validate_object
logger.error "event=receive status=abort reason='no corresponding post' type=#{@object.class} " \ raise Diaspora::XMLNotParseable if @object.nil?
"recipient=#{@user_person.diaspora_handle} sender=#{@sender.diaspora_handle}" raise Diaspora::ContactRequiredUnlessRequest if contact_required_unless_request
return true raise Diaspora::RelayableObjectWithoutParent if relayable_without_parent?
end
assign_sender_handle_if_request
raise Diaspora::AuthorXMLAuthorMismatch if author_does_not_match_xml_author?
end end
def contact_required_unless_request def contact_required_unless_request
unless @object.is_a?(Request) || @user.contact_for(@sender) unless @object.is_a?(Request) || @user.contact_for(@author)
logger.error "event=receive status=abort reason='sender not connected to recipient' type=#{@object.class} " \ logger.error "event=receive status=abort reason='sender not connected to recipient' type=#{@object.class} " \
"recipient=#{@user_person.diaspora_handle} sender=#{@sender.diaspora_handle}" "recipient=#{@user_person.diaspora_handle} sender=#{@author.diaspora_handle}"
return true return true
end end
end end
...@@ -104,7 +95,7 @@ class Postzord::Receiver::Private < Postzord::Receiver ...@@ -104,7 +95,7 @@ class Postzord::Receiver::Private < Postzord::Receiver
def assign_sender_handle_if_request def assign_sender_handle_if_request
#special casey #special casey
if @object.is_a?(Request) if @object.is_a?(Request)
@object.sender_handle = @sender.diaspora_handle @object.sender_handle = @author.diaspora_handle
end end
end end
end end
...@@ -9,8 +9,6 @@ class Postzord::Receiver::Public < Postzord::Receiver ...@@ -9,8 +9,6 @@ class Postzord::Receiver::Public < Postzord::Receiver
def initialize(xml) def initialize(xml)
@salmon = Salmon::Slap.from_xml(xml) @salmon = Salmon::Slap.from_xml(xml)
@author = Webfinger.new(@salmon.author_id).fetch @author = Webfinger.new(@salmon.author_id).fetch
logger.info "Receiving public object from person:#{@author.id}"
end end
# @return [Boolean] # @return [Boolean]
...@@ -23,7 +21,7 @@ class Postzord::Receiver::Public < Postzord::Receiver ...@@ -23,7 +21,7 @@ class Postzord::Receiver::Public < Postzord::Receiver
return unless verified_signature? return unless verified_signature?
# return false unless account_deletion_is_from_author # return false unless account_deletion_is_from_author
return unless save_object parse_and_receive(@salmon.parsed_data)
logger.info "received a #{@object.inspect}" logger.info "received a #{@object.inspect}"
if @object.is_a?(SignedRetraction) # feels like a hack if @object.is_a?(SignedRetraction) # feels like a hack
...@@ -51,15 +49,19 @@ class Postzord::Receiver::Public < Postzord::Receiver ...@@ -51,15 +49,19 @@ class Postzord::Receiver::Public < Postzord::Receiver
receiver.notify_users receiver.notify_users
end end
# @return [Object] # @return [void]
def save_object def parse_and_receive(xml)
@object = Diaspora::Parser.from_xml(@salmon.parsed_data) @object = Diaspora::Parser.from_xml(xml)
raise Diaspora::XMLNotParseable if @object.nil?
raise Diaspora::NonPublic if object_can_be_public_and_it_is_not? logger.info "starting public receive from person:#{@author.guid}"
raise Diaspora::RelayableObjectWithoutParent if object_must_have_parent_and_does_not?
raise Diaspora::AuthorXMLAuthorMismatch if author_does_not_match_xml_author? validate_object
receive_object
end
# @return [void]
def receive_object
@object.save! if @object.respond_to?(:save!) @object.save! if @object.respond_to?(:save!)
@object
end end
# @return [Array<Integer>] User ids # @return [Array<Integer>] User ids
...@@ -78,6 +80,15 @@ class Postzord::Receiver::Public < Postzord::Receiver ...@@ -78,6 +80,15 @@ class Postzord::Receiver::Public < Postzord::Receiver
private private
# validations
def validate_object
raise Diaspora::XMLNotParseable if @object.nil?
raise Diaspora::NonPublic if object_can_be_public_and_it_is_not?
raise Diaspora::RelayableObjectWithoutParent if relayable_without_parent?
raise Diaspora::AuthorXMLAuthorMismatch if author_does_not_match_xml_author?
end
def account_deletion_is_from_author def account_deletion_is_from_author
return true unless @object.is_a?(AccountDeletion) return true unless @object.is_a?(AccountDeletion)
return false if @object.diaspora_handle != @author.diaspora_handle return false if @object.diaspora_handle != @author.diaspora_handle
...@@ -88,12 +99,4 @@ class Postzord::Receiver::Public < Postzord::Receiver ...@@ -88,12 +99,4 @@ class Postzord::Receiver::Public < Postzord::Receiver
def object_can_be_public_and_it_is_not? def object_can_be_public_and_it_is_not?
@object.respond_to?(:public) && !@object.public? @object.respond_to?(:public) && !@object.public?
end end
def object_must_have_parent_and_does_not?
if @object.respond_to?(:relayable?) # comment, like
@object.parent.nil?
else
false
end
end
end end
...@@ -18,7 +18,7 @@ describe Postzord::Receiver::Private do ...@@ -18,7 +18,7 @@ describe Postzord::Receiver::Private do
zord = Postzord::Receiver::Private.new(bob, :person => alice.person, :object => @alices_post) zord = Postzord::Receiver::Private.new(bob, :person => alice.person, :object => @alices_post)
expect(zord.instance_variable_get(:@user)).not_to be_nil expect(zord.instance_variable_get(:@user)).not_to be_nil
expect(zord.instance_variable_get(:@sender)).not_to be_nil expect(zord.instance_variable_get(:@author)).not_to be_nil
expect(zord.instance_variable_get(:@object)).not_to be_nil expect(zord.instance_variable_get(:@object)).not_to be_nil
end end
...@@ -32,7 +32,7 @@ describe Postzord::Receiver::Private do ...@@ -32,7 +32,7 @@ describe Postzord::Receiver::Private do
zord = Postzord::Receiver::Private.new(bob, :salmon_xml => @salmon_xml) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => @salmon_xml)
expect(zord.instance_variable_get(:@user)).not_to be_nil expect(zord.instance_variable_get(:@user)).not_to be_nil
expect(zord.instance_variable_get(:@sender)).not_to be_nil expect(zord.instance_variable_get(:@author)).not_to be_nil
expect(zord.instance_variable_get(:@salmon_xml)).not_to be_nil expect(zord.instance_variable_get(:@salmon_xml)).not_to be_nil
end end
end end
...@@ -45,13 +45,13 @@ describe Postzord::Receiver::Private do ...@@ -45,13 +45,13 @@ describe Postzord::Receiver::Private do
context "does not parse and receive" do context "does not parse and receive" 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(:@author, nil)
expect(@zord).not_to receive(:parse_and_receive) expect(@zord).not_to receive(:parse_and_receive)
@zord.receive! @zord.receive!
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, FactoryGirl.create(:person)) @zord.instance_variable_set(:@author, FactoryGirl.create(:person))
expect(@zord).not_to receive(:parse_and_receive) expect(@zord).not_to receive(:parse_and_receive)
@zord.receive! @zord.receive!
end end
......
...@@ -47,7 +47,7 @@ describe Postzord::Receiver::Public do ...@@ -47,7 +47,7 @@ describe Postzord::Receiver::Public do
it "does not save the object if signature is not verified" do it "does not save the object if signature is not verified" do
expect(@receiver).to receive(:verified_signature?).and_return(false) expect(@receiver).to receive(:verified_signature?).and_return(false)
expect(@receiver).not_to receive(:save_object) expect(@receiver).not_to receive(:parse_and_receive)
@receiver.perform! @receiver.perform!
end end
...@@ -58,7 +58,7 @@ describe Postzord::Receiver::Public do ...@@ -58,7 +58,7 @@ describe Postzord::Receiver::Public do
end end
it 'saves the parsed object' do it 'saves the parsed object' do
expect(@receiver).to receive(:save_object) expect(@receiver).to receive(:parse_and_receive).and_call_original
@receiver.perform! @receiver.perform!
end end
...@@ -120,14 +120,15 @@ describe Postzord::Receiver::Public do ...@@ -120,14 +120,15 @@ describe Postzord::Receiver::Public do
end end
end end
describe "#save_object" do describe "#parse_and_receive" do
before do before do
@receiver = Postzord::Receiver::Public.new(@xml) @receiver = Postzord::Receiver::Public.new(@xml)
@parsed_salmon = Salmon::Slap.from_xml(@xml)
end end
it "should raise a Diaspora::XMLNotParseable when the parsed object is nil" do it "should raise a Diaspora::XMLNotParseable when the parsed object is nil" do
expect(Diaspora::Parser).to receive(:from_xml).and_return(nil) expect(Diaspora::Parser).to receive(:from_xml).and_return(nil)
expect { @receiver.save_object }.to raise_error(Diaspora::XMLNotParseable) expect { @receiver.parse_and_receive(@parsed_salmon.parsed_data) }.to raise_error(Diaspora::XMLNotParseable)
end end
end end
end end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# 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 'spec_helper' require "spec_helper"
describe Postzord::Receiver do describe Postzord::Receiver do
before do before do
...@@ -14,10 +14,53 @@ describe Postzord::Receiver do ...@@ -14,10 +14,53 @@ describe Postzord::Receiver do
allow(@receiver).to receive(:receive!).and_return(true) allow(@receiver).to receive(:receive!).and_return(true)
end end
it 'calls receive!' do it "calls receive!" do
expect(@receiver).to receive(:receive!) expect(@receiver).to receive(:receive!)
@receiver.perform! @receiver.perform!
end end
end end
end
describe "#author_does_not_match_xml_author?" do
before do
@receiver.instance_variable_set(:@author, alice.person)
allow(@receiver).to receive(:xml_author).and_return(alice.diaspora_handle)
end
it "should return false if the author matches" do
allow(@receiver).to receive(:xml_author).and_return(alice.diaspora_handle)
expect(@receiver.send(:author_does_not_match_xml_author?)).to be_falsey
end
it "should return true if the author does not match" do
allow(@receiver).to receive(:xml_author).and_return(bob.diaspora_handle)
expect(@receiver.send(:author_does_not_match_xml_author?)).to be_truthy
end
end
describe "#relayable_without_parent?" do
before do
@receiver.instance_variable_set(:@author, alice.person)
end
it "should return false if object is not relayable" do
@receiver.instance_variable_set(:@object, nil)
expect(@receiver.send(:relayable_without_parent?)).to be_falsey
end
context "if object is relayable" do
before do
@comment = bob.build_comment(text: "yo", post: FactoryGirl.create(:status_message))
@receiver.instance_variable_set(:@object, @comment)
end
it "should return false if object has parent" do
expect(@receiver.send(:relayable_without_parent?)).to be_falsey
end
it "should return true if object has no parent" do
@comment.parent = nil
expect(@receiver.send(:relayable_without_parent?)).to be_truthy
end
end
end
end
...@@ -2,7 +2,6 @@ require 'spec_helper' ...@@ -2,7 +2,6 @@ require 'spec_helper'
require Rails.root.join("spec", "shared_behaviors", "relayable") require Rails.root.join("spec", "shared_behaviors", "relayable")
describe PollParticipation, :type => :model do describe PollParticipation, :type => :model do
before do before do
@alices_aspect = alice.aspects.first @alices_aspect = alice.aspects.first
@status = bob.post(:status_message, :text => "hello", :to => bob.aspects.first.id) @status = bob.post(:status_message, :text => "hello", :to => bob.aspects.first.id)
...@@ -98,7 +97,8 @@ describe PollParticipation, :type => :model do ...@@ -98,7 +97,8 @@ describe PollParticipation, :type => :model do
allow_any_instance_of(Person).to receive(:local?).and_return(false) allow_any_instance_of(Person).to receive(:local?).and_return(false)
expect{ expect{
salmon = Salmon::Slap.create_by_user_and_activity(alice, @poll_participation_alice.to_diaspora_xml).xml_for(@poll_participant) salmon = Salmon::Slap.create_by_user_and_activity(alice, @poll_participation_alice.to_diaspora_xml).xml_for(@poll_participant)
Postzord::Receiver::Public.new(salmon).save_object parsed_salmon = Salmon::Slap.from_xml(salmon)
Postzord::Receiver::Public.new(salmon).parse_and_receive(parsed_salmon.parsed_data)
}.to_not raise_error }.to_not raise_error
end end
end end
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter