Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 272 lines (238 sloc) 9.864 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
require 'test_helper'

class ShipwireTest < Test::Unit::TestCase
  def setup
    Base.mode = :test
    
    @shipwire = ShipwireService.new(
                  :login => 'cody@example.com',
                  :password => 'test'
                )
    
    @options = {
      :warehouse => '01',
      :shipping_method => 'UPS Ground'
    }
    
    @address = {
      :name => 'Fred Brooks',
      :address1 => '1234 Penny Lane',
      :city => 'Jonsetown',
      :state => 'NC',
      :country => 'US',
      :zip => '23456',
      :company => 'MyCorp',
      :email => 'buyer@jadedpallet.com'
    }
    
    @line_items = [ { :sku => '9999', :quantity => 25 } ]
  end
  
  def test_missing_login
    assert_raise(ArgumentError) do
      ShipwireService.new(:password => 'test')
    end
  end
  
  def test_missing_password
    assert_raise(ArgumentError) do
      ShipwireService.new(:login => 'cody')
    end
  end
  
  def test_missing_credentials
    assert_raise(ArgumentError) do
      ShipwireService.new(:password => 'test')
    end
  end
  
  def test_credentials_present
    assert_nothing_raised do
      ShipwireService.new(
        :login => 'cody',
        :password => 'test'
      )
    end
  end
  
  def test_country_format
    xml = REXML::Document.new(@shipwire.send(:build_fulfillment_request, '123456', @address, @line_items, @options))
    country_node = REXML::XPath.first(xml, "//Country")
    assert_equal 'US', country_node.text
  end

  def test_stock_levels_dont_include_pending_by_default
    @shipwire.expects(:ssl_post).returns(xml_fixture('shipwire/inventory_get_response'))

    response = @shipwire.fetch_stock_levels
    assert response.success?
    assert_equal 926, response.stock_levels['BlackDog']
    assert_equal -1, response.stock_levels['MoustacheCat']
    assert_equal 677, response.stock_levels['KingMonkey']
  end

  def test_stock_levels_include_pending_when_set
    @shipwire = ShipwireService.new(
                  :login => 'cody@example.com',
                  :password => 'test',
                  :include_pending_stock => true
                )
    @shipwire.expects(:ssl_post).returns(xml_fixture('shipwire/inventory_get_response'))

    response = @shipwire.fetch_stock_levels
    assert response.success?
    assert @shipwire.include_pending_stock?
    assert_equal 926, response.stock_levels['BlackDog']
    assert_equal 805, response.stock_levels['MoustacheCat']
    assert_equal 921, response.stock_levels['KingMonkey']
  end

  def test_inventory_request_with_include_empty_tag
     @shipwire = ShipwireService.new(
                  :login => 'cody@example.com',
                  :password => 'test',
                  :include_empty_stock => true
                )
    xml = REXML::Document.new(@shipwire.send(:build_inventory_request, {}))
    assert REXML::XPath.first(xml, '//IncludeEmpty')
  end

  def test_no_tracking_numbers_available
    @shipwire.expects(:ssl_post).returns(successful_empty_tracking_response)
    response = @shipwire.fetch_tracking_numbers(['1234'])
    assert response.success?
    assert_equal Hash.new, response.tracking_numbers
  end
  
  def test_successful_tracking
    expected = { "2986" => "1ZW682E90326614239",
                 "2987" => "1ZW682E90326795080" }
    
    @shipwire.expects(:ssl_post).returns(successful_tracking_response)
    response = @shipwire.fetch_tracking_numbers(["2986", "2987"])
    assert response.success?
    assert_equal "3", response.params["total_orders"]
    assert_equal "Test", response.params["status"]
    assert_equal "2", response.params["total_shipped_orders"]
    
    assert_equal expected, response.tracking_numbers
  end
  
  def test_successful_tracking_with_live_data
    @shipwire.expects(:ssl_post).returns(successful_live_tracking_response)
    response = @shipwire.fetch_tracking_numbers([
        '21', '22', '23', '24', '25',
        '26', '2581', '2576', '2593', '2598',
      '2610', '2611', '2613', '2616', '2631'
    ])
    assert response.success?
    assert_equal "15", response.params["total_orders"]
    assert_equal "0", response.params["status"]
    assert_equal "13", response.params["total_shipped_orders"]
    
    assert_equal 13, response.tracking_numbers.size
  end
  
  def test_valid_credentials
    @shipwire.expects(:ssl_post).returns(successful_empty_tracking_response)
    assert @shipwire.valid_credentials?
  end
  
  def test_invalid_credentials
    @shipwire.expects(:ssl_post).returns(invalid_login_response)
    assert !@shipwire.valid_credentials?
  end

  def test_affiliate_id
    ActiveMerchant::Fulfillment::ShipwireService.affiliate_id = 'affiliate_id'

    xml = REXML::Document.new(@shipwire.send(:build_fulfillment_request, '123456', @address, @line_items, @options))
    affiliate_id = REXML::XPath.first(xml, "//AffiliateId")
    assert_equal 'affiliate_id', affiliate_id.text
  end

  def test_company_name_in_request
    xml = REXML::Document.new(@shipwire.send(:build_fulfillment_request, '123456', @address, @line_items, @options))
    company_node = REXML::XPath.first(xml, "//Company")
    assert_equal 'MyCorp', company_node.text
  end

  def test_order_excludes_note_by_default
    xml = REXML::Document.new(@shipwire.send(:build_fulfillment_request, '123456', @address, @line_items, @options))
    note_node = REXML::XPath.first(xml, "//Note").cdatas.first
    assert_nil note_node
  end

  def test_order_includes_note_when_present
    @options[:note] = "A test note"
    xml = REXML::Document.new(@shipwire.send(:build_fulfillment_request, '123456', @address, @line_items, @options))
    note_node = REXML::XPath.first(xml, "//Note").cdatas.first
    assert_equal "A test note", note_node.to_s
  end

  def test_error_response_cdata_parsing
    @shipwire.expects(:ssl_post).returns(xml_fixture('shipwire/fulfillment_failure_response'))
    assert !@shipwire.valid_credentials?
  end

  private
  def successful_empty_tracking_response
    "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<TrackingUpdateResponse><Status>Test</Status><TotalOrders></TotalOrders><TotalShippedOrders></TotalShippedOrders><TotalProducts></TotalProducts><Bookmark></Bookmark></TrackingUpdateResponse>"
  end
  
  def successful_tracking_response
    <<-XML
