require 'test_helper'

class SagePayFormReturnTest < Test::Unit::TestCase
  include OffsitePayments::Integrations

  def setup
    @options = {:credential2 => 'EncryptionKey123'}
  end

  def test_successful_purchase
    r = SagePayForm::Return.new(successful_purchase, @options)
    assert r.success?
    assert_equal 'Successfully Authorised Transaction', r.message
  end

  def test_failed_purchase
    r = SagePayForm::Return.new(failed_purchase, @options)
    assert !r.success?
    assert_equal 'NOTAUTHED message generated by Simulator', r.message
  end

  def test_bogus_crypt
    r = SagePayForm::Return.new('crypt=SomeInvalidCryptField', @options)
    assert !r.success?
    assert_equal 'Invalid data received from SagePay', r.message
  end

  def test_missing_crypt
    r = SagePayForm::Return.new('other=stuff', @options)
    assert !r.success?
    assert_equal 'No data received from SagePay', r.message
  end

  def test_missing_key
    r = SagePayForm::Return.new(successful_purchase, {})
    assert !r.success?
    assert_equal 'No merchant decryption key supplied', r.message
  end

  def test_notification
    r = SagePayForm::Return.new(successful_purchase, @options)

    assert r.notification
    assert_kind_of SagePayForm::Notification, r.notification
    assert r.notification.complete?
    assert_equal 'Successfully Authorised Transaction', r.notification.message
  end

  private
  def successful_purchase
    'utm_nooverride=1&crypt=@BE1F6226E7478FACFA239E0F882B10E7BAE1149909C1F8D163038E623A188E693C6CFC594A49D5BB257E2CBBA58D3436A6FED00FCD7268FB9D0978CEEE3086A8B25F92FF65F059886A07BFE7E228AA965F44B9018AD73612BA2FFDFD3A2A468B88E4E77D81252B3F28173E9DCD5559D5A89937BBB88AE8FE358FE89974FC476152C23B0DA71DBEA5CA9BAB5A19541445ECB33A7D7043F5F2216C793BE026E1A799ECC9311CB4A5D5089F6CF00477E2591553764121FC936ECE25A8A5E899771D47E82B7654340E7FCA7C2B8AADAFF7AAAE986546AD409E6C2E7F8D604E7A045A0962980AC61C9D683A526C112C426F8C1EA8BCEA73ED49298E93E73BDFFA25A22861E3DD1184C17A12ECFC7CE8369B9B8D9A441BB950016DFEB438697EFEC125A68F564928CA06E63081028474B8B922720AFD5932A6B290EEFF572B267159D2'
  end

  def failed_purchase
    'utm_nooverride=1&crypt=@A60BFC79F8FAA1C02E548AD3D3D2EC888C6FF1DA74889EB45D6ED7D680CC45087A449B3E3E8727AA90AC451C0B3AC0D61F3761F3269EFEA8276DF568905DA1EFB19EE7526DA4EDC894C29A1D1D92831C22C1BC0614B86C6C6262ED29F5E8178BD80C1D9389A4B7A78305A625D49522E19C6D623C28CF34D9F87E4607F2D361894B344037120F745BC267E055762D7BD642847647FA98E6573245AFD125C2212319499D7C332893DF2E9EB085C6F58D7FD34665AC5365F46FEE276E3D17615C3FA97ACB335045BF79B6F2F612FED564587EC52CBAA558D910209ACD88E5859F6EA0FD4FC7160CD4D68E3E6B8894737708AB212A2E33CE22EB245E117AFCE6F7B29A51E903BE9209E6EA8E3C9C543F85F4A9F3109A464A0BE85FE7706BC24F7F257BE9C2D54FE46E3B382F726B53E9876D292ACB76931AA2ACD0EED18766D2D142'
  end
end
