From 8c75761ba16bcf5a5c9ea14eabd1008ca5eb2bd2 Mon Sep 17 00:00:00 2001
From: ilya <ilya@laptop.(none)>
Date: Sat, 10 Jul 2010 02:25:48 -0400
Subject: [PATCH] RS IZ Friday night, key_fingerprint serialization weirdness

---
 app/models/person.rb          |   4 ++++
 app/models/request.rb         |   8 +++++---
 app/models/user.rb            |   9 +++++----
 gpg/diaspora-test/random_seed | Bin 600 -> 600 bytes
 lib/common.rb                 |   4 ++++
 spec/factories.rb             |   4 +++-
 spec/models/request_spec.rb   |   2 +-
 spec/user_encryption_spec.rb  |  24 +++++++++++++++++++++++-
 8 files changed, 45 insertions(+), 10 deletions(-)

diff --git a/app/models/person.rb b/app/models/person.rb
index 48999c664b..fc09436144 100644
--- a/app/models/person.rb
+++ b/app/models/person.rb
@@ -6,6 +6,7 @@ class Person
   xml_accessor :url
   xml_accessor :profile, :as => Profile
   xml_accessor :_id
+  xml_accessor :key_fingerprint
   
   key :email, String
   key :url, String
@@ -37,6 +38,9 @@ class Person
     GPGME::Ctx.new.get_key key_fingerprint
   end
 
+  def export_key
+    GPGME::export(key_fingerprint, :armor => true)
+  end
 
   protected
   
diff --git a/app/models/request.rb b/app/models/request.rb
index 95623d9a89..9740373de8 100644
--- a/app/models/request.rb
+++ b/app/models/request.rb
@@ -8,12 +8,14 @@ class Request
   xml_accessor :person, :as => Person
   xml_accessor :destination_url
   xml_accessor :callback_url
+  xml_accessor :exported_key
 
   key :destination_url, String
   key :callback_url, String
-  key :person_id, ObjectId
+  key :person, Person#_id, ObjectId
+  key :exported_key, String
 
-  belongs_to :person
+  #belongs_to :person
   
   validates_presence_of :destination_url, :callback_url
 
@@ -22,7 +24,7 @@ class Request
 
   def self.instantiate(options ={})
     person = options[:from]
-    self.new(:destination_url => options[:to], :callback_url => person.url, :person => person)
+    self.new(:destination_url => options[:to], :callback_url => person.url, :person => person, :exported_key => person.export_key)
   end
 
   def activate_friend 
diff --git a/app/models/user.rb b/app/models/user.rb
index aa1cbb296e..83fd248882 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -28,10 +28,10 @@ class User < Person
   ######### Friend Requesting
   def send_friend_request_to(friend_url)
     unless Person.where(:url => friend_url).first
-      p = Request.instantiate(:to => friend_url, :from => self)
-      if p.save
-        p.push_to_url friend_url
-        p
+      request = Request.instantiate(:to => friend_url, :from => self)
+      if request.save
+        request.push_to_url friend_url
+        request
       end
     end
   end 
@@ -41,6 +41,7 @@ class User < Person
     request.activate_friend
     request.person = self
     request.destination_url = request.callback_url
+    request.exported_key = self.export_key
     request.push_to_url(request.callback_url)
     request.destroy
   end
diff --git a/gpg/diaspora-test/random_seed b/gpg/diaspora-test/random_seed
index 43eed01099182d4478a216fa9e71643377647dd7..b7077ad63746b9d872bd00e3de10f8ed9c31f551 100644
GIT binary patch
literal 600
zcmV-e0;m1?U}L4itnsTAbzhz6ewf&qBl#pfqDGcR%z1UcrWLGQ$1EPH*83=ZR|3ZH
zK&^dE>0(a(xa0x&Ve@wELg9!&rjE2gzoWrkh{z47WR^mC5YbMUGcdCyJ!RyB*<uyL
zganwZOvjedrOQaB=*U^!GzA4b6LX~AdHwWx11K#}D)svpzXMb~FPMC}+caW!(WX$d
zFyh?E{f1~BdTvMWC)}9CgNa{PON=a$>0rjIG!GDUyD<bodnrNZWWt3Mbu|PDw*V_1
z9AYDWA@Eira_MclY-gi42Hr3xt6&ug-S=p_udKOMy9^I;$yzZ6&}W1+JFmt%`MZ?N
zxFdh<yd&m+a0`;UqUwYnU}Q!MVbm8$jZREn8Qm};phYjfauC^Ve*bzz>k+JFcW)fM
zjIov{ASZiJVE^4#M0GsFwB9u=3sk3d;XF7CWiNQa>L?u}a7~B{f!-LqI@d@lMA?1p
zIjDKt`_D^lX%`JndA+Rtj1UCGpf*HK@J<%Yr-4~STICuIS=(BCeOwE`KW7w@=Rn4!
zl<3Nd!zxjp8R%|?u;I1M2|W1i9bTzM&^l#K`YlL2MCxgqKW{r4om!ufgsWWV;CEZ^
z((<7ypP_v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17,11 @@ module Diaspora
       body = parse_body_contents_from_xml(xml)
       body.children.each do |post|
         begin
+          puts "people: #{Person.count}"
+          puts "requests: #{Request.count}"
           object = post.name.camelize.constantize.from_xml post.to_s
+          puts "people: #{Person.count}"
+          puts "requests: #{Request.count}"          
           object.person =  parse_owner_from_xml post.to_s if object.respond_to? :person  
           objects << object 
         rescue
diff --git a/spec/factories.rb b/spec/factories.rb
index 5467a6b395..90eddd3ba1 100644
--- a/spec/factories.rb
+++ b/spec/factories.rb
@@ -10,8 +10,9 @@ Factory.define :profile do |p|
 end
 
 Factory.define :person do |p|
-  p.email "bob@aol.com"
+  p.email "bob-person@aol.com"
   p.sequence(:url)  {|n|"http://google-#{n}.com/"}
+  p.key_fingerprint GPGME::list_keys("Aditi").first.subkeys.first.fingerprint
   p.profile Profile.new( :first_name => "Robert", :last_name => "Grimm" )
 end
 
@@ -20,6 +21,7 @@ Factory.define :user do |u|
   u.password "bluepin7"
   u.password_confirmation "bluepin7"
   u.url  "www.example.com/"
+  u.key_fingerprint  GPGME.list_keys(nil, true).first.subkeys.first.fingerprint
   u.profile Profile.new( :first_name => "Bob", :last_name => "Smith" )
 end
 
diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb
index f3357fc739..a6289c4be9 100644
--- a/spec/models/request_spec.rb
+++ b/spec/models/request_spec.rb
@@ -11,7 +11,7 @@ describe Request do
   end
 
   it 'should generate xml for the User as a Person' do 
-    user = User.create(:email => "rob@bob.com")
+    user = Factory.build(:user, :email => "rob@bob.com")
 
     user.profile = Factory.create(:profile)
     
diff --git a/spec/user_encryption_spec.rb b/spec/user_encryption_spec.rb
index b07fb39d15..b3737feae9 100644
--- a/spec/user_encryption_spec.rb
+++ b/spec/user_encryption_spec.rb
@@ -1,4 +1,5 @@
 require File.dirname(__FILE__) + '/spec_helper'
+include ApplicationHelper
 
 describe 'user encryption' do
   before :all do
@@ -28,7 +29,28 @@ describe 'user encryption' do
   end
 
   describe 'key exchange on friending' do
-    
+    it 'should send over a public key' do
+      Comment.send(:class_variable_get, :@@queue).stub!(:add_post_request)
+      request = @u.send_friend_request_to("http://example.com/") 
+      Request.build_xml_for([request]).include?( @u.export_key).should be true
+    end
+
+    it 'should receive and marshal a public key from a request' do
+      puts "THIS IS FUCKED UP"
+      person  = Factory.build(:person ) 
+      original_key = person.export_key 
+      person.save
+      
+      request = Request.instantiate(:to =>"http://www.google.com/", :from => person)
+      
+      xml = Request.build_xml_for [request]
+      person.destroy
+      
+      store_objects_from_xml(xml)
+      
+      new_person = Person.first(:url => request.callback_url)
+      new_person.export_key.should == original_key 
+    end
   end
 
   describe 'signing and verifying' do
-- 
GitLab