diff --git a/monkey/infection_monkey/telemetry/credentials_telem.py b/monkey/infection_monkey/telemetry/credentials_telem.py index c0573d942..4f5c43aa4 100644 --- a/monkey/infection_monkey/telemetry/credentials_telem.py +++ b/monkey/infection_monkey/telemetry/credentials_telem.py @@ -17,6 +17,10 @@ class CredentialsTelem(BaseTelem): """ self._credentials = credentials + @property + def credentials(self) -> Iterable[Credentials]: + return iter(self._credentials) + def send(self, log_data=True): super().send(log_data=False) diff --git a/monkey/tests/unit_tests/infection_monkey/telemetry/test_credentials_telem.py b/monkey/tests/unit_tests/infection_monkey/telemetry/test_credentials_telem.py index a3d1e3f6f..13c93f60f 100644 --- a/monkey/tests/unit_tests/infection_monkey/telemetry/test_credentials_telem.py +++ b/monkey/tests/unit_tests/infection_monkey/telemetry/test_credentials_telem.py @@ -1,37 +1,51 @@ import json +import pytest + from infection_monkey.credential_collectors import Password, SSHKeypair, Username from infection_monkey.i_puppet import Credentials from infection_monkey.telemetry.credentials_telem import CredentialsTelem +USERNAME = "m0nkey" +PASSWORD = "mmm" +PUBLIC_KEY = "pub_key" +PRIVATE_KEY = "priv_key" -def test_credential_telem_send(spy_send_telemetry): - username = "m0nkey" - password = "mmm" - public_key = "pub_key" - private_key = "priv_key" + +@pytest.fixture +def credentials_for_test(): + + return Credentials( + [Username(USERNAME)], [Password(PASSWORD), SSHKeypair(PRIVATE_KEY, PUBLIC_KEY)] + ) + + +def test_credential_telem_send(spy_send_telemetry, credentials_for_test): expected_data = [ { - "identities": [{"username": username, "credential_type": "USERNAME"}], + "identities": [{"username": USERNAME, "credential_type": "USERNAME"}], "secrets": [ - {"password": password, "credential_type": "PASSWORD"}, + {"password": PASSWORD, "credential_type": "PASSWORD"}, { - "private_key": "pub_key", - "public_key": "priv_key", + "private_key": PRIVATE_KEY, + "public_key": PUBLIC_KEY, "credential_type": "SSH_KEYPAIR", }, ], } ] - credentials = Credentials( - [Username(username)], [Password(password), SSHKeypair(public_key, private_key)] - ) - - telem = CredentialsTelem([credentials]) + telem = CredentialsTelem([credentials_for_test]) telem.send() expected_data = json.dumps(expected_data, cls=telem.json_encoder) assert spy_send_telemetry.data == expected_data assert spy_send_telemetry.telem_category == "credentials" + + +def test_credentials_property(credentials_for_test): + telem = CredentialsTelem([credentials_for_test]) + + assert len(list(telem.credentials)) == 1 + assert list(telem.credentials)[0] == credentials_for_test