From a2eab2fa5d2586a422e805c3d9ff40d2ad29df8f Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 18 Nov 2021 11:09:12 -0500 Subject: [PATCH] Island: Add a check for None credentials in get_user_credentials() --- .../cc/services/authentication/json_file_user_datastore.py | 2 +- .../cc/services/test_json_file_user_datastore.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py b/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py index 7dd8d82b7..c54489ebd 100644 --- a/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py +++ b/monkey/monkey_island/cc/services/authentication/json_file_user_datastore.py @@ -51,7 +51,7 @@ class JsonFileUserDatastore(IUserDatastore): json.dump(self._credentials.to_dict(), f, indent=2) def get_user_credentials(self, username: str) -> UserCreds: - if self._credentials.username != username: + if self._credentials is None or self._credentials.username != username: raise UnknownUserError(f"User {username} does not exist.") return self._credentials diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py index 35b342a9f..f3924b8e7 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_json_file_user_datastore.py @@ -75,3 +75,8 @@ def test_get_user_credentials_from_file(tmp_path): def test_get_unknown_user(populated_datastore): with pytest.raises(UnknownUserError): populated_datastore.get_user_credentials("unregistered_user") + + +def test_get_user_credentials__no_user_registered(empty_datastore): + with pytest.raises(UnknownUserError): + empty_datastore.get_user_credentials("unregistered_user")