From 1b48572674968f47e1f6d1fdfcd0677d2c45f36c Mon Sep 17 00:00:00 2001 From: maxwell Date: Thu, 28 Oct 2010 15:03:50 -0700 Subject: [PATCH] MS tests are now green --- app/controllers/publics_controller.rb | 35 +++-- config/deploy.rb | 7 +- lib/diaspora/user/friending.rb | 22 ++- lib/diaspora/user/receiving.rb | 5 - spec/controllers/publics_controller_spec.rb | 142 +++++++++++++------- spec/lib/em-webfinger_spec.rb | 2 +- spec/lib/message_handler_spec.rb | 8 ++ spec/models/user/user_friending_spec.rb | 27 +++- spec/spec_helper.rb | 1 + 9 files changed, 172 insertions(+), 77 deletions(-) diff --git a/app/controllers/publics_controller.rb b/app/controllers/publics_controller.rb index 66efa8c34..ae00cc7df 100644 --- a/app/controllers/publics_controller.rb +++ b/app/controllers/publics_controller.rb @@ -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. @@ -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 - Rails.logger.error("Received post for nonexistent person #{params[:id]}") + if params[:xml].nil? + render :nothing => true, :status => 422 return end - - EM::next_tick { - puts "foobar!" - @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 diff --git a/config/deploy.rb b/config/deploy.rb index 034041a5c..c8deabe2f 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -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 } diff --git a/lib/diaspora/user/friending.rb b/lib/diaspora/user/friending.rb index 653097042..70c65c0ae 100644 --- a/lib/diaspora/user/friending.rb +++ b/lib/diaspora/user/friending.rb @@ -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 diff --git a/lib/diaspora/user/receiving.rb b/lib/diaspora/user/receiving.rb index 1ea857e06..44170f20d 100644 --- a/lib/diaspora/user/receiving.rb +++ b/lib/diaspora/user/receiving.rb @@ -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 diff --git a/spec/controllers/publics_controller_spec.rb b/spec/controllers/publics_controller_spec.rb index cc5ac6667..8f3aa6d68 100644 --- a/spec/controllers/publics_controller_spec.rb +++ b/spec/controllers/publics_controller_spec.rb @@ -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_account_identifier).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 diff --git a/spec/lib/em-webfinger_spec.rb b/spec/lib/em-webfinger_spec.rb index 1211c3cda..3dab4b972 100644 --- a/spec/lib/em-webfinger_spec.rb +++ b/spec/lib/em-webfinger_spec.rb @@ -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 diff --git a/spec/lib/message_handler_spec.rb b/spec/lib/message_handler_spec.rb index 876b67c2e..8ca12ab21 100644 --- a/spec/lib/message_handler_spec.rb +++ b/spec/lib/message_handler_spec.rb @@ -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" diff --git a/spec/models/user/user_friending_spec.rb b/spec/models/user/user_friending_spec.rb index 500bb4362..816e3c36b 100644 --- a/spec/models/user/user_friending_spec.rb +++ b/spec/models/user/user_friending_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d26e845da..4d529e1a2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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