require File.dirname(__FILE__) + "/../test_helper" class RequestTest < Test::Unit::TestCase def test_empty_construction assert_nothing_raised { assert_not_nil EWS::Transaction::Request.new } end def test_hash_construction r = EWS::Transaction::Request.new(basic_params) assert_equal "10.13", r.amount assert_equal "Simon Brown", r.cardholder_name assert_equal "4111111111111111", r.cc_number assert_equal "1012", r.cc_expiry end def test_errors_with_invalid_transaction_types assert_nothing_raised { EWS::Transaction::Request.new(basic_params(:transaction_type => "00")) } assert_nothing_raised { EWS::Transaction::Request.new(basic_params(:transaction_type => :purchase)) } assert_raises(ArgumentError) { EWS::Transaction::Request.new(basic_params(:transaction_type => nil)) } assert_raises(ArgumentError) { EWS::Transaction::Request.new(basic_params(:transaction_type => "")) } assert_raises(ArgumentError) { EWS::Transaction::Request.new(basic_params(:transaction_type => "barry")) } end def test_value_setting r = nil assert_nothing_raised do r = EWS::Transaction::Request.new(sample_everything_params) end assert_not_nil r sample_everything_params.each do |k,v| assert_equal v, r.send(k) end end def test_default_submission transporter = EWS::Transporter.new(@@credentials.config['location']) resp = transporter.submit(basic_new_transaction) assert_equal "00", resp.exact_resp_code assert_equal "Transaction Normal", resp.exact_message assert_equal 1, resp.transaction_approved assert_not_nil resp.transaction_tag end def test_json_submission transporter = EWS::Transporter.new(@@credentials.config['location']) resp = transporter.submit(basic_new_transaction, :json) assert_equal "00", resp.exact_resp_code assert_equal "Transaction Normal", resp.exact_message assert_equal 1, resp.transaction_approved assert_not_nil resp.transaction_tag end def test_rest_submission transporter = EWS::Transporter.new(@@credentials.config['location']) resp = transporter.submit(basic_new_transaction, :rest) assert_equal "00", resp.exact_resp_code assert_equal "Transaction Normal", resp.exact_message assert_equal 1, resp.transaction_approved assert_not_nil resp.transaction_tag end def test_soap_submission transporter = EWS::Transporter.new(@@credentials.config['location']) resp = transporter.submit(basic_new_transaction, :soap) assert_equal "00", resp.exact_resp_code assert_equal "Transaction Normal", resp.exact_message assert_equal 1, resp.transaction_approved assert_not_nil resp.transaction_tag end def test_json_find transporter = EWS::Transporter.new(@@credentials.config['location']) original_resp = transporter.submit(basic_new_transaction, :json) req = basic_find_transaction(:transaction_tag => original_resp.transaction_tag) resp = transporter.submit(req, :json) assert_equal original_resp.exact_resp_code, resp.exact_resp_code assert_equal original_resp.exact_message, resp.exact_message assert_match original_resp.bank_message, resp.bank_message assert_equal original_resp.transaction_tag, resp.transaction_tag end def test_rest_find transporter = EWS::Transporter.new(@@credentials.config['location']) original_resp = transporter.submit(basic_new_transaction, :rest) req = basic_find_transaction(:transaction_tag => original_resp.transaction_tag) resp = transporter.submit(req, :rest) assert_equal original_resp.exact_resp_code, resp.exact_resp_code assert_equal original_resp.exact_message, resp.exact_message assert_match original_resp.bank_message, resp.bank_message assert_equal original_resp.transaction_tag, resp.transaction_tag end def test_soap_find transporter = EWS::Transporter.new(@@credentials.config['location']) original_resp = transporter.submit(basic_new_transaction, :soap) req = basic_find_transaction(:transaction_tag => original_resp.transaction_tag) resp = transporter.submit(req, :soap) assert_equal original_resp.exact_resp_code, resp.exact_resp_code assert_equal original_resp.exact_message, resp.exact_message assert_match original_resp.bank_message, resp.bank_message assert_equal original_resp.transaction_tag, resp.transaction_tag end def test_stubbing_and_fake_requests request = {:nonsense => "this is nonsense"} fake_response = EWS::Transaction::FakeResponse.valid(request) transporter = EWS::Transporter.new(@@credentials.config['location']) transporter.stubs(:submit).returns(fake_response) response = transporter.submit(request) assert_equal fake_response, response assert response.approved? assert_equal "Transaction Normal", response.exact_message end def test_wont_send_invalid_amounts [-0.01, "-0.01", 100000, "100000"].each do |amt| transporter = EWS::Transporter.new(@@credentials.config['location']) transporter.expects(:build_http_request).never original_resp = transporter.submit(basic_new_transaction(:amount => amt), :json) assert !original_resp end end private def basic_find_transaction(options = {}) params = { :transaction_type => :transaction_details }.merge(options) ::EWS::Transaction::Request.new(params.merge(@@credentials.current_gateway)) end def basic_new_transaction(options = {}) params = { :transaction_type => :purchase, :amount => "10.13", :cardholder_name => "Simon Brown", :cc_number => "4111111111111111", :cc_expiry => "1012", :reference_no => "987987", }.merge(options) ::EWS::Transaction::Request.new(params.merge(@@credentials.current_gateway)) end end