From 3a94930dcdb2175fe75147db964f7ff427a249da Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Fri, 17 Jul 2015 23:41:21 +0200 Subject: [PATCH] add test for Discovery --- lib/diaspora_federation/discovery.rb | 1 + .../discovery/discovery_spec.rb | 192 ++++++++++++++++++ spec/spec_helper.rb | 3 - spec/support/fixture_generation.rb | 6 + 4 files changed, 199 insertions(+), 3 deletions(-) create mode 100644 spec/lib/diaspora_federation/discovery/discovery_spec.rb diff --git a/lib/diaspora_federation/discovery.rb b/lib/diaspora_federation/discovery.rb index d917b32..95cd7e9 100644 --- a/lib/diaspora_federation/discovery.rb +++ b/lib/diaspora_federation/discovery.rb @@ -11,3 +11,4 @@ require "diaspora_federation/discovery/xrd_document" require "diaspora_federation/discovery/host_meta" require "diaspora_federation/discovery/web_finger" require "diaspora_federation/discovery/h_card" +require "diaspora_federation/discovery/discovery" diff --git a/spec/lib/diaspora_federation/discovery/discovery_spec.rb b/spec/lib/diaspora_federation/discovery/discovery_spec.rb new file mode 100644 index 0000000..f209495 --- /dev/null +++ b/spec/lib/diaspora_federation/discovery/discovery_spec.rb @@ -0,0 +1,192 @@ +module DiasporaFederation + describe Discovery::Discovery do + let(:host_meta_xrd) { FixtureGeneration.load_fixture("host-meta") } + let(:webfinger_xrd) { FixtureGeneration.load_fixture("legacy-webfinger") } + let(:hcard_html) { FixtureGeneration.load_fixture("hcard") } + let(:account) { alice.diaspora_handle } + let(:default_image) { "http://localhost:3000/assets/user/default.png" } + + describe "#intialize" do + it "sets handle" do + discovery = Discovery::Discovery.new("some_user@example.com") + expect(discovery.handle).to eq("some_user@example.com") + end + + it "downcases account and strips whitespace, and sub 'acct:'" do + discovery = Discovery::Discovery.new("acct:BIGBOY@Example.Com ") + expect(discovery.handle).to eq("bigboy@example.com") + end + end + + describe ".fetch" do + it "fetches the userdata and returns a person object" do + stub_request(:get, "https://localhost:3000/.well-known/host-meta") + .to_return(status: 200, body: host_meta_xrd) + stub_request(:get, "http://localhost:3000/webfinger?q=acct:#{account}") + .to_return(status: 200, body: webfinger_xrd) + stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}") + .to_return(status: 200, body: hcard_html) + + person = Discovery::Discovery.new(account).fetch + + expect(person.guid).to eq(alice.guid) + expect(person.diaspora_handle).to eq(account) + expect(person.url).to eq(alice.url) + expect(person.exported_key).to eq(alice.serialized_public_key) + + profile = person.profile + + expect(profile.diaspora_handle).to eq(alice.diaspora_handle) + expect(profile.first_name).to eq("Dummy") + expect(profile.last_name).to eq("User") + + expect(profile.image_url).to eq(default_image) + expect(profile.image_url_medium).to eq(default_image) + expect(profile.image_url_small).to eq(default_image) + end + + it "falls back to http if https fails with 404" do + stub_request(:get, "https://localhost:3000/.well-known/host-meta") + .to_return(status: 404) + stub_request(:get, "http://localhost:3000/.well-known/host-meta") + .to_return(status: 200, body: host_meta_xrd) + stub_request(:get, "http://localhost:3000/webfinger?q=acct:#{account}") + .to_return(status: 200, body: webfinger_xrd) + stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}") + .to_return(status: 200, body: hcard_html) + + person = Discovery::Discovery.new(account).fetch + + expect(person.guid).to eq(alice.guid) + expect(person.diaspora_handle).to eq(account) + end + + it "falls back to http if https fails with ssl error" do + stub_request(:get, "https://localhost:3000/.well-known/host-meta") + .to_raise(OpenSSL::SSL::SSLError) + stub_request(:get, "http://localhost:3000/.well-known/host-meta") + .to_return(status: 200, body: host_meta_xrd) + stub_request(:get, "http://localhost:3000/webfinger?q=acct:#{account}") + .to_return(status: 200, body: webfinger_xrd) + stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}") + .to_return(status: 200, body: hcard_html) + + person = Discovery::Discovery.new(account).fetch + + expect(person.guid).to eq(alice.guid) + expect(person.diaspora_handle).to eq(account) + end + + it "fails if the handle does not match" do + modified_webfinger = webfinger_xrd.gsub(account, "anonther_user@example.com") + + stub_request(:get, "https://localhost:3000/.well-known/host-meta") + .to_return(status: 200, body: host_meta_xrd) + stub_request(:get, "http://localhost:3000/webfinger?q=acct:#{account}") + .to_return(status: 200, body: modified_webfinger) + + expect { Discovery::Discovery.new(account).fetch }.to raise_error Discovery::DiscoveryError + end + + it "fails if the handle was not found" do + stub_request(:get, "https://localhost:3000/.well-known/host-meta") + .to_return(status: 200, body: host_meta_xrd) + stub_request(:get, "http://localhost:3000/webfinger?q=acct:#{account}") + .to_return(status: 404) + + expect { Discovery::Discovery.new(account).fetch }.to raise_error Discovery::DiscoveryError + end + + it "reads old hcard without guid and public key" do + historic_hcard_html = <<-HTML +
+

