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")