Merge branch 'emw-merge'

This commit is contained in:
maxwell 2010-10-29 11:38:47 -07:00
commit a5eb4bf1a7
33 changed files with 755 additions and 366 deletions

View file

@ -1,7 +1,7 @@
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require File.join(Rails.root, 'lib/em-webfinger')
class DevUtilitiesController < ApplicationController
before_filter :authenticate_user!, :except => [:set_backer_number, :log]
include ApplicationHelper
@ -10,14 +10,31 @@ class DevUtilitiesController < ApplicationController
def zombiefriends
render :nothing => true
bkr_info = backer_info
if current_user.email == "tom@tom.joindiaspora.com"
puts bkr_info.inspect
bkr_info.each do |backer|
backer_email = "#{backer['username']}@#{backer['username']}.joindiaspora.com"
rel_hash = relationship_flow(backer_email)
logger.info "Zombiefriending #{backer['given_name']} #{backer['family_name']}"
logger.info "Calling send_friend_request with #{rel_hash[:friend]} and #{current_user.aspects.first}"
current_user.send_friend_request_to(rel_hash[:friend], current_user.aspects.first)
webfinger = EMWebfinger.new(backer_email)
webfinger.on_person { |person|
puts person.inspect
if person.respond_to? :diaspora_handle
rel_hash = {:friend => person}
logger.info "Zombiefriending #{backer['given_name']} #{backer['family_name']}"
logger.info "Calling send_friend_request with #{rel_hash[:friend]} and #{current_user.aspects.first}"
begin
current_user.send_friend_request_to(rel_hash[:friend], current_user.aspects.first)
rescue Exception => e
logger.info e.inspect
puts e.inspect
end
else
puts "error: #{person}"
end
}
end
end
end

View file

@ -1,4 +1,4 @@
# Copyright (c) 2010, Diaspora Inc. This file is
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
@ -22,8 +22,8 @@ class PublicsController < ApplicationController
end
def webfinger
@person = Person.by_webfinger(params[:q], :local => true) if params[:q]
unless @person.nil? || @person.owner.nil?
@person = Person.local_by_account_identifier(params[:q]) if params[:q]
unless @person.nil?
render 'webfinger', :content_type => 'application/xrd+xml'
else
render :nothing => true, :status => 404
@ -33,20 +33,31 @@ class PublicsController < ApplicationController
def hub
if params['hub.mode'] == 'subscribe' || params['hub.mode'] == 'unsubscribe'
render :text => params['hub.challenge'], :status => 202, :layout => false
end
end
end
def receive
render :nothing => true
return unless params[:xml]
begin
person = Person.first(:id => params[:id])
@user = person.owner
rescue NoMethodError => e
Rails.logger.error("Received post for nonexistent person #{params[:id]}")
if params[:xml].nil?
render :nothing => true, :status => 422
return
end
@user.receive_salmon params[:xml]
end
person = Person.first(:id => params[:id])
if person.owner_id.nil?
Rails.logger.error("Received post for nonexistent person #{params[:id]}")
render :nothing => true, :status => 404
return
end
@user = person.owner
begin
@user.receive_salmon(params[:xml])
rescue Exception => e
Rails.logger.info("bad salmon: #{e.message}")
end
render :nothing => true, :status => 200
end
end

View file

@ -2,6 +2,8 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require File.join(Rails.root, 'lib/em-webfinger')
class RequestsController < ApplicationController
before_filter :authenticate_user!
include RequestsHelper
@ -29,67 +31,40 @@ class RequestsController < ApplicationController
@request = Request.new
end
def create
def create
aspect = current_user.aspect_by_id(params[:request][:aspect_id])
account = params[:request][:destination_url].strip
begin
finger = EMWebfinger.new(account)
finger.on_person{ |person|
if person.class == Person
rel_hash = {:friend => person}
begin
rel_hash = relationship_flow(params[:request][:destination_url].strip)
rescue Exception => e
if e.message.include? "not found"
flash[:error] = I18n.t 'requests.create.error'
elsif e.message.include? "Connection timed out"
flash[:error] = I18n.t 'requests.create.error_server'
elsif e.message == "Identifier is invalid"
flash[:error] = I18n.t 'requests.create.invalid_identity'
else
raise e
end
Rails.logger.debug("Sending request: #{rel_hash}")
if params[:getting_started]
redirect_to getting_started_path(:step=>params[:getting_started])
begin
@request = current_user.send_friend_request_to(rel_hash[:friend], aspect)
rescue Exception => e
Rails.logger.debug("error: #{e.message}")
flash[:error] = e.message
end
else
respond_with :location => aspect
#socket to tell people this failed?
end
return
}
rescue Exception => e
flash[:error] = e.message
end
# rel_hash = {:friend => params[:friend_handle]}
Rails.logger.debug("Sending request: #{rel_hash}")
begin
@request = current_user.send_friend_request_to(rel_hash[:friend], aspect)
rescue Exception => e
if e.message.include? "yourself"
flash[:error] = I18n.t 'requests.create.yourself', :destination_url => params[:request][:destination_url]
elsif e.message.include? "already"
flash[:notice] = I18n.t 'requests.create.already_friends', :destination_url => params[:request][:destination_url]
else
raise e
end
if params[:getting_started]
redirect_to getting_started_path(:step=>params[:getting_started])
else
respond_with :location => aspect
end
return
end
if @request
flash[:notice] = I18n.t 'requests.create.success',:destination_url => @request.destination_url
if params[:getting_started]
redirect_to getting_started_path(:step=>params[:getting_started])
else
respond_with :location => aspect
end
if params[:getting_started]
redirect_to getting_started_path(:step=>params[:getting_started])
else
flash[:error] = I18n.t 'requests.create.horribly_wrong'
if params[:getting_started]
redirect_to getting_started_path(:step=>params[:getting_started])
else
respond_with :location => aspect
end
end
flash[:notice] = "we tried our best to send a message to #{account}" unless flash[:error]
respond_with :location => aspects_manage_path
return
end
end
end

View file

@ -3,38 +3,4 @@
# the COPYRIGHT file.
module RequestsHelper
def subscription_mode(profile)
if diaspora?(profile)
:friend
else
:none
end
end
def diaspora?(profile)
profile_contains(profile, 'http://joindiaspora.com/seed_location')
end
def profile_contains(profile, rel)
profile.links.each{|x| return true if x.rel == rel}
false
end
def subscription_url(action, profile)
if action == :friend
profile.links.select{|x| x.rel == 'http://joindiaspora.com/seed_location'}.first.href
else
nil
end
end
def relationship_flow(identifier)
action = :none
person = nil
person = Person.by_webfinger identifier
if person
action = (person == current_user.person ? :none : :friend)
end
{ action => person }
end
end

View file

@ -23,6 +23,13 @@ class Person
one :profile, :class_name => 'Profile'
validate :profile_is_valid
before_save :downcase_diaspora_handle
def downcase_diaspora_handle
diaspora_handle.downcase!
end
def profile_is_valid
if profile.present? && !profile.valid?
profile.errors.full_messages.each { |m| errors.add(:base, m) }
@ -97,61 +104,35 @@ class Person
@serialized_public_key = new_key
end
def self.by_webfinger(identifier, opts = {})
# Raise an error if identifier has a port number
raise "Identifier is invalid" if(identifier.strip.match(/\:\d+$/))
# Raise an error if identifier is not a valid email (generous regexp)
raise "Identifier is invalid" if !(identifier =~ /\A.*\@.*\..*\Z/)
query = /\A^#{Regexp.escape(identifier.gsub('acct:', '').to_s)}\z/i
local_person = Person.first(:diaspora_handle => query)
if local_person
Rails.logger.info("Do not need to webfinger, found a local person #{local_person.real_name}")
local_person
elsif !identifier.include?("localhost") && !opts[:local]
#Get remote profile
begin
Rails.logger.info("Webfingering #{identifier}")
f = Redfinger.finger(identifier)
rescue SocketError => e
raise "Diaspora server for #{identifier} not found" if e.message =~ /Name or service not known/
rescue Errno::ETIMEDOUT => e
raise "Connection timed out to Diaspora server for #{identifier}"
end
raise "No webfinger profile found at #{identifier}" if f.nil? || f.links.empty?
Person.from_webfinger_profile(identifier, f)
end
#database calls
def self.by_account_identifier(identifier)
identifier = identifier.strip.downcase.gsub('acct:', '') if identifier
self.first(:diaspora_handle => identifier)
end
def self.from_webfinger_profile(identifier, profile)
def self.local_by_account_identifier(identifier)
person = self.by_account_identifier(identifier)
(person.nil? || person.remote?) ? nil : person
end
def self.build_from_webfinger(profile, hcard)
return nil if profile.nil? || !profile.valid_diaspora_profile?
new_person = Person.new
new_person.exported_key = profile.public_key
new_person.id = profile.guid
new_person.diaspora_handle = profile.account
new_person.url = profile.seed_location
public_key_entry = profile.links.select { |x| x.rel == 'diaspora-public-key' }
return nil unless public_key_entry
pubkey = public_key_entry.first.href
new_person.exported_key = Base64.decode64 pubkey
guid = profile.links.select { |x| x.rel == 'http://joindiaspora.com/guid' }.first.href
new_person.id = guid
new_person.diaspora_handle = identifier
hcard = HCard.find profile.hcard.first[:href]
#hcard_profile = HCard.find profile.hcard.first[:href]
Rails.logger.info("hcard: #{ hcard.inspect}")
new_person.url = hcard[:url]
new_person.profile = Profile.new(:first_name => hcard[:given_name], :last_name => hcard[:family_name], :image_url => hcard[:photo])
if new_person.save
new_person
else
nil
end
new_person.save! ? new_person : nil
end
def remote?
owner.nil?
owner_id.nil?
end
def as_json(opts={})
@ -159,7 +140,7 @@ class Person
:person => {
:id => self.id,
:name => self.real_name,
:diaspora_handle => self.diaspora_handle,
:diaspora_handle => self.diaspora_handle,
:url => self.url,
:exported_key => exported_key
}

View file

