Skip to content
Extraits de code Groupes Projets
Valider 1b485726 rédigé par maxwell's avatar maxwell
Parcourir les fichiers

MS tests are now green

parent b8a90256
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
# 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.
......@@ -37,20 +37,27 @@ class PublicsController < ApplicationController
end
def receive
render :nothing => true
return unless params[:xml]
begin
person = Person.first(:id => params[:id])
@user = person.owner
rescue NoMethodError => e
if params[:xml].nil?
render :nothing => true, :status => 422
return
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
EM::next_tick {
puts "foobar!"
@user.receive_salmon params[:xml]
}
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
......@@ -148,7 +148,12 @@ namespace :db do
timer = EventMachine::PeriodicTimer.new(5) do
q.pop {|x| x.call}
EM.stop if q.size == 0
if q.size == 0
EventMachine::Timer.new(30) do
EM.stop
end
end
end
}
......
......@@ -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
......
......@@ -8,15 +8,10 @@ module Diaspora
webfinger = EMWebfinger.new(salmon.author_email)
webfinger.on_person { |salmon_author|
begin
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
rescue Exception => e
puts e.inspect
Rails.logger e.inspect
end
}
end
......
......@@ -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)
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
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 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")
friend_users(user, aspect1, user2, aspect2)
user.reload
user.visible_post_ids.include?(message.id).should be false
it 'should add the pending request to the right user if the target person does not exist locally' do
Person.should_receive(:by_account_identifier).with(user2.person.diaspora_handle).and_return(user2.person)
user2.person.delete
user2.delete
post :receive, :id => user.person.id, :xml => xml
xml1 = user2.salmon(message).xml_for(user.person)
assigns(:user).should eq(user)
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
......@@ -60,7 +60,7 @@ describe EMWebfinger do
n.on_person{|person| puts "foo"}
n.instance_variable_get(:@callbacks).count.should be 1
end
end
end
describe '#fetch' do
it 'should require a callback' do
......
......@@ -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"
......
......@@ -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
......
......@@ -29,6 +29,7 @@ RSpec.configure do |config|
config.before(:each) do
stub_sockets
MessageHandler.any_instance.stubs(:add_post_request)
DatabaseCleaner.clean
end
end
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter