<commit_before>require 'test_helper'

class CardStreamTest < Test::Unit::TestCase
  include CommStub

  def setup
    @gateway = CardStreamGateway.new(
      :login => 'login',
      :shared_secret => 'secret'
    )

    @visacreditcard = credit_card('4929421234600821',
      :month => '12',
      :year => '2014',
      :verification_value => '356',
      :brand => :visa
    )

    @visacredit_options = {
      :billing_address => {
        :address1 => "Flat 6, Primrose Rise",
        :address2 => "347 Lavender Road",
        :city => "",
        :state => "Northampton",
        :zip => 'NN17 8YG '
      },
      :order_id => generate_unique_id,
      :description => 'AM test purchase'
    }

    @declined_card = credit_card('4000300011112220',
      :month => '9',
      :year => '2014'
    )
  end

  def test_successful_visacreditcard_authorization
    @gateway.expects(:ssl_post).returns(successful_authorization_response)

    assert responseAuthorization = @gateway.authorize(142, @visacreditcard, @visacredit_options)
    assert_equal 'APPROVED', responseAuthorization.message
    assert_success responseAuthorization
    assert responseAuthorization.test?
    assert !responseAuthorization.authorization.blank?
  end

  def test_successful_avs_and_cvv_results
    @gateway.expects(:ssl_post).returns(successful_authorization_response)

    assert response = @gateway.authorize(142, @visacreditcard, @visacredit_options)
    assert_success response
    assert response.avs_result
    assert_equal "M", response.avs_result['code']
    assert_equal "Street address and postal code match.", response.avs_result['message']
    assert_equal "Y", response.avs_result['street_match']
    assert_equal "Y", response.avs_result['postal_match']

    assert response.cvv_result
    assert_equal "M", response.cvv_result['code']
  end

  def test_successful_visacreditcard_capture
    @gateway.expects(:ssl_post).returns(successful_capture_response)

    assert responseCapture = @gateway.capture(142, 'authorization', @visacredit_options)
    assert_equal 'APPROVED', responseCapture.message
    assert_success responseCapture
    assert responseCapture.test?
  end

  def test_successful_visacreditcard_refund
    @gateway.expects(:ssl_post).returns(successful_refund_response)

    assert responseRefund = @gateway.refund(142, "authorization", @visacredit_options)
    assert_equal 'APPROVED', responseRefund.message
    assert_success responseRefund
    assert responseRefund.test?
  end

  def test_successful_visacreditcard_void
    @gateway.expects(:ssl_post).returns(successful_void_response)

    assert responseRefund = @gateway.void("authorization", @visacredit_options)
    assert_equal 'APPROVED', responseRefund.message
    assert_success responseRefund
    assert responseRefund.test?
  end

  def test_successful_visacreditcard_purchase
    @gateway.expects(:ssl_post).returns(successful_purchase_response)

    assert responseRefund = @gateway.purchase(142, @visacreditcard, @visacredit_options)
    assert_equal 'APPROVED', responseRefund.message
    assert_success responseRefund
    assert responseRefund.test?
  end

  def test_successful_visacreditcard_purchase_via_reference
    @gateway.expects(:ssl_post).returns(successful_reference_purchase_response)

    assert responsePurchase = @gateway.purchase(142, 'authorization', @visacredit_options)
    assert_equal 'APPROVED', responsePurchase.message
    assert_success responsePurchase
    assert responsePurchase.test?
  end

  def test_failed_visacreditcard_purchase_via_reference
    @gateway.expects(:ssl_post).returns(failed_reference_purchase_response)

    assert responsePurchase = @gateway.purchase(142, 'authorization', @visacredit_options)
    assert_equal 'DB ERROR', responsePurchase.message
    assert_failure responsePurchase
    assert responsePurchase.test?
  end

  def test_declined_mastercard_purchase
    @gateway.expects(:ssl_post).returns(failed_purchase_card_declined_response)

    assert response = @gateway.purchase(10000, @visacreditcard, @visacredit_options)
    assert_equal 'CARD DECLINED', response.message
    assert_failure response
    assert response.test?
  end

  def test_successful_verify
    response = stub_comms do
      @gateway.verify(@visacreditcard, @visacredit_options)
    end.respond_with(successful_authorization_response, failed_void_response)
    assert_success response
    assert_equal "APPROVED", response.message
  end

  def test_failed_verify
    response = stub_comms do
      @gateway.verify(@declined_card, @visacredit_options)
    end.respond_with(failed_authorization_response, successful_void_response)
    assert_failure response
    assert_equal "CARD DECLINED", response.message
  end

  def test_purchase_options

    # Default
    purchase = stub_comms do
      @gateway.purchase(142, @visacreditcard, @visacredit_options)
    end.check_request do |endpoint, data, headers|
      assert_match(/type=1/, data)
    end.respond_with(successful_purchase_response)

    assert_success purchase

    purchase = stub_comms do
      @gateway.purchase(142, @visacreditcard, @visacredit_options.merge(type: 2))
    end.check_request do |endpoint, data, headers|
      assert_match(/type=2/, data)
    end.respond_with(successful_purchase_response)

    assert_success purchase
  end

  def test_successful_purchase_without_street_address
    @gateway.expects(:ssl_post).returns(successful_purchase_response)
    assert response = @gateway.purchase(142, @visacreditcard, billing_address: {state: "Northampton"})
    assert_equal 'APPROVED', response.message
  end

  def test_successful_purchase_without_any_address
    @gateway.expects(:ssl_post).returns(successful_purchase_response)
    assert response = @gateway.purchase(142, @visacreditcard)
    assert_equal 'APPROVED', response.message
  end

  def test_hmac_signature_added_to_post
    post_params = "action=SALE&amount=10000&cardCVV=356&cardExpiryMonth=12&cardExpiryYear=14&cardNumber=4929421234600821&countryCode=GB&currencyCode=826&customerAddress=Flat+6%2C+Primrose+Rise+347+Lavender+Road&customerName=Longbob+Longsen&customerPostCode=NN17+8YG+&merchantID=login&orderRef=AM+test+purchase&threeDSRequired=N&transactionUnique=#{@visacredit_options[:order_id]}&type=1"
    expected_signature = Digest::SHA512.hexdigest("#{post_params}#{@gateway.options[:shared_secret]}")

    @gateway.expects(:ssl_post).with do |url, data|
      data.include?("signature=#{expected_signature}")
    end.returns(successful_authorization_response)

    @gateway.purchase(10000, @visacreditcard, @visacredit_options)
  end

  def test_3ds_response
    purchase = stub_comms do
      @gateway.purchase(142, @visacreditcard, @visacredit_options.merge(threeds_required: true))
    end.check_request do |endpoint, data, headers|
      assert_match(/threeDSRequired=Y/, data)<commit_msg>Fix CardStream HMAC signature calculation unit test<commit_after>require 'test_helper'

