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

Cs/sc 3360 support road network algorithm #34024

Closed
wants to merge 16 commits into from
Closed
15 changes: 10 additions & 5 deletions corehq/apps/geospatial/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,13 @@ def plaintext_api_token(self):

@plaintext_api_token.setter
def plaintext_api_token(self, value):
if value and not value.startswith(f'${ALGO_AES}$'):
ciphertext = b64_aes_encrypt(value)
self.api_token = f'${ALGO_AES}${ciphertext}'
else:
self.api_token = None
try:
if value is None:
self.api_token = None
elif value and not value.startswith(f'${ALGO_AES}$'):
ciphertext = b64_aes_encrypt(value)
self.api_token = f'${ALGO_AES}${ciphertext}'
else:
raise Exception("Unexpected value set for plaintext api token")
except AttributeError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the improvement @Charl1996
Can you share what will raise the exception AttributeError?

Additionally, possible to keep the two messages different? If one gets an error, it wont be clear where it came from.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When raising exceptions its nice to be explicit what code would raise the exception. It's not clear to me here what will raise the exception AttributeError, though if only the necessary code is kept in the try block, its easier to locate.

raise Exception("Unexpected value set for plaintext api token")
7 changes: 7 additions & 0 deletions corehq/apps/geospatial/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ def test_geo_config_api_token(self):
self.assertEqual(config.plaintext_api_token, None)
self.assertEqual(config.api_token, None)

def test_geo_config_api_token_cannot_be_empty(self):
with self.assertRaises(Exception) as context:
with self.get_geo_config() as config:
config.plaintext_api_token = ""

self.assertEqual(str(context.exception), "Unexpected value set for plaintext api token")

@contextmanager
def get_geo_config(self):
conf = GeoConfig(
Expand Down
Loading