From b6a912dd02b827da26c2ae493b448328628396d0 Mon Sep 17 00:00:00 2001
From: ilya <ilya@laptop.(none)>
Date: Thu, 30 Sep 2010 15:17:53 -0700
Subject: [PATCH] slowly getting specs to pass on with the new api

---
 app/models/user.rb               | 11 +++---
 lib/diaspora/user/friending.rb   |  4 +-
 lib/diaspora/user/receiving.rb   |  2 +-
 lib/salmon/salmon.rb             | 20 +---------
 spec/lib/salmon_salmon_spec.rb   | 63 ++++++++++++--------------------
 spec/models/user/receive_spec.rb |  5 ++-
 6 files changed, 36 insertions(+), 69 deletions(-)

diff --git a/app/models/user.rb b/app/models/user.rb
index ca248a8f29..0e19eb6af3 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -170,8 +170,9 @@ class User
   end
 
   def push_to_people(post, people)
+    salmon = salmon(post)
     people.each{|person|
-      salmon(post, :to => person)
+      push_to_person( person, salmon.xml_for( person ))
     }
   end
 
@@ -182,10 +183,8 @@ class User
 
   end
 
-  def salmon( post, opts = {} )
-    salmon = Salmon::SalmonSlap.create(self, post.to_diaspora_xml)
-    push_to_person( opts[:to], salmon.to_xml)
-    salmon
+  def salmon( post )
+    Salmon::SalmonSlap.create(self, post.to_diaspora_xml)
   end
 
   ######## Commenting  ########
@@ -217,7 +216,7 @@ class User
       push_to_people comment, people_in_aspects(aspects_with_post(comment.post.id))
     elsif owns? comment
       comment.save
-      salmon comment, :to => comment.post.person
+      push_to_people comment, [comment.post.person]
     end
   end
 
diff --git a/lib/diaspora/user/friending.rb b/lib/diaspora/user/friending.rb
index 0f6cc6c5ce..36850c7454 100644
--- a/lib/diaspora/user/friending.rb
+++ b/lib/diaspora/user/friending.rb
@@ -22,7 +22,7 @@ module Diaspora
           aspect.requests << request
           aspect.save
 
-          salmon request, :to => desired_friend
+          push_to_people request, [desired_friend]
         end
         request
       end
@@ -80,7 +80,7 @@ module Diaspora
       def unfriend(bad_friend)
         Rails.logger.info("#{self.real_name} is unfriending #{bad_friend.inspect}")
         retraction = Retraction.for(self)
-        salmon( retraction, :to => bad_friend)
+        push_to_people retraction, [bad_friend]
         remove_friend(bad_friend)
       end
 
diff --git a/lib/diaspora/user/receiving.rb b/lib/diaspora/user/receiving.rb
index 671db1a598..fe6b7d8c81 100644
--- a/lib/diaspora/user/receiving.rb
+++ b/lib/diaspora/user/receiving.rb
@@ -3,7 +3,7 @@ module Diaspora
     module Receiving
       def receive_salmon ciphertext
         cleartext = decrypt( ciphertext)
-        salmon = Salmon::SalmonSlap.parse cleartext
+        salmon = Salmon::SalmonSlap.parse cleartext, self
         if salmon.verified_for_key?(salmon.author.public_key)
           Rails.logger.info("data in salmon: #{salmon.parsed_data}")
           self.receive(salmon.parsed_data)
diff --git a/lib/salmon/salmon.rb b/lib/salmon/salmon.rb
index bd2c0b1b96..5ce1156be6 100644
--- a/lib/salmon/salmon.rb
+++ b/lib/salmon/salmon.rb
@@ -62,9 +62,7 @@ module Salmon
 
       ### Header ##
       decrypted_header = user.decrypt(doc.search('encrypted_header').text)
-      puts decrypted_header
       header_doc = Nokogiri::XML(decrypted_header)
-      puts header_doc.inspect
       slap.aes_key    = header_doc.search('aes_key').text
       slap.iv         = header_doc.search('iv').text
 
@@ -88,20 +86,6 @@ module Salmon
       slap
     end
 
-    def to_xml
-      xml =<<ENTRY
-    <?xml version='1.0' encoding='UTF-8'?>
-    <entry xmlns='http://www.w3.org/2005/Atom'>
-    <author>
-      <name>#{@author.real_name}</name>
-      <uri>acct:#{@author.diaspora_handle}</uri>
-    </author>
-      #{@magic_sig.to_xml}
-      </entry>
-ENTRY
-
-    end
-
     def xml_for person
       xml =<<ENTRY
     <?xml version='1.0' encoding='UTF-8'?>
