username can be any case, but diaspora handle should always, always, ALWAYS be lowercase, for webfinger reasons

This commit is contained in:
maxwell 2010-10-18 22:55:09 -07:00
parent 8894fa4daa
commit 8ada8414c7
5 changed files with 126 additions and 4 deletions

View file

@ -27,12 +27,21 @@ class Person
timestamps! timestamps!
before_save :strip_and_downcase_diaspora_handle
before_destroy :remove_all_traces before_destroy :remove_all_traces
before_validation :clean_url before_validation :clean_url
validates_presence_of :url, :profile, :serialized_public_key validates_presence_of :url, :profile, :serialized_public_key
validates_format_of :url, :with => validates_format_of :url, :with =>
/^(https?):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*(\.[a-z]{2,5})?(:[0-9]{1,5})?(\/.*)?$/ix /^(https?):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*(\.[a-z]{2,5})?(:[0-9]{1,5})?(\/.*)?$/ix
def strip_and_downcase_diaspora_handle
if self.diaspora_handle
self.diaspora_handle.strip!
self.diaspora_handle.downcase!
end
end
def self.search(query) def self.search(query)
return Person.all if query.to_s.empty? return Person.all if query.to_s.empty?
query_tokens = query.to_s.strip.split(" ") query_tokens = query.to_s.strip.split(" ")

View file

@ -388,7 +388,7 @@ class User
end end
def diaspora_handle def diaspora_handle
"#{self.username}@#{APP_CONFIG[:terse_pod_url]}" "#{self.username}@#{APP_CONFIG[:terse_pod_url]}".downcase
end end
def as_json(opts={}) def as_json(opts={})

View file

@ -0,0 +1,103 @@
# 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) { Factory(:user) }
let(:user2) { Factory(:user) }
let(:account) {"tom@tom.joindiaspora.com"}
let(:finger){EMWebfinger.new(account)}
let(:person){ Factory(:person) }
let(:good_request) { FakeHttpRequest.new(:success)}
let(:bad_request) { FakeHttpRequest.new(:failure)}
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'))}
let(:diaspora_finger) {File.open(File.join(Rails.root, 'spec/fixtures/finger_xrd'))}
let(:hcard_xml) {File.open(File.join(Rails.root, 'spec/fixtures/hcard_response'))}
let(:non_diaspora_xrd) {File.open(File.join(Rails.root, 'spec/fixtures/nonseed_finger_xrd'))}
let(:non_diaspora_hcard) {File.open(File.join(Rails.root, 'spec/fixtures/evan_hcard'))}
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
pending
proc{EMWebfinger.new("asfadfasdf")}.should raise_error
end
end
describe '#on_person' do
it 'should set a callback' do
n = EMWebfinger.new("mbs@gmail.com")
n.on_person{|person| puts "foo"}
n.instance_variable_get(:@callbacks).count.should be 1
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
it 'should'
end
end
context 'webfinger query chain processing' do
describe '#webfinger_profile_url' do
it 'should parse out the webfinger template' do
finger.webfinger_profile_url(account, diaspora_xrd).should == "http://example.com/webfinger/?q=#{account}"
end
end
describe '#xrd_url' do
it 'should return canonical host-meta url' do
finger.xrd_url(account).should be "http://tom.joindiaspora.com/.well-known/host-meta"
end
it 'can return the https version' do
finger.xrd_url(account, true).should be "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
pending
end
end
end
class FakeHttpRequest
def initialize(callback_wanted)
@callback = callback_wanted
end
def response
end
def post; end
def get; end
def callback(&b)
b.call if @callback == :success
end
def errback(&b)
b.call if @callback == :failure
end
end

View file

@ -14,6 +14,11 @@ describe Person do
end end
describe '#diaspora_handle' do describe '#diaspora_handle' do
it 'should downcase and strip the handle before it saves' do
p = Factory.build(:person, :diaspora_handle => " FOOBaR@example.com ")
p.save
p.diaspora_handle.should == "foobar@example.com"
end
context 'local people' do context 'local people' do
it 'uses the pod config url to set the diaspora_handle' do it 'uses the pod config url to set the diaspora_handle' do
@user.person.diaspora_handle.should == @user.username + "@" + APP_CONFIG[:terse_pod_url] @user.person.diaspora_handle.should == @user.username + "@" + APP_CONFIG[:terse_pod_url]

View file

@ -83,6 +83,11 @@ describe User do
it 'uses the pod config url to set the diaspora_handle' do it 'uses the pod config url to set the diaspora_handle' do
user.diaspora_handle.should == user.username + "@" + APP_CONFIG[:terse_pod_url] user.diaspora_handle.should == user.username + "@" + APP_CONFIG[:terse_pod_url]
end end
it 'should be lowercase, even if username is uppercase' do
user.username = "fooBAR"
user.diaspora_handle.should == (user.username + "@" + APP_CONFIG[:terse_pod_url]).downcase
end
end end
context 'profiles' do context 'profiles' do