require 'test_helper'

class MetricsGlobalTest < Test::Unit::TestCase
  def setup
    Base.mode = :test

    @gateway = MetricsGlobalGateway.new(fixtures(:metrics_global))
    @amount = 100
    @credit_card = credit_card('4111111111111111', verification_value: '999')
    @options = {
      order_id: generate_unique_id,
      billing_address: address(address1: '888 Test Street', zip: '77777'),
      description: 'Store purchase'
    }
  end

  def test_successful_purchase
    assert response = @gateway.purchase(@amount, @credit_card, @options)
    assert_success response
    assert response.test?
    assert_equal 'This transaction has been approved', response.message
    assert response.authorization
  end

  def test_declined_authorization
    @amount = 10
    assert response = @gateway.purchase(@amount, @credit_card, @options)
    assert_failure response
    assert response.test?
    assert_equal 'This transaction has been declined', response.message
  end

  def test_successful_authorization
    assert response = @gateway.authorize(@amount, @credit_card, @options)
    assert_success response
    assert_equal 'This transaction has been approved', response.message
    assert response.authorization
  end

  def test_authorization_and_capture
    assert authorization = @gateway.authorize(@amount, @credit_card, @options)
    assert_success authorization

    assert capture = @gateway.capture(@amount, authorization.authorization)
    assert_success capture
    assert_equal 'This transaction has been approved', capture.message
  end

  def test_authorization_and_void
    assert authorization = @gateway.authorize(@amount, @credit_card, @options)
    assert_success authorization

    assert void = @gateway.void(authorization.authorization)
    assert_success void
    assert_equal 'This transaction has been approved', void.message
  end

  def test_bad_login
    gateway = MetricsGlobalGateway.new(
      login: 'X',
      password: 'Y'
    )

    assert response = gateway.purchase(@amount, @credit_card)

    assert_equal Response, response.class
    assert_equal %w[avs_result_code
                    card_code
                    response_code
                    response_reason_code
                    response_reason_text
                    transaction_id], response.params.keys.sort

    assert_match(/Authentication Failed/, response.message)

    assert_equal false, response.success?
  end

  def test_using_test_request
    gateway = MetricsGlobalGateway.new(
      login: 'X',
      password: 'Y'
    )

    assert response = gateway.purchase(@amount, @credit_card)

    assert_equal Response, response.class
    assert_equal %w[avs_result_code
                    card_code
                    response_code
                    response_reason_code
                    response_reason_text
                    transaction_id], response.params.keys.sort

    assert_match(/Authentication Failed/, response.message)

    assert_equal false, response.success?
  end
end
