<commit_before>require_relative '../test_helper'

class TestApplicationRequest < MiniTest::Unit::TestCase
  def setup
    keys_path = File.expand_path('../nordea_test_keys', __FILE__)
    @xml_templates_path = File.expand_path('../../../lib/sepa/xml_templates/application_request',
    __FILE__)

    @params = {
      private_key: "#{keys_path}/nordea.key",
      cert: "#{keys_path}/nordea.crt",
      command: :download_file,
      customer_id: '11111111',
      environment: 'PRODUCTION',
      status: 'NEW',
      target_id: '11111111A1',
      language: 'FI',
      file_type: 'TITO',
      wsdl: 'sepa/wsdl/wsdl_nordea.xml',
      content: Base64.encode64("haisuli"),
      file_reference: "11111111A12006030329501800000014"
    }

    @ar_file = Sepa::ApplicationRequest.new(@params)
    @params[:command] = :get_user_info
    @ar_get = Sepa::ApplicationRequest.new(@params)
    @params[:command] = :download_file_list
    @ar_list = Sepa::ApplicationRequest.new(@params)
    @params[:command] = :upload_file
    @ar_up = Sepa::ApplicationRequest.new(@params)

    @doc_file = Nokogiri::XML(Base64.decode64(@ar_file.get_as_base64))
    @doc_get = Nokogiri::XML(Base64.decode64(@ar_get.get_as_base64))
    @doc_list = Nokogiri::XML(Base64.decode64(@ar_list.get_as_base64))
    @doc_up = Nokogiri::XML(Base64.decode64(@ar_up.get_as_base64))
  end

  def test_xml_templates_are_unmodified
    sha1 = OpenSSL::Digest::SHA1.new

    get_user_info_template = File.read("#{@xml_templates_path}/get_user_info.xml")
    download_file_list_template = File.read("#{@xml_templates_path}/download_file_list.xml")
    download_file_template = File.read("#{@xml_templates_path}/download_file.xml")
    upload_file_template = File.read("#{@xml_templates_path}/upload_file.xml")

    get_user_info_digest = sha1.digest(get_user_info_template)
    sha1.reset
    download_file_list_digest = sha1.digest(download_file_list_template)
    sha1.reset
    download_file_digest = sha1.digest(download_file_template)
    sha1.reset
    upload_file_digest = sha1.digest(upload_file_template)

    assert_equal Base64.encode64(get_user_info_digest).strip, "LW5J5R7SnPFPurAa2pM7weTWL1Y="
    assert_equal Base64.encode64(download_file_list_digest).strip, "dYtf4lOP1TXfXPVjYLvaTozhVrg="
    assert_equal Base64.encode64(download_file_digest).strip, "lY+8u+BhXlQmUyQiOiXcUfCUikc="
    assert_equal Base64.encode64(upload_file_digest).strip, "zRQTrNHkq4OLSX3u3ogxU05RJsI="
  end

  def test_ar_should_initialize_with_proper_params
    assert Sepa::ApplicationRequest.new(@params)
  end

  def test_should_get_key_error_if_private_key_missing
    @params.delete(:private_key)

    assert_raises(KeyError) do
      Sepa::ApplicationRequest.new(@params)
    end
  end

  def test_should_get_key_error_if_cert_missing
    @params.delete(:cert)

    assert_raises(KeyError) do
      Sepa::ApplicationRequest.new(@params)
    end
  end

  def test_should_get_key_error_if_command_missing
    @params.delete(:command)

    assert_raises(KeyError) do
      Sepa::ApplicationRequest.new(@params)
    end
  end

  def test_should_get_key_error_if_customer_id_missing
    @params.delete(:customer_id)

    assert_raises(KeyError) do
      Sepa::ApplicationRequest.new(@params)
    end
  end

  def test_should_have_customer_id_set_in_with_all_commands
    assert_equal @doc_file.at_css("CustomerId").content, @params[:customer_id]
    assert_equal @doc_get.at_css("CustomerId").content, @params[:customer_id]
    assert_equal @doc_list.at_css("CustomerId").content, @params[:customer_id]
    assert_equal @doc_up.at_css("CustomerId").content, @params[:customer_id]
  end

  def test_should_have_timestamp_set_properly_with_all_commands
    timestamp_file = Time.strptime(@doc_file.at_css("Timestamp").content, '%Y-%m-%dT%H:%M:%S%z')
    timestamp_get = Time.strptime(@doc_get.at_css("Timestamp").content, '%Y-%m-%dT%H:%M:%S%z')
    timestamp_list = Time.strptime(@doc_list.at_css("Timestamp").content, '%Y-%m-%dT%H:%M:%S%z')
    timestamp_up = Time.strptime(@doc_up.at_css("Timestamp").content, '%Y-%m-%dT%H:%M:%S%z')
    assert timestamp_file <= Time.now && timestamp_file > (Time.now - 60),
    "Timestamp was not set correctly"
    assert timestamp_get <= Time.now && timestamp_get > (Time.now - 60),
    "Timestamp was not set correctly"
    assert timestamp_list <= Time.now && timestamp_list > (Time.now - 60),
    "Timestamp was not set correctly"
    assert timestamp_up <= Time.now && timestamp_up > (Time.now - 60),
    "Timestamp was not set correctly"
  end

  def test_should_have_command_set_when_get_user_info
<commit_msg>Test that raises key error if environment missing<commit_after>require_relative '../test_helper'

