From a8400ad4607e518ecf1b9a1d416956f2bd455317 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Thu, 14 Jul 2011 19:08:03 -0700 Subject: [PATCH] 2 more specs failing. wip --- app/models/reshare.rb | 14 ++++++++---- spec/models/reshare_spec.rb | 43 ++++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/app/models/reshare.rb b/app/models/reshare.rb index 9b80ba533..716780a7d 100644 --- a/app/models/reshare.rb +++ b/app/models/reshare.rb @@ -39,11 +39,17 @@ class Reshare < Post root_author = Webfinger.new(@root_diaspora_id).fetch root_author.save! - unless self.root = Post.where(:guid => @root_guid).first - self.root = Diaspora::Parser.from_xml(Faraday.get(root_author.url + "/p/#{@root_guid}.xml").body) - self.root.save! + local_post = Post.where(:guid => @root_guid).select('id').first + + unless local_post && self.root_id = local_post.id + received_post = Diaspora::Parser.from_xml(Faraday.get(root_author.url + "/p/#{@root_guid}.xml").body) + unless post = received_post.class.where(:guid => received_post.guid).first + post = received_post + post.save + end + + self.root_id = post.id end - end def root_must_be_public diff --git a/spec/models/reshare_spec.rb b/spec/models/reshare_spec.rb index f029d5b2d..c3b02d9ed 100644 --- a/spec/models/reshare_spec.rb +++ b/spec/models/reshare_spec.rb @@ -78,21 +78,11 @@ describe Reshare do @root_object = @reshare.root.delete end - it 'fetches the root post from root_guid' do - response = mock - response.stub(:body).and_return(@root_object.to_diaspora_xml) - Faraday.default_connection.should_receive(:get).with(@reshare.root.author.url + public_post_path(:guid => @root_object.guid, :format => "xml")).and_return(response) - - root = Reshare.from_xml(@xml).root - - [:text, :guid, :diaspora_handle, :type].each do |attr| - root.send(attr).should == @reshare.root.send(attr) - end - end - it 'fetches the root author from root_diaspora_id' do - @original_profile = @reshare.root.author.profile - @original_author = @reshare.root.author.delete + @original_profile = @reshare.root.author.profile.dup + @reshare.root.author.profile.delete + @original_author = @reshare.root.author.dup + @reshare.root.author.delete wf_prof_mock = mock wf_prof_mock.should_receive(:fetch).and_return(@original_author) @@ -102,9 +92,32 @@ describe Reshare do response.stub(:body).and_return(@root_object.to_diaspora_xml) Faraday.default_connection.should_receive(:get).with(@original_author.url + public_post_path(:guid => @root_object.guid, :format => "xml")).and_return(response) - Reshare.from_xml(@xml) end + + context 'saving the post' do + before do + response = mock + response.stub(:body).and_return(@root_object.to_diaspora_xml) + Faraday.default_connection.should_receive(:get).with(@reshare.root.author.url + public_post_path(:guid => @root_object.guid, :format => "xml")).and_return(response) + end + + it 'fetches the root post from root_guid' do + root = Reshare.from_xml(@xml).root + + [:text, :guid, :diaspora_handle, :type, :public].each do |attr| + root.send(attr).should == @reshare.root.send(attr) + end + end + + it 'correctly saves the type' do + Reshare.from_xml(@xml).root.reload.type.should == "StatusMessage" + end + + it 'correctly sets the author' do + Reshare.from_xml(@xml).root.reload.author.reload.should == @original_author + end + end end end end