diff --git a/spec/models/comment_signature_spec.rb b/spec/models/comment_signature_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..396dbb9038f7d86217485321b86e0686d5685e46 --- /dev/null +++ b/spec/models/comment_signature_spec.rb @@ -0,0 +1,11 @@ +# 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 CommentSignature, type: :model do + it_behaves_like "signature data" do + let(:relayable_type) { :comment } + end +end diff --git a/spec/models/like_signature_spec.rb b/spec/models/like_signature_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..d31a374333960a7f4c15bdf0916fffa23636a3fa --- /dev/null +++ b/spec/models/like_signature_spec.rb @@ -0,0 +1,11 @@ +# 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 LikeSignature, type: :model do + it_behaves_like "signature data" do + let(:relayable_type) { :like } + end +end diff --git a/spec/models/poll_participation_signature_spec.rb b/spec/models/poll_participation_signature_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..6427d40fbaea01db94be92c432a20ba6e1250134 --- /dev/null +++ b/spec/models/poll_participation_signature_spec.rb @@ -0,0 +1,11 @@ +# 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 PollParticipationSignature, type: :model do + it_behaves_like "signature data" do + let(:relayable_type) { :poll_participation } + end +end diff --git a/spec/models/signature_order_spec.rb b/spec/models/signature_order_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..646bbcd2e86694d8104f2abf9c83a041771dd073 --- /dev/null +++ b/spec/models/signature_order_spec.rb @@ -0,0 +1,25 @@ +# 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 SignatureOrder, type: :model do + context "validation" do + it "requires an order" do + order = SignatureOrder.new + expect(order).not_to be_valid + + order.order = "author guid" + expect(order).to be_valid + end + + it "doesn't allow the same order twice" do + first = SignatureOrder.create!(order: "author guid") + expect(first).to be_valid + + second = SignatureOrder.new(order: first.order) + expect(second).not_to be_valid + end + end +end diff --git a/spec/shared_behaviors/relayable.rb b/spec/shared_behaviors/relayable.rb index 1043ca463545d437e234485511ccb4bb387bb4f7..2fd48d904db74e7f0c6df18f0416a838c2e2bc0c 100644 --- a/spec/shared_behaviors/relayable.rb +++ b/spec/shared_behaviors/relayable.rb @@ -68,4 +68,32 @@ shared_examples_for "it is relayable" do end end end + + describe "#signature" do + let(:signature_class) { described_class.reflect_on_association(:signature).klass } + + before do + remote_object_on_local_parent.signature = signature_class.new( + author_signature: "signature", + additional_data: {"new_property" => "some text"}, + signature_order: FactoryGirl.create(:signature_order) + ) + end + + it "returns the signature data" do + signature = described_class.find(remote_object_on_local_parent.id).signature + expect(signature).not_to be_nil + expect(signature.author_signature).to eq("signature") + expect(signature.additional_data).to eq("new_property" => "some text") + expect(signature.order).to eq(%w(guid parent_guid text author)) + end + + it "deletes the signature when destroying the relayable" do + id = remote_object_on_local_parent.id + remote_object_on_local_parent.destroy! + + signature = signature_class.find_by(signature_class.primary_key => id) + expect(signature).to be_nil + end + end end diff --git a/spec/shared_behaviors/signature.rb b/spec/shared_behaviors/signature.rb new file mode 100644 index 0000000000000000000000000000000000000000..3b40d751ef613e37ea6890a0d2aa69803fb380fb --- /dev/null +++ b/spec/shared_behaviors/signature.rb @@ -0,0 +1,57 @@ +require "spec_helper" + +shared_examples_for "signature data" do + let(:relayable) { FactoryGirl.create(relayable_type) } + let(:signature) { + described_class.new( + relayable_type => relayable, + :author_signature => "signature", + :additional_data => {"additional_data" => "some data"}, + :signature_order => SignatureOrder.new(order: "author guid parent_guid") + ) + } + + describe "#order" do + it "it returns the order as array" do + expect(signature.order).to eq(%w(author guid parent_guid)) + end + end + + describe "#additional_data" do + it "is stored as hash" do + signature.save + + entity = described_class.reflect_on_association(relayable_type).klass.find(relayable.id) + expect(entity.signature.additional_data).to eq("additional_data" => "some data") + end + + it "can be missing" do + signature.additional_data = nil + signature.save + + entity = described_class.reflect_on_association(relayable_type).klass.find(relayable.id) + expect(entity.signature.additional_data).to eq({}) + end + end + + context "validation" do + it "is valid" do + expect(signature).to be_valid + end + + it "requires a linked relayable" do + signature.public_send("#{relayable_type}=", nil) + expect(signature).not_to be_valid + end + + it "requires a signature_order" do + signature.signature_order = nil + expect(signature).not_to be_valid + end + + it "requires a author_signature" do + signature.author_signature = nil + expect(signature).not_to be_valid + end + end +end