1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'spec_helper'
require File.join(Rails.root, 'lib/em-webfinger')
describe EMWebfinger do
let(:user1) { make_user }
let(:user2) { make_user }
let(:account) {"foo@tom.joindiaspora.com"}
let(:person){ Factory(:person, :diaspora_handle => account)}
let(:finger){EMWebfinger.new(account)}
let(:good_request) { FakeHttpRequest.new(:success)}
let(:stub_good) {EventMachine::HttpRequest.stub!(:new).and_return(good_request)}
let(:stub_bad) {EventMachine::HttpRequest.stub!(:new).and_return(bad_request)}
let(:diaspora_xrd) {File.open(File.join(Rails.root, 'spec/fixtures/host_xrd')).read}
let(:diaspora_finger) {File.open(File.join(Rails.root, 'spec/fixtures/finger_xrd')).read}
let(:hcard_xml) {File.open(File.join(Rails.root, 'spec/fixtures/hcard_response')).read}
let(:non_diaspora_xrd) {File.open(File.join(Rails.root, 'spec/fixtures/nonseed_finger_xrd')).read}
let(:non_diaspora_hcard) {File.open(File.join(Rails.root, 'spec/fixtures/evan_hcard')).read}
context 'setup' do
let(:action){ Proc.new{|person| puts person.inspect }}
describe '#intialize' do
it 'sets account ' do
n = EMWebfinger.new("mbs348@gmail.com")
n.instance_variable_get(:@account).should_not be nil
end
it 'should raise an error on an unresonable email' do
proc{
EMWebfinger.new("joe.valid.email@my-address.com")
}.should_not raise_error(RuntimeError, "Identifier is invalid")
end
it 'should not allow port numbers' do
proc{
EMWebfinger.new('eviljoe@diaspora.local:3000')
}.should raise_error(RuntimeError, "Identifier is invalid")
end
end
describe '#on_person' do
it 'should set a callback' do
n = EMWebfinger.new("mbs@gmail.com")
n.stub(:fetch).and_return(true)
n.on_person{|person| puts "foo"}
n.instance_variable_get(:@callbacks).count.should be 1
end
it 'should not blow up if the returned xrd is nil' do
http = FakeHttpRequest.new(:success)
fake_account = 'foo@example.com'
http.callbacks = ['']
EventMachine::HttpRequest.should_receive(:new).and_return(http)
n = EMWebfinger.new("foo@example.com")
n.on_person{|person|
person.should == "webfinger does not seem to be enabled for #{fake_account}'s host"
}
end
end
describe '#fetch' do
it 'should require a callback' do
proc{finger.fetch }.should raise_error "you need to set a callback before calling fetch"
end
end
context 'webfinger query chain processing' do
describe '#webfinger_profile_url' do
it 'should parse out the webfinger template' do
finger.send(:webfinger_profile_url, diaspora_xrd).should == "http://tom.joindiaspora.com/webfinger/?q=#{account}"
end
it 'should return nil if not an xrd' do
finger.send(:webfinger_profile_url, '<html></html>').should be nil
end
it 'should return the template for xrd' do
finger.send(:webfinger_profile_url, diaspora_xrd).should == 'http://tom.joindiaspora.com/webfinger/?q=foo@tom.joindiaspora.com'
end
end
describe '#xrd_url' do
it 'should return canonical host-meta url' do
finger.send(:xrd_url).should == "http://tom.joindiaspora.com/.well-known/host-meta"
end
it 'can return the https version' do
finger.send(:xrd_url, true).should == "https://tom.joindiaspora.com/.well-known/host-meta"
end
end
end
context 'webfingering local people' do
it 'should return a person from the database if it matches its handle' do
person.save
EM.run do
finger.on_person { |p|
p.should == person
EM.stop
}
end
end
it 'should fetch a diaspora webfinger and make a person for them' do
good_request.callbacks = [diaspora_xrd, diaspora_finger, hcard_xml]
#new_person = Factory.build(:person, :diaspora_handle => "tom@tom.joindiaspora.com")
# http://tom.joindiaspora.com/.well-known/host-meta
f = EMWebfinger.new("tom@tom.joindiaspora.com")
EventMachine::HttpRequest.should_receive(:new).exactly(3).times.and_return(good_request)
EM.run {
f.on_person{ |p|
p.valid?.should be true
EM.stop
}
}
end
end
end
end
|