require 'test_helper'
class AmazonTest < Test::Unit::TestCase
def setup
@service = AmazonService.new(
:login => 'l',
:password => 'p'
)
@options = {
:shipping_method => 'Standard',
:order_date => Time.now.utc.yesterday,
:comment => "Delayed due to tornados"
}
@address = { :name => 'Johnny Chase',
:address1 => '100 Information Super Highway',
:address2 => 'Suite 66',
:city => 'Beverly Hills',
:state => 'CA',
:country => 'US',
:zip => '90210'
}
@line_items = [
{ :sku => 'SETTLERS1',
:quantity => 1,
:comment => 'Awesome'
}
]
end
def test_successful_fulfillment
@service.expects(:ssl_post).returns(successful_fulfillment_response)
response = @service.fulfill('12345678', @address, @line_items, @options)
assert response.success?
end
def test_invalid_arguments
http_response = build_mock_response(invalid_create_response, "", "500")
@service.expects(:ssl_post).raises(ActiveMerchant::ResponseError.new(http_response))
response = @service.fulfill('12345678', @address, @line_items, @options)
assert !response.success?
assert_equal "aws:Client.MissingParameter The request must contain the parameter Item.", response.params['response_comment']
end
def test_missing_order_comment
@options.delete(:comment)
assert_raise(ArgumentError) { @service.fulfill('12345678', @address, @line_items, @options) }
end
def test_missing_order_date
@options.delete(:order_date)
assert_raise(ArgumentError) { @service.fulfill('12345678', @address, @line_items, @options) }
end
def test_missing_shipping_method
@options.delete(:shipping_method)
assert_raise(ArgumentError) { @service.fulfill('12345678', @address, @line_items, @options) }
end
def test_get_inventory
@service.expects(:ssl_post).returns(xml_fixture('amazon/inventory_get_response'))
response = @service.fetch_stock_levels(:sku => '2R-JAXZ-P0IB')
assert response.success?
assert_equal 142, response.stock_levels['2R-JAXZ-P0IB']
end
def test_list_inventory
@service.expects(:ssl_post).returns(xml_fixture('amazon/inventory_list_response'))
response = @service.fetch_stock_levels
assert response.success?
assert_equal 202, response.stock_levels['GN-00-01A']
assert_equal 199, response.stock_levels['GN-00-02A']
end
def test_list_inventory_multipage
@service.expects(:ssl_post).twice.returns(
xml_fixture('amazon/inventory_list_response_with_next_1'),
xml_fixture('amazon/inventory_list_response_with_next_2')
)
response = @service.fetch_stock_levels
assert response.success?
assert_equal 42, response.stock_levels['GN-01-01A']
assert_equal 80, response.stock_levels['GN-01-02A']
assert_equal 123, response.stock_levels['GN-02-01A']
assert_equal 555, response.stock_levels['GN-02-02A']
end
def test_fetch_tracking_numbers
@service.expects(:ssl_post).twice.returns(
xml_fixture('amazon/tracking_response_1'),
xml_fixture('amazon/tracking_response_2')
)
response = @service.fetch_tracking_numbers(['TEST-00000001', 'TEST-00000002'])
assert response.success?
assert_equal %w{UPS00000001}, response.tracking_numbers['TEST-00000001']
assert_nil response.tracking_numbers['TEST-00000002']
end
def test_fetch_tracking_numbers_ignores_not_found
response = mock('response')
response.stubs(:code).returns(500)
response.stubs(:message).returns('Internal Server Error')
response.stubs(:body).returns(xml_fixture('amazon/tracking_response_not_found'))
@service.expects(:ssl_post).times(3).
returns(xml_fixture('amazon/tracking_response_1')).
raises(ActiveMerchant::ResponseError.new(response)).
returns(xml_fixture('amazon/tracking_response_2'))
response = @service.fetch_tracking_numbers(['TEST-00000001', '#1337-1', 'TEST-00000002'])
assert response.success?
assert_equal %w{UPS00000001}, response.tracking_numbers['TEST-00000001']
end
def test_fetch_tracking_numbers_aborts_on_error
response = mock('response')
response.stubs(:code).returns(500)
response.stubs(:message).returns('Internal Server Error')
response.stubs(:body).returns(xml_fixture('amazon/tracking_response_error'))
@service.expects(:ssl_post).twice.
returns(xml_fixture('amazon/tracking_response_1')).
raises(ActiveMerchant::ResponseError.new(response))
response = @service.fetch_tracking_numbers(['TEST-00000001', 'ERROR', 'TEST-00000002'])
assert !response.success?
assert_equal 'Something bad happened!', response.faultstring
end
def test_404_error
http_response = build_mock_response(response_from_404, "Not Found", "404")
@service.expects(:ssl_post).raises(ActiveMerchant::ResponseError.new(http_response))
response = @service.fulfill('12345678', @address, @line_items, @options)
assert !response.success?
assert_equal "404: Not Found", response.message
assert_equal "404", response.params["http_code"]
assert_equal "Not Found", response.params["http_message"]
assert_equal response_from_404, response.params["http_body"]
end
def test_soap_fault
http_response = build_mock_response(invalid_create_response, "500", "")
@service.expects(:ssl_post).raises(ActiveMerchant::ResponseError.new(http_response))
response = @service.fulfill('12345678', @address, @line_items, @options)
assert !response.success?
assert_equal 'aws:Client.MissingParameter', response.params['faultcode']
assert_equal 'The request must contain the parameter Item.', response.params['faultstring']
assert_equal 'aws:Client.MissingParameter The request must contain the parameter Item.', response.message
end
def test_valid_credentials
@service.expects(:ssl_post).returns(successful_status_response)
assert @service.valid_credentials?
end
def test_invalid_credentials
http_response = build_mock_response(invalid_login_response, "500", "")
@service.expects(:ssl_post).raises(ActiveMerchant::ResponseError.new(http_response))
assert !@service.valid_credentials?
end
def test_successful_service_status
@service.expects(:ssl_request).returns(successful_status_response)
response = @service.status
assert response.success?
end
private
def build_mock_response(response, message, code = "200")
http_response = mock(:code => code, :message => message)
http_response.stubs(:body).returns(response)
http_response
end
def response_for_empty_request
'
type Status report
message Servlet XinoServlet is not available
description The requested resource (Servlet XinoServlet is not available) is not available.