require 'test_helper'
class WebgistixTest < Test::Unit::TestCase
def setup
Base.mode = :test
@service = WebgistixService.new(
:login => 'cody@example.com',
:password => 'test'
)
@options = {
:shipping_method => 'UPS Ground'
}
@address = { :name => 'Fred Brooks',
:address1 => '1234 Penny Lane',
:city => 'Jonsetown',
:state => 'NC',
:country => 'US',
:zip => '23456',
:email => 'buyer@jadedpallet.com'
}
@line_items = [
{ :sku => '9999',
:quantity => 25
}
]
end
def test_missing_login
assert_raise(ArgumentError) do
WebgistixService.new(:password => 'test')
end
end
def test_missing_password
assert_raise(ArgumentError) do
WebgistixService.new(:login => 'cody')
end
end
def test_missing_credentials
assert_raise(ArgumentError) do
WebgistixService.new(:password => 'test')
end
end
def test_credentials_present
assert_nothing_raised do
WebgistixService.new(
:login => 'cody',
:password => 'test'
)
end
end
def test_successful_fulfillment
@service.expects(:ssl_post).returns(successful_response)
response = @service.fulfill('123456', @address, @line_items, @options)
assert response.success?
assert response.test?
assert_equal WebgistixService::SUCCESS_MESSAGE, response.message
assert_equal '619669', response.params['order_id']
end
def test_minimal_successful_fulfillment
@service.expects(:ssl_post).returns(minimal_successful_response)
response = @service.fulfill('123456', @address, @line_items, @options)
assert response.success?
assert response.test?
assert_equal WebgistixService::SUCCESS_MESSAGE, response.message
assert_nil response.params['order_id']
end
def test_failed_fulfillment
@service.expects(:ssl_post).returns(failure_response)
response = @service.fulfill('123456', @address, @line_items, @options)
assert !response.success?
assert response.test?
assert_equal WebgistixService::FAILURE_MESSAGE, response.message
assert_nil response.params['order_id']
assert_equal 'No Address Line 1', response.params['error_0']
assert_equal 'Unknown ItemID: testitem', response.params['error_1']
assert_equal 'Unknown ItemID: WX-01-1000', response.params['error_2']
end
def test_failed_login
@service.expects(:ssl_post).returns(invalid_login_response)
response = @service.fulfill('123456', @address, @line_items, @options)
assert !response.success?
assert response.test?
assert_equal 'Access Denied', response.message
assert_nil response.params['order_id']
assert_equal 'Access Denied', response.params['error_0']
end
def test_garbage_response
@service.expects(:ssl_post).returns(garbage_response)
response = @service.fulfill('123456', @address, @line_items, @options)
assert !response.success?
assert response.test?
assert_equal WebgistixService::FAILURE_MESSAGE, response.message
assert_nil response.params['order_id']
end
def test_valid_credentials
@service.expects(:ssl_post).returns(failure_response)
assert @service.valid_credentials?
end
def test_invalid_credentials
@service.expects(:ssl_post).returns(invalid_login_response)
assert !@service.valid_credentials?
end
private
def minimal_successful_response
'True'
end
def successful_response
'True619669'
end
def invalid_login_response
'Access Denied'
end
def failure_response
'No Address Line 1Unknown ItemID: testitemUnknown ItemID: WX-01-1000'
end
def garbage_response
'/XML/shippingTest.asp, line 39'
end
end