Skip to content
Extraits de code Groupes Projets
Valider 0907d7a9 rédigé par Raphael's avatar Raphael
Parcourir les fichiers

RS, DG; Cleaned up the switch in user.receive

parent bee71c5a
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -25,7 +25,7 @@ class PublicsController < ApplicationController
def receive
@user = Person.first(:id => params[:id]).owner
Rails.logger.debug "PublicsController has received: #{params[:xml]}"
@user.receive params[:xml]
@user.receive params[:xml] if params[:xml]
render :nothing => true
end
......
......@@ -120,7 +120,7 @@ class User
###### Receiving #######
def receive xml
object = Diaspora::Parser.parse_from_xml(xml)
object = Diaspora::Parser.from_xml(xml)
Rails.logger.debug("Receiving object:\n#{object.inspect}")
if object.is_a? Retraction
......@@ -128,7 +128,7 @@ class User
object.perform
elsif object.is_a? Request
person = get_or_create_person_object_from_xml( xml )
person = Diaspora::Parser.get_or_create_person_object_from_xml( xml )
person.serialized_key ||= object.exported_key
object.person = person
object.person.save
......@@ -136,10 +136,10 @@ class User
receive_friend_request(object)
elsif object.is_a? Profile
person = Diaspora::Parser.parse_owner_id_from_xml xml
person = Diaspora::Parser.owner_id_from_xml xml
person.profile = object
person.save
elsif object.respond_to?(:person) && !(object.person.nil?) && !(object.person.is_a? User)
else
Rails.logger.debug("Saving object with success: #{object.save}")
end
end
......
module Diaspora
module Parser
def parse_body_contents_from_xml(xml)
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
doc.xpath("/XML/post")
end
def parse_owner_id_from_xml(xml)
def self.owner_id_from_xml(xml)
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
id = doc.xpath("//person_id").text.to_s
Person.first(:id => id)
end
def get_or_create_person_object_from_xml(xml)
def self.get_or_create_person_object_from_xml(xml)
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
person_xml = doc.xpath("//request/person").to_s
person_id = doc.xpath("//request/person/_id").text.to_s
......@@ -19,9 +14,10 @@ module Diaspora
person ? person : Person.from_xml( person_xml)
end
def parse_from_xml(xml)
def self.from_xml(xml)
return unless body = parse_body_contents_from_xml(xml).children.first
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
return unless body = doc.xpath("/XML/post").children.first
begin
body.name.camelize.constantize.from_xml body.to_s
......
......@@ -10,29 +10,35 @@ describe Diaspora::Parser do
@user = Factory.create(:user, :email => "bob@aol.com")
@person = Factory.create(:person_with_private_key, :email => "bill@gates.com")
end
describe 'with encryption' do
before do
unstub_mocha_stubs
end
after do
stub_signature_verification
end
it "should not store posts from me" do
10.times {
message = Factory.build(:status_message, :person => @user)
xml = message.to_diaspora_xml
@user.receive xml
}
StatusMessage.count.should == 0
end
it "should reject xml with no sender" do
xml = "<XML>
<head>
</head>
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
<post><person></person></post>
<post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
</XML>"
@user.receive xml
Post.count.should == 0
it "should not store posts from me" do
10.times {
message = Factory.build(:status_message, :person => @user)
xml = message.to_diaspora_xml
@user.receive xml
}
StatusMessage.count.should == 0
end
it "should reject xml with no sender" do
xml = "<XML>
<head>
</head>
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
<post><person></person></post>
<post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
</XML>"
@user.receive xml
Post.count.should == 0
end
end
end
it 'should discard types which are not of type post' do
xml = "<XML>
<post><person></person></post>
......@@ -47,12 +53,6 @@ describe Diaspora::Parser do
before do
@xml = Factory.build(:status_message).to_diaspora_xml
end
it 'should be able to parse the body\'s contents' do
body = parse_body_contents_from_xml(@xml).to_s
body.should include "<post>"
body.should include "</post>"
end
it 'should be able to correctly handle comments' do
person = Factory.create(:person, :email => "test@testing.com")
......@@ -60,7 +60,7 @@ describe Diaspora::Parser do
comment = Factory.build(:comment, :post => post, :person => person, :text => "Freedom!")
xml = comment.to_diaspora_xml
comment = parse_from_xml(xml)
comment = Diaspora::Parser.from_xml(xml)
comment.text.should == "Freedom!"
comment.person.should == person
comment.post.should == post
......
require File.dirname(__FILE__) + '/../spec_helper'
include Diaspora::Parser
describe User do
before do
@user = Factory.create(:user)
......
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