@@ -119,14 +103,14 @@ ENTRY
 
     def decrypted_header
       header =<<HEADER
-    <header>
+    <decrypted_header>
     <iv>#{iv}</iv>
     <aes_key>#{aes_key}</aes_key>
     <author>
       <name>#{@author.real_name}</name>
       <uri>acct:#{@author.diaspora_handle}</uri>
     </author>
-    </header>
+    </decrypted_header>
 HEADER
     end
 
diff --git a/spec/lib/salmon_salmon_spec.rb b/spec/lib/salmon_salmon_spec.rb
index 116e0719da..e10ad5b48d 100644
--- a/spec/lib/salmon_salmon_spec.rb
+++ b/spec/lib/salmon_salmon_spec.rb
@@ -64,55 +64,38 @@ describe Salmon do
 
     end
 
-    it 'contains the original data' do
-      parsed_salmon.parsed_data.should == post.to_diaspora_xml
-    end
-
-  end
+    describe '#author' do
+      before do
+        stub_success("tom@tom.joindiaspora.com")
+      end
 
-  context 'round trip' do
-    before do
-      @sent_salmon = Salmon::SalmonSlap.create(user, post.to_diaspora_xml)
-      @parsed_salmon =
-      stub_success("tom@tom.joindiaspora.com")
-    end
-
-
-    it 'should verify the signature on a roundtrip' do
+      it 'should reference a local author' do
+        parsed_salmon.author.should == user.person
+      end
 
-      @sent_salmon.magic_sig.data.should == @parsed_salmon.magic_sig.data
+      it 'should reference a remote author' do
+        parsed_salmon.author_email = 'tom@tom.joindiaspora.com'
+        parsed_salmon.author.public_key.should_not be_nil
+      end
 
-      @sent_salmon.magic_sig.sig.should == @parsed_salmon.magic_sig.sig
-      @sent_salmon.magic_sig.signable_string.should == @parsed_salmon.magic_sig.signable_string
-
-      @parsed_salmon.verified_for_key?(OpenSSL::PKey::RSA.new(user.exported_key)).should be true
-      @sent_salmon.verified_for_key?(OpenSSL::PKey::RSA.new(user.exported_key)).should be true
+      it 'should fail to reference a nonexistent remote author' do
+        parsed_salmon.author_email = 'idsfug@difgubhpsduh.rgd'
+        proc {
+          Redfinger.stub(:finger).and_return(nil) #Redfinger returns nil when there is no profile
+          parsed_salmon.author.real_name}.should raise_error /No webfinger profile found/
+      end
     end
 
-    it 'should return the data so it can be "received"' do
-
-      xml = post.to_diaspora_xml
-
-      @parsed_salmon.parsed_data.should == xml
+    it 'verifies the signature for the sender' do
+      parsed_salmon.verified_for_key?(user.public_key).should be true
     end
 
-
-
-    it 'should reference a local author' do
-      @parsed_salmon.author.should == user.person
+    it 'contains the original data' do
+      parsed_salmon.parsed_data.should == post.to_diaspora_xml
     end
 
-    it 'should reference a remote author' do
-      @parsed_salmon.author_email = 'tom@tom.joindiaspora.com'
-      @parsed_salmon.author.public_key.should_not be_nil
-    end
+  end
+
 
-    it 'should fail to reference a nonexistent remote author' do
-      @parsed_salmon.author_email = 'idsfug@difgubhpsduh.rgd'
-      proc {
-        Redfinger.stub(:finger).and_return(nil) #Redfinger returns nil when there is no profile
-        @parsed_salmon.author.real_name}.should raise_error /No webfinger profile found/
-    end
 
-  end
 end
diff --git a/spec/models/user/receive_spec.rb b/spec/models/user/receive_spec.rb
index 5bdddce67a..f11de234f1 100644
--- a/spec/models/user/receive_spec.rb
+++ b/spec/models/user/receive_spec.rb
@@ -173,11 +173,12 @@ describe User do
   describe 'salmon' do
     before do
       @post = @user.post :status_message, :message => "hello", :to => @aspect.id
-      @salmon = @user.salmon( @post, :to => @user2.person )
+      @salmon = @user.salmon( @post )
     end
 
     it 'should receive a salmon for a post' do
-      @user2.receive_salmon( @user2.person.encrypt(@salmon.to_xml) )
+      puts @salmon.inspect
+      @user2.receive_salmon( @salmon.xml_for @user2.person )
       @user2.visible_post_ids.include?(@post.id).should be true
     end
   end
-- 
GitLab