Skip to content
Extraits de code Groupes Projets
diaspora_parser_spec.rb 5,67 ko
Newer Older
  • Learn to ignore specific revisions
  • require File.dirname(__FILE__) + '/../spec_helper'
    
    include ApplicationHelper 
    
    include Diaspora::DiasporaParser
    
    describe Diaspora::DiasporaParser do
    
      before do
    
        @user = Factory.create(:user, :email => "bob@aol.com")
    
        @person = Factory.create(:person, :email => "bill@gates.com")
    
      it "should not store posts from me" do
    
        status_messages = []
    
        10.times { status_messages << Factory.build(:status_message, :person => @user)}
    
        xml = Post.build_xml_for(status_messages) 
    
        store_objects_from_xml(xml) 
    
      it "should reject xml with no sender" do
        xml = "<XML>
        <head>
        </head><posts>
          <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><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>
          </posts></XML>"
    
        store_objects_from_xml(xml)
    
      it "should reject xml with a sender not in the database" do
        xml = "<XML>
        <head>
          <sender>
            <email>foo@example.com</email>
          </sender>
        </head><posts>
          <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><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>
          </posts></XML>"
    
        store_objects_from_xml(xml)
    
      it 'should discard types which are not of type post' do
    
        </head>
        <posts>
    
        </posts></XML>"
    
        store_objects_from_xml(xml)
    
    ilya's avatar
    ilya a validé
        Post.count.should == 0
    
    maxwell's avatar
    maxwell a validé
    
    
      describe "parsing compliant XML object" do 
        before do
    
    maxwell's avatar
    maxwell a validé
          @status_messages = []
          10.times { @status_messages << Factory.build(:status_message)}
          @xml = Post.build_xml_for(@status_messages) 
    
        end
    
        it 'should be able to parse the body\'s contents' do
          body = parse_body_contents_from_xml(@xml).to_s
          body.should_not include "<head>"
          body.should_not include "</head>"
          body.should_not include "<posts>"
          body.should_not include "</posts>"
          body.should include "<post>"
          body.should include "</post>"
    
    maxwell's avatar
    maxwell a validé
        end
    
    
        it 'should be able to extract all posts to an array' do
    
          posts = parse_objects_from_xml(@xml)
    
          posts.is_a?(Array).should be true
          posts.count.should == 10
    
    maxwell's avatar
    maxwell a validé
        end
    
        
        it 'should be able to correctly handle comments' do
    
          person = Factory.create(:person, :email => "test@testing.com")
    
          post = Factory.create(:status_message)
    
          comment = Factory.build(:comment, :post => post, :person => person, :text => "Freedom!")
          xml = "<XML>
    
          <posts>
    
            <post>#{comment.to_xml}</post>
    
          </posts></XML>"
    
          objects = parse_objects_from_xml(xml)
          comment = objects.first
          comment.text.should == "Freedom!"
    
          comment.post.should == post
        end
    
        
        it 'should marshal retractions' do
    
          person = Factory.create(:person)
          message = Factory.create(:status_message, :person => person)
    
          retraction = Retraction.for(message)
          request = Post.build_xml_for( [retraction] )
    
    maxwell's avatar
    maxwell a validé
    
    
          StatusMessage.count.should == 1
          store_objects_from_xml( request )
          StatusMessage.count.should == 0
        end
    
        it "should create a new person upon getting a person request" do
    
    maxwell's avatar
    maxwell a validé
          request = Request.instantiate(:to =>"http://www.google.com/", :from => @person)
    
          
          original_person_id = @person.id
    
    maxwell's avatar
    maxwell a validé
          xml = Request.build_xml_for [request]
    
          Person.all.count.should be 1
    
          Person.all.count.should be 2
    
          Person.where(:url => request.callback_url).first.id.should == original_person_id
    
        it "should activate the Person if I initiated a request to that url" do 
    
    maxwell's avatar
    maxwell a validé
          request = Request.instantiate(:to => @person.url, :from => @user).save
    
    Raphael's avatar
    Raphael a validé
          
          request_remote = Request.new
    
    maxwell's avatar
    maxwell a validé
          request_remote.destination_url = @user.url
          request_remote.callback_url = @user.url
    
          request_remote.person = @person
    
    Raphael's avatar
    Raphael a validé
          request_remote.exported_key = @person.export_key
    
    maxwell's avatar
    maxwell a validé
          xml = Request.build_xml_for [request_remote]
    
          
          @person.destroy
          request_remote.destroy
          store_objects_from_xml(xml)
    
    Raphael's avatar
    Raphael a validé
          Person.first(:url => @person.url).active.should be true
    
    maxwell's avatar
    maxwell a validé
    
        it 'should marshal a retraction for a person' do
          retraction = Retraction.for(@user)
          request = Retraction.build_xml_for( [retraction] )
    
          Person.count.should == 2
          store_objects_from_xml( request )
          Person.count.should == 1
        end
    
        
        it 'should marshal a profile for a person' do
          person = Factory.create(:person)
          
          person.profile = Profile.new(:first_name => 'bob', :last_name => 'billytown', :image_url => "http://clown.com")
          old_profile = person.profile
          
          puts person.profile.inspect
    
           xml = Post.build_xml_for(person.profile)
           person.profile = nil
           person.save
    
           puts person.profile.inspect
           person.profile.should_be nil    
           store_objects_from_xml xml
          
          person = Person.first(:id => person.id)
          
          person.profile.should == old_profile
          person.profile.should_not be nil 
    
          end
    
    maxwell's avatar
    maxwell a validé
      end