diff --git a/spec/factories.rb b/spec/factories.rb index 88da019..61ed6fd 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -5,8 +5,37 @@ DiasporaFederation::Test::Factories.federation_factories FactoryGirl.define do factory :person do diaspora_id - url "http://localhost:3000/" + url "http://somehost:3000/" serialized_public_key { generate(:public_key) } after(:create, &:save) end + + factory :user, class: Person do + diaspora_id + url "http://localhost:3000/" + after(:build) do |user| + private_key = OpenSSL::PKey::RSA.generate(1024) + user.serialized_private_key = private_key.export + user.serialized_public_key = private_key.public_key.export + end + after(:create, &:save) + end + + factory :post, class: Entity do + entity_type "Post" + author { FactoryGirl.build(:person) } + after(:create, &:save) + end + + factory :poll, class: Entity do + entity_type "Poll" + author { FactoryGirl.build(:person) } + after(:create, &:save) + end + + factory :conversation, class: Entity do + entity_type "Conversation" + author { FactoryGirl.build(:person) } + after(:create, &:save) + end end diff --git a/spec/lib/diaspora_federation/entities/comment_spec.rb b/spec/lib/diaspora_federation/entities/comment_spec.rb index 75b8656..d6c0613 100644 --- a/spec/lib/diaspora_federation/entities/comment_spec.rb +++ b/spec/lib/diaspora_federation/entities/comment_spec.rb @@ -1,12 +1,13 @@ module DiasporaFederation describe Entities::Comment do - let(:data) { FactoryGirl.build(:comment_entity).to_h } + let(:parent) { FactoryGirl.create(:post, author: bob) } + let(:data) { FactoryGirl.build(:comment_entity, diaspora_id: alice.diaspora_id, parent_guid: parent.guid).to_h } let(:xml) { <<-XML #{data[:guid]} - #{data[:parent_guid]} + #{parent.guid} #{data[:parent_author_signature]} #{data[:author_signature]} #{data[:text]} diff --git a/spec/lib/diaspora_federation/entities/conversation_spec.rb b/spec/lib/diaspora_federation/entities/conversation_spec.rb index fd15d6f..9e7e493 100644 --- a/spec/lib/diaspora_federation/entities/conversation_spec.rb +++ b/spec/lib/diaspora_federation/entities/conversation_spec.rb @@ -1,20 +1,23 @@ module DiasporaFederation describe Entities::Conversation do - let(:msg1_data) { FactoryGirl.build(:message_entity).to_h } - let(:msg2_data) { FactoryGirl.build(:message_entity).to_h } - let(:msg1) { FactoryGirl.build(:message_entity, msg1_data) } - let(:msg2) { FactoryGirl.build(:message_entity, msg2_data) } + let(:parent) { FactoryGirl.create(:conversation, author: bob) } + let(:msg1) { FactoryGirl.build(:message_entity, diaspora_id: alice.diaspora_id, parent_guid: parent.guid).to_h } + let(:msg2) { FactoryGirl.build(:message_entity, diaspora_id: alice.diaspora_id, parent_guid: parent.guid).to_h } + let(:signed_msg1) { Entities::Message.new(msg1) } + let(:signed_msg2) { Entities::Message.new(msg2) } let(:data) { FactoryGirl.attributes_for(:conversation_entity).merge!( - messages: [msg1, msg2], - participant_ids: "#{FactoryGirl.generate(:diaspora_id)};#{FactoryGirl.generate(:diaspora_id)}" + messages: [signed_msg1, signed_msg2], + diaspora_id: bob.diaspora_id, + guid: parent.guid, + participant_ids: "#{bob.diaspora_id};#{FactoryGirl.generate(:diaspora_id)}" ) } let(:xml) { <<-XML - #{data[:guid]} + #{parent.guid} #{data[:subject]} #{data[:created_at]} #{data[:messages].map {|a| a.to_xml.to_s.indent(2) }.join("\n")} diff --git a/spec/lib/diaspora_federation/entities/like_spec.rb b/spec/lib/diaspora_federation/entities/like_spec.rb index 7782f4a..3097ab0 100644 --- a/spec/lib/diaspora_federation/entities/like_spec.rb +++ b/spec/lib/diaspora_federation/entities/like_spec.rb @@ -1,14 +1,22 @@ module DiasporaFederation describe Entities::Like do - let(:data) { FactoryGirl.build(:like_entity).to_h } + let(:parent) { FactoryGirl.create(:post, author: bob) } + let(:data) { + FactoryGirl.build( + :like_entity, + diaspora_id: alice.diaspora_id, + parent_guid: parent.guid, + parent_type: parent.entity_type + ).to_h + } let(:xml) { <<-XML #{data[:positive]} #{data[:guid]} - #{data[:parent_type]} - #{data[:parent_guid]} + #{parent.entity_type} + #{parent.guid} #{data[:parent_author_signature]} #{data[:author_signature]} #{data[:diaspora_id]} diff --git a/spec/lib/diaspora_federation/entities/message_spec.rb b/spec/lib/diaspora_federation/entities/message_spec.rb index c023fc7..42b9dcb 100644 --- a/spec/lib/diaspora_federation/entities/message_spec.rb +++ b/spec/lib/diaspora_federation/entities/message_spec.rb @@ -1,12 +1,13 @@ module DiasporaFederation describe Entities::Message do - let(:data) { FactoryGirl.build(:message_entity).to_h } + let(:parent) { FactoryGirl.create(:conversation, author: bob) } + let(:data) { FactoryGirl.build(:message_entity, diaspora_id: alice.diaspora_id, parent_guid: parent.guid).to_h } let(:xml) { <<-XML #{data[:guid]} - #{data[:parent_guid]} + #{parent.guid} #{data[:parent_author_signature]} #{data[:author_signature]} #{data[:text]} diff --git a/spec/lib/diaspora_federation/entities/participation_spec.rb b/spec/lib/diaspora_federation/entities/participation_spec.rb index 7007880..b6768e9 100644 --- a/spec/lib/diaspora_federation/entities/participation_spec.rb +++ b/spec/lib/diaspora_federation/entities/participation_spec.rb @@ -1,13 +1,21 @@ module DiasporaFederation describe Entities::Participation do - let(:data) { FactoryGirl.build(:participation_entity).to_h } + let(:parent) { FactoryGirl.create(:post, author: bob) } + let(:data) { + FactoryGirl.build( + :participation_entity, + diaspora_id: alice.diaspora_id, + parent_guid: parent.guid, + parent_type: parent.entity_type + ).to_h + } let(:xml) { <<-XML #{data[:guid]} - #{data[:parent_type]} - #{data[:parent_guid]} + #{parent.entity_type} + #{parent.guid} #{data[:parent_author_signature]} #{data[:author_signature]} #{data[:diaspora_id]} diff --git a/spec/lib/diaspora_federation/entities/poll_participation_spec.rb b/spec/lib/diaspora_federation/entities/poll_participation_spec.rb index f60fa96..160fb86 100644 --- a/spec/lib/diaspora_federation/entities/poll_participation_spec.rb +++ b/spec/lib/diaspora_federation/entities/poll_participation_spec.rb @@ -1,12 +1,15 @@ module DiasporaFederation describe Entities::PollParticipation do - let(:data) { FactoryGirl.build(:poll_participation_entity).to_h } + let(:parent) { FactoryGirl.create(:poll, author: bob) } + let(:data) { + FactoryGirl.build(:poll_participation_entity, diaspora_id: alice.diaspora_id, parent_guid: parent.guid).to_h + } let(:xml) { <<-XML #{data[:guid]} - #{data[:parent_guid]} + #{parent.guid} #{data[:parent_author_signature]} #{data[:author_signature]} #{data[:diaspora_id]} diff --git a/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb b/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb index b498c1b..972acd7 100644 --- a/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb +++ b/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb @@ -1,6 +1,6 @@ module DiasporaFederation describe Entities::RelayableRetraction do - let(:data) { FactoryGirl.build(:relayable_retraction_entity).to_h } + let(:data) { FactoryGirl.build(:relayable_retraction_entity, diaspora_id: alice.diaspora_id).to_h } let(:xml) { <<-XML diff --git a/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb b/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb index 7823995..34c73e0 100644 --- a/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb +++ b/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb @@ -1,6 +1,6 @@ module DiasporaFederation describe Entities::SignedRetraction do - let(:data) { FactoryGirl.build(:signed_retraction_entity).to_h } + let(:data) { FactoryGirl.build(:signed_retraction_entity, diaspora_id: alice.diaspora_id).to_h } let(:xml) { <<-XML diff --git a/spec/lib/diaspora_federation/salmon/xml_payload_spec.rb b/spec/lib/diaspora_federation/salmon/xml_payload_spec.rb index c60bee9..5b0fd94 100644 --- a/spec/lib/diaspora_federation/salmon/xml_payload_spec.rb +++ b/spec/lib/diaspora_federation/salmon/xml_payload_spec.rb @@ -145,7 +145,7 @@ XML context "relayable signature verification feature support" do it "calls signatures verification on relayable unpack" do - entity = FactoryGirl.build(:comment_entity) + entity = FactoryGirl.build(:comment_entity, diaspora_id: alice.diaspora_id) payload = Salmon::XmlPayload.pack(entity) payload.at_xpath("post/*[1]/author_signature").content = nil diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 70c2fa7..a3755fb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -33,6 +33,10 @@ def alice @alice ||= Person.find_by(diaspora_id: "alice@localhost:3000") end +def bob + @bob ||= Person.find_by(diaspora_id: "bob@localhost:3000") +end + def test_pkey DiasporaFederation.callbacks.trigger(:fetch_private_key_by_diaspora_id) end diff --git a/spec/support/fixture_builder.rb b/spec/support/fixture_builder.rb index a737a58..9f729ac 100644 --- a/spec/support/fixture_builder.rb +++ b/spec/support/fixture_builder.rb @@ -11,6 +11,7 @@ FixtureBuilder.configure do |fbuilder| # now declare objects fbuilder.factory do - FactoryGirl.create(:person, diaspora_id: "alice@localhost:3000") + FactoryGirl.create(:user, diaspora_id: "alice@localhost:3000") + FactoryGirl.create(:user, diaspora_id: "bob@localhost:3000") end end diff --git a/spec/support/shared_entity_specs.rb b/spec/support/shared_entity_specs.rb index f724180..ec7795c 100644 --- a/spec/support/shared_entity_specs.rb +++ b/spec/support/shared_entity_specs.rb @@ -87,8 +87,10 @@ shared_examples "a relayable Entity" do author_signature = xml.at_xpath("post/*[1]/author_signature").text parent_author_signature = xml.at_xpath("post/*[1]/parent_author_signature").text - expect(legacy_verify_signature(test_pkey, author_signature, signed_string)).to be_truthy - expect(legacy_verify_signature(test_pkey, parent_author_signature, signed_string)).to be_truthy + alice_public_key = OpenSSL::PKey::RSA.new(alice.serialized_public_key) + bob_public_key = OpenSSL::PKey::RSA.new(bob.serialized_public_key) + expect(legacy_verify_signature(alice_public_key, author_signature, signed_string)).to be_truthy + expect(legacy_verify_signature(bob_public_key, parent_author_signature, signed_string)).to be_truthy end end end diff --git a/test/dummy/app/models/entity.rb b/test/dummy/app/models/entity.rb new file mode 100644 index 0000000..d68e72d --- /dev/null +++ b/test/dummy/app/models/entity.rb @@ -0,0 +1,5 @@ +class Entity < ActiveRecord::Base + include ::Diaspora::Guid + + belongs_to :author, class_name: "Person" +end diff --git a/test/dummy/config/initializers/diaspora_federation.rb b/test/dummy/config/initializers/diaspora_federation.rb index eabee9b..4ef7289 100644 --- a/test/dummy/config/initializers/diaspora_federation.rb +++ b/test/dummy/config/initializers/diaspora_federation.rb @@ -60,32 +60,34 @@ DiasporaFederation.configure do |config| end end - def privkey - @test_privkey ||= OpenSSL::PKey::RSA.generate(1024) + on :fetch_private_key_by_diaspora_id do |diaspora_id| + key = Person.where(diaspora_id: diaspora_id).pluck(:serialized_private_key).first + OpenSSL::PKey::RSA.new(key) unless key.nil? end - on :fetch_private_key_by_diaspora_id do - privkey + on :fetch_author_private_key_by_entity_guid do |entity_type, guid| + key = Entity.where(entity_type: entity_type, guid: guid).joins(:author).pluck(:serialized_private_key).first + OpenSSL::PKey::RSA.new(key) unless key.nil? end - on :fetch_author_private_key_by_entity_guid do - privkey + on :fetch_public_key_by_diaspora_id do |diaspora_id| + key = Person.where(diaspora_id: diaspora_id).pluck(:serialized_public_key).first + key = DiasporaFederation::Discovery::Discovery.new(diaspora_id).fetch_and_save.exported_key if key.nil? + OpenSSL::PKey::RSA.new(key) unless key.nil? end - on :fetch_public_key_by_diaspora_id do - privkey.public_key + on :fetch_author_public_key_by_entity_guid do |entity_type, guid| + key = Entity.where(entity_type: entity_type, guid: guid).joins(:author).pluck(:serialized_public_key).first + OpenSSL::PKey::RSA.new(key) unless key.nil? end - on :fetch_author_public_key_by_entity_guid do - privkey.public_key + on :entity_author_is_local? do |entity_type, guid| + Entity.where(entity_type: entity_type, guid: guid).joins(:author) + .where.not("people.serialized_private_key" => nil).exists? end - on :entity_author_is_local? do - false - end - - on :fetch_entity_author_id_by_guid do - nil + on :fetch_entity_author_id_by_guid do |entity_type, guid| + Entity.where(entity_type: entity_type, guid: guid).joins(:author).pluck(:diaspora_id).first end on :queue_public_receive do diff --git a/test/dummy/db/migrate/20160202020724_add_serialized_private_key_to_person.rb b/test/dummy/db/migrate/20160202020724_add_serialized_private_key_to_person.rb new file mode 100644 index 0000000..6d56d4f --- /dev/null +++ b/test/dummy/db/migrate/20160202020724_add_serialized_private_key_to_person.rb @@ -0,0 +1,5 @@ +class AddSerializedPrivateKeyToPerson < ActiveRecord::Migration + def change + add_column :people, :serialized_private_key, :text + end +end diff --git a/test/dummy/db/migrate/20160202221606_create_entity.rb b/test/dummy/db/migrate/20160202221606_create_entity.rb new file mode 100644 index 0000000..2bd4e8b --- /dev/null +++ b/test/dummy/db/migrate/20160202221606_create_entity.rb @@ -0,0 +1,11 @@ +class CreateEntity < ActiveRecord::Migration + def change + create_table :entities do |t| + t.belongs_to :author, class_name: "Person", null: false + t.string :guid, null: false + t.string :entity_type, null: false + + t.timestamps null: false + end + end +end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index dfcda3a..df45c3b 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -11,15 +11,24 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150722224751) do +ActiveRecord::Schema.define(version: 20160202221606) do + + create_table "entities", force: :cascade do |t| + t.integer "author_id", null: false + t.string "guid", null: false + t.string "entity_type", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end create_table "people", force: :cascade do |t| - t.string "guid", null: false - t.text "url", null: false - t.string "diaspora_id", null: false - t.text "serialized_public_key", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.string "guid", null: false + t.text "url", null: false + t.string "diaspora_id", null: false + t.text "serialized_public_key", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.text "serialized_private_key" end end