require 'test_helper'

class NotificationTest < Test::Unit::TestCase
  include OffsitePayments::Integrations

  def setup
    @notification = Notification.new(http_raw_data)
  end

  def test_raw
    assert_equal http_raw_data, @notification.raw
  end

  def test_parse
    assert_equal "500.00", @notification.params['mc_gross']
    assert_equal "confirmed", @notification.params['address_status']
    assert_equal "EVMXCLDZJV77Q", @notification.params['payer_id']
    assert_equal "Completed", @notification.params['payment_status']
    assert_equal "ru", @notification.params['yamoney-lang']
    assert_equal '', @notification.params['empty_params']
    assert_equal CGI.unescape("15%3A23%3A54+Apr+15%2C+2005+PDT"), @notification.params['payment_date']
  end

  def test_accessors
    assert_raise(NotImplementedError){ p @notification.status }
    assert_raise(NotImplementedError){ p @notification.gross }
    assert_raise(NotImplementedError){ p @notification.gross_cents }
  end

  def test_notification_data_with_period
    notification = Notification.new(http_raw_data_with_period)
    assert_equal 'clicked', notification.params['checkout.x']
  end

  def test_valid_sender_always_true_in_testmode
    assert_equal OffsitePayments.mode, :test
    assert @notification.valid_sender?(nil)
    assert @notification.valid_sender?('localhost')
  end

  def test_valid_sender_always_true_when_no_ips
    OffsitePayments.mode = :production
    assert @notification.valid_sender?(nil)
    assert @notification.valid_sender?('localhost')
  ensure
    OffsitePayments.mode = :test
  end

  def test_iso_currency
    assert_equal 'USD', CurrencyNotificationStub.new('currency=usd').iso_currency
    assert_equal 'CAD', CurrencyNotificationStub.new('currency=CAD').iso_currency

    assert_equal 'TWD', CurrencyNotificationStub.new('currency=NTD').iso_currency
    assert_equal 'CNY', CurrencyNotificationStub.new('currency=rmb').iso_currency

    assert_raise ActiveUtils::InvalidCurrencyCodeError do
      CurrencyNotificationStub.new('currency=not_real').iso_currency
    end
  end

  def test_encoding
    @notification = Notification.new(http_raw_data_iso_8859_1)
    assert_equal "Köhler", @notification.params['last_name']
  end

  private
  def http_raw_data
    "mc_gross=500.00&address_status=confirmed&payer_id=EVMXCLDZJV77Q&tax=0.00&address_street=164+Waverley+Street&payment_date=15%3A23%3A54+Apr+15%2C+2005+PDT&payment_status=Completed&address_zip=K2P0V6&first_name=Tobias&mc_fee=15.05&address_country_code=CA&address_name=Tobias+Luetke&notify_version=1.7&custom=&payer_status=unverified&business=tobi%40leetsoft.com&address_country=Canada&address_city=Ottawa&quantity=1&payer_email=tobi%40snowdevil.ca&verify_sign=AEt48rmhLYtkZ9VzOGAtwL7rTGxUAoLNsuf7UewmX7UGvcyC3wfUmzJP&txn_id=6G996328CK404320L&payment_type=instant&last_name=Luetke&address_state=Ontario&receiver_email=tobi%40leetsoft.com&payment_fee=&receiver_id=UQ8PDYXJZQD9Y&txn_type=web_accept&item_name=Store+Purchase&mc_currency=CAD&item_number=&test_ipn=1&payment_gross=&shipping=0.00&yamoney-lang=ru&empty_params="
  end

  def http_raw_data_with_period
    "mc_gross=500.00&address_status=confirmed&payer_id=EVMXCLDZJV77Q&tax=0.00&address_street=164+Waverley+Street&payment_date=15%3A23%3A54+Apr+15%2C+2005+PDT&payment_status=Completed&address_zip=K2P0V6&first_name=Tobias&mc_fee=15.05&address_country_code=CA&address_name=Tobias+Luetke&notify_version=1.7&custom=&payer_status=unverified&business=tobi%40leetsoft.com&address_country=Canada&address_city=Ottawa&quantity=1&payer_email=tobi%40snowdevil.ca&verify_sign=AEt48rmhLYtkZ9VzOGAtwL7rTGxUAoLNsuf7UewmX7UGvcyC3wfUmzJP&txn_id=6G996328CK404320L&payment_type=instant&last_name=Luetke&address_state=Ontario&receiver_email=tobi%40leetsoft.com&payment_fee=&receiver_id=UQ8PDYXJZQD9Y&txn_type=web_accept&item_name=Store+Purchase&mc_currency=CAD&item_number=&test_ipn=1&payment_gross=&shipping=0.00&checkout.x=clicked&yamoney-lang=ru&empty_params="
  end

  def http_raw_data_iso_8859_1
    "mc_gross=500.00&address_status=confirmed&payer_id=EVMXCLDZJV77Q&tax=0.00&address_street=164+Waverley+Street&payment_date=15%3A23%3A54+Apr+15%2C+2005+PDT&payment_status=Completed&address_zip=K2P0V6&first_name=Tobias&mc_fee=15.05&address_country_code=CA&address_name=Tobias+Luetke&notify_version=1.7&custom=&payer_status=unverified&business=tobi%40leetsoft.com&address_country=Canada&address_city=Ottawa&quantity=1&payer_email=tobi%40snowdevil.ca&verify_sign=AEt48rmhLYtkZ9VzOGAtwL7rTGxUAoLNsuf7UewmX7UGvcyC3wfUmzJP&txn_id=6G996328CK404320L&payment_type=instant&last_name=K%F6hler&address_state=Ontario&receiver_email=tobi%40leetsoft.com&payment_fee=&receiver_id=UQ8PDYXJZQD9Y&txn_type=web_accept&item_name=Store+Purchase&mc_currency=CAD&item_number=&test_ipn=1&payment_gross=&shipping=0.00&yamoney-lang=ru&empty_params="
  end
end

class CurrencyNotificationStub < OffsitePayments::Notification
  def currency
    params['currency']
  end
end
