Skip to content
Extraits de code Groupes Projets
user_encryption_spec.rb 4,17 ko
Newer Older
  • Learn to ignore specific revisions
  • Raphael's avatar
    Raphael a validé
    #   Copyright (c) 2010, Diaspora Inc.  This file is
    
    Raphael's avatar
    Raphael a validé
    #   licensed under the Affero General Public License version 3 or later.  See
    
    Raphael's avatar
    Raphael a validé
    #   the COPYRIGHT file.
    
    require 'spec_helper'
    
    
    describe 'user encryption' do
    
      before do
    
    Raphael's avatar
    Raphael a validé
        @user = Factory.create(:user)
    
    Raphael's avatar
    Raphael a validé
        @aspect = @user.aspect(:name => 'dudes')
    
    Raphael's avatar
    Raphael a validé
    
    
        @user2 = Factory.create(:user)
        @aspect2 = @user2.aspect(:name => 'dudes')
    
        #gpgdir = File.expand_path("../../db/gpg-#{Rails.env}", __FILE__)
        #ctx = GPGME::Ctx.new
        #keys = ctx.keys
        #keys.each{|k| ctx.delete_key(k, true)}
    
      it 'should have a key' do
    
        @user.encryption_key.should_not be nil
    
    ilya's avatar
    ilya a validé
      describe 'key exchange on friending' do
    
        it 'should send over a public key' do
          message_queue.stub!(:add_post_request)
    
    Raphael's avatar
    Raphael a validé
          request = @user.send_friend_request_to(Factory.create(:person), @aspect)
    
          request.to_diaspora_xml.include?( @user.exported_key).should be true
    
        end
    
        it 'should receive and marshal a public key from a request' do
    
          remote_user = Factory.build(:user)
          remote_user.encryption_key.nil?.should== false
    
          #should move this to friend request, but i found it here
    
          id = remote_user.person.id
    
          request = remote_user.send_friend_request_to(
    
    Raphael's avatar
    Raphael a validé
            @user.person, remote_user.aspect(:name => "temp"))
    
          xml = request.to_diaspora_xml
    
    ilya's avatar
    ilya a validé
          remote_user.person.delete
          remote_user.delete
    
          person_count = Person.all.count
    
    ilya's avatar
    ilya a validé
          @user.receive xml, remote_user.person
            
    
          Person.all.count.should == person_count + 1
          new_person = Person.first(:id => id)
    
          new_person.exported_key.should == original_key
    
        end
    
    ilya's avatar
    ilya a validé
      end
    
    
    Raphael's avatar
    Raphael a validé
      describe 'encryption' do
        before do
    
    Raphael's avatar
    Raphael a validé
          @message = @user.post :status_message, :message => "hi", :to => @aspect.id
    
    Raphael's avatar
    Raphael a validé
        end
        it 'should encrypt large messages' do
          ciphertext = @user.encrypt @message.to_diaspora_xml
          ciphertext.include?(@message.to_diaspora_xml).should be false
          @user.decrypt(ciphertext).include?(@message.to_diaspora_xml).should be true
        end
      end
    
    
    Raphael's avatar
    Raphael a validé
      describe 'comments' do
        before do
    
          friend_users(@user, @aspect, @user2, @aspect2)
          @remote_message = @user2.post :status_message, :message => "hello", :to => @aspect2.id
    
    
    
    Raphael's avatar
    Raphael a validé
          @message = @user.post :status_message, :message => "hi", :to => @aspect.id
    
    Raphael's avatar
    Raphael a validé
        end
        it 'should attach the creator signature if the user is commenting' do
          @user.comment "Yeah, it was great", :on => @remote_message
    
          @remote_message.comments.first.signature_valid?.should be true
    
    Raphael's avatar
    Raphael a validé
        end
    
        it 'should sign the comment if the user is the post creator' do
    
    Raphael's avatar
    Raphael a validé
          message = @user.post :status_message, :message => "hi", :to => @aspect.id
    
    Raphael's avatar
    Raphael a validé
          @user.comment "Yeah, it was great", :on => message
    
          message.comments.first.signature_valid?.should be true
    
    Raphael's avatar
    Raphael a validé
          message.comments.first.verify_post_creator_signature.should be true
    
    Raphael's avatar
    Raphael a validé
        end
    
    Raphael's avatar
    Raphael a validé
        it 'should verify a comment made on a remote post by a different friend' do
    
          comment = Comment.new(:person => @user2.person, :text => "cats", :post => @remote_message)
          comment.creator_signature = comment.send(:sign_with_key,@user2.encryption_key)
    
          comment.signature_valid?.should be true
    
          comment.verify_post_creator_signature.should be false
    
          comment.post_creator_signature = comment.send(:sign_with_key,@user.encryption_key)
    
          comment.verify_post_creator_signature.should be true
    
    Raphael's avatar
    Raphael a validé
        end
    
        it 'should reject comments on a remote post with only a creator sig' do
    
          comment = Comment.new(:person => @user2.person, :text => "cats", :post => @remote_message)
          comment.creator_signature = comment.send(:sign_with_key,@user2.encryption_key)
    
          comment.signature_valid?.should be true
    
          comment.verify_post_creator_signature.should be false
    
    Raphael's avatar
    Raphael a validé
        end
    
        it 'should receive remote comments on a user post with a creator sig' do
    
          comment = Comment.new(:person => @user2.person, :text => "cats", :post => @message)
          comment.creator_signature = comment.send(:sign_with_key,@user2.encryption_key)
    
          comment.signature_valid?.should be true
    
          comment.verify_post_creator_signature.should be false
    
    Raphael's avatar
    Raphael a validé
        end
    
      end