@ -42,7 +42,7 @@ class User
key :getting_started, Boolean, :default => true
before_validation :strip_username, :on => :create
before_validation :strip_and_downcase_username, :on => :create
validates_presence_of :username
validates_uniqueness_of :username, :case_sensitive => false
validates_format_of :username, :with => /\A[A-Za-z0-9_.]+\z/
@ -69,9 +69,10 @@ class User
before_destroy :unfriend_everyone, :remove_person
def strip_username
def strip_and_downcase_username
if username.present?
username.strip!
username.downcase!
end
end

View file

@ -10,13 +10,13 @@
</dd>
</dl>
<dl class="entity_given_name">
<dt>Full name</dt>
<dt>First name</dt>
<dd>
<span class="given_name" ><%= @person.profile.first_name %></span>
</dd>
</dl>
<dl class="entity_family_name">
<dt>Full name</dt>
<dt>Family name</dt>
<dd>
<span class="family_name" ><%= @person.profile.last_name %></span>
</dd>

View file

@ -2,7 +2,10 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'rubygems'
require 'eventmachine'
config = YAML.load_file(File.dirname(__FILE__) + '/deploy_config.yml')
all = config['cross_server']
set :backers, config['servers']['backer']
@ -134,11 +137,27 @@ namespace :db do
task :tom_seed, :roles => :tom do
run "cd #{current_path} && bundle exec rake db:seed:tom --trace RAILS_ENV=#{rails_env}"
run "curl -silent -u tom@tom.joindiaspora.com:evankorth http://tom.joindiaspora.com/zombiefriends"
backers.each do |backer|
run "curl -silent -u #{backer['username']}@#{backer['username']}.joindiaspora.com:#{backer['username']}#{backer['pin']} http://#{backer['username']}.joindiaspora.com/zombiefriendaccept"
#run "curl -silent -u #{backer['username']}@#{backer['username']}.joindiaspora.com:#{backer['username']}#{backer['pin']} http://#{backer['username']}.joindiaspora.com/set_profile_photo"
end
EM.run {
q = EM::Queue.new
backers.each do |backer|
q.push( lambda{run "curl -silent -u #{backer['username']}@#{backer['username']}.joindiaspora.com:#{backer['username']}#{backer['pin']} http://#{backer['username']}.joindiaspora.com/zombiefriendaccept"})
end
timer = EventMachine::PeriodicTimer.new(5) do
q.pop {|x| x.call}
if q.size == 0
q.pop {|x| x.call}
EventMachine::Timer.new(60) do
EM.stop
end
end
end
}
end
task :backer_seed, :roles => :backer do
@ -152,7 +171,6 @@ namespace :db do
purge
backer_seed
tom_seed
deploy::restart
end
end

View file

@ -6,7 +6,7 @@ cross_server:
deploy_to: '/usr/local/app/diaspora'
user: 'root'
repo: 'git://github.com/diaspora/diaspora.git'
branch: 'master'
branch: 'emw-merge'
default_env: 'development'
servers:
tom:

View file

@ -63,22 +63,26 @@ module Diaspora
def receive_friend_request(friend_request)
Rails.logger.info("receiving friend request #{friend_request.to_json}")
from_me = request_from_me?(friend_request)
know_about_request = know_about_request?(friend_request)
destination_aspect = self.aspect_by_id(friend_request.aspect_id) if friend_request.aspect_id
#response from a friend request you sent
if request_from_me?(friend_request) && self.aspect_by_id(friend_request.aspect_id)
aspect = self.aspect_by_id(friend_request.aspect_id)
activate_friend(friend_request.person, aspect)
if from_me && know_about_request && destination_aspect
activate_friend(friend_request.person, destination_aspect)
Rails.logger.info("#{self.real_name}'s friend request has been accepted")
friend_request.destroy
Notifier.request_accepted(self, friend_request.person, aspect).deliver
Notifier.request_accepted(self, friend_request.person, destination_aspect).deliver
#this is a new friend request
else
elsif !from_me
self.pending_requests << friend_request
self.save
Rails.logger.info("#{self.real_name} has received a friend request")
friend_request.save
Notifier.new_request(self, friend_request.person).deliver
else
Rails.logger.info("unsolicited friend request: #{friend_request.to_json}")
end
end
@ -128,7 +132,11 @@ module Diaspora
end
def request_from_me?(request)
(pending_request_ids.include?(request.id.to_id)) && (request.callback_url == person.receive_url)
request.callback_url == person.receive_url
end
def know_about_request?(request)
pending_request_ids.include?(request.id.to_id) unless request.nil? || request.id.nil?
end
def requests_for_me

View file

