improve dummy app callbacks

This commit is contained in:
Benjamin Neff 2016-02-02 03:46:26 +01:00
parent cfbf401585
commit eb486b2a18
18 changed files with 141 additions and 49 deletions

View file

@ -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

View file

@ -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
<comment>
<guid>#{data[:guid]}</guid>
<parent_guid>#{data[:parent_guid]}</parent_guid>
<parent_guid>#{parent.guid}</parent_guid>
<parent_author_signature>#{data[:parent_author_signature]}</parent_author_signature>
<author_signature>#{data[:author_signature]}</author_signature>
<text>#{data[:text]}</text>

View file

@ -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
<conversation>
<guid>#{data[:guid]}</guid>
<guid>#{parent.guid}</guid>
<subject>#{data[:subject]}</subject>
<created_at>#{data[:created_at]}</created_at>
#{data[:messages].map {|a| a.to_xml.to_s.indent(2) }.join("\n")}

View file

@ -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
<like>
<positive>#{data[:positive]}</positive>
<guid>#{data[:guid]}</guid>
<target_type>#{data[:parent_type]}</target_type>
<parent_guid>#{data[:parent_guid]}</parent_guid>
<target_type>#{parent.entity_type}</target_type>
<parent_guid>#{parent.guid}</parent_guid>
<parent_author_signature>#{data[:parent_author_signature]}</parent_author_signature>
<author_signature>#{data[:author_signature]}</author_signature>
<diaspora_handle>#{data[:diaspora_id]}</diaspora_handle>

View file

@ -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
<message>
<guid>#{data[:guid]}</guid>
<parent_guid>#{data[:parent_guid]}</parent_guid>
<parent_guid>#{parent.guid}</parent_guid>
<parent_author_signature>#{data[:parent_author_signature]}</parent_author_signature>
<author_signature>#{data[:author_signature]}</author_signature>
<text>#{data[:text]}</text>

View file

@ -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
<participation>
<guid>#{data[:guid]}</guid>
<target_type>#{data[:parent_type]}</target_type>
<parent_guid>#{data[:parent_guid]}</parent_guid>
<target_type>#{parent.entity_type}</target_type>
<parent_guid>#{parent.guid}</parent_guid>
<parent_author_signature>#{data[:parent_author_signature]}</parent_author_signature>
<author_signature>#{data[:author_signature]}</author_signature>
<diaspora_handle>#{data[:diaspora_id]}</diaspora_handle>

View file

@ -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
<poll_participation>
<guid>#{data[:guid]}</guid>
<parent_guid>#{data[:parent_guid]}</parent_guid>
<parent_guid>#{parent.guid}</parent_guid>
<parent_author_signature>#{data[:parent_author_signature]}</parent_author_signature>
<author_signature>#{data[:author_signature]}</author_signature>
<diaspora_handle>#{data[:diaspora_id]}</diaspora_handle>

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,5 @@
class Entity < ActiveRecord::Base
include ::Diaspora::Guid
belongs_to :author, class_name: "Person"
end

View file

@ -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

View file

@ -0,0 +1,5 @@
class AddSerializedPrivateKeyToPerson < ActiveRecord::Migration
def change
add_column :people, :serialized_private_key, :text
end
end

View file

@ -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

View file

@ -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