Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Triage "trust me bro" bug #755

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/meshapi/tests/sample_data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from geopy import Location, Point

from meshapi.models import Device, Install, Node

sample_member = {
Expand Down Expand Up @@ -57,3 +59,38 @@
}
]
}


sample_osm_address_response = Location(
"151, Broome Street, Manhattan Community Board 3, Manhattan, New York County, City of New York, New York, 10002, United States",
Point(40.7162281, -73.98489654157149, 0.0),
{
"address": {
"ISO3166-2-lvl4": "US-NY",
"city": "City of New York",
"country": "United States",
"country_code": "us",
"county": "New York County",
"house_number": "151",
"neighbourhood": "Manhattan Community Board 3",
"postcode": "10002",
"road": "Broome Street",
"state": "New York",
"suburb": "Manhattan",
},
"addresstype": "building",
"boundingbox": ["40.7160582", "40.7164320", "-73.9852426", "-73.9847390"],
"class": "building",
"display_name": "151, Broome Street, Manhattan Community Board 3, Manhattan, New York County, City of New York, New York, 10002, United States",
"importance": 9.175936522464359e-05,
"lat": "40.7162281",
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
"lon": "-73.98489654157149",
"name": "",
"osm_id": 250268365,
"osm_type": "way",
"place_id": 333450671,
"place_rank": 30,
"type": "yes",
},
)
117 changes: 117 additions & 0 deletions src/meshapi/tests/test_join_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,123 @@ def test_valid_join_form_with_member_confirmation(self, submission):
)
validate_successful_join_form_submission(self, "Valid Join Form", s, response)

def test_valid_join_form_use_original_with_results_from_geosearch(self):
submission = valid_join_form_submission
submission["street_address"] = "151 B Street"
submission["city"] = "NYC"
submission["phone_number"] = "1111111111"

self.requests_mocker.get(
NYC_PLANNING_LABS_GEOCODE_URL,
json=submission["dob_addr_response"],
)

request, s = pull_apart_join_form_submission(submission)
s.street_address = "151 B Street"
request["trust_me_bro"] = False

response = self.c.post("/api/v1/join/", request, content_type="application/json")
code = 409
self.assertEqual(
code,
response.status_code,
f"status code incorrect for Valid Join Form. Should be {code}, but got {response.status_code}.\n Response is: {response.content.decode('utf-8')}",
)

request["trust_me_bro"] = True

response = self.c.post("/api/v1/join/", request, content_type="application/json")
code = 201
self.assertEqual(
code,
response.status_code,
f"status code incorrect for Valid Join Form. Should be {code}, but got {response.status_code}.\n Response is: {response.content.decode('utf-8')}",
)

install = Install.objects.get(id=response.json()["install_id"])
self.assertEqual(install.building.street_address, "151 B Street")
self.assertEqual(install.building.city, "NYC")
self.assertEqual(install.member.phone_number, "+1 111 111 1111")

validate_successful_join_form_submission(self, "Valid Join Form", s, response)

def test_valid_join_form_use_original_without_results_from_geosearch(self):
submission = valid_join_form_submission
submission["street_address"] = "151 B Street"
submission["city"] = "NYC"
submission["phone_number"] = "1111111111"

self.requests_mocker.get(NYC_PLANNING_LABS_GEOCODE_URL, json={"features": []})

request, s = pull_apart_join_form_submission(submission)
s.street_address = "151 B Street"
request["trust_me_bro"] = False

response = self.c.post("/api/v1/join/", request, content_type="application/json")
code = 409
self.assertEqual(
code,
response.status_code,
f"status code incorrect for Valid Join Form. Should be {code}, but got {response.status_code}.\n Response is: {response.content.decode('utf-8')}",
)

request["trust_me_bro"] = True

response = self.c.post("/api/v1/join/", request, content_type="application/json")
code = 201
self.assertEqual(
code,
response.status_code,
f"status code incorrect for Valid Join Form. Should be {code}, but got {response.status_code}.\n Response is: {response.content.decode('utf-8')}",
)

install = Install.objects.get(id=response.json()["install_id"])
self.assertEqual(install.building.street_address, "151 B Street")
self.assertEqual(install.building.city, "NYC")
self.assertEqual(install.member.phone_number, "+1 111 111 1111")

validate_successful_join_form_submission(self, "Valid Join Form", s, response)

def test_valid_trust_me_bro_bad_zip(self):
submission = valid_join_form_submission
submission["zip_code"] = "00000"

