Skip to content
Extraits de code Groupes Projets
Valider 663835ed rédigé par Benjamin Neff's avatar Benjamin Neff Validation de Jonne Haß
Parcourir les fichiers

remove old webfinger/hcard code

closes #6310
parent d28e03f0
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de avec 1 ajout et 777 suppressions
......@@ -8,6 +8,7 @@
* Extract StatusMessageService from StatusMessagesController [#6280](https://github.com/diaspora/diaspora/pull/6280)
* Refactor HomeController#toggle\_mobile [#6260](https://github.com/diaspora/diaspora/pull/6260)
* Extract CommentService from CommentsController [#6307](https://github.com/diaspora/diaspora/pull/6307)
* Extract user/profile discovery into the diaspora\_federation-rails gem [#6310](https://github.com/diaspora/diaspora/pull/6310)
## Bug fixes
* Fix indentation and a link title on the default home page [#6212](https://github.com/diaspora/diaspora/pull/6212)
......
......@@ -265,32 +265,6 @@ class Person < ActiveRecord::Base
where(guid: guid, closed_account: false).where.not(owner: nil).take
end
def self.create_from_webfinger(profile, hcard)
return nil if profile.nil? || !profile.valid_diaspora_profile?
new_person = Person.new
new_person.serialized_public_key = profile.public_key
new_person.guid = profile.guid
new_person.diaspora_handle = profile.account
new_person.url = profile.seed_location
#hcard_profile = HCard.find profile.hcard.first[:href]
::Logging::Logger[self].info "event=webfinger_marshal valid=#{new_person.valid?} " \
"target=#{new_person.diaspora_handle}"
new_person.assign_new_profile_from_hcard(hcard)
new_person.save!
new_person.profile.save!
new_person
end
def assign_new_profile_from_hcard(hcard)
self.profile = Profile.new(:first_name => hcard[:given_name],
:last_name => hcard[:family_name],
:image_url => hcard[:photo],
:image_url_medium => hcard[:photo_medium],
:image_url_small => hcard[:photo_small],
:searchable => hcard[:searchable])
end
def remote?
owner_id.nil?
end
......
......@@ -14,13 +14,10 @@ require 'diaspora'
require 'direction_detector'
require 'email_inviter'
require 'evil_query'
require 'h_card'
require 'hydra_wrapper'
require 'postzord'
require 'publisher'
require 'pubsubhubbub'
require 'salmon'
require 'stream'
require 'webfinger'
require 'webfinger_profile'
require 'account_deleter'
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
module HCard
def self.parse(doc)
{
given_name: doc.css(".given_name").text,
family_name: doc.css(".family_name").text,
url: doc.css("#pod_location").text,
photo: doc.css(".entity_photo .photo[src]").attribute("src").text,
photo_small: doc.css(".entity_photo_small .photo[src]").attribute("src").text,
photo_medium: doc.css(".entity_photo_medium .photo[src]").attribute("src").text,
searchable: doc.css(".searchable").text == "true"
}
end
def self.build(raw_hcard)
parse Nokogiri::HTML(raw_hcard)
end
end
# Copyright (c) 2010-2012, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
class Webfinger
include Diaspora::Logging
attr_accessor :host_meta_xrd, :webfinger_profile_xrd,
:webfinger_profile, :hcard, :hcard_xrd, :person,
:account, :ssl
def initialize(account)
self.account = account
self.ssl = true
end
def fetch
return person if existing_person_with_profile?
create_or_update_person_from_webfinger_profile!
end
def self.in_background(account, opts={})
Workers::FetchWebfinger.perform_async(account)
end
#everything below should be private I guess
def account=(str)
@account = str.strip.gsub('acct:','').to_s.downcase
end
def get(url)
logger.info "Getting: #{url} for #{account}"
begin
res = Faraday.get(url)
unless res.success?
raise "Failed to fetch #{url}: #{res.status}"
end
res.body
rescue OpenSSL::SSL::SSLError => e
logger.error "Failed to fetch #{url}: SSL setup invalid"
raise e
rescue => e
logger.error "Failed to fetch: #{url} for #{account}; #{e.message}"
raise e
end
end
def existing_person_with_profile?
cached_person.present? && cached_person.profile.present?
end
def cached_person
self.person ||= Person.by_account_identifier(account)
end
def create_or_update_person_from_webfinger_profile!
logger.info "webfingering #{account}, it is not known or needs updating"
if person #update my profile please
person.assign_new_profile_from_hcard(self.hcard)
else
person = make_person_from_webfinger
end
logger.info "successfully webfingered #{@account}" if person
person
end
#this tries the xrl url with https first, then falls back to http
def host_meta_xrd
begin
get(host_meta_url)
rescue => e
if self.ssl
self.ssl = false
retry
else
raise "there was an error getting the xrd from account #{@account}: #{e.message}"
end
end
end
def hcard
@hcard ||= HCard.build(hcard_xrd)
end
def webfinger_profile
@webfinger_profile ||= WebfingerProfile.new(account, webfinger_profile_xrd)
end
def hcard_url
self.webfinger_profile.hcard
end
def webfinger_profile_url
doc = Nokogiri::XML(self.host_meta_xrd)
return nil if doc.namespaces["xmlns"] != "http://docs.oasis-open.org/ns/xri/xrd-1.0"
swizzle doc.search('Link').find{|x| x['rel']=='lrdd'}['template']
end
def webfinger_profile_xrd
@webfinger_profile_xrd ||= get(webfinger_profile_url)
logger.warn "#{@account} doesn't exists anymore" if @webfinger_profile_xrd == false
@webfinger_profile_xrd
end
def hcard_xrd
@hcard_xrd ||= get(hcard_url)
end
def make_person_from_webfinger
Person.create_from_webfinger(webfinger_profile, hcard) unless webfinger_profile_xrd == false
end
def host_meta_url
domain = account.split('@')[1]
"http#{'s' if self.ssl}://#{domain}/.well-known/host-meta"
end
def swizzle(template)
template.gsub('{uri}', account)
end
end
class WebfingerProfile
include Diaspora::Logging
attr_accessor :webfinger_profile, :account, :links, :hcard, :guid, :public_key, :seed_location
def initialize(account, webfinger_profile)
@account = account
@webfinger_profile = webfinger_profile
@links = {}
set_fields
end
def valid_diaspora_profile?
!(@webfinger_profile.nil? || @account.nil? || @links.nil? || @hcard.nil? ||
@guid.nil? || @public_key.nil? || @seed_location.nil? )
end
private
def set_fields
doc = Nokogiri::XML.parse(webfinger_profile)
doc.remove_namespaces!
account_string = doc.css('Subject').text.gsub('acct:', '').strip
raise "account in profile(#{account_string}) and account requested (#{@account}) do not match" if account_string != @account
doc.css('Link').each do |l|
rel = text_of_attribute(l, 'rel')
href = text_of_attribute(l, 'href')
@links[rel] = href
case rel
when "http://microformats.org/profile/hcard"
@hcard = href
when "http://joindiaspora.com/guid"
@guid = href
when "http://joindiaspora.com/seed_location"
@seed_location = href
end
end
begin
pubkey = text_of_attribute( doc.at('Link[rel=diaspora-public-key]'), 'href')
@public_key = Base64.decode64 pubkey
rescue => e
logger.warn "event=invalid_profile identifier=#{@account}"
end
end
def text_of_attribute(doc, attr)
doc.attribute(attr) ? doc.attribute(attr).text : nil
end
end
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require "spec_helper"
# this is temporarily needed for fixture generation
# TODO: remove this after the parsing is also in the diaspora_federation gem
describe DiasporaFederation do
routes { DiasporaFederation::Engine.routes }
let(:fixture_path) { Rails.root.join("spec", "fixtures") }
describe DiasporaFederation::WebfingerController, type: :controller do
it "generates the host_meta fixture", fixture: true do
get :host_meta
expect(response).to be_success
expect(response.body).to match(/webfinger/)
save_fixture(response.body, "host-meta", fixture_path)
end
it "generates the webfinger fixture", fixture: true do
post :legacy_webfinger, "q" => alice.person.diaspora_handle
expect(response).to be_success
save_fixture(response.body, "webfinger", fixture_path)
end
end
describe DiasporaFederation::HCardController, type: :controller do
it "generates the hCard fixture", fixture: true do
post :hcard, "guid" => alice.person.guid.to_s
expect(response).to be_success
save_fixture(response.body, "hcard", fixture_path)
end
end
end
<body id="hcard">
<div id="wrap">
<div id="core">
<dl id="site_nav_local_views">
<dt>Local views</dt>
<dd></dd>
</dl>
<div id="content">
<h1>Evan Prodromou</h1>
<div id="content_inner">
<div id="i" class="entity_profile vcard author">
<h2>User profile</h2>
<dl class="entity_depiction">
<dt>Photo</dt>
<dd>
<img src="http://avatar.status.net/evan/1-96-20100726204409.jpeg" class="photo avatar" width="96" height="96" alt="evan"/>
</dd>
</dl>
<dl class="entity_nickname">
<dt>Nickname</dt>
<dd>
<a href="http://evan.status.net/" rel="me" class="nickname url uid">evan</a>
</dd>
</dl>
<dl class="entity_fn">
<dt>Full name</dt>
<dd>
<span class="fn">Evan Prodromou</span>
</dd>
</dl>
<dl class="entity_location">
<dt>Location</dt>
<dd class="label">Montreal, QC, Canada</dd>
</dl>
<dl class="entity_url">
<dt>URL</dt>
<dd>
<a href="http://evan.prodromou.name/" rel="me" class="url">http://evan.prodromou.name/</a>
</dd>
</dl>
<dl class="entity_note">
<dt>Note</dt>
<dd class="note">Montreal hacker and entrepreneur. Founder of identi.ca, lead developer of StatusNet, CEO of StatusNet Inc.</dd>
</dl>
</div>
</div>
</div>
</div>
<div id="footer">
<dl id="licenses">
<dt id="site_statusnet_license">StatusNet software license</dt>
<dd><p><strong>Evan Prodromou</strong> is a microblogging service brought to you by <a href="http://status.net/">Status.net</a>. It runs the <a href="http://status.net/">StatusNet</a> microblogging software, version 0.9.5, available under the <a href="http://www.fsf.org/licensing/licenses/agpl-3.0.html">GNU Affero General Public License</a>.</p>
</dd>
<dt id="site_content_license">Site content license</dt>
<dd id="site_content_license_cc">
<p>
<img id="license_cc" src="http://i.creativecommons.org/l/by/3.0/80x15.png" alt="Creative Commons Attribution 3.0" width="80" height="15"/>:w
All Evan Prodromou content and data are available under the <a class="license" rel="external license" href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0</a> license.</p>
</dd>
</dl>
</div>
</div>
</body>
<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
<Subject>acct:tom@tom.joindiaspora.com</Subject>
<Alias>"http://tom.joindiaspora.com/"</Alias>
<Link rel="http://microformats.org/profile/hcard" type="text/html" href="http://tom.joindiaspora.com/hcard/users/4c8eccce34b7da59ff000002"/>
<Link rel="http://joindiaspora.com/seed_location" type="text/html" href="http://tom.joindiaspora.com/"/>
<Link rel="http://joindiaspora.com/guid" type="text/html" href="4c8eccce34b7da59ff000002"/>
<Link rel='http://webfinger.net/rel/profile-page' type='text/html' href="http://tom.joindiaspora.com/u/tom"/>
<Link rel="http://schemas.google.com/g/2010#updates-from" type="application/atom+xml" href="http://tom.joindiaspora.com/u/tom.atom"/>
<Link rel="diaspora-public-key" type="RSA" href="LS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tCk1JSUNDZ0tDQWdFQXlt dHpUdWQ3SytXQklPVVYwMmxZN2Z1NjdnNWQrbTBra1ZIQlgzTk1uYXB5bnZL a0VSemoKbkxma2JrTVpEVGdPNG1UaThmWFI3Q1ZSK3Q1SFN4b2Vub0JWazVX eUFabkEzWmpTRjBPcC9RakhlYzhvK0dVSApDOFluNFJ5N01hQ0R1cUNpNnJv c2RlbUlLTm1Fa2dsVVY1VzZ4WFd4Vmtrb21oL2VCQ2FmaVdMTXFRMG82NGox Ckw3aXNjQjVOM3ZkbnBrUmU3SkFxLzNDUTI3dWhDS0ZIWG1JYm1iVmhJQTNC R0J6YStPV3NjK1Z5cjV0Mm1wSlIKU1RXMk9UL20rS0NPK21kdnpmenQ0TzEr UHc1M1pJMjRpMlc2cW1XdThFZ1Z6QVcyMStuRGJManZiNHpzVHlrNQppN1JM cG8rUFl2VUJlLy8wM1lkQUJoRlJhVXpTL0RtcWRubEVvb0VvK0VmYzRkQ1NF bWVkMUgrek01c2xqQm1rCks5amsvOHNQZDB0SVZmMWZXdW9BcWZTSmErSXdr OHNybkdZbEVlaFV1dVhIY0x2b2JlUXJKYWdiRGc1Qll5TnIKeTAzcHpKTHlS ZU9UcC9RK1p0TXpMOFJMZWJsUzlWYXdNQzNDVzc5K0RGditTWGZ0eTl3NC8w d2NpUHpKejg2bgp2VzJ5K3crTThOWG52enBWNU81dGI4azZxZ2N0WjBmRzFu eXQ0RklsSHNVaUVoNnZLZmNLSmFPeWFRSGNGcWVxCjkwUkpoMm9TMDJBdFJx TFRSWDJJQjFnYXZnWEFXN1NYanJNbUNlVzlCdVBKYU5nZkp3WFFaelVoa0tC V1k0VnMKZTRFVWRob3R5RWkvUmE0RXVZU01ZcnZEeUFRUHJsY0wveDliaU1p bHVPcU9OMEpJZ1VodEZQRUNBd0VBQVE9PQotLS0tLUVORCBSU0EgUFVCTElD IEtFWS0tLS0tCg== "/>
</XRD>
<div id='content'>
<h1>Alexander Hamiltom</h1>
<div id='content_inner'>
<div class='entity_profile vcard author' id='i'>
<h2>User profile</h2>
<dl class='entity_nickname'>
<dt>Nickname</dt>
<dd>
<a class='nickname url uid' href='http://localhost:3000/' rel='me'>Alexander Hamiltom</a>
</dd>
</dl>
<dl class='entity_given_name'>
<dt>First name</dt>
<dd>
<span class='given_name'>Alexander</span>
</dd>
</dl>
<dl class='entity_family_name'>
<dt>Family name</dt>
<dd>
<span class='family_name'>Hamiltom</span>
</dd>
</dl>
<dl class='entity_fn'>
<dt>Full name</dt>
<dd>
<span class='fn'>Alexander Hamiltom</span>
</dd>
</dl>
<dl class='entity_url'>
<dt>URL</dt>
<dd>
<a class='url' href='http://localhost:3000/' id='pod_location' rel='me'>http://localhost:3000/</a>
</dd>
</dl>
<dl class='entity_photo'>
<dt>Photo</dt>
<dd>
<img class='photo avatar' height='300px' src='http://localhost:3000/uploads/images/thumb_large_8rxQAwC4Vx4cf5667d37db5b0fef000003.jpg' width='300px'>
</dd>
</dl>
<dl class='entity_photo_medium'>
<dt>Photo</dt>
<dd>
<img class='photo avatar' height='100px' src='http://localhost:3000/uploads/images/thumb_medium_8rxQAwC4Vx4cf5667d37db5b0fef000003.jpg' width='100px'>
</dd>
</dl>
<dl class='entity_photo_small'>
<dt>Photo</dt>
<dd>
<img class='photo avatar' height='50px' src='http://localhost:3000/uploads/images/thumb_small_8rxQAwC4Vx4cf5667d37db5b0fef000003.jpg' width='50px'>
</dd>
</dl>
<dl class='entity_searchable'>
<dt>Searchable</dt>
<dd>
<span class='searchable'>false</span>
</dd>
</dl>
</div>
</div>
</div>
<?xml version='1.0' encoding='UTF-8'?>
<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>
<Link rel='lrdd'
template='http://tom.joindiaspora.com/webfinger/?q={uri}'>
<Title>Resource Descriptor</Title>
</Link>
</XRD>
<XRD>
<Subject>acct:evan@status.net</Subject>
<Alias>acct:evan@evan.status.net</Alias>
<Alias>http://evan.status.net/user/1</Alias>
<Link rel="http://webfinger.net/rel/profile-page" type="text/html" href="http://evan.status.net/user/1"/>
<Link rel="http://schemas.google.com/g/2010#updates-from" href="http://evan.status.net/api/statuses/user_timeline/1.atom" type="application/atom+xml"/>
<Link rel="http://microformats.org/profile/hcard" type="text/html" href="http://evan.status.net/hcard"/>
<Link rel="http://gmpg.org/xfn/11" type="text/html" href="http://evan.status.net/user/1"/>
<Link rel="describedby" type="application/rdf+xml" href="http://evan.status.net/foaf"/>
<Link rel="salmon" href="http://evan.status.net/main/salmon/user/1"/>
<Link rel="http://salmon-protocol.org/ns/salmon-replies" href="http://evan.status.net/main/salmon/user/1"/>
<Link rel="http://salmon-protocol.org/ns/salmon-mention" href="http://evan.status.net/main/salmon/user/1"/>
<Link rel="magic-public-key" href="data:application/magic-public-key,RSA.vyohOlwX03oJUg6R8BQP4V-6QQUfPg9gzOwk3ENQjqeGorHN8RNI4rhCQp7tACe9DEdEKtzZHbSvQC2zRICQ9JG_SIcpiU9jcT2imN5cPLZZQuPFZWwG4xPu_8LKRHuXeLGkzQMjvg6jFBl7qdo_iPnlbtIBb-mEuAnfRMcdUPE=.AQAB"/>
<Link rel="http://ostatus.org/schema/1.0/subscribe" template="http://evan.status.net/main/ostatussub?profile={uri}"/>
</XRD>
......@@ -19,46 +19,6 @@ module HelperMethods
:receiving => true)
end
def stub_success(address = 'abc@example.com', opts = {})
host = address.split('@')[1]
stub_request(:get, "https://#{host}/.well-known/host-meta").to_return(:status => 200, :body => host_xrd)
stub_request(:get, "http://#{host}/.well-known/host-meta").to_return(:status => 200, :body => host_xrd)
if opts[:diaspora] || host.include?("diaspora")
stub_request(:get, /webfinger\/\?q=#{address}/).to_return(:status => 200, :body => finger_xrd)
stub_request(:get, "http://#{host}/hcard/users/4c8eccce34b7da59ff000002").to_return(:status => 200, :body => hcard_response)
else
stub_request(:get, /webfinger\/\?q=#{address}/).to_return(:status => 200, :body => nonseed_finger_xrd)
stub_request(:get, 'http://evan.status.net/hcard').to_return(:status => 200, :body => evan_hcard)
end
end
def stub_failure(address = 'abc@example.com')
host = address.split('@')[1]
stub_request(:get, "https://#{host}/.well-known/host-meta").to_return(:status => 200, :body => host_xrd)
stub_request(:get, "http://#{host}/.well-known/host-meta").to_return(:status => 200, :body => host_xrd)
stub_request(:get, /webfinger\/\?q=#{address}/).to_return(:status => 500)
end
def host_xrd
File.open(File.dirname(__FILE__) + '/fixtures/host_xrd').read
end
def finger_xrd
File.open(File.dirname(__FILE__) + '/fixtures/finger_xrd').read
end
def hcard_response
File.open(File.dirname(__FILE__) + '/fixtures/hcard_response').read
end
def nonseed_finger_xrd
File.open(File.dirname(__FILE__) + '/fixtures/nonseed_finger_xrd').read
end
def evan_hcard
File.open(File.dirname(__FILE__) + '/fixtures/evan_hcard').read
end
def uploaded_photo
fixture_filename = 'button.png'
fixture_name = File.join(File.dirname(__FILE__), 'fixtures', fixture_filename)
......
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require "spec_helper"
describe HCard do
it "should parse an hcard" do
raw_hcard = hcard_response
hcard = HCard.build raw_hcard
expect(hcard[:family_name].include?("Hamiltom")).to be true
expect(hcard[:given_name].include?("Alex")).to be true
expect(hcard[:photo].include?("thumb_large")).to be true
expect(hcard[:photo_medium].include?("thumb_medium")).to be true
expect(hcard[:photo_small].include?("thumb_small")).to be true
expect(hcard[:url]).to eq("http://localhost:3000/")
expect(hcard[:searchable]).to eq(false)
end
it "should parse an hcard with searchable true" do
raw_hcard = hcard_response.sub("<span class='searchable'>false</span>", "<span class='searchable'>true</span>")
hcard = HCard.build raw_hcard
expect(hcard[:family_name].include?("Hamiltom")).to be true
expect(hcard[:given_name].include?("Alex")).to be true
expect(hcard[:photo].include?("thumb_large")).to be true
expect(hcard[:photo_medium].include?("thumb_medium")).to be true
expect(hcard[:photo_small].include?("thumb_small")).to be true
expect(hcard[:url]).to eq("http://localhost:3000/")
expect(hcard[:searchable]).to eq(true)
end
it "should parse an hcard with empty searchable" do
raw_hcard = hcard_response.sub("<span class='searchable'>false</span>", "<span class='searchable'></span>")
hcard = HCard.build raw_hcard
expect(hcard[:family_name].include?("Hamiltom")).to be true
expect(hcard[:given_name].include?("Alex")).to be true
expect(hcard[:photo].include?("thumb_large")).to be true
expect(hcard[:photo_medium].include?("thumb_medium")).to be true
expect(hcard[:photo_small].include?("thumb_small")).to be true
expect(hcard[:url]).to eq("http://localhost:3000/")
expect(hcard[:searchable]).to eq(false)
end
end
require 'spec_helper'
describe WebfingerProfile do
let(:webfinger_profile){File.open(Rails.root.join("spec", "fixtures", "finger_xrd")).read.strip}
let(:not_diaspora_webfinger){File.open(Rails.root.join("spec", "fixtures", "nonseed_finger_xrd")).read.strip}
let(:account){"tom@tom.joindiaspora.com"}
let(:profile){ WebfingerProfile.new(account, webfinger_profile) }
context "parsing a diaspora profile" do
describe '#valid_diaspora_profile?' do
it 'should check all of the required fields' do
expect(manual_nil_check(profile)).to eq(profile.valid_diaspora_profile?)
end
end
describe '#set_fields' do
it 'should check to make sure it has a the right webfinger profile' do
expect{ WebfingerProfile.new("nottom@tom.joindiaspora.com", webfinger_profile)}.to raise_error
end
it 'should handle a non-diaspora profile without blowing up' do
expect{ WebfingerProfile.new("evan@status.net", not_diaspora_webfinger)}.not_to raise_error
end
[:links, :hcard, :guid, :seed_location, :public_key].each do |field|
it 'should sets the #{field} field' do
expect(profile.send(field)).to be_present
end
end
end
end
def manual_nil_check(profile)
profile.instance_variables.each do |var|
var = var.to_s.gsub('@', '')
return false if profile.send(var).nil? == true
end
true
end
end
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'spec_helper'
describe Webfinger do
let(:host_meta_xrd) { File.open(Rails.root.join('spec', 'fixtures', 'host-meta.fixture.html')).read }
let(:webfinger_xrd) { File.open(Rails.root.join('spec', 'fixtures', 'webfinger.fixture.html')).read }
let(:hcard_xml) { File.open(Rails.root.join('spec', 'fixtures', 'hcard.fixture.html')).read }
let(:account){'foo@bar.com'}
let(:account_in_fixtures){"alice@localhost:9887"}
let(:finger){Webfinger.new(account)}
let(:host_meta_url){"http://#{AppConfig.pod_uri.authority}/webfinger?q="}
describe '#intialize' do
it 'sets account ' do
n = Webfinger.new("mbs348@gmail.com")
expect(n.account).not_to be nil
end
it "downcases account and strips whitespace, and gsub 'acct:'" do
n = Webfinger.new("acct:BIGBOY@Example.Org ")
expect(n.account).to eq('bigboy@example.org')
end
it 'should set ssl as the default' do
foo = Webfinger.new(account)
expect(foo.ssl).to be true
end
end
describe '.in_background' do
it 'enqueues a Workers::FetchWebfinger job' do
expect(Workers::FetchWebfinger).to receive(:perform_async).with(account)
Webfinger.in_background(account)
end
end
describe '#fetch' do
it 'works' do
finger = Webfinger.new(account_in_fixtures)
allow(finger).to receive(:host_meta_xrd).and_return(host_meta_xrd)
allow(finger).to receive(:hcard_xrd).and_return(hcard_xml)
allow(finger).to receive(:webfinger_profile_xrd).and_return(webfinger_xrd)
person = finger.fetch
expect(person).to be_valid
expect(person).to be_a Person
end
end
describe '#get' do
it 'makes a request and grabs the body' do
url ="https://bar.com/.well-known/host-meta"
stub_request(:get, url).
to_return(:status => 200, :body => host_meta_xrd)
expect(finger.get(url)).to eq(host_meta_xrd)
end
it 'follows redirects' do
redirect_url = "http://whereami.whatisthis/host-meta"
stub_request(:get, "https://bar.com/.well-known/host-meta").
to_return(:status => 302, :headers => { 'Location' => redirect_url })
stub_request(:get, redirect_url).
to_return(:status => 200, :body => host_meta_xrd)
finger.host_meta_xrd
expect(a_request(:get, redirect_url)).to have_been_made
end
it 'raises on 404' do
url ="https://bar.com/.well-known/host-meta"
stub_request(:get, url).
to_return(:status => 404, :body => nil)
expect {
expect(finger.get(url)).to eq(false)
}.to raise_error
end
end
describe 'existing_person_with_profile?' do
it 'returns true if cached_person is present and has a profile' do
expect(finger).to receive(:cached_person).twice.and_return(FactoryGirl.create(:person))
expect(finger.existing_person_with_profile?).to be true
end
it 'returns false if it has no person' do
allow(finger).to receive(:cached_person).and_return false
expect(finger.existing_person_with_profile?).to be false
end
it 'returns false if the person has no profile' do
p = FactoryGirl.create(:person)
p.profile = nil
allow(finger).to receive(:cached_person).and_return(p)
expect(finger.existing_person_with_profile?).to be false
end
end
describe 'cached_person' do
it 'sets the person by looking up the account from Person.by_account_identifier' do
person = double
expect(Person).to receive(:by_account_identifier).with(account).and_return(person)
expect(finger.cached_person).to eq(person)
expect(finger.person).to eq(person)
end
end
describe 'create_or_update_person_from_webfinger_profile!' do
context 'with a cached_person' do
it 'calls Person#assign_new_profile_from_hcard with the fetched hcard' do
finger.hcard_xrd = hcard_xml
allow(finger).to receive(:person).and_return(bob.person)
expect(bob.person).to receive(:assign_new_profile_from_hcard).with(finger.hcard)
finger.create_or_update_person_from_webfinger_profile!
end
end
context 'with no cached person' do
it 'sets person based on make_person_from_webfinger' do
allow(finger).to receive(:person).and_return(nil)
expect(finger).to receive(:make_person_from_webfinger)
finger.create_or_update_person_from_webfinger_profile!
end
end
end
describe '#host_meta_xrd' do
it 'calls #get with host_meta_url' do
allow(finger).to receive(:host_meta_url).and_return('meta')
expect(finger).to receive(:get).with('meta')
finger.host_meta_xrd
end
it 'should retry with ssl off a second time' do
expect(finger).to receive(:get).and_raise(StandardError)
expect(finger).to receive(:get)
finger.host_meta_xrd
expect(finger.ssl).to be false
end
end
describe '#hcard' do
it 'calls HCard.build' do
allow(finger).to receive(:hcard_xrd).and_return(hcard_xml)
expect(HCard).to receive(:build).with(hcard_xml).and_return true
expect(finger.hcard).not_to be_nil
end
end
describe '#webfinger_profile' do
it 'constructs a new WebfingerProfile object' do
allow(finger).to receive(:webfinger_profile_xrd).and_return(webfinger_xrd)
expect(WebfingerProfile).to receive(:new).with(account, webfinger_xrd)
finger.webfinger_profile
end
end
describe '#webfinger_profile_url' do
it 'returns the llrd link for a valid host meta' do
allow(finger).to receive(:host_meta_xrd).and_return(host_meta_xrd)
expect(finger.webfinger_profile_url).not_to be_nil
end
it 'returns nil if no link is found' do
allow(finger).to receive(:host_meta_xrd).and_return(nil)
expect(finger.webfinger_profile_url).to be_nil
end
end
describe '#webfinger_profile_xrd' do
it 'calls #get with the hcard_url' do
allow(finger).to receive(:hcard_url).and_return("url")
expect(finger).to receive(:get).with("url")
finger.hcard_xrd
end
end
describe '#make_person_from_webfinger' do
it 'with an hcard and a webfinger_profile, it calls Person.create_from_webfinger' do
allow(finger).to receive(:hcard).and_return("hcard")
allow(finger).to receive(:webfinger_profile_xrd).and_return("webfinger_profile_xrd")
allow(finger).to receive(:webfinger_profile).and_return("webfinger_profile")
expect(Person).to receive(:create_from_webfinger).with("webfinger_profile", "hcard")
finger.make_person_from_webfinger
end
it 'with an false xrd it does not call Person.create_from_webfinger' do
allow(finger).to receive(:webfinger_profile_xrd).and_return(false)
expect(Person).not_to receive(:create_from_webfinger)
finger.make_person_from_webfinger
end
end
describe '#host_meta_url' do
it 'should return canonical host-meta url for http' do
finger.ssl = false
expect(finger.host_meta_url).to eq("http://bar.com/.well-known/host-meta")
end
it 'can return the https version' do
expect(finger.host_meta_url).to eq("https://bar.com/.well-known/host-meta")
end
end
describe 'swizzle' do
it 'gsubs out {uri} for the account' do
string = "{uri} is the coolest"
expect(finger.swizzle(string)).to eq("#{finger.account} is the coolest")
end
end
end
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter