require 'spec_helper'

describe ProtxForm do
  before(:each) do
    @protx = ProtxForm.new(:encryption_key => 'foo', :app_key => 'testapp')
  end
  
  describe 'decrypting a real protx failure string' do
    before(:each) do
      @protx = ProtxForm.new(:encryption_key => 'DVeedh7YZ9hwBSHF', :app_key => 'testapp')
      @crypt = 'FyIEEREbChcVbSkiFhsNAmIFEQQQHUQdP00JHi5uBgkQFzAxLC1zeTdcGwQjNC1mIzMLABYJQzw GQoOYgAhKzE6BBELGhEPP1cMGDAHMAUrMgBYNAlDKzNaA1khPCYgLTFIBxYBVDIpFFlRFAMbEjwfAVgfKg4abwAqMXB eAJ2FUhRJlwEdBt7LkVvEXwFcRNcJCdYAmhqRE42Lzw9KDBrVFVCKWEKGW9aSgMfBGYJFzEmLE52PT5LDQQxAS01MToRWCkpYxoSfCxREjw7Mgc5AQA2DUQsNk1VOgMHCw4BEkMmMlplPClMBAN/HgkSBx4gIUIvXj8ueAETf2NudQAFAAYRGlIKLlgcAjFuBw1iFSQzMlV6Fw5sJE56FRtxdmMjJz4yBA1ubDA/ZBApNCACHBUBVWEQCXhOOyMgPHIAPwIMEBsKYG4MXQ=='
    end
    
    it "should decrypt the string" do
      @protx.send(:decrypt_from_protx, @crypt).should == {
        "Status"=>"NOTAUTHED", 
        "VPSTxId"=>"{B9C59BF2-0D2C-4B43-ABF2-B4C5E9AC0510}", 
        "CardType"=>"VISA", 
        "VendorTxCode"=>"Patrick.config-bricks-1", 
        "StatusDetail"=>"NOTAUTHED message generated by Simulator", 
        "GiftAid"=>"0", 
        "AVSCV2"=>"ALL MATCH", 
        "Amount"=>"10", 
        "Last4Digits"=>"9455", 
        "CAVV"=>"MNTUL98FS725FBZZ3T4UXH", 
        "3DSecureStatus"=>"OK", 
        "CV2Result"=>"MATCHED", 
        "PostCodeResult"=>"MATCHED", 
        "AddressResult"=>"MATCHED"
      }
    end
  end
  
  describe 'encrypting/decrypting' do
    def perform_encrypt_decrypt(hash)
      crypt = @protx.send(:encrypt_for_protx, hash)
      @protx.send(:decrypt_from_protx, crypt)
    end
    
    it "should work on a small hash" do
      hash = { 'foo' => 'bar' }
      perform_encrypt_decrypt(hash).should == hash
    end
    
    it "should work on a real Protx hash" do
      hash = {
        "Status"=>"OK", "VPSTxId"=>"{D89F4C3B-6FA8-4DC1-BB2E-8653AA3FACB6}", 
        "VendorTxCode"=>"livro.lan-bricks-14", "StatusDetail"=>"Successfully Authorised Transaction", 
        "GiftAid"=>"0", "AVSCV2"=>"ALL MATCH", "Amount"=>"10", "TxAuthNo"=>"7349", 
        "CAVV"=>"MN3K9KDW22ENJLARAGIXDK", "3DSecureStatus"=>"OK", "CV2Result"=>"MATCHED", 
        "PostCodeResult"=>"MATCHED",  "AddressResult"=>"MATCHED"
      }
      perform_encrypt_decrypt(hash).should == hash
    end
    
    it "should work on a real Protx hash with a long string" do
      string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
      hash = {
        "Status"=>"OK", "VPSTxId"=>"{D89F4C3B-6FA8-4DC1-BB2E-8653AA3FACB6}", 
        "VendorTxCode"=>"livro.lan-bricks-14", "StatusDetail"=>"Successfully Authorised Transaction", 
        "GiftAid"=>"0", "AVSCV2"=>"ALL MATCH", "Amount"=>"10", "TxAuthNo"=>"7349", 
        "CAVV"=>"MN3K9KDW22ENJLARAGIXDK", "3DSecureStatus"=>"OK", "CV2Result"=>"MATCHED", 
        "PostCodeResult"=>"MATCHED",  "AddressResult"=>"MATCHED", "foo" => string
      }
      perform_encrypt_decrypt(hash).should == hash
    end
  end
  
  describe "object details for a brick" do
    before(:each) do
      @brick = Brick.create(:value => 250, :first_name => 'first', :last_name => 'last')
      @object_details = @protx.send(:object_details, @brick, 'success', 'failure')
    end
    
    it "should set the vendor tx code to use the brick url key" do
      @object_details['VendorTxCode'].should == "testapp-bricks-#{@brick.id}"
    end
  end
  
  describe 'parsing Protx result' do
    
  end
end
