Update to_map_url and add tests. Fixes #86

master
Desai, Kartikey H 2019-02-07 10:31:51 -05:00
parent dc91c9cbf4
commit 8be704a5b9
2 changed files with 79 additions and 5 deletions

View File

@ -263,3 +263,76 @@ def test_location_lat_or_lon_dependency_missing(data, msg):
stix2.parse(data)
assert msg in str(excinfo.value)
def test_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)
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()
assert loc_url == EXPECTED_URL
def test_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)
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()
assert loc_url == EXPECTED_URL
def test_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)
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()
assert loc_url == EXPECTED_URL
def test_map_url_invalid_map_engine_provided():
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,
)
with pytest.raises(ValueError) as excinfo:
loc.to_maps_url("Fake Maps")
assert "is not a valid or currently-supported map engine" in str(excinfo.value)

View File

@ -267,12 +267,13 @@ class Location(STIXDomainObject):
def to_maps_url(self, map_engine="Google Maps"):
params = []
if self.get('latitude') is not None and self.get('longitude') is not None:
latitude = self.get('latitude')
longitude = self.get('longitude')
latitude = self.get('latitude', None)
longitude = self.get('longitude', None)
if latitude is not None and longitude is not None:
params.extend([str(latitude), str(longitude)])
else:
properties = ['street_address', 'city', 'region', 'administrative_area', 'country', 'postal_code']
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)
@ -287,7 +288,7 @@ class Location(STIXDomainObject):
final_url = url_base + quote_plus(url_ending)
return final_url
else:
return "Other map engines are not currently supported."
raise ValueError(map_engine + " is not a valid or currently-supported map engine")
class Malware(STIXDomainObject):