1# frozen_string_literal: true
2
3require "test_helper"
4require "bandwidth_tn_repo"
5require "local_calling_guide_repo"
6require "geo_code_repo"
7
8BandwidthTnRepo::DB = Minitest::Mock.new
9
10class BandwidthTnRepoTest < Minitest::Test
11 def test_local_inventory_recycled_with_price
12 bw_body = Nokogiri::XML::Builder.new { |xml|
13 xml.Response {
14 xml.TelephoneNumberDetails {
15 xml.City "Austin"
16 xml.State "TX"
17 }
18 }
19 }.to_xml
20
21 stub_request(
22 :get,
23 "https://dashboard.bandwidth.com/v1.0/tns/5565555555/tndetails"
24 )
25 .with(
26 headers: {
27 "Accept" => "application/xml",
28 "Accept-Encoding" => "gzip, compressed",
29 "Authorization" => "Bearer test_bw_oauth_token",
30 "User-Agent" => "Ruby-Bandwidth-Iris"
31 }
32 ).to_return(status: 200, body: bw_body, headers: {})
33
34 tel = "+15565555555"
35
36 BandwidthTnRepo::DB.expect(
37 :exec,
38 nil,
39 [
40 BandwidthTnRepo::STASH_QUERY,
41 [tel, "TX", "Austin", "test_bw_account", 10]
42 ]
43 )
44
45 BandwidthTnRepo.new.disconnect(tel, "test").sync
46
47 assert_mock BandwidthTnRepo::DB
48 end
49 em :test_local_inventory_recycled_with_price
50
51 def test_local_inventory_recycled_no_price
52 bw_body = Nokogiri::XML::Builder.new { |xml|
53 xml.Response {
54 xml.TelephoneNumberDetails {
55 xml.City "Austin"
56 xml.State "TX"
57 }
58 }
59 }.to_xml
60
61 stub_request(
62 :get,
63 "https://dashboard.bandwidth.com/v1.0/tns/5575555555/tndetails"
64 )
65 .with(
66 headers: {
67 "Accept" => "application/xml",
68 "Accept-Encoding" => "gzip, compressed",
69 "Authorization" => "Bearer test_bw_oauth_token",
70 "User-Agent" => "Ruby-Bandwidth-Iris"
71 }
72 ).to_return(status: 200, body: bw_body, headers: {})
73
74 tel = "+15575555555"
75
76 BandwidthTnRepo::DB.expect(
77 :exec,
78 nil,
79 [
80 BandwidthTnRepo::STASH_QUERY,
81 [tel, "TX", "Austin", "test_bw_account", 0]
82 ]
83 )
84
85 BandwidthTnRepo.new.disconnect(tel, "test").sync
86
87 assert_mock BandwidthTnRepo::DB
88 end
89 em :test_local_inventory_recycled_no_price
90
91 def test_stash_falls_back_to_lcg_on_unreadable
92 tel = "+15565555555"
93
94 # bw_r bw_l geo_r geo_l lcg_r lcg_l exp_r exp_l
95 [
96 ["TX", "Austin", "CA", "LA", "NY", "NYC", "TX", "Austin"],
97 ["TX", "1City", "CA", "LA", "NY", "NYC", "TX", "LA"],
98 ["TX", "1City", "CA", "2City", "NY", "NYC", "TX", "NYC"],
99 ["1ST", "Austin", "CA", "LA", "NY", "NYC", "CA", "Austin"],
100 ["1ST", "1City", "CA", "LA", "NY", "NYC", "CA", "LA"],
101 ["1ST", "1City", "CA", "2City", "NY", "NYC", "CA", "NYC"],
102 ["1ST", "Austin", "2ST", "LA", "NY", "NYC", "NY", "Austin"],
103 ["1ST", "1City", "2ST", "LA", "NY", "NYC", "NY", "LA"],
104 ["1ST", "1City", "2ST", "2City", "NY", "NYC", "NY", "NYC"],
105 ["TX", nil, "CA", "LA", "NY", "NYC", "TX", "LA"],
106 ["TX", nil, "CA", nil, "NY", "NYC", "TX", "NYC"],
107 [nil, "Austin", "CA", "LA", "NY", "NYC", "CA", "Austin"],
108 [nil, nil, "CA", "LA", "NY", "NYC", "CA", "LA"],
109 [nil, nil, "CA", nil, "NY", "NYC", "CA", "NYC"],
110 [nil, "Austin", nil, "LA", "NY", "NYC", "NY", "Austin"],
111 [nil, nil, nil, "LA", "NY", "NYC", "NY", "LA"],
112 [nil, nil, nil, nil, "NY", "NYC", "NY", "NYC"]
113 ].each do |bw_r, bw_l, geo_r, geo_l, lcg_r, lcg_l, exp_r, exp_l|
114 WebMock.reset!
115 stub_bw_oauth_token
116 stub_bw_tndetails(bw_r, bw_l)
117
118 lcg_body = Nokogiri::XML::Builder.new { |xml|
119 xml.root {
120 xml.prefixdata {
121 xml.send(:"rc-lat", "30.267153")
122 xml.send(:"rc-lon", "-97.743061")
123 xml.rc(lcg_l)
124 xml.region(lcg_r)
125 }
126 }
127 }.to_xml
128
129 geo_json = {
130 "latt" => "30.267153",
131 "longt" => "-97.743061"
132 }
133 geo_json["prov"] = geo_r if geo_r
134 geo_json["city"] = geo_l if geo_l
135
136 stub_request(
137 :get,
138 "https://dashboard.bandwidth.com/v1.0/tns/5565555555/tndetails"
139 )
140 .with(
141 headers: {
142 "Accept" => "application/xml",
143 "Accept-Encoding" => "gzip, compressed",
144 "Authorization" =>
145 "Basic dGVzdF9id191c2VyOnRlc3RfYndfcGFzc3dvcmQ=",
146 "User-Agent" => "Ruby-Bandwidth-Iris"
147 }
148 ).to_return(status: 200, body: bw_body, headers: {})
149
150 stub_request(
151 :get,
152 "https://localcallingguide.com/xmlprefix.php?npa=556&nxx=555"
153 ).to_return(status: 200, body: lcg_body)
154
155 stub_request(
156 :get,
157 "https://geocoder.ca/?json=1&latt=30.267153&longt=-97.743061"
158 ).to_return(status: 200, body: geo_json.to_json)
159
160 BandwidthTnRepo::DB.expect(
161 :exec,
162 nil,
163 [
164 BandwidthTnRepo::STASH_QUERY,
165 [tel, exp_r, exp_l, "test_bw_account", 10]
166 ]
167 )
168
169 BandwidthTnRepo.new(
170 local_calling_guide_repo: LocalCallingGuideRepo.new,
171 geo_code_repo: GeoCodeRepo.new(memcache: FakeMemcache.new)
172 ).disconnect(tel, "test").sync
173
174 assert_mock BandwidthTnRepo::DB
175 end
176 end
177 em :test_stash_falls_back_to_lcg_on_unreadable
178
179 def test_rate_center_resolves_region_and_locality_independently
180 fake_btn = Struct.new(:telephone_number, :bw_state, :bw_city) {
181 def get_details
182 { city: bw_city, state: bw_state }
183 end
184 }
185
186 fake_prefix = Struct.new(:lat, :lon, :region, :locality) {
187 def to_h
188 { "region" => region, "locality" => locality }
189 end
190 }
191
192 fake_geo_code = Struct.new(:locality, :region) {
193 def to_h
194 { locality: locality, region: region }
195 end
196 }
197
198 fake_lcg_repo = Struct.new(:prefix) {
199 def find(_npa, _nxx)
200 EMPromise.resolve(prefix)
201 end
202 }
203
204 fake_geo_repo = Struct.new(:result) {
205 def reverse(_lat, _lon)
206 EMPromise.resolve(result)
207 end
208 }
209
210 # bw_r bw_l geo_r geo_l lcg_r lcg_l exp_r exp_l
211 [
212 ["TX", "Austin", "CA", "LA", "NY", "NYC", "TX", "Austin"],
213 ["TX", "1City", "CA", "LA", "NY", "NYC", "TX", "LA"],
214 ["TX", "1City", "CA", "2City", "NY", "NYC", "TX", "NYC"],
215 ["1ST", "Austin", "CA", "LA", "NY", "NYC", "CA", "Austin"],
216 ["1ST", "1City", "CA", "LA", "NY", "NYC", "CA", "LA"],
217 ["1ST", "1City", "CA", "2City", "NY", "NYC", "CA", "NYC"],
218 ["1ST", "Austin", "2ST", "LA", "NY", "NYC", "NY", "Austin"],
219 ["1ST", "1City", "2ST", "LA", "NY", "NYC", "NY", "LA"],
220 ["1ST", "1City", "2ST", "2City", "NY", "NYC", "NY", "NYC"],
221 ["TX", nil, "CA", "LA", "NY", "NYC", "TX", "LA"],
222 ["TX", nil, "CA", nil, "NY", "NYC", "TX", "NYC"],
223 [nil, "Austin", "CA", "LA", "NY", "NYC", "CA", "Austin"],
224 [nil, nil, "CA", "LA", "NY", "NYC", "CA", "LA"],
225 [nil, nil, "CA", nil, "NY", "NYC", "CA", "NYC"],
226 [nil, "Austin", nil, "LA", "NY", "NYC", "NY", "Austin"],
227 [nil, nil, nil, "LA", "NY", "NYC", "NY", "LA"],
228 [nil, nil, nil, nil, "NY", "NYC", "NY", "NYC"]
229 ].each do |bw_r, bw_l, geo_r, geo_l, lcg_r, lcg_l, exp_r, exp_l|
230 btn = fake_btn.new("5565555555", bw_r, bw_l)
231 prefix = fake_prefix.new(30.0, -97.0, lcg_r, lcg_l)
232 geo = fake_geo_code.new(geo_l, geo_r)
233
234 result = BandwidthTnRepo::RateCenter.for(
235 btn,
236 lcg_repo: fake_lcg_repo.new(prefix),
237 geo_code_repo: fake_geo_repo.new(geo)
238 ).sync
239
240 assert_equal(
241 [exp_r, exp_l],
242 result.to_a,
243 "bw=#{[bw_r, bw_l]} geo=#{[geo_r, geo_l]} " \
244 "lcg=#{[lcg_r, lcg_l]}"
245 )
246 end
247 end
248 em :test_rate_center_resolves_region_and_locality_independently
249
250 def stub_bw_tndetails(bw_r, bw_l)
251 if bw_r == :error
252 stub_request(
253 :get,
254 "https://dashboard.bandwidth.com/v1.0/tns/5565555555/tndetails"
255 ).to_return(status: 404)
256 return
257 end
258
259 bw_body = Nokogiri::XML::Builder.new { |xml|
260 xml.Response {
261 xml.TelephoneNumberDetails {
262 xml.City(bw_l) if bw_l
263 xml.State(bw_r) if bw_r
264 }
265 }
266 }.to_xml
267
268 stub_request(
269 :get,
270 "https://dashboard.bandwidth.com/v1.0/tns/5565555555/tndetails"
271 )
272 .with(
273 headers: {
274 "Accept" => "application/xml",
275 "Accept-Encoding" => "gzip, compressed",
276 "Authorization" => "Bearer test_bw_oauth_token",
277 "User-Agent" => "Ruby-Bandwidth-Iris"
278 }
279 ).to_return(status: 200, body: bw_body, headers: {})
280 end
281
282 def stub_geocoder(geo_r, geo_l)
283 if geo_r == :error
284 stub_request(
285 :get,
286 "https://geocoder.ca/?json=1&latt=30.267153&longt=-97.743061"
287 ).to_return(status: 500)
288 return
289 end
290
291 geo_json = {
292 "latt" => "30.267153",
293 "longt" => "-97.743061"
294 }
295 geo_json["prov"] = geo_r if geo_r
296 geo_json["city"] = geo_l if geo_l
297
298 stub_request(
299 :get,
300 "https://geocoder.ca/?json=1&latt=30.267153&longt=-97.743061"
301 ).to_return(status: 200, body: geo_json.to_json)
302 end
303end