From edfe0ba51adbc4b5ecd581752cd35a8c7539da93 Mon Sep 17 00:00:00 2001 From: "Desai, Kartikey H" Date: Fri, 8 Feb 2019 09:37:27 -0500 Subject: [PATCH] Add support for Bing Maps and corresponding tests. Fixes #86 --- stix2/test/v21/test_location.py | 61 +++++++++++++++++++++++++++++++-- stix2/v21/sdo.py | 32 ++++++++++++----- 2 files changed, 81 insertions(+), 12 deletions(-) diff --git a/stix2/test/v21/test_location.py b/stix2/test/v21/test_location.py index 9244b1d..c8bf0fc 100644 --- a/stix2/test/v21/test_location.py +++ b/stix2/test/v21/test_location.py @@ -265,7 +265,7 @@ def test_location_lat_or_lon_dependency_missing(data, msg): assert msg in str(excinfo.value) -def test_map_url_long_lat_provided(): +def test_google_map_url_long_lat_provided(): EXPECTED_URL = "https://www.google.com/maps/search/?api=1&query=41.862401%2C-87.616001" now = dt.datetime(2019, 2, 7, 12, 34, 56, tzinfo=pytz.utc) @@ -282,7 +282,7 @@ def test_map_url_long_lat_provided(): assert loc_url == EXPECTED_URL -def test_map_url_multiple_props_no_long_lat_provided(): +def test_google_map_url_multiple_props_no_long_lat_provided(): EXPECTED_URL = "https://www.google.com/maps/search/?api=1&query=1410+Museum+Campus+Drive%2C+Chicago%2C+IL+60605%2CUnited+States+of+America%2CNorth+America" now = dt.datetime(2019, 2, 7, 12, 34, 56, tzinfo=pytz.utc) @@ -300,7 +300,7 @@ def test_map_url_multiple_props_no_long_lat_provided(): assert loc_url == EXPECTED_URL -def test_map_url_multiple_props_and_long_lat_provided(): +def test_google_map_url_multiple_props_and_long_lat_provided(): EXPECTED_URL = "https://www.google.com/maps/search/?api=1&query=41.862401%2C-87.616001" now = dt.datetime(2019, 2, 7, 12, 34, 56, tzinfo=pytz.utc) @@ -336,3 +336,58 @@ def test_map_url_invalid_map_engine_provided(): loc.to_maps_url("Fake Maps") assert "is not a valid or currently-supported map engine" in str(excinfo.value) + + +def test_bing_map_url_long_lat_provided(): + EXPECTED_URL = "https://bing.com/maps/default.aspx?where1=41.862401%2C-87.616001&lvl=16" + now = dt.datetime(2019, 2, 7, 12, 34, 56, tzinfo=pytz.utc) + + loc = stix2.v21.Location( + type="location", + id=LOCATION_ID, + created=now, + modified=now, + latitude=41.862401, + longitude=-87.616001, + ) + + loc_url = loc.to_maps_url("Bing Maps") + assert loc_url == EXPECTED_URL + + +def test_bing_map_url_multiple_props_no_long_lat_provided(): + EXPECTED_URL = "https://bing.com/maps/default.aspx?where1=1410+Museum+Campus+Drive%2C+Chicago%2C+IL+60605%2CUnited+States+of+America%2CNorth+America&lvl=16" + now = dt.datetime(2019, 2, 7, 12, 34, 56, tzinfo=pytz.utc) + + loc = stix2.v21.Location( + type="location", + id=LOCATION_ID, + created=now, + modified=now, + region="North America", + country="United States of America", + street_address="1410 Museum Campus Drive, Chicago, IL 60605", + ) + + loc_url = loc.to_maps_url("Bing Maps") + assert loc_url == EXPECTED_URL + + +def test_bing_map_url_multiple_props_and_long_lat_provided(): + EXPECTED_URL = "https://bing.com/maps/default.aspx?where1=41.862401%2C-87.616001&lvl=16" + now = dt.datetime(2019, 2, 7, 12, 34, 56, tzinfo=pytz.utc) + + loc = stix2.v21.Location( + type="location", + id=LOCATION_ID, + created=now, + modified=now, + region="North America", + country="United States of America", + street_address="1410 Museum Campus Drive, Chicago, IL 60605", + latitude=41.862401, + longitude=-87.616001, + ) + + loc_url = loc.to_maps_url("Bing Maps") + assert loc_url == EXPECTED_URL diff --git a/stix2/v21/sdo.py b/stix2/v21/sdo.py index 6fbb6f5..cff8ba6 100644 --- a/stix2/v21/sdo.py +++ b/stix2/v21/sdo.py @@ -276,20 +276,34 @@ class Location(STIXDomainObject): properties = ['street_address', 'city', 'country', 'region', 'administrative_area', 'postal_code'] params = [self.get(prop) for prop in properties if self.get(prop) is not None] - return self._to_maps_url_creator(map_engine, params) + return self._to_maps_url_dispatcher(map_engine, params) - def _to_maps_url_creator(self, map_engine, params): + def _to_maps_url_dispatcher(self, map_engine, params): if map_engine == "Google Maps": - url_base = "https://www.google.com/maps/search/?api=1&query=" - url_ending = params[0] - for i in range(1, len(params)): - url_ending = url_ending + "," + params[i] - - final_url = url_base + quote_plus(url_ending) - return final_url + return self._to_google_maps_url(params) + elif map_engine == "Bing Maps": + return self._to_bing_maps_url(params) else: raise ValueError(map_engine + " is not a valid or currently-supported map engine") + def _to_google_maps_url(self, params): + url_base = "https://www.google.com/maps/search/?api=1&query=" + url_ending = params[0] + for i in range(1, len(params)): + url_ending = url_ending + "," + params[i] + + final_url = url_base + quote_plus(url_ending) + return final_url + + def _to_bing_maps_url(self, params): + url_base = "https://bing.com/maps/default.aspx?where1=" + url_ending = params[0] + for i in range(1, len(params)): + url_ending = url_ending + "," + params[i] + + final_url = url_base + quote_plus(url_ending) + "&lvl=16" # level 16 zoom so long/lat searches shown more clearly + return final_url + class Malware(STIXDomainObject): # TODO: Add link