@ -1,12 +1,18 @@
require File.join(Rails.root, 'lib/em-webfinger')
module Diaspora
module UserModules
module Receiving
def receive_salmon salmon_xml
salmon = Salmon::SalmonSlap.parse salmon_xml, self
if salmon.verified_for_key?(salmon.author.public_key)
Rails.logger.info("data in salmon: #{salmon.parsed_data}")
self.receive(salmon.parsed_data, salmon.author)
end
webfinger = EMWebfinger.new(salmon.author_email)
webfinger.on_person { |salmon_author|
if salmon.verified_for_key?(salmon_author.public_key)
Rails.logger.info("data in salmon: #{salmon.parsed_data}")
self.receive(salmon.parsed_data, salmon_author)
end
}
end
def receive xml, salmon_author
@ -14,29 +20,45 @@ module Diaspora
Rails.logger.debug("Receiving object for #{self.real_name}:\n#{object.inspect}")
Rails.logger.debug("From: #{object.person.inspect}") if object.person
sender_in_xml = sender(object, xml)
if (salmon_author != sender_in_xml)
raise "Malicious Post, #{salmon_author.real_name} with id #{salmon_author.id} is sending a #{object.class} as #{sender_in_xml.real_name} with id #{sender_in_xml.id} "
end
if object.is_a?(Comment)
e = EMWebfinger.new(object.diaspora_handle)
if object.is_a? Request
return receive_request object, sender_in_xml
end
raise "Not friends with that person" unless self.contact_for(salmon_author)
e.on_person { |person|
if person.class == Person
sender_in_xml = sender(object, xml, person)
if (salmon_author != sender_in_xml)
raise "Malicious Post, #{salmon_author.real_name} with id #{salmon_author.id} is sending a #{object.class} as #{sender_in_xml.real_name} with id #{sender_in_xml.id} "
end
receive_comment object, xml
end
}
if object.is_a? Retraction
receive_retraction object, xml
elsif object.is_a? Profile
receive_profile object, xml
elsif object.is_a?(Comment)
receive_comment object, xml
else
receive_post object, xml
sender_in_xml = sender(object, xml)
if (salmon_author != sender_in_xml)
raise "Malicious Post, #{salmon_author.real_name} with id #{salmon_author.id} is sending a #{object.class} as #{sender_in_xml.real_name} with id #{sender_in_xml.id} "
end
if object.is_a? Request
return receive_request object, sender_in_xml
end
raise "Not friends with that person" unless self.contact_for(salmon_author)
if object.is_a? Retraction
receive_retraction object, xml
elsif object.is_a? Profile
receive_profile object, xml
else
receive_post object, xml
end
end
end
def sender(object, xml)
def sender(object, xml, webfingered_person = nil)
if object.is_a? Retraction
sender = object.person
elsif object.is_a? Request
@ -44,7 +66,7 @@ module Diaspora
elsif object.is_a? Profile
sender = Diaspora::Parser.owner_id_from_xml xml
elsif object.is_a?(Comment)
object.person = Person.by_webfinger(object.diaspora_handle)
object.person = webfingered_person
sender = (owns?(object.post))? object.person : object.post.person
else
sender = object.person

97
lib/em-webfinger.rb Normal file
View file

@ -0,0 +1,97 @@
require File.join(Rails.root, 'lib/hcard')
require File.join(Rails.root, 'lib/webfinger_profile')
class EMWebfinger
TIMEOUT = 5
def initialize(account)
@account = account.strip.gsub('acct:','').to_s
@callbacks = []
# Raise an error if identifier has a port number
raise "Identifier is invalid" if(@account.strip.match(/\:\d+$/))
# Raise an error if identifier is not a valid email (generous regexp)
raise "Identifier is invalid" if !(@account=~ /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/)
end
def fetch
raise 'you need to set a callback before calling fetch' if @callbacks.empty?
person = Person.by_account_identifier(@account)
if person
process_callbacks person
else
get_xrd
end
end
def on_person(&block)
@callbacks << block
self.fetch
end
private
def get_xrd
http = EventMachine::HttpRequest.new(xrd_url).get :timeout => TIMEOUT
http.callback {
profile_url = webfinger_profile_url(http.response)
if profile_url
get_webfinger_profile(profile_url)
else
process_callbacks "webfinger does not seem to be enabled for #{@account}"
end
}
http.errback { process_callbacks "there was an error getting the xrd from account#{@account}" }
end
def get_webfinger_profile(profile_url)
http = EventMachine::HttpRequest.new(profile_url).get :timeout => TIMEOUT
http.callback{ make_person_from_webfinger(http.response) }
http.errback{ process_callbacks "failed to fetch webfinger profile for #{profile_url}"}
end
def make_person_from_webfinger(webfinger_profile)
unless webfinger_profile.strip == ""
wf_profile = WebfingerProfile.new(@account, webfinger_profile)
http = EventMachine::HttpRequest.new(wf_profile.hcard).get :timeout => TIMEOUT
http.callback{
hcard = HCard.build http.response
p = Person.build_from_webfinger(wf_profile, hcard)
process_callbacks(p)
}
http.errback{process_callbacks "there was a problem fetching the hcard for #{@account}"}
end
end
def process_callbacks(person)
@callbacks.each { |c| c.call(person) }
end
##helpers
private
def check_nil_response(html)
end
def webfinger_profile_url(xrd_response)
doc = Nokogiri::XML::Document.parse(xrd_response)
return nil if doc.namespaces["xmlns"] != "http://docs.oasis-open.org/ns/xri/xrd-1.0"
swizzle doc.at('Link[rel=lrdd]').attribute('template').value
end
def xrd_url(ssl = false)
domain = @account.split('@')[1]
"http#{'s' if ssl}://#{domain}/.well-known/host-meta"
end
def swizzle(template)
template.gsub '{uri}', @account
end
end

View file

@ -8,13 +8,19 @@ module HCard
end
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(".photo[src]").attribute('src').text }
{
:given_name => doc.css(".given_name").text,
:family_name => doc.css(".family_name").text,
:url => doc.css("#pod_location").text,
:photo => doc.css(".photo[src]").attribute('src').text
}
end
def self.find url
self.parse self.fetch(url)
end
def self.build(raw_hcard)
self.parse Nokogiri::HTML(raw_hcard)
end
end

