require 'test_helper'
require 'active_merchant/billing/gateways/heartland_portico'

class RemoteHeartlandPorticoTest < Test::Unit::TestCase
  def setup
    @gateway = HeartlandPorticoGateway.new(fixtures(:heartland_portico))

    @amount = 100
    @declined_amount = 1025
    @credit_card = credit_card('', fixtures(:heartland_portico_mastercard))

    @options = {
      :order_id => '1',
      :billing_address => {
        :name     => "Longbob Longsen",
        :address1 => "6860 Dallas Pkwy",
        :address2 => "",
        :city     => "Plano",
        :state    => "TX",
        :zip      => "75024",
        :contry   => "US",
        :phone    => "(555) 555-5555",
      },
      :invoice            => "ABC123",
      :invoice_ship_month => Date.today.month,
      :invoice_ship_day   => Date.today.day,
      :description => 'Online Purchase',
      :allow_dup => 'Y',
    }
  end

  def test_verify
    assert response = @gateway.verify(@credit_card, @options)
    assert_success response
  end

  def test_successful_purchase
    assert response = @gateway.purchase(@amount, @credit_card, @options)
    assert_success response
    assert_equal 'APPROVAL', response.message
  end

  def test_unsuccessful_purchase
    assert response = @gateway.purchase(@declined_amount, @credit_card, @options)
    assert_failure response
    assert_equal 'DECLINE', response.message
  end

  def test_authorize_and_capture
    amount = @amount
    assert auth = @gateway.authorize(amount, @credit_card, @options)
    assert_success auth
    assert_equal 'APPROVAL', auth.message
    assert auth.authorization
    assert capture = @gateway.capture(amount, auth.authorization)
    assert_success capture
  end

  def test_failed_capture
    assert response = @gateway.capture(@amount, '42')
    assert_failure response
    assert_equal 'ERROR', response.message
  end

  def test_invalid_login
    gateway = HeartlandPorticoGateway.new(
                :user_name     => '',
                :password      => '',
                :developer_i_d => '',
                :device_id     => '',
                :license_id    => '',
                :site_id       => '',
                :version_nbr   => ''
              )
    assert response = gateway.purchase(@amount, @credit_card, @options)
    assert_failure response
    assert_equal 'ERROR', response.message
  end

  def test_avs
    # Magic amounts to generate AVS responses. See "HPS TEST Hardcode Values.xlsx"
    assert response = @gateway.purchase(9008, @credit_card, @options)
    assert_equal 'Y', response.avs_result['code']
    # assert response.avs_address
    # assert response.avs_zip

    assert response = @gateway.purchase(9002, @credit_card, @options)
    assert_equal 'N', response.avs_result['code']
    # assert !response.avs_address
    # assert !response.avs_zip
  end

  def test_cvv
    # Magic amounts to generate CVV responses. See "HPS TEST Hardcode Values.xlsx"
    assert response = @gateway.purchase(9501, @credit_card, @options)
    assert response.cvv_result['code'] == "M"

    assert response = @gateway.purchase(9502, @credit_card, @options)
    assert response.cvv_result['code'] == "N"
  end

  def test_return
    assert purchase = @gateway.purchase(@amount, @credit_card, @options)
    assert_success purchase

    assert response = @gateway.return(@amount, @credit_card)
    assert_success response
  end

  def test_reversal
    assert purchase = @gateway.purchase(@amount, @credit_card, @options)
    assert_success purchase

    assert response = @gateway.reverse(@amount, purchase.authorization)
    assert_success response
    assert_equal "APPROVAL", response.message
  end

  def test_void
    assert purchase = @gateway.purchase(@amount, @credit_card, @options)
    assert_success purchase

    assert response = @gateway.void(purchase.authorization)
    assert_success response
  end
end