add save callback for webfinger

This commit is contained in:
Benjamin Neff 2015-08-07 22:07:24 +02:00 committed by Jonne Haß
parent 65b056341c
commit 21e5bd8697
3 changed files with 143 additions and 0 deletions

View file

@ -41,5 +41,25 @@ DiasporaFederation.configure do |config|
) )
end end
end end
on :save_person_after_webfinger do |person|
# find existing person or create a new one
person_entity = Person.find_by(diaspora_handle: person.diaspora_id) ||
Person.new(diaspora_handle: person.diaspora_id, guid: person.guid,
serialized_public_key: person.exported_key, url: person.url)
profile = person.profile
profile_entity = person_entity.profile ||= Profile.new
# fill or update profile
profile_entity.first_name = profile.first_name
profile_entity.last_name = profile.last_name
profile_entity.image_url = profile.image_url
profile_entity.image_url_medium = profile.image_url_medium
profile_entity.image_url_small = profile.image_url_small
profile_entity.searchable = profile.searchable
person_entity.save!
end
end end
end end

View file

@ -294,4 +294,33 @@ FactoryGirl.define do
end end
factory(:status, :parent => :status_message) factory(:status, :parent => :status_message)
# Factories for the DiasporaFederation-gem
factory(:federation_person_from_webfinger, class: DiasporaFederation::Entities::Person) do
sequence(:guid) { UUID.generate :compact }
sequence(:diaspora_id) {|n| "bob-person-#{n}#{r_str}@example.net" }
url AppConfig.pod_uri.to_s
exported_key OpenSSL::PKey::RSA.generate(1024).public_key.export
profile {
DiasporaFederation::Entities::Profile.new(
FactoryGirl.attributes_for(:federation_profile_from_hcard, diaspora_id: diaspora_id))
}
end
factory(:federation_profile_from_hcard, class: DiasporaFederation::Entities::Profile) do
sequence(:diaspora_id) {|n| "bob-person-#{n}#{r_str}@example.net" }
sequence(:first_name) {|n| "My Name#{n}#{r_str}" }
last_name nil
image_url "/assets/user/default.png"
image_url_medium "/assets/user/default.png"
image_url_small "/assets/user/default.png"
searchable true
end
factory :federation_profile_from_hcard_with_image_url, parent: :federation_profile_from_hcard do
image_url "http://example.com/image.jpg"
image_url_medium "http://example.com/image_mid.jpg"
image_url_small "http://example.com/image_small.jpg"
end
end end

View file

@ -53,4 +53,98 @@ describe "diaspora federation callbacks" do
expect(hcard).to be_nil expect(hcard).to be_nil
end end
end end
describe ":save_person_after_webfinger" do
context "new person" do
it "creates a new person" do
person = DiasporaFederation::Entities::Person.new(FactoryGirl.attributes_for(:federation_person_from_webfinger))
DiasporaFederation.callbacks.trigger(:save_person_after_webfinger, person)
person_entity = Person.find_by(diaspora_handle: person.diaspora_id)
expect(person_entity.guid).to eq(person.guid)
expect(person_entity.serialized_public_key).to eq(person.exported_key)
expect(person_entity.url).to eq(person.url)
profile = person.profile
profile_entity = person_entity.profile
expect(profile_entity.first_name).to eq(profile.first_name)
expect(profile_entity.last_name).to eq(profile.last_name)
expect(profile_entity[:image_url]).to be_nil
expect(profile_entity[:image_url_medium]).to be_nil
expect(profile_entity[:image_url_small]).to be_nil
expect(profile_entity.searchable).to eq(profile.searchable)
end
it "creates a new person with images" do
person = DiasporaFederation::Entities::Person.new(
FactoryGirl.attributes_for(
:federation_person_from_webfinger,
profile: DiasporaFederation::Entities::Profile.new(
FactoryGirl.attributes_for(:federation_profile_from_hcard_with_image_url))
)
)
DiasporaFederation.callbacks.trigger(:save_person_after_webfinger, person)
person_entity = Person.find_by(diaspora_handle: person.diaspora_id)
expect(person_entity.guid).to eq(person.guid)
expect(person_entity.serialized_public_key).to eq(person.exported_key)
expect(person_entity.url).to eq(person.url)
profile = person.profile
profile_entity = person_entity.profile
expect(profile_entity.first_name).to eq(profile.first_name)
expect(profile_entity.last_name).to eq(profile.last_name)
expect(profile_entity.image_url).to eq(profile.image_url)
expect(profile_entity.image_url_medium).to eq(profile.image_url_medium)
expect(profile_entity.image_url_small).to eq(profile.image_url_small)
expect(profile_entity.searchable).to eq(profile.searchable)
end
end
context "update profile" do
let(:existing_person_entity) { FactoryGirl.create(:person) }
let(:person) {
DiasporaFederation::Entities::Person.new(
FactoryGirl.attributes_for(:federation_person_from_webfinger,
diaspora_id: existing_person_entity.diaspora_handle)
)
}
it "updates an existing profile" do
DiasporaFederation.callbacks.trigger(:save_person_after_webfinger, person)
person_entity = Person.find_by(diaspora_handle: existing_person_entity.diaspora_handle)
profile = person.profile
profile_entity = person_entity.profile
expect(profile_entity.first_name).to eq(profile.first_name)
expect(profile_entity.last_name).to eq(profile.last_name)
end
it "should not change the existing person" do
DiasporaFederation.callbacks.trigger(:save_person_after_webfinger, person)
person_entity = Person.find_by(diaspora_handle: existing_person_entity.diaspora_handle)
expect(person_entity.guid).to eq(existing_person_entity.guid)
expect(person_entity.serialized_public_key).to eq(existing_person_entity.serialized_public_key)
expect(person_entity.url).to eq(existing_person_entity.url)
end
it "creates profile for existing person if no profile present" do
existing_person_entity.profile = nil
existing_person_entity.save
DiasporaFederation.callbacks.trigger(:save_person_after_webfinger, person)
person_entity = Person.find_by(diaspora_handle: existing_person_entity.diaspora_handle)
profile = person.profile
profile_entity = person_entity.profile
expect(profile_entity.first_name).to eq(profile.first_name)
expect(profile_entity.last_name).to eq(profile.last_name)
end
end
end
end end