From 1d8ce00bef21501554d27f0a6739e3ebab7b8fd6 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sun, 24 Jul 2016 22:48:18 +0200 Subject: [PATCH] save signature data on receive --- lib/diaspora/federation/receive.rb | 10 ++- spec/lib/diaspora/federation/receive_spec.rb | 75 ++++++++++++++++++-- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/lib/diaspora/federation/receive.rb b/lib/diaspora/federation/receive.rb index 9b0ceea71..fb0953210 100644 --- a/lib/diaspora/federation/receive.rb +++ b/lib/diaspora/federation/receive.rb @@ -254,12 +254,20 @@ module Diaspora yield.tap do |relayable| retract_if_author_ignored(relayable) - relayable.author_signature = entity.author_signature if relayable.parent.author.local? + relayable.signature = build_signature(klass, entity) if relayable.parent.author.local? relayable.save! end end end + private_class_method def self.build_signature(klass, entity) + klass.reflect_on_association(:signature).klass.new( + author_signature: entity.author_signature, + additional_data: entity.additional_xml_elements, + signature_order: SignatureOrder.find_or_create_by!(order: entity.xml_order.join(" ")) + ) + end + private_class_method def self.retract_if_author_ignored(relayable) parent_author = relayable.parent.author.owner return unless parent_author && parent_author.ignored_people.include?(relayable.author) diff --git a/spec/lib/diaspora/federation/receive_spec.rb b/spec/lib/diaspora/federation/receive_spec.rb index 6f720e0a2..b7ac57098 100644 --- a/spec/lib/diaspora/federation/receive_spec.rb +++ b/spec/lib/diaspora/federation/receive_spec.rb @@ -17,7 +17,19 @@ describe Diaspora::Federation::Receive do end describe ".comment" do - let(:comment_entity) { FactoryGirl.build(:comment_entity, author: sender.diaspora_handle, parent_guid: post.guid) } + let(:comment_data) { + FactoryGirl.attributes_for( + :comment_entity, + author: sender.diaspora_handle, + parent_guid: post.guid, + author_signature: "aa" + ) + } + let(:comment_entity) { + DiasporaFederation::Entities::Comment.new( + comment_data, [:author, :guid, :parent_guid, :text, "new_property"], "new_property" => "data" + ) + } it "saves the comment" do received = Diaspora::Federation::Receive.perform(comment_entity) @@ -39,6 +51,17 @@ describe Diaspora::Federation::Receive do expect(comment.post).to eq(post) end + it "saves the signature data" do + Diaspora::Federation::Receive.perform(comment_entity) + + comment = Comment.find_by!(guid: comment_entity.guid) + + expect(comment.signature).not_to be_nil + expect(comment.signature.author_signature).to eq("aa") + expect(comment.signature.additional_data).to eq("new_property" => "data") + expect(comment.signature.order).to eq(%w(author guid parent_guid text new_property)) + end + let(:entity) { comment_entity } it_behaves_like "it ignores existing object received twice", Comment it_behaves_like "it rejects if the parent author ignores the author", Comment @@ -164,7 +187,19 @@ describe Diaspora::Federation::Receive do end describe ".like" do - let(:like_entity) { FactoryGirl.build(:like_entity, author: sender.diaspora_handle, parent_guid: post.guid) } + let(:like_data) { + FactoryGirl.attributes_for( + :like_entity, + author: sender.diaspora_handle, + parent_guid: post.guid, + author_signature: "aa" + ) + } + let(:like_entity) { + DiasporaFederation::Entities::Like.new( + like_data, [:author, :guid, :parent_guid, :parent_type, :positive, "new_property"], "new_property" => "data" + ) + } it "saves the like" do received = Diaspora::Federation::Receive.perform(like_entity) @@ -185,6 +220,17 @@ describe Diaspora::Federation::Receive do expect(like.target).to eq(post) end + it "saves the signature data" do + Diaspora::Federation::Receive.perform(like_entity) + + like = Like.find_by!(guid: like_entity.guid) + + expect(like.signature).not_to be_nil + expect(like.signature.author_signature).to eq("aa") + expect(like.signature.additional_data).to eq("new_property" => "data") + expect(like.signature.order).to eq(%w(author guid parent_guid parent_type positive new_property)) + end + let(:entity) { like_entity } it_behaves_like "it ignores existing object received twice", Like it_behaves_like "it rejects if the parent author ignores the author", Like @@ -310,12 +356,20 @@ describe Diaspora::Federation::Receive do describe ".poll_participation" do let(:post_with_poll) { FactoryGirl.create(:status_message_with_poll, author: alice.person) } - let(:poll_participation_entity) { - FactoryGirl.build( + let(:poll_participation_data) { + FactoryGirl.attributes_for( :poll_participation_entity, author: sender.diaspora_handle, parent_guid: post_with_poll.poll.guid, - poll_answer_guid: post_with_poll.poll.poll_answers.first.guid + poll_answer_guid: post_with_poll.poll.poll_answers.first.guid, + author_signature: "aa" + ) + } + let(:poll_participation_entity) { + DiasporaFederation::Entities::PollParticipation.new( + poll_participation_data, + [:author, :guid, :parent_guid, :poll_answer_guid, "new_property"], + "new_property" => "data" ) } @@ -338,6 +392,17 @@ describe Diaspora::Federation::Receive do expect(poll_participation.poll).to eq(post_with_poll.poll) end + it "saves the signature data" do + Diaspora::Federation::Receive.perform(poll_participation_entity) + + poll_participation = PollParticipation.find_by!(guid: poll_participation_entity.guid) + + expect(poll_participation.signature).not_to be_nil + expect(poll_participation.signature.author_signature).to eq("aa") + expect(poll_participation.signature.additional_data).to eq("new_property" => "data") + expect(poll_participation.signature.order).to eq(%w(author guid parent_guid poll_answer_guid new_property)) + end + let(:entity) { poll_participation_entity } it_behaves_like "it ignores existing object received twice", PollParticipation it_behaves_like "it rejects if the parent author ignores the author", PollParticipation