Skip to content

Commit

Permalink
island: Simplify UserCreds constructor by removing defaults
Browse files Browse the repository at this point in the history
The default values were only really used by the test code. We can
simplify the Usercreds's interface and test code by removing
functionality (read: complication) we don't really need.
  • Loading branch information
mssalvatore committed May 4, 2021
1 parent 1aed5f3 commit d56cb5c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
2 changes: 1 addition & 1 deletion monkey/monkey_island/cc/environment/user_creds.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class UserCreds:
def __init__(self, username="", password_hash=""):
def __init__(self, username, password_hash):
self.username = username
self.password_hash = password_hash

Expand Down
8 changes: 5 additions & 3 deletions monkey/tests/monkey_island/cc/environment/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
STANDARD_WITH_CREDENTIALS = None
STANDARD_ENV = None

EMPTY_CREDS = UserCreds("", "")


# This fixture is a dirty hack that can be removed once these tests are converted from
# unittest to pytest. Instead, the appropriate fixtures from conftest.py can be used.
Expand Down Expand Up @@ -67,7 +69,7 @@ def get_server_config_file_path_test_version():
class TestEnvironment(TestCase):
class EnvironmentCredentialsNotRequired(Environment):
def __init__(self):
config = StubEnvironmentConfig("test", "test", UserCreds())
config = StubEnvironmentConfig("test", "test", EMPTY_CREDS)
super().__init__(config)

_credentials_required = False
Expand All @@ -77,7 +79,7 @@ def get_auth_users(self):

class EnvironmentCredentialsRequired(Environment):
def __init__(self):
config = StubEnvironmentConfig("test", "test", UserCreds())
config = StubEnvironmentConfig("test", "test", EMPTY_CREDS)
super().__init__(config)

_credentials_required = True
Expand All @@ -101,7 +103,7 @@ def test_try_add_user(self):
credentials = UserCreds(username="test", password_hash="1231234")
env.try_add_user(credentials)

credentials = UserCreds(username="test")
credentials = UserCreds(username="test", password_hash="")
with self.assertRaises(InvalidRegistrationCredentialsError):
env.try_add_user(credentials)

Expand Down
10 changes: 1 addition & 9 deletions monkey/tests/monkey_island/cc/environment/test_user_creds.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def test_to_dict_empty_creds():
user_creds = UserCreds()
user_creds = UserCreds("", "")
assert user_creds.to_dict() == {}


Expand All @@ -23,14 +23,6 @@ def test_to_auth_user_full_credentials():
assert auth_user.secret == "abc1231234"


def test_to_auth_user_username_only():
user_creds = UserCreds(username="Test")
auth_user = user_creds.to_auth_user()
assert auth_user.id == 1
assert auth_user.username == "Test"
assert auth_user.secret == ""


def test_get_from_cleartext(monkeypatch):
monkeypatch.setattr(bcrypt, "gensalt", lambda: TEST_SALT)

Expand Down

0 comments on commit d56cb5c

Please sign in to comment.