diff --git a/app/models/person.rb b/app/models/person.rb index 1359b82df557cf962068f0ad6d546325c4ddf9c1..c44203e1e0211b7b7ee96fa81598b89385b0b634 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -26,13 +26,22 @@ class Person belongs_to :owner, :class_name => 'User' timestamps! - + + before_save :strip_and_downcase_diaspora_handle before_destroy :remove_all_traces before_validation :clean_url validates_presence_of :url, :profile, :serialized_public_key validates_format_of :url, :with => /^(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) return Person.all if query.to_s.empty? query_tokens = query.to_s.strip.split(" ") @@ -46,7 +55,7 @@ class Person | Person.all('profile.last_name' => /^#{q}/i) \ | p end - + return p end diff --git a/app/models/user.rb b/app/models/user.rb index 1593ee29cbbcba3cb5fd7cb5b234a104dad8d5ac..9351e34efddefcb66a5e03a257ed718dcecb4ae5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -388,7 +388,7 @@ class User end def diaspora_handle - "#{self.username}@#{APP_CONFIG[:terse_pod_url]}" + "#{self.username}@#{APP_CONFIG[:terse_pod_url]}".downcase end def as_json(opts={}) diff --git a/spec/lib/em-webfinger_spec.rb b/spec/lib/em-webfinger_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..bc34a10a510d02727fc9e032ee6d8fa63d30fdae --- /dev/null +++ b/spec/lib/em-webfinger_spec.rb @@ -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 + diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index 5a84811db6b5cfedfa3153aadb63efce532a6f53..a77aee76bdfdf83fc26584ced41264a1a16fc3a7 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -14,6 +14,11 @@ describe Person do end 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 it 'uses the pod config url to set the diaspora_handle' do @user.person.diaspora_handle.should == @user.username + "@" + APP_CONFIG[:terse_pod_url] @@ -32,7 +37,7 @@ describe Person do person_two.valid?.should == false end - describe 'xml' do + describe 'xml' do before do @xml = @person.to_xml.to_s end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 61e37b3aa89f935d33e0d7edb37c5bb9e753c330..d190fc27ba7f8413e2f5b47ae473e893638a2ede 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -83,6 +83,11 @@ describe User do it 'uses the pod config url to set the diaspora_handle' do user.diaspora_handle.should == user.username + "@" + APP_CONFIG[:terse_pod_url] 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 context 'profiles' do