improve key specs in federation_callbacks_spec.rb

This commit is contained in:
Benjamin Neff 2015-12-20 21:41:39 +01:00 committed by Dennis Schubert
parent 5c8f0c1671
commit 05e4c8dc51

View file

@ -149,120 +149,114 @@ describe "diaspora federation callbacks" do
end end
end end
def create_a_local_person let(:local_person) { FactoryGirl.create(:user).person }
FactoryGirl.create(:user).person let(:remote_person) { FactoryGirl.create(:person) }
end let(:post_by_a_local_person) { FactoryGirl.create(:status_message, author: local_person) }
let(:post_by_a_remote_person) { FactoryGirl.create(:status_message, author: remote_person) }
def create_a_remote_person describe ":fetch_private_key_by_diaspora_id" do
FactoryGirl.create(:person)
end
def create_post_by_a_local_person
FactoryGirl.create(:status_message, author: create_a_local_person).guid
end
def create_post_by_a_remote_person
FactoryGirl.create(:status_message, author: create_a_remote_person).guid
end
describe :fetch_private_key_by_diaspora_id do
it "returns a private key for a local user" do it "returns a private key for a local user" do
expect( key = DiasporaFederation.callbacks.trigger(:fetch_private_key_by_diaspora_id, local_person.diaspora_handle)
DiasporaFederation.callbacks.trigger(described_class, create_a_local_person.diaspora_handle) expect(key).to be_a(OpenSSL::PKey::RSA)
).not_to be_nil expect(key.to_s).to eq(local_person.owner.serialized_private_key)
end end
it "returns nil for a remote user" do it "returns nil for a remote user" do
expect( expect(
DiasporaFederation.callbacks.trigger(described_class, create_a_remote_person.diaspora_handle) DiasporaFederation.callbacks.trigger(:fetch_private_key_by_diaspora_id, remote_person.diaspora_handle)
).to be_nil ).to be_nil
end end
it "returns nil for an unknown id" do it "returns nil for an unknown id" do
expect( expect(
DiasporaFederation.callbacks.trigger(described_class, FactoryGirl.generate(:diaspora_id)) DiasporaFederation.callbacks.trigger(:fetch_private_key_by_diaspora_id, FactoryGirl.generate(:diaspora_id))
).to be_nil ).to be_nil
end end
end end
describe :fetch_author_private_key_by_entity_guid do describe ":fetch_author_private_key_by_entity_guid" do
it "returns a private key for a post by a local user" do it "returns a private key for a post by a local user" do
expect( key = DiasporaFederation.callbacks.trigger(:fetch_author_private_key_by_entity_guid,
DiasporaFederation.callbacks.trigger(described_class, "Post", create_post_by_a_local_person) "Post", post_by_a_local_person.guid)
).not_to be_nil expect(key).to be_a(OpenSSL::PKey::RSA)
expect(key.to_s).to eq(post_by_a_local_person.author.owner.serialized_private_key)
end end
it "returns nil for a post by a remote user" do it "returns nil for a post by a remote user" do
expect( expect(
DiasporaFederation.callbacks.trigger(described_class, "Post", create_post_by_a_remote_person) DiasporaFederation.callbacks.trigger(:fetch_author_private_key_by_entity_guid,
"Post", post_by_a_remote_person.guid)
).to be_nil ).to be_nil
end end
it "returns nil for an unknown post" do it "returns nil for an unknown post" do
expect( expect(
DiasporaFederation.callbacks.trigger(described_class, "Post", FactoryGirl.generate(:guid)) DiasporaFederation.callbacks.trigger(:fetch_author_private_key_by_entity_guid,
"Post", FactoryGirl.generate(:guid))
).to be_nil ).to be_nil
end end
end end
describe :fetch_public_key_by_diaspora_id do describe ":fetch_public_key_by_diaspora_id" do
it "returns a public key for a person" do it "returns a public key for a person" do
expect( key = DiasporaFederation.callbacks.trigger(:fetch_public_key_by_diaspora_id, remote_person.diaspora_handle)
DiasporaFederation.callbacks.trigger(described_class, create_a_remote_person.diaspora_handle) expect(key).to be_a(OpenSSL::PKey::RSA)
).not_to be_nil expect(key.to_s).to eq(remote_person.serialized_public_key)
end end
it "returns nil for an unknown person" do it "returns nil for an unknown person" do
expect( expect(
DiasporaFederation.callbacks.trigger(described_class, FactoryGirl.generate(:diaspora_id)) DiasporaFederation.callbacks.trigger(:fetch_public_key_by_diaspora_id, FactoryGirl.generate(:diaspora_id))
).to be_nil ).to be_nil
end end
end end
describe :fetch_author_public_key_by_entity_guid do describe ":fetch_author_public_key_by_entity_guid" do
it "returns a public key for a known post" do it "returns a public key for a known post" do
expect( key = DiasporaFederation.callbacks.trigger(:fetch_author_public_key_by_entity_guid,
DiasporaFederation.callbacks.trigger(described_class, "Post", create_post_by_a_remote_person) "Post", post_by_a_remote_person.guid)
).not_to be_nil expect(key).to be_a(OpenSSL::PKey::RSA)
expect(key.to_s).to eq(post_by_a_remote_person.author.serialized_public_key)
end end
it "returns nil for an unknown post" do it "returns nil for an unknown post" do
expect( expect(
DiasporaFederation.callbacks.trigger(described_class, "Post", FactoryGirl.generate(:guid)) DiasporaFederation.callbacks.trigger(:fetch_author_public_key_by_entity_guid,
"Post", FactoryGirl.generate(:guid))
).to be_nil ).to be_nil
end end
end end
describe :entity_author_is_local? do describe ":entity_author_is_local?" do
it "returns true for a post by a local user" do it "returns true for a post by a local user" do
expect( expect(
DiasporaFederation.callbacks.trigger(described_class, "Post", create_post_by_a_local_person) DiasporaFederation.callbacks.trigger(:entity_author_is_local?, "Post", post_by_a_local_person.guid)
).to be(true) ).to be_truthy
end end
it "returns false for a post by a remote user" do it "returns false for a post by a remote user" do
expect( expect(
DiasporaFederation.callbacks.trigger(described_class, "Post", create_post_by_a_remote_person) DiasporaFederation.callbacks.trigger(:entity_author_is_local?, "Post", post_by_a_remote_person.guid)
).to be(false) ).to be_falsey
end end
it "returns false for a unknown post" do it "returns false for a unknown post" do
expect( expect(
DiasporaFederation.callbacks.trigger(described_class, "Post", FactoryGirl.generate(:diaspora_id)) DiasporaFederation.callbacks.trigger(:entity_author_is_local?, "Post", FactoryGirl.generate(:diaspora_id))
).to be(false) ).to be_falsey
end end
end end
describe :fetch_entity_author_id_by_guid do describe ":fetch_entity_author_id_by_guid" do
it "returns id for a existing guid" do it "returns id for a existing guid" do
expect( expect(
DiasporaFederation.callbacks.trigger(described_class, "Post", create_post_by_a_remote_person) DiasporaFederation.callbacks.trigger(:fetch_entity_author_id_by_guid, "Post", post_by_a_remote_person.guid)
).not_to be_nil ).not_to eq(post_by_a_remote_person.author_id)
end end
it "returns nil for a non-existing guid" do it "returns nil for a non-existing guid" do
expect( expect(
DiasporaFederation.callbacks.trigger(described_class, "Post", FactoryGirl.generate(:guid)) DiasporaFederation.callbacks.trigger(:fetch_entity_author_id_by_guid, "Post", FactoryGirl.generate(:guid))
).to be_nil ).to be_nil
end end
end end