#{account}

+
+
+

User profile

+
+
Nickname
+
+ +
+
+
+
First name
+
+ +
+
+
+
Family name
+
+ +
+
+
+
Full name
+
+ +
+
+
+
URL
+
+#{alice.url} +
+
+
+
Photo
+
+ +
+
+
+
Photo
+
+ +
+
+
+
Photo
+
+ +
+
+
+
Searchable
+
+true +
+
+
+
+
+ HTML + + stub_request(:get, "https://localhost:3000/.well-known/host-meta") + .to_return(status: 200, body: host_meta_xrd) + stub_request(:get, "http://localhost:3000/webfinger?q=acct:#{account}") + .to_return(status: 200, body: webfinger_xrd) + stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}") + .to_return(status: 200, body: historic_hcard_html) + + person = Discovery::Discovery.new(account).fetch + + expect(person.guid).to eq(alice.guid) + expect(person.diaspora_handle).to eq(account) + expect(person.url).to eq(alice.url) + expect(person.exported_key).to eq(alice.serialized_public_key) + + profile = person.profile + + expect(profile.diaspora_handle).to eq(alice.diaspora_handle) + expect(profile.first_name).to be_nil + expect(profile.last_name).to be_nil + + expect(profile.image_url).to eq(default_image) + expect(profile.image_url_medium).to eq(default_image) + expect(profile.image_url_small).to eq(default_image) + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9767095..4d25d10 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -32,9 +32,6 @@ def alice @alice ||= Person.find_by(diaspora_handle: "alice@localhost:3000") end -# Force fixture rebuild -FileUtils.rm_f(Rails.root.join("tmp", "fixture_builder.yml")) - # Requires supporting files with custom matchers and macros, etc, # in ./support/ and its subdirectories. fixture_builder_file = "#{File.dirname(__FILE__)}/support/fixture_builder.rb" diff --git a/spec/support/fixture_generation.rb b/spec/support/fixture_generation.rb index d7081e7..e06dec1 100644 --- a/spec/support/fixture_generation.rb +++ b/spec/support/fixture_generation.rb @@ -9,6 +9,12 @@ module FixtureGeneration file.puts(markup) end end + + def self.load_fixture(name, fixture_path=nil) + fixture_path = Rails.root.join("tmp", "fixtures") unless fixture_path + fixture_file = fixture_path.join("#{name}.fixture.html") + File.open(fixture_file).read + end end RSpec::Rails::ControllerExampleGroup.class_eval do