self.requests_mocker.get(
NYC_PLANNING_LABS_GEOCODE_URL,
json=submission["dob_addr_response"],
)

request, s = pull_apart_join_form_submission(submission)
request["trust_me_bro"] = True

response = self.c.post("/api/v1/join/", request, content_type="application/json")
code = 400
self.assertEqual(
code,
response.status_code,
f"status code incorrect for Valid Join Form. Should be {code}, but got {response.status_code}.\n Response is: {response.content.decode('utf-8')}",
)

def test_valid_trust_me_bro_bad_state(self):
submission = valid_join_form_submission
submission["state"] = "CA"

self.requests_mocker.get(
NYC_PLANNING_LABS_GEOCODE_URL,
json=submission["dob_addr_response"],
)

request, s = pull_apart_join_form_submission(submission)
request["trust_me_bro"] = True

response = self.c.post("/api/v1/join/", request, content_type="application/json")
code = 400
self.assertEqual(
code,
response.status_code,
f"status code incorrect for Valid Join Form. Should be {code}, but got {response.status_code}.\n Response is: {response.content.decode('utf-8')}",
)

def test_valid_join_form_aussie_intl_phone(self):
request, s = pull_apart_join_form_submission(valid_join_form_submission)

Expand Down
61 changes: 59 additions & 2 deletions src/meshapi/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from django.test import TestCase

from meshapi.exceptions import AddressAPIError, AddressError
from meshapi.tests.sample_data import sample_address_response
from meshapi.validation import NYCAddressInfo
from meshapi.tests.sample_data import sample_address_response, sample_osm_address_response
from meshapi.util.constants import INVALID_ALTITUDE
from meshapi.validation import AddressInfo, NYCAddressInfo, OSMAddressInfo


class TestValidationNYCAddressInfo(TestCase):
Expand Down Expand Up @@ -107,3 +108,59 @@ def test_validate_address_open_data_invalid_response(self, mock_requests):
assert nyc_addr_info.latitude == 40.716245
assert nyc_addr_info.altitude is None
assert nyc_addr_info.bin == 1234


class TestValidationOSMAddressInfo(TestCase):
def test_invalid_state(self):
with self.assertRaises(ValueError):
OSMAddressInfo("151 Broome St", "New York", "ny", "10002")

@patch("meshapi.validation.Nominatim")
def test_validate_address_geosearch_empty_response(self, mock_nominatim):
mock_geocode_func = MagicMock()
mock_geolocator = MagicMock()
mock_geolocator.geocode = mock_geocode_func
mock_nominatim.return_value = mock_geolocator

mock_geocode_func.return_value = None

with self.assertRaises(AddressError):
OSMAddressInfo("151 Broome St", "New York", "NY", "10002")

@patch("meshapi.validation.Nominatim")
def test_validate_address_geosearch_network_or_throttle(self, mock_nominatim):
mock_geocode_func = MagicMock()
mock_geolocator = MagicMock()
mock_geolocator.geocode = mock_geocode_func
mock_nominatim.return_value = mock_geolocator
mock_geocode_func.side_effect = Exception("Pretend this is a network issue")

with self.assertRaises(AddressAPIError):
OSMAddressInfo("151 Broome St", "New York", "NY", "10002")

@patch("meshapi.validation.Nominatim")
def test_validate_address_good(self, mock_nominatim):
mock_geocode_func = MagicMock()
mock_geolocator = MagicMock()
mock_geolocator.geocode = mock_geocode_func
mock_nominatim.return_value = mock_geolocator
mock_geocode_func.return_value = sample_osm_address_response

osm_addr_info = OSMAddressInfo("151 broome Street", "New York", "NY", "10002")

assert osm_addr_info is not None
assert osm_addr_info.osm_id == 250268365
assert osm_addr_info.osm_place_id == 333450671
assert osm_addr_info.street_address == "151 Broome Street"
assert osm_addr_info.city == "New York"
assert osm_addr_info.state == "NY"
assert osm_addr_info.zip == "10002"
assert osm_addr_info.longitude == -73.98489654157149
assert osm_addr_info.latitude == 40.7162281
assert osm_addr_info.altitude == INVALID_ALTITUDE


class TestValidationRawAddressInfo(TestCase):
def test_invalid_state(self):
with self.assertRaises(TypeError):
AddressInfo("151 Broome St", "New York", "NY", "10002")
Loading
Loading