This repository has been archived by the owner on Jun 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 545
/
Copy pathusps_test.rb
243 lines (205 loc) · 7.55 KB
/
usps_test.rb
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
require 'test_helper'
class RemoteUSPSTest < ActiveSupport::TestCase
include ActiveShipping::Test::Credentials
include ActiveShipping::Test::Fixtures
def setup
@usps_credentials = credentials(:usps)
@carrier = USPS.new(@usps_credentials)
rescue NoCredentialsFound => e
skip(e.message)
end
def test_tracking_with_bad_number
assert_raises(ResponseError) do
@carrier.find_tracking_info('abc123xyz', test: false)
end
end
def test_zip_to_zip
response = @carrier.find_rates(
Location.new(:zip => 40524),
Location.new(:zip => 40515),
Package.new(16, [12, 6, 2], :units => :imperial)
)
assert response.success?, response.message
refute response.rates.empty?
end
def test_just_country_given
response = @carrier.find_rates(
location_fixtures[:beverly_hills],
Location.new(:country => 'CZ'),
Package.new(100, [5, 10, 20])
)
assert response.success?, response.message
refute response.rates.empty?
end
def test_us_to_canada
response = @carrier.find_rates(
location_fixtures[:beverly_hills],
location_fixtures[:ottawa],
package_fixtures.values_at(:american_wii),
:test => true
)
assert response.success?, response.message
refute response.rates.empty?
end
def test_domestic_rates
response = @carrier.find_rates(
location_fixtures[:new_york],
location_fixtures[:beverly_hills],
package_fixtures.values_at(:book, :american_wii),
:test => true
)
assert response.success?, response.message
assert_instance_of Hash, response.params
assert_instance_of String, response.xml
assert_instance_of Array, response.rates
refute response.rates.empty?
rate = response.rates.first
assert_equal 'USPS', rate.carrier
assert_equal 'USD', rate.currency
assert_kind_of Integer, rate.total_price
assert_kind_of Integer, rate.price
assert_instance_of String, rate.service_name
assert_instance_of String, rate.service_code
assert_instance_of Array, rate.package_rates
assert_equal package_fixtures.values_at(:book, :american_wii), rate.packages
package_rate = rate.package_rates.first
assert_instance_of Hash, package_rate
assert_instance_of Package, package_rate[:package]
refute_nil package_rate[:rate]
other_than_two = response.rates.map(&:package_count).reject { |n| n == 2 }
assert_equal [], other_than_two, "Some RateEstimates do not refer to the right number of packages (#{other_than_two.inspect})"
end
def test_international_rates
response = @carrier.find_rates(
location_fixtures[:beverly_hills],
location_fixtures[:ottawa],
package_fixtures.values_at(:book, :american_wii),
:test => true
)
assert response.success?, response.message
assert_instance_of Hash, response.params
assert_instance_of String, response.xml
assert_instance_of Array, response.rates
refute response.rates.empty?
rate = response.rates.first
assert_equal 'USPS', rate.carrier
assert_equal 'USD', rate.currency
assert_kind_of Integer, rate.total_price
assert_kind_of Integer, rate.price
assert_instance_of String, rate.service_name
assert_instance_of String, rate.service_code
assert_instance_of Array, rate.package_rates
assert_equal package_fixtures.values_at(:book, :american_wii), rate.packages
package_rate = rate.package_rates.first
assert_instance_of Hash, package_rate
assert_instance_of Package, package_rate[:package]
refute_nil package_rate[:rate]
other_than_two = response.rates.map(&:package_count).reject { |n| n == 2 }
assert_equal [], other_than_two, "Some RateEstimates do not refer to the right number of packages (#{other_than_two.inspect})"
end
def test_us_to_us_possession
response = @carrier.find_rates(
location_fixtures[:beverly_hills],
location_fixtures[:puerto_rico],
package_fixtures.values_at(:american_wii),
:test => true
)
assert response.success?, response.message
refute response.rates.empty?
end
def test_bare_packages_domestic
response = @carrier.find_rates(
location_fixtures[:beverly_hills], # imperial (U.S. origin)
location_fixtures[:new_york],
Package.new(0, 0),
:test => true
)
assert response.success?, response.message
refute response.rates.empty?
end
def test_bare_packages_international
response = @carrier.find_rates(
location_fixtures[:beverly_hills], # imperial (U.S. origin)
location_fixtures[:ottawa],
Package.new(0, 0),
:test => true
)
assert response.success?, response.message
refute response.rates.empty?
end
def test_first_class_packages_with_mail_type
response = @carrier.find_rates(
location_fixtures[:beverly_hills], # imperial (U.S. origin)
location_fixtures[:new_york],
Package.new(0, 0),
:test => true,
:service => :first_class,
:first_class_mail_type => :parcel
)
assert response.success?, response.message
refute response.rates.empty?
end
def test_first_class_packages_without_mail_type
assert_raises(ResponseError, "Invalid First Class Mail Type.") do
@carrier.find_rates(
location_fixtures[:beverly_hills], # imperial (U.S. origin)
location_fixtures[:new_york],
Package.new(0, 0),
:test => true,
:service => :first_class
)
end
end
def test_first_class_packages_with_invalid_mail_type
assert_raises(ResponseError, "Invalid First Class Mail Type.") do
@carrier.find_rates(
location_fixtures[:beverly_hills], # imperial (U.S. origin)
location_fixtures[:new_york],
Package.new(0, 0),
:test => true,
:service => :first_class,
:first_class_mail_tpe => :invalid
)
end
end
def test_correct_login_passes_valid_credentials?
assert USPS.new(@usps_credentials.merge(:test => true)).valid_credentials?
end
def test_wrong_login_fails_in_valid_credentials?
refute USPS.new(:login => 'ABCDEFGHIJKL', :test => true).valid_credentials?
end
def test_blank_login_fails_in_valid_credentials?
refute USPS.new(:login => '', :test => true).valid_credentials?
end
def test_nil_login_fails_in_valid_credentials?
refute USPS.new(:login => nil, :test => true).valid_credentials?
end
def test_must_provide_login_creds_when_instantiating
assert_raises ArgumentError do
USPS.new(:test => true)
end
end
# Uncomment and switch out SPECIAL_COUNTRIES with some other batch to see which
# countries are currently working. Commented out here just because it's a lot of
# hits to their server at once:
# ALL_COUNTRIES = ActiveUtils::Country.const_get('COUNTRIES').map {|c| c[:alpha2]}
# SPECIAL_COUNTRIES = USPS.const_get('COUNTRY_NAME_CONVERSIONS').keys.sort
# NORMAL_COUNTRIES = (ALL_COUNTRIES - SPECIAL_COUNTRIES)
#
# SPECIAL_COUNTRIES.each do |code|
# unless ActiveUtils::Country.find(code).name == USPS.const_get('COUNTRY_NAME_CONVERSIONS')[code]
# define_method("test_country_#{code}") do
# response = nil
# begin
# response = @carrier.find_rates( location_fixtures[:beverly_hills],
# Location.new(:country => code),
# package_fixtures.values_at(:american_wii),
# :test => true)
# rescue Exception => e
# flunk(e.inspect + "\nrequest: " + @carrier.last_request)
# end
# assert_not_equal [], response.rates.length
# end
# end
# end
end