<commit_before>require 'test_helper'
class CanadaPostPwsRatingTest < Test::Unit::TestCase

  def setup
    login = fixtures(:canada_post_pws)
    
    # 100 grams, 93 cm long, 10 cm diameter, cylinders have different volume calculations
    @pkg1 = Package.new(25, [93,10], :cylinder => true)
    # 7.5 lbs, times 16 oz/lb., 15x10x4.5 inches, not grams, not centimetres
    @pkg2 = Package.new((7.5 * 16), [15, 10, 4.5], :units => :imperial)
    
    @home_params = {
      :name        => "<NAME>", 
      :company     => "test",
      :phone       => "613-555-1212",
      :address1    => "123 Elm St.",
      :city        => 'Ottawa', 
      :province    => 'ON', 
      :country     => 'CA', 
      :postal_code => 'K1P 1J1'
    }
    @home = Location.new(@home_params)

    @dest_params = {
      :name     => "<NAME>",
      :address1 => '999 Wiltshire Blvd',
      :city     => 'Beverly Hills', 
      :state    => 'CA', 
      :country  => 'US', 
      :zip      => '90210'
    }
    @dest = Location.new(@dest_params)

    @shipping_opts1 = {:dc => true, :cod => :true, :cod_amount => 50.00, :cod_includes_shipping => true, 
                       :cod_method_of_payment => 'CSH', :cov => true, :cov_amount => 100.00, 
                       :so => true, :pa18 => true}

    @customer_number = '654321'

    @cp = CanadaPostPWS.new(login)
    @cp.logger = Logger.new(STDOUT)
    @french_cp = CanadaPostPWS.new(login.merge(:language => 'fr'))
    @cp_customer_number = CanadaPostPWS.new(login.merge(:customer_number => @customer_number))

    @default_options = {:customer_number => '123456'}
  end
  
  # rating

  def test_language_header
    assert_equal 'fr-CA', @french_cp.language
    assert_equal 'en-CA', @cp.language
  end

  def test_find_rates
    response = xml_fixture('canadapost_pws/rates_info')
    expected_headers = {
      'Authorization'   => "#{@cp.send(:encoded_authorization)}",
      'Accept-Language' => 'en-CA',
      'Accept'          => 'application/vnd.cpc.ship.rate+xml',
      'Content-Type'    => 'application/vnd.cpc.ship.rate+xml'
    }
    CanadaPostPWS.any_instance.expects(:ssl_post).with(anything, anything, expected_headers).returns(response)

    rates_response = @cp.find_rates(@home_params, @dest_params, [@pkg1, @pkg2], @default_options)
    
    assert_equal 4, rates_response.rates.size
    rate = rates_response.rates.first
    assert_equal RateEstimate, rate.class
    assert_equal "Canada Post PWS", rate.carrier
    assert_equal @home.to_s, Location.new(rate.origin).to_s
    assert_equal @dest.to_s, Location.new(rate.destination).to_s
  end

  def test_find_rates_with_error
    response = xml_fixture('canadapost_pws/rates_info_error')
    http_response = mock()
    http_response.stubs(:code).returns('400')
    http_response.stubs(:body).returns(response)
    response_error = ActiveMerchant::ResponseError.new(http_response)
    @cp.expects(:ssl_post).raises(response_error)

    exception = assert_raises ActiveMerchant::Shipping::ResponseError do 
      @cp.find_rates(@home_params, @dest_params, [@pkg1, @pkg2], @default_options)
    end
    
    assert_equal "You cannot mail on behalf of the requested customer.", exception.message
  end

  def test_find_rates_line_items_single_object
    response = xml_fixture('canadapost_pws/rates_info')
    expected_headers = {
      'Authorization'   => "#{@cp.send(:encoded_authorization)}",
      'Accept-Language' => 'en-CA',
      'Accept'          => 'application/vnd.cpc.ship.rate+xml',
      'Content-Type'    => 'application/vnd.cpc.ship.rate+xml'
    }
    CanadaPostPWS.any_instance.expects(:ssl_post).with(anything, anything, expected_headers).returns(response)

    rates_response = @cp.find_rates(@home_params, @dest_params, @pkg1, @default_options)
    
    assert_equal 4, rates_response.rates.size
    rate = rates_response.rates.first
    assert_equal RateEstimate, rate.class
    assert_equal "Canada Post PWS", rate.carrier
    assert_equal @home.to_s, Location.new(rate.origin).to_s
    assert_equal @dest.to_s, Location.new(rate.destination).to_s
  end

  # build rates

  def test_build_rates_request
    xml = @cp.build_rates_request(@home_params, @dest_params, [@pkg1, @pkg2], @default_options)
    doc = Nokogiri::HTML(xml)

    assert_equal @default_options[:customer_number], doc.xpath('//customer-number').first.content
    assert_equal 'K1P1J1', doc.xpath('//origin-postal-code').first.content
    assert_equal 'united-states', doc.xpath('//destination').children.first.name
    assert !doc.xpath('//parcel-characteristics').empty?
    assert_equal "3.427", doc.xpath('//parcel-characteristics//weight').first.content
  end

  def test_build_rates_request_use_carrier_customer_number
    xml = @cp_customer_number.build_rates_request(@home_params, @dest_params, [@pkg1, @pkg2])
    doc = Nokogiri::HTML(xml)

    assert_equal @customer_number, doc.xpath('//customer-number').first.content
    assert_equal 'K1P1J1', doc.xpath('//origin-postal-code').first.content
    assert_equal 'united-states', doc.xpath('//destination').children.first.name
    assert !doc.xpath('//parcel-characteristics').empty?
    assert_equal "3.427", doc.xpath('//parcel-characteristics//weight').first.content
  end

  def test_build_rates_request_location_object
    xml = @cp.build_rates_request(Location.new(@home_params), Location.new(@dest_params), [@pkg1, @pkg2], @default_options)
    doc = Nokogiri::HTML(xml)

    assert_equal @default_options[:customer_number], doc.xpath('//customer-number').first.content
    assert_equal 'K1P1J1', doc.xpath('//origin-postal-code').first.content
    assert_equal 'united-states', doc.xpath('//destination').children.first.name
    assert !doc.xpath('//parcel-characteristics').empty?
    assert_equal "3.427", doc.xpath('//parcel-characteristics//weight').first.content
  end

  def test_build_rates_request_domestic
    @dest_params = {       
      :name        => "<NAME>", 
      :company     => "test",
      :phone       => "613-555-1212",<commit_msg>Test that carrier customer number can be overriden by rates request options<commit_after>require 'test_helper'
class CanadaPostPwsRatingTest < Test::Unit::TestCase

  def setup
    login = fixtures(:canada_post_pws)
    
    # 100 grams, 93 cm long, 10 cm diameter, cylinders have different volume calculations
    @pkg1 = Package.new(25, [93,10], :cylinder => true)
    # 7.5 lbs, times 16 oz/lb., 15x10x4.5 inches, not grams, not centimetres
    @pkg2 = Package.new((7.5 * 16), [15, 10, 4.5], :units => :imperial)
    
    @home_params = {
      :name        => "<NAME>", 
      :company     => "test",
      :phone       => "613-555-1212",
      :address1    => "123 Elm St.",
      :city        => 'Ottawa', 
      :province    => 'ON', 
      :country     => 'CA', 
      :postal_code => 'K1P 1J1'
    }
    @home = Location.new(@home_params)

    @dest_params = {
      :name     => "<NAME>",
      :address1 => '999 Wiltshire Blvd',
      :city     => 'Beverly Hills', 
      :state    => 'CA', 
      :country  => 'US', 
      :zip      => '90210'
    }
    @dest = Location.new(@dest_params)

    @shipping_opts1 = {:dc => true, :cod => :true, :cod_amount => 50.00, :cod_includes_shipping => true, 
                       :cod_method_of_payment => 'CSH', :cov => true, :cov_amount => 100.00, 
                       :so => true, :pa18 => true}

    @customer_number = '654321'

    @cp = CanadaPostPWS.new(login)
    @cp.logger = Logger.new(STDOUT)
    @french_cp = CanadaPostPWS.new(login.merge(:language => 'fr'))
    @cp_customer_number = CanadaPostPWS.new(login.merge(:customer_number => @customer_number))

    @default_options = {:customer_number => '123456'}
  end
  
  # rating

  def test_language_header
    assert_equal 'fr-CA', @french_cp.language
    assert_equal 'en-CA', @cp.language
  end

  def test_find_rates
    response = xml_fixture('canadapost_pws/rates_info')
    expected_headers = {
      'Authorization'   => "#{@cp.send(:encoded_authorization)}",
      'Accept-Language' => 'en-CA',
      'Accept'          => 'application/vnd.cpc.ship.rate+xml',
      'Content-Type'    => 'application/vnd.cpc.ship.rate+xml'
    }
    CanadaPostPWS.any_instance.expects(:ssl_post).with(anything, anything, expected_headers).returns(response)

    rates_response = @cp.find_rates(@home_params, @dest_params, [@pkg1, @pkg2], @default_options)
    
    assert_equal 4, rates_response.rates.size
    rate = rates_response.rates.first
    assert_equal RateEstimate, rate.class
    assert_equal "Canada Post PWS", rate.carrier
    assert_equal @home.to_s, Location.new(rate.origin).to_s
    assert_equal @dest.to_s, Location.new(rate.destination).to_s
  end

  def test_find_rates_with_error
    response = xml_fixture('canadapost_pws/rates_info_error')
    http_response = mock()
    http_response.stubs(:code).returns('400')
    http_response.stubs(:body).returns(response)
    response_error = ActiveMerchant::ResponseError.new(http_response)
    @cp.expects(:ssl_post).raises(response_error)

    exception = assert_raises ActiveMerchant::Shipping::ResponseError do 
      @cp.find_rates(@home_params, @dest_params, [@pkg1, @pkg2], @default_options)
    end
    
    assert_equal "You cannot mail on behalf of the requested customer.", exception.message
  end

  def test_find_rates_line_items_single_object
    response = xml_fixture('canadapost_pws/rates_info')
    expected_headers = {
      'Authorization'   => "#{@cp.send(:encoded_authorization)}",
      'Accept-Language' => 'en-CA',
      'Accept'          => 'application/vnd.cpc.ship.rate+xml',
      'Content-Type'    => 'application/vnd.cpc.ship.rate+xml'
    }
    CanadaPostPWS.any_instance.expects(:ssl_post).with(anything, anything, expected_headers).returns(response)

    rates_response = @cp.find_rates(@home_params, @dest_params, @pkg1, @default_options)
    
    assert_equal 4, rates_response.rates.size
    rate = rates_response.rates.first
    assert_equal RateEstimate, rate.class
    assert_equal "Canada Post PWS", rate.carrier
    assert_equal @home.to_s, Location.new(rate.origin).to_s
    assert_equal @dest.to_s, Location.new(rate.destination).to_s
  end

  # build rates

  def test_build_rates_request
    xml = @cp.build_rates_request(@home_params, @dest_params, [@pkg1, @pkg2], @default_options)
    doc = Nokogiri::HTML(xml)

    assert_equal @default_options[:customer_number], doc.xpath('//customer-number').first.content
    assert_equal 'K1P1J1', doc.xpath('//origin-postal-code').first.content
    assert_equal 'united-states', doc.xpath('//destination').children.first.name
    assert !doc.xpath('//parcel-characteristics').empty?
    assert_equal "3.427", doc.xpath('//parcel-characteristics//weight').first.content
  end

  def test_build_rates_request_use_carrier_customer_number
    xml = @cp_customer_number.build_rates_request(@home_params, @dest_params, [@pkg1, @pkg2])
    doc = Nokogiri::HTML(xml)

    assert_equal @customer_number, doc.xpath('//customer-number').first.content
    assert_equal 'K1P1J1', doc.xpath('//origin-postal-code').first.content
    assert_equal 'united-states', doc.xpath('//destination').children.first.name
    assert !doc.xpath('//parcel-characteristics').empty?
    assert_equal "3.427", doc.xpath('//parcel-characteristics//weight').first.content
  end

  def test_build_rates_request_override_carrier_customer_number
    xml = @cp_customer_number.build_rates_request(@home_params, @dest_params, [@pkg1, @pkg2], @default_options)
    doc = Nokogiri::HTML(xml)

    assert_equal @default_options[:customer_number], doc.xpath('//customer-number').first.content
    assert_equal 'K1P1J1', doc.xpath('//origin-postal-code').first.content
    assert_equal 'united-states', doc.xpath('//destination').children.first.name
    assert !doc.xpath('//parcel-characteristics').empty?
    assert_equal "3.427", doc.xpath('//parcel-characteristics//weight').first.content
  end

  def test_build_rates_request_location_object
    xml = @cp.build_rates_request(Location.new(@home_params), Location.new(@dest_params), [@pkg1, @pkg2], @default_options)
    doc = Nokogiri::HTML(xml)

    assert_equal @default_options[:customer_number], doc.xpath('//customer-number').first.content
    assert_equal 'K1P1J1', doc.xpath('//origin-postal-code').first.content
    assert_equal 'united-states', doc.xpath('//destination').children.first.name
    assert !doc.xpath('//parcel-characteristics').empty?
    assert_equal "3.427", doc.xpath('//parcel-characteristics//weight').first.content
  end

  def test_build_rates_request_domestic
    @dest_params = {       
      :name        => "<NAME>", 
      :company     => "test",
      :phone       => "613-555-1212",
{'fraction_non_alphanumeric': 0.14296954913840584, 'fraction_numerical': 0.021480840349358724, 'mean_word_length': 3.615105301379811, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 28}, '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': '7480874', 'n_tokens_mistral': 4494, 'n_tokens_neox': 4191, 'n_words': 701}