<?xml version="1.0"?>
<TrackingUpdateResponse>
<Status>Test</Status>
<Order id="2986" shipped="YES" shipper="UPS GD" handling="1.00" shipping="13.66" total="14.66">
<TrackingNumber>1ZW682E90326614239</TrackingNumber>
</Order>
<Order id="2987" shipped="YES" shipper="UPS GD" handling="1.50" shipping="9.37" total="10.87">
<TrackingNumber>1ZW682E90326795080</TrackingNumber>
</Order>
<Order id="2988" shipped="NO" shipper="UPS GD" handling="" shipping="" total="">
<TrackingNumber></TrackingNumber>
</Order>
<TotalOrders>3</TotalOrders>
<TotalShippedOrders>2</TotalShippedOrders>
<Bookmark>2006-04-28 20:35:45</Bookmark>
</TrackingUpdateResponse>
XML
  end
  
  def successful_live_tracking_response
    <<-XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<TrackingUpdateResponse>
<Status>0</Status>
<Order id="21" shipped="YES" shipper="5" handling="0.00" shipping="6.58" total="6.58">
<TrackingNumber>1Z6296VW0398500001</TrackingNumber>
</Order>
<Order id="22" shipped="YES" shipper="5" handling="0.00" shipping="8.13" total="8.13">
<TrackingNumber>1Z6296VW0390790002</TrackingNumber>
</Order>
<Order id="23" shipped="YES" shipper="5" handling="0.00" shipping="7.63" total="7.63">
<TrackingNumber>1Z6296VW0396490003</TrackingNumber>
</Order>
<Order id="24" shipped="YES" shipper="5" handling="0.00" shipping="8.97" total="8.97">
<TrackingNumber>1Z6296VW0390200004</TrackingNumber>
</Order>
<Order id="25" shipped="YES" shipper="5" handling="0.00" shipping="8.42" total="8.42">
<TrackingNumber>1Z6296VW0393240005</TrackingNumber>
</Order>
<Order id="26" shipped="YES" shipper="5" handling="0.00" shipping="8.42" total="8.42">
<TrackingNumber>1Z6296VW0396400006</TrackingNumber>
</Order>
<Order id="2581" shipped="YES" shipper="5" handling="0.00" shipping="8.21" total="8.21">
<TrackingNumber>1Z6296VW0391160007</TrackingNumber>
</Order>
<Order id="2576" shipped="YES" shipper="43" handling="0.00" shipping="18.60" total="18.60">
<TrackingNumber>CJ3026000018US</TrackingNumber>
</Order>
<Order id="2593" shipped="YES" shipper="5" handling="0.00" shipping="7.63" total="7.63">
<TrackingNumber>1Z6296VW0398660008</TrackingNumber>
</Order>
<Order id="2598" shipped="YES" shipper="5" handling="0.00" shipping="9.84" total="9.84">
<TrackingNumber>1Z6296VW0391610009</TrackingNumber>
</Order>
<Order id="2610" shipped="YES" shipper="5" handling="0.00" shipping="7.63" total="7.63">
<TrackingNumber>1Z6296VW0395650010</TrackingNumber>
</Order>
<Order id="2611" shipped="YES" shipper="5" handling="0.00" shipping="7.13" total="7.13">
<TrackingNumber>1Z6296VW0397050011</TrackingNumber>
</Order>
<Order id="2613" shipped="YES" shipper="5" handling="0.00" shipping="8.97" total="8.97">
<TrackingNumber>1Z6296VW0398970012</TrackingNumber>
</Order>
<Order id="2616" shipped="NO" shipper="5" handling="0.00" shipping="9.84" total="9.84">
<TrackingNumber></TrackingNumber>
</Order>
<Order id="2631" shipped="NO" shipper="" handling="" shipping="" total="">
<TrackingNumber></TrackingNumber>
</Order>
<TotalOrders>15</TotalOrders>
<TotalShippedOrders>13</TotalShippedOrders>
<TotalProducts/>
<Bookmark/>
</TrackingUpdateResponse>

XML
  end
  
  def invalid_login_response
    <<-XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<TrackingUpdateResponse><Status>Error</Status><ErrorMessage>
Error with Valid Username/EmailAddress and Password Required.
There is an error in XML document.
</ErrorMessage></TrackingUpdateResponse>
XML
  end
end
Something went wrong with that request. Please try again.