View file

@ -5,7 +5,7 @@
class MessageHandler
NUM_TRIES = 3
TIMEOUT = 5 #seconds
TIMEOUT = 10 #seconds
def initialize
@queue = EM::Queue.new

View file

@ -110,11 +110,11 @@ HEADER
end
def author
if @author
@author
else
@author ||= Person.by_webfinger @author_email
if @author.nil?
@author ||= Person.by_account_identifier @author_email
raise "did you remember to async webfinger?" if @author.nil?
end
@author
end
# Decode URL-safe-Base64. This implements
@ -130,10 +130,6 @@ HEADER
Base64.urlsafe_decode64 string
end
# def verified?
#
# end
# Check whether this envelope's signature can be verified with the
# provided OpenSSL::PKey::RSA public_key.
# Example:

52
lib/webfinger_profile.rb Normal file
View file

@ -0,0 +1,52 @@
class WebfingerProfile
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)
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
if doc.at('Link[rel=diaspora-public-key]')
begin
pubkey = text_of_attribute( doc.at('Link[rel=diaspora-public-key]'), 'href')
@public_key = Base64.decode64 pubkey
rescue Exception => e
puts "probally not diaspora..."
end
end
end
def text_of_attribute(doc, attr)
doc.attribute(attr) ? doc.attribute(attr).text : nil
end
end

View file

@ -4,38 +4,87 @@
require 'spec_helper'
describe PublicsController do
render_views
let(:user) { Factory.create :user }
let(:user2) { Factory.create :user }
let(:aspect1) { user.aspect(:name => "foo") }
let(:aspect2) { user2.aspect(:name => "far") }
let!(:user) { Factory.create :user }
let!(:user2) { Factory.create :user }
let!(:aspect1) { user.aspect(:name => "foo") }
let!(:aspect2) { user2.aspect(:name => "far") }
let!(:aspect2) { user2.aspect(:name => 'disciples') }
let!(:req) { user2.send_friend_request_to(user.person, aspect2) }
let!(:xml) { user2.salmon(req).xml_for(user.person) }
let(:person){Factory(:person)}
before do
sign_in :user, user
end
describe 'receive endpoint' do
it 'should have a and endpoint and return a 200 on successful receipt of a request' do
post :receive, :id =>user.person.id
response.code.should == '200'
end
describe '#receive' do
context 'success cases' do
it 'should 200 on successful receipt of a request' do
EM::run {
person_mock = mock()
user_mock = mock()
user_mock.stub!(:receive_salmon).and_return(true)
user_mock.should_receive(:receive_salmon).and_return(true)
person_mock.stub!(:owner_id).and_return(true)
person_mock.stub!(:owner).and_return(user_mock)
Person.stub!(:first).and_return(person_mock)
post :receive, :id =>user.person.id, :xml => xml
response.code.should == '200'
EM.stop
}
end
it 'should set the user based on their person_id' do
EM::run {
person_mock = mock()
person_mock.stub!(:owner_id).and_return(true)
person_mock.stub!(:owner).and_return(user)
Person.stub!(:first).and_return(person_mock)
post :receive, :id => user.person.id, :xml => xml
assigns[:user].should == user
EM.stop
}
end
it 'should have the xml processed as salmon on success' do
EM::run{
person_mock = mock()
user_mock = mock()
user_mock.stub!(:receive_salmon).and_return(true)
person_mock.stub!(:owner_id).and_return(true)
person_mock.stub!(:owner).and_return(user_mock)
Person.stub!(:first).and_return(person_mock)
post :receive, :id => user.person.id, :xml => xml
EM.stop
}
end
end
it 'should accept a post from another node and save the information' do
message = user2.build_post(:status_message, :message => "hi")
friend_users(user, aspect1, user2, aspect2)
it 'should return a 422 if no xml is passed' do
post :receive, :id => person.id
response.code.should == '422'
end
user.reload
user.visible_post_ids.include?(message.id).should be false
xml = user2.salmon(message).xml_for(user.person)
post :receive, :id => user.person.id, :xml => xml
user.reload
user.visible_post_ids.include?(message.id).should be true
it 'should return a 404 if no user is found' do
post :receive, :id => person.id, :xml => xml
response.code.should == '404'
end
end
describe '#hcard' do
it 'queries by person id' do
post :hcard, :id => user.person.id
@ -50,7 +99,7 @@ describe PublicsController do
end
end
describe 'webfinger' do
describe '#webfinger' do
it "succeeds when the person and user exist locally" do
user = Factory(:user)
post :webfinger, 'q' => user.person.diaspora_handle
@ -76,33 +125,36 @@ describe PublicsController do
end
end
describe 'friend requests' do
let(:aspect2) { user2.aspect(:name => 'disciples') }
let!(:req) { user2.send_friend_request_to(user.person, aspect2) }
let!(:xml) { user2.salmon(req).xml_for(user.person) }
before do
deliverable = Object.new
deliverable.stub!(:deliver)
Notifier.stub!(:new_request).and_return(deliverable)
req.delete
user2.reload
user2.pending_requests.count.should be 1
end
context 'intergration tests that should not be in this file' do
describe 'friend requests' do
before do
deliverable = Object.new
deliverable.stub!(:deliver)
Notifier.stub!(:new_request).and_return(deliverable)
req.delete
user2.reload
user2.pending_requests.count.should be 1
end
it 'should add the pending request to the right user if the target person exists locally' do
user2.delete
post :receive, :id => user.person.id, :xml => xml
assigns(:user).should eq(user)
end
it 'should accept a post from another node and save the information' do
pending
message = user2.build_post(:status_message, :message => "hi")
it 'should add the pending request to the right user if the target person does not exist locally' do
Person.should_receive(:by_webfinger).with(user2.person.diaspora_handle).and_return(user2.person)
user2.person.delete
user2.delete
post :receive, :id => user.person.id, :xml => xml
friend_users(user, aspect1, user2, aspect2)
assigns(:user).should eq(user)
user.reload
user.visible_post_ids.include?(message.id).should be false
xml1 = user2.salmon(message).xml_for(user.person)
EM::run{
post :receive, :id => user.person.id, :xml => xml1
EM.stop
}
user.reload
user.visible_post_ids.include?(message.id).should be true
end
end
end
end