class CardStreamTest < Test::Unit::TestCase
  include CommStub

  def setup
    @gateway = CardStreamGateway.new(
      :login => 'login',
      :shared_secret => 'secret'
    )

    @visacreditcard = credit_card('4929421234600821',
      :month => '12',
      :year => '2014',
      :verification_value => '356',
      :brand => :visa
    )

    @visacredit_options = {
      :billing_address => {
        :address1 => "Flat 6, Primrose Rise",
        :address2 => "347 Lavender Road",
        :city => "",
        :state => "Northampton",
        :zip => 'NN17 8YG '
      },
      :order_id => generate_unique_id,
      :description => 'AM test purchase'
    }

    @declined_card = credit_card('4000300011112220',
      :month => '9',
      :year => '2014'
    )
  end

  def test_successful_visacreditcard_authorization
    @gateway.expects(:ssl_post).returns(successful_authorization_response)

    assert responseAuthorization = @gateway.authorize(142, @visacreditcard, @visacredit_options)
    assert_equal 'APPROVED', responseAuthorization.message
    assert_success responseAuthorization
    assert responseAuthorization.test?
    assert !responseAuthorization.authorization.blank?
  end

  def test_successful_avs_and_cvv_results
    @gateway.expects(:ssl_post).returns(successful_authorization_response)

    assert response = @gateway.authorize(142, @visacreditcard, @visacredit_options)
    assert_success response
    assert response.avs_result
    assert_equal "M", response.avs_result['code']
    assert_equal "Street address and postal code match.", response.avs_result['message']
    assert_equal "Y", response.avs_result['street_match']
    assert_equal "Y", response.avs_result['postal_match']

    assert response.cvv_result
    assert_equal "M", response.cvv_result['code']
  end

  def test_successful_visacreditcard_capture
    @gateway.expects(:ssl_post).returns(successful_capture_response)

    assert responseCapture = @gateway.capture(142, 'authorization', @visacredit_options)
    assert_equal 'APPROVED', responseCapture.message
    assert_success responseCapture
    assert responseCapture.test?
  end

  def test_successful_visacreditcard_refund
    @gateway.expects(:ssl_post).returns(successful_refund_response)

    assert responseRefund = @gateway.refund(142, "authorization", @visacredit_options)
    assert_equal 'APPROVED', responseRefund.message
    assert_success responseRefund
    assert responseRefund.test?
  end

  def test_successful_visacreditcard_void
    @gateway.expects(:ssl_post).returns(successful_void_response)

    assert responseRefund = @gateway.void("authorization", @visacredit_options)
    assert_equal 'APPROVED', responseRefund.message
    assert_success responseRefund
    assert responseRefund.test?
  end

  def test_successful_visacreditcard_purchase
    @gateway.expects(:ssl_post).returns(successful_purchase_response)

    assert responseRefund = @gateway.purchase(142, @visacreditcard, @visacredit_options)
    assert_equal 'APPROVED', responseRefund.message
    assert_success responseRefund
    assert responseRefund.test?
  end

  def test_successful_visacreditcard_purchase_via_reference
    @gateway.expects(:ssl_post).returns(successful_reference_purchase_response)

    assert responsePurchase = @gateway.purchase(142, 'authorization', @visacredit_options)
    assert_equal 'APPROVED', responsePurchase.message
    assert_success responsePurchase
    assert responsePurchase.test?
  end

  def test_failed_visacreditcard_purchase_via_reference
    @gateway.expects(:ssl_post).returns(failed_reference_purchase_response)

    assert responsePurchase = @gateway.purchase(142, 'authorization', @visacredit_options)
    assert_equal 'DB ERROR', responsePurchase.message
    assert_failure responsePurchase
    assert responsePurchase.test?
  end

  def test_declined_mastercard_purchase
    @gateway.expects(:ssl_post).returns(failed_purchase_card_declined_response)

    assert response = @gateway.purchase(10000, @visacreditcard, @visacredit_options)
    assert_equal 'CARD DECLINED', response.message
    assert_failure response
    assert response.test?
  end

  def test_successful_verify
    response = stub_comms do
      @gateway.verify(@visacreditcard, @visacredit_options)
    end.respond_with(successful_authorization_response, failed_void_response)
    assert_success response
    assert_equal "APPROVED", response.message
  end

  def test_failed_verify
    response = stub_comms do
      @gateway.verify(@declined_card, @visacredit_options)
    end.respond_with(failed_authorization_response, successful_void_response)
    assert_failure response
    assert_equal "CARD DECLINED", response.message
  end

  def test_purchase_options

    # Default
    purchase = stub_comms do
      @gateway.purchase(142, @visacreditcard, @visacredit_options)
    end.check_request do |endpoint, data, headers|
      assert_match(/type=1/, data)
    end.respond_with(successful_purchase_response)

    assert_success purchase

    purchase = stub_comms do
      @gateway.purchase(142, @visacreditcard, @visacredit_options.merge(type: 2))
    end.check_request do |endpoint, data, headers|
      assert_match(/type=2/, data)
    end.respond_with(successful_purchase_response)

    assert_success purchase
  end

  def test_successful_purchase_without_street_address
    @gateway.expects(:ssl_post).returns(successful_purchase_response)
    assert response = @gateway.purchase(142, @visacreditcard, billing_address: {state: "Northampton"})
    assert_equal 'APPROVED', response.message
  end

  def test_successful_purchase_without_any_address
    @gateway.expects(:ssl_post).returns(successful_purchase_response)
    assert response = @gateway.purchase(142, @visacreditcard)
    assert_equal 'APPROVED', response.message
  end

  def test_hmac_signature_added_to_post
    post_params = "action=SALE&amount=10000&captureDelay=0&cardCVV=356&cardExpiryMonth=12&cardExpiryYear=14&cardNumber=4929421234600821&countryCode=GB&currencyCode=826&customerAddress=Flat+6%2C+Primrose+Rise+347+Lavender+Road&customerName=Longbob+Longsen&customerPostCode=NN17+8YG+&merchantID=login&orderRef=AM+test+purchase&threeDSRequired=N&transactionUnique=#{@visacredit_options[:order_id]}&type=1"
    expected_signature = Digest::SHA512.hexdigest("#{post_params}#{@gateway.options[:shared_secret]}")

    @gateway.expects(:ssl_post).with do |url, data|
      data.include?("signature=#{expected_signature}")
    end.returns(successful_authorization_response)

    @gateway.purchase(10000, @visacreditcard, @visacredit_options)
  end

  def test_3ds_response
    purchase = stub_comms do
      @gateway.purchase(142, @visacreditcard, @visacredit_options.merge(threeds_required: true))
    end.check_request do |endpoint, data, headers|
      assert_match(/threeDSRequired=Y/, data)
{'fraction_non_alphanumeric': 0.09129835256549822, 'fraction_numerical': 0.02155453951665578, 'mean_word_length': 5.176602420439265, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '396689', 'n_tokens_mistral': 4500, 'n_tokens_neox': 4310, 'n_words': 697}