class TestApplicationRequest < MiniTest::Unit::TestCase
  def setup
    keys_path = File.expand_path('../nordea_test_keys', __FILE__)
    @xml_templates_path = File.expand_path('../../../lib/sepa/xml_templates/application_request',
    __FILE__)

    @params = {
      private_key: "#{keys_path}/nordea.key",
      cert: "#{keys_path}/nordea.crt",
      command: :download_file,
      customer_id: '11111111',
      environment: 'PRODUCTION',
      status: 'NEW',
      target_id: '11111111A1',
      language: 'FI',
      file_type: 'TITO',
      wsdl: 'sepa/wsdl/wsdl_nordea.xml',
      content: Base64.encode64("haisuli"),
      file_reference: "11111111A12006030329501800000014"
    }

    @ar_file = Sepa::ApplicationRequest.new(@params)
    @params[:command] = :get_user_info
    @ar_get = Sepa::ApplicationRequest.new(@params)
    @params[:command] = :download_file_list
    @ar_list = Sepa::ApplicationRequest.new(@params)
    @params[:command] = :upload_file
    @ar_up = Sepa::ApplicationRequest.new(@params)

    @doc_file = Nokogiri::XML(Base64.decode64(@ar_file.get_as_base64))
    @doc_get = Nokogiri::XML(Base64.decode64(@ar_get.get_as_base64))
    @doc_list = Nokogiri::XML(Base64.decode64(@ar_list.get_as_base64))
    @doc_up = Nokogiri::XML(Base64.decode64(@ar_up.get_as_base64))
  end

  def test_xml_templates_are_unmodified
    sha1 = OpenSSL::Digest::SHA1.new

    get_user_info_template = File.read("#{@xml_templates_path}/get_user_info.xml")
    download_file_list_template = File.read("#{@xml_templates_path}/download_file_list.xml")
    download_file_template = File.read("#{@xml_templates_path}/download_file.xml")
    upload_file_template = File.read("#{@xml_templates_path}/upload_file.xml")

    get_user_info_digest = sha1.digest(get_user_info_template)
    sha1.reset
    download_file_list_digest = sha1.digest(download_file_list_template)
    sha1.reset
    download_file_digest = sha1.digest(download_file_template)
    sha1.reset
    upload_file_digest = sha1.digest(upload_file_template)

    assert_equal Base64.encode64(get_user_info_digest).strip, "LW5J5R7SnPFPurAa2pM7weTWL1Y="
    assert_equal Base64.encode64(download_file_list_digest).strip, "dYtf4lOP1TXfXPVjYLvaTozhVrg="
    assert_equal Base64.encode64(download_file_digest).strip, "lY+8u+BhXlQmUyQiOiXcUfCUikc="
    assert_equal Base64.encode64(upload_file_digest).strip, "zRQTrNHkq4OLSX3u3ogxU05RJsI="
  end

  def test_ar_should_initialize_with_proper_params
    assert Sepa::ApplicationRequest.new(@params)
  end

  def test_should_get_key_error_if_private_key_missing
    @params.delete(:private_key)

    assert_raises(KeyError) do
      Sepa::ApplicationRequest.new(@params)
    end
  end

  def test_should_get_key_error_if_cert_missing
    @params.delete(:cert)

    assert_raises(KeyError) do
      Sepa::ApplicationRequest.new(@params)
    end
  end

  def test_should_get_key_error_if_command_missing
    @params.delete(:command)

    assert_raises(KeyError) do
      Sepa::ApplicationRequest.new(@params)
    end
  end

  def test_should_get_key_error_if_customer_id_missing
    @params.delete(:customer_id)

    assert_raises(KeyError) do
      Sepa::ApplicationRequest.new(@params)
    end
  end

  def test_should_get_key_error_if_environment_missing
    @params.delete(:environment)

    assert_raises(KeyError) do
      Sepa::ApplicationRequest.new(@params)
    end
  end

  def test_should_have_customer_id_set_in_with_all_commands
    assert_equal @doc_file.at_css("CustomerId").content, @params[:customer_id]
    assert_equal @doc_get.at_css("CustomerId").content, @params[:customer_id]
    assert_equal @doc_list.at_css("CustomerId").content, @params[:customer_id]
    assert_equal @doc_up.at_css("CustomerId").content, @params[:customer_id]
  end

  def test_should_have_timestamp_set_properly_with_all_commands
    timestamp_file = Time.strptime(@doc_file.at_css("Timestamp").content, '%Y-%m-%dT%H:%M:%S%z')
    timestamp_get = Time.strptime(@doc_get.at_css("Timestamp").content, '%Y-%m-%dT%H:%M:%S%z')
    timestamp_list = Time.strptime(@doc_list.at_css("Timestamp").content, '%Y-%m-%dT%H:%M:%S%z')
    timestamp_up = Time.strptime(@doc_up.at_css("Timestamp").content, '%Y-%m-%dT%H:%M:%S%z')
    assert timestamp_file <= Time.now && timestamp_file > (Time.now - 60),
    "Timestamp was not set correctly"
    assert timestamp_get <= Time.now && timestamp_get > (Time.now - 60),
    "Timestamp was not set correctly"
    assert timestamp_list <= Time.now && timestamp_list > (Time.now - 60),
    "Timestamp was not set correctly"
    assert timestamp_up <= Time.now && timestamp_up > (Time.now - 60),
    "Timestamp was not set correctly"
  end

  def test_should_have_command_set_when_get_user_info

{'fraction_non_alphanumeric': 0.12193568927093282, 'fraction_numerical': 0.026106335561922954, 'mean_word_length': 4.94949494949495, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 24}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '351541', 'n_tokens_mistral': 3534, 'n_tokens_neox': 3311, 'n_words': 435}