View file

@ -19,7 +19,7 @@ describe RequestsController do
"aspect_id" => @user.aspects[0].id
}
)
response.should redirect_to aspect_path(@user.aspects[0].id.to_s)
response.should redirect_to aspects_manage_path
end
it "should not error out when requesting an invalid identity" do
@ -28,7 +28,7 @@ describe RequestsController do
"aspect_id" => @user.aspects[0].id
}
)
response.should redirect_to aspect_path(@user.aspects[0].id.to_s)
response.should redirect_to aspects_manage_path
end
it "should not error out when requesting an invalid identity with a port number" do
@ -37,7 +37,7 @@ describe RequestsController do
"aspect_id" => @user.aspects[0].id
}
)
response.should redirect_to aspect_path(@user.aspects[0].id.to_s)
response.should redirect_to aspects_manage_path
end
it "should not error out when requesting an identity from an invalid server" do
@ -47,9 +47,14 @@ describe RequestsController do
"aspect_id" => @user.aspects[0].id
}
)
response.should redirect_to aspect_path(@user.aspects[0].id.to_s)
response.should redirect_to aspects_manage_path
end
it 'should redirect to the page which you called it from ' do
pending "i need to figure out how to do this"
end
it 'should not blow up if there is a problem mid way thru the webfinger process' do
pending "i need to do this tomorrow"
end
end

View file

@ -1,7 +1,7 @@
<?xml version='1.0' encoding='UTF-8'?>
<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>
<Link rel='lrdd'
template='http://example.com/webfinger/?q={uri}'>
template='http://tom.joindiaspora.com/webfinger/?q={uri}'>
<Title>Resource Descriptor</Title>
</Link>
</XRD>

View file

@ -6,29 +6,4 @@ require 'spec_helper'
describe RequestsHelper do
before do
stub_success("tom@tom.joindiaspora.com")
stub_success("evan@status.net")
@tom = Redfinger.finger('tom@tom.joindiaspora.com')
@evan = Redfinger.finger('evan@status.net')
end
describe "profile" do
it 'should detect how to subscribe to a diaspora or webfinger profile' do
subscription_mode(@tom).should == :friend
subscription_mode(@evan).should == :none
end
end
describe "#relationship_flow" do
let(:tom){ Factory(:user, :email => 'tom@tom.joindiaspora.com') }
before do
stub!(:current_user).and_return(tom)
end
it 'should return the correct tag and url for a given address' do
relationship_flow('tom@tom.joindiaspora.com')[:friend].receive_url.should include("receive/user")
end
end
end

View file

@ -18,10 +18,8 @@ describe Diaspora::Parser do
comment = Factory.create(:comment, :post => post, :person => person, :diaspora_handle => person.diaspora_handle, :text => "Freedom!")
comment.delete
xml = comment.to_diaspora_xml
puts xml
comment_from_xml = Diaspora::Parser.from_xml(xml)
comment_from_xml.diaspora_handle person.diaspora_handle
comment_from_xml.diaspora_handle.should == person.diaspora_handle
comment_from_xml.post.should == post
comment_from_xml.text.should == "Freedom!"
comment_from_xml.should_not be comment

View file

@ -0,0 +1,138 @@
# 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) {"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}"
}
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
EventMachine::HttpRequest.should_not_receive(:new)
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

View file

@ -19,7 +19,8 @@ describe 'user encryption' do
deliverable = Object.new
deliverable.stub!(:deliver)
Notifier.stub!(:new_request).and_return(deliverable)
Person.should_receive(:by_webfinger).and_return(remote_user.person)
Person.should_receive(:by_account_identifier).and_return(remote_user.person)
remote_user.should_receive(:push_to_people).and_return(true)
#should move this to friend request, but i found it here
id = remote_user.person.id
original_key = remote_user.exported_key

View file

