add entities tests and shared examples for entities

This commit is contained in:
Benjamin Neff 2015-07-29 01:48:50 +02:00
parent fe704fb981
commit 2b8aad766d
5 changed files with 85 additions and 38 deletions

View file

@ -5,6 +5,23 @@ module DiasporaFederation
let(:photo_medium_url) { "#{person.url}/upload/medium.png" }
let(:photo_small_url) { "#{person.url}/upload/small.png" }
let(:data) {
{
guid: person.guid,
nickname: person.nickname,
full_name: person.full_name,
url: person.url,
photo_large_url: photo_large_url,
photo_medium_url: photo_medium_url,
photo_small_url: photo_small_url,
public_key: person.serialized_public_key,
searchable: person.searchable,
first_name: person.first_name,
last_name: person.last_name
}
}
let(:klass) { Discovery::HCard }
let(:html) {
<<-HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
@ -92,31 +109,13 @@ module DiasporaFederation
HTML
}
it "must not create blank instances" do
expect { Discovery::HCard.new({}) }.to raise_error ArgumentError
end
it_behaves_like "an Entity subclass"
context "generation" do
it "creates an instance from a data hash" do
hcard = Discovery::HCard.new(
guid: person.guid,
nickname: person.nickname,
full_name: person.full_name,
url: person.url,
photo_large_url: photo_large_url,
photo_medium_url: photo_medium_url,
photo_small_url: photo_small_url,
public_key: person.serialized_public_key,
searchable: person.searchable,
first_name: person.first_name,
last_name: person.last_name
)
hcard = Discovery::HCard.new(data)
expect(hcard.to_html).to eq(html)
end
it "fails if nil was given" do
expect { Discovery::HCard.new(nil) }.to raise_error ArgumentError, "expected a Hash"
end
end
context "parsing" do

View file

@ -4,6 +4,21 @@ module DiasporaFederation
let(:acct) { "acct:#{person.diaspora_id}" }
let(:public_key_base64) { Base64.strict_encode64(person.serialized_public_key) }
let(:data) {
{
acct_uri: "acct:#{person.diaspora_id}",
alias_url: person.alias_url,
hcard_url: person.hcard_url,
seed_url: person.url,
profile_url: person.profile_url,
atom_url: person.atom_url,
salmon_url: person.salmon_url,
guid: person.guid,
public_key: person.serialized_public_key
}
}
let(:klass) { Discovery::WebFinger }
let(:xml) {
<<-XML
<?xml version="1.0" encoding="UTF-8"?>
@ -21,29 +36,13 @@ module DiasporaFederation
XML
}
it "must not create blank instances" do
expect { Discovery::WebFinger.new({}) }.to raise_error ArgumentError
end
it_behaves_like "an Entity subclass"
context "generation" do
it "creates a nice XML document" do
wf = Discovery::WebFinger.new(
acct_uri: "acct:#{person.diaspora_id}",
alias_url: person.alias_url,
hcard_url: person.hcard_url,
seed_url: person.url,
profile_url: person.profile_url,
atom_url: person.atom_url,
salmon_url: person.salmon_url,
guid: person.guid,
public_key: person.serialized_public_key
)
wf = Discovery::WebFinger.new(data)
expect(wf.to_xml).to eq(xml)
end
it "fails if nil was given" do
expect { Discovery::WebFinger.new(nil) }.to raise_error ArgumentError, "expected a Hash"
end
end
context "parsing" do

View file

@ -0,0 +1,8 @@
module DiasporaFederation
describe Entities::Person do
let(:data) { FactoryGirl.attributes_for(:person_entity) }
let(:klass) { Entities::Person }
it_behaves_like "an Entity subclass"
end
end

View file

@ -0,0 +1,8 @@
module DiasporaFederation
describe Entities::Profile do
let(:data) { FactoryGirl.attributes_for(:profile_entity) }
let(:klass) { Entities::Profile }
it_behaves_like "an Entity subclass"
end
end

View file

@ -0,0 +1,33 @@
shared_examples "an Entity subclass" do
it "should be an Entity" do
expect(klass).to be < DiasporaFederation::Entity
end
it "has its properties set" do
expect(klass.class_prop_names).to include(*data.keys)
end
context "behaviour" do
let(:instance) { klass.new(data) }
describe "initialize" do
it "must not create blank instances" do
expect { klass.new({}) }.to raise_error ArgumentError
end
it "fails if nil was given" do
expect { klass.new(nil) }.to raise_error ArgumentError, "expected a Hash"
end
it "should be frozen" do
expect(instance).to be_frozen
end
end
describe "#to_h" do
it "should resemble the input data" do
expect(instance.to_h).to eq(data)
end
end
end
end