@ -5,7 +5,6 @@
require 'spec_helper'
require File.join(Rails.root, 'lib/hcard')
describe HCard do
it 'should retreive and parse an hcard' do
stub_success("tom@tom.joindiaspora.com")
@ -13,6 +12,7 @@ describe HCard do
hcard = HCard.find f.hcard.first[:href]
hcard[:family_name].include?("Hamiltom").should be true
hcard[:given_name].include?("Alex").should be true
hcard[:photo].include?("tom.jpg").should be true
hcard[:url].should == "http://tom.joindiaspora.com/"
end
end

View file

@ -5,6 +5,14 @@
require 'spec_helper'
describe MessageHandler do
before do
unstub_mocha_stubs
end
after do
stub_sockets
MessageHandler.any_instance.stubs(:add_post_request)
end
before do
@handler = MessageHandler.new
@message_body = "I want to pump you up"
@ -154,20 +162,3 @@ describe MessageHandler do
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

@ -65,25 +65,18 @@ describe Salmon do
end
describe '#author' do
before do
stub_success("tom@tom.joindiaspora.com")
end
it 'should reference a local author' do
parsed_salmon.author.should == user.person
end
it 'should reference a remote author' do
it 'should fail if no author is found' do
parsed_salmon.author_email = 'tom@tom.joindiaspora.com'
parsed_salmon.author.public_key.should_not be_nil
proc {parsed_salmon.author.public_key}.should raise_error "did you remember to async webfinger?"
end
it 'should fail to reference a nonexistent remote author' do
parsed_salmon.author_email = 'idsfug@difgubhpsduh.rgd'
proc {
Redfinger.stub(:finger).and_return(nil) #Redfinger returns nil when there is no profile
parsed_salmon.author.real_name}.should raise_error /No webfinger profile found/
end
end
it 'verifies the signature for the sender' do

View file

@ -0,0 +1,36 @@
require 'spec_helper'
describe WebfingerProfile do
let(:webfinger_profile){File.open(File.join(Rails.root, "spec/fixtures/finger_xrd")).read.strip}
let(:not_diaspora_webfinger){File.open(File.join(Rails.root, "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
manual_nil_check(profile).should == profile.valid_diaspora_profile?
end
end
describe '#set_fields' do
it 'should check to make sure it has a the right webfinger profile' do
proc{ WebfingerProfile.new("nottom@tom.joindiaspora.com", webfinger_profile)}.should raise_error
end
it 'should handle a non-diaspora profile without blowing up' do
proc{ WebfingerProfile.new("evan@status.net", not_diaspora_webfinger)}.should_not raise_error
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

View file

@ -73,7 +73,6 @@ describe Comment do
it 'should send a comment a person made on your post to all people' do
comment = Comment.new(:person_id => @person.id, :diaspora_handle => @person.diaspora_handle, :text => "cats", :post => @user_status)
User::QUEUE.should_receive(:add_post_request).twice
Person.should_receive(:by_webfinger).and_return(@person)
user.receive comment.to_diaspora_xml, @person
end

View file

@ -117,7 +117,7 @@ describe Person do
end
end
describe '::search' do
describe '#search' do
before do
@friend_one = Factory.create(:person)
@friend_two = Factory.create(:person)
@ -167,74 +167,71 @@ describe Person do
end
end
describe ".by_webfinger" do
context "local people" do
before do
@local_person = Factory(:person)
Redfinger.should_not_receive :finger
context 'people finders for webfinger' do
let(:user) {Factory(:user)}
let(:person) {Factory(:person)}
describe '.by_account_identifier' do
it 'should find a local users person' do
p = Person.by_account_identifier(user.diaspora_handle)
p.should == user.person
end
it "finds the local person without calling out" do
person = Person.by_webfinger(@local_person.diaspora_handle)
person.should == @local_person
it 'should find remote users person' do
p = Person.by_account_identifier(person.diaspora_handle)
p.should == person
end
it 'should downcase and strip the diaspora_handle' do
dh_upper = " " + user.diaspora_handle.upcase + " "
Person.by_account_identifier(dh_upper).should == user.person
end
it "finds a local person with a mixed-case username" do
user = Factory(:user, :username => "SaMaNtHa")
person = Person.by_webfinger(user.person.diaspora_handle)
person = Person.by_account_identifier(user.person.diaspora_handle)
person.should == user.person
end
it "is case insensitive" do
user = Factory(:user, :username => "SaMaNtHa")
person = Person.by_webfinger(user.person.diaspora_handle.upcase)
person.should == user.person
user1 = Factory(:user, :username => "SaMaNtHa")
person = Person.by_account_identifier(user1.person.diaspora_handle.upcase)
person.should == user1.person
end
end
it 'should only find people who are exact matches' do
it 'should only find people who are exact matches (1/2)' do
user = Factory(:user, :username => "SaMaNtHa")
person = Factory(:person, :diaspora_handle => "tomtom@tom.joindiaspora.com")
user.person.diaspora_handle = "tom@tom.joindiaspora.com"
user.person.save
Person.by_webfinger("tom@tom.joindiaspora.com").diaspora_handle.should == "tom@tom.joindiaspora.com"
Person.by_account_identifier("tom@tom.joindiaspora.com").diaspora_handle.should == "tom@tom.joindiaspora.com"
end
it 'should return nil if there is not an exact match' do
Redfinger.stub!(:finger).and_return(nil)
it 'should only find people who are exact matches (2/2)' do
person = Factory(:person, :diaspora_handle => "tomtom@tom.joindiaspora.com")
person1 = Factory(:person, :diaspora_handle => "tom@tom.joindiaspora.comm")
#Person.by_webfinger("tom@tom.joindiaspora.com").should_be false
proc{ Person.by_webfinger("tom@tom.joindiaspora.com")}.should raise_error
f = Person.by_account_identifier("tom@tom.joindiaspora.com")
f.should be nil
end
it 'identifier should be a valid email' do
stub_success("joe.valid+email@my-address.com")
Proc.new {
Person.by_webfinger("joe.valid+email@my-address.com")
}.should_not raise_error(RuntimeError, "Identifier is invalid")
stub_success("not_a_@valid_email")
Proc.new {
Person.by_webfinger("not_a_@valid_email")
}.should raise_error(RuntimeError, "Identifier is invalid")
end
it 'should not accept a port number' do
stub_success("eviljoe@diaspora.local:3000")
Proc.new {
Person.by_webfinger('eviljoe@diaspora.local:3000')
}.should raise_error(RuntimeError, "Identifier is invalid")
end
describe '.local_by_account_identifier' do
it 'should find local users people' do
p = Person.local_by_account_identifier(user.diaspora_handle)
p.should == user.person
end
it 'creates a stub for a remote user' do
stub_success("tom@tom.joindiaspora.com")
tom = Person.by_webfinger('tom@tom.joindiaspora.com')
tom.real_name.include?("Hamiltom").should be true
it 'should not find a remote person' do
p = Person.local_by_account_identifier(@person.diaspora_handle)
p.should be nil
end
it 'should call .by_account_identifier' do
Person.should_receive(:by_account_identifier)
Person.local_by_account_identifier(@person.diaspora_handle)
end
end
end
end

View file

@ -123,7 +123,8 @@ describe User do
remote_person.delete
user3.delete
Person.should_receive(:by_webfinger).twice.and_return{ |handle| if handle == user.person.diaspora_handle; user.person.save
#stubs async webfinger
Person.should_receive(:by_account_identifier).twice.and_return{ |handle| if handle == user.person.diaspora_handle; user.person.save
user.person; else; remote_person.save; remote_person; end }

View file

@ -25,6 +25,13 @@ describe Diaspora::UserModules::Friending do
Notifier.stub!(:request_accepted).and_return(deliverable)
end
before :all do
User.any_instance.stubs(:push_to_people)
end
after :all do
unstub_mocha_stubs
end
describe '#contact_for' do
@ -56,7 +63,6 @@ describe Diaspora::UserModules::Friending do
user.friends << contact
user2.contact_for(person_one).should be_nil
end
end
@ -83,13 +89,13 @@ describe Diaspora::UserModules::Friending do
request.reverse_for(user2)
proc{user.receive_friend_request(request)}.should change(user.reload.friends, :count).by(1)
end
end
context 'received a friend request' do
let(:request_for_user) {Request.instantiate(:to => user.receive_url, :from => friend)}
let(:request2_for_user) {Request.instantiate(:to => user.receive_url, :from => person_one)}
let(:request_from_myself) {Request.instantiate(:to => user.receive_url, :from => user.person)}
before do
request_for_user.save
user.receive_friend_request(request_for_user)
@ -106,6 +112,21 @@ describe Diaspora::UserModules::Friending do
proc { user.ignore_friend_request(request_for_user.id) }.should change(
user.reload.pending_requests, :count ).by(-1)
end
it 'should ignore a friend request from yourself' do
user.pending_requests.delete_all
user.save
request = user.send_friend_request_to(user.person, aspect)
request.reverse_for(user)
request.aspect_id = nil
user.pending_requests.delete_all
user.save
proc { user.receive_friend_request(request) }.should change(
user.reload.pending_requests, :count ).by(0)
end
end
it 'should not be able to friend request an existing friend' do
@ -171,8 +192,6 @@ describe Diaspora::UserModules::Friending do
Notifier.should_receive(:new_request).and_return(mail_obj)
user.receive @req_xml, person_one
end
end
context 'Two users receiving requests from one person' do

View file

@ -79,12 +79,14 @@ describe User do
end
it "keeps the original case" do
pending "do we want this?"
user = Factory.build(:user, :username => "WeIrDcAsE")
user.should be_valid
user.username.should == "WeIrDcAsE"
end
it "fails if the requested username is only different in case from an existing username" do
pending "do we want this?"
duplicate_user = Factory.build(:user, :username => user.username.upcase)
duplicate_user.should_not be_valid
end

View file

@ -29,9 +29,41 @@ RSpec.configure do |config|
config.before(:each) do
stub_sockets
MessageHandler.any_instance.stubs(:add_post_request)
DatabaseCleaner.clean
end
end
ImageUploader.enable_processing = false
class FakeHttpRequest
def initialize(callback_wanted)
@callback = callback_wanted
@callbacks = []
end
def callbacks=(rs)
@callbacks += rs.reverse
end
def response
@callbacks.pop unless @callbacks.nil? || @callbacks.empty?
end
def post(opts = nil);
self
end
def get(opts = nil)
self
end
def callback(&b)
b.call if @callback == :success
end
def errback(&b)
b.call if @callback == :failure
end
end