From f662369a07ae3c55d67edc1b5b7577d356418627 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 21 Sep 2021 12:51:55 -0400 Subject: [PATCH] Tests: Decouple test_report_model.py from StringListEncryptor --- .../cc/models/test_report_model.py | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/models/test_report_model.py b/monkey/tests/unit_tests/monkey_island/cc/models/test_report_model.py index 6eedb33b8..1b1e9ed07 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/models/test_report_model.py +++ b/monkey/tests/unit_tests/monkey_island/cc/models/test_report_model.py @@ -1,9 +1,10 @@ +from typing import List + import pytest from monkey_island.cc.models import Report -from monkey_island.cc.models.utils.field_encryptors.string_list_encryptor import StringListEncryptor +from monkey_island.cc.models.utils.field_encryptors.i_field_encryptor import IFieldEncryptor from monkey_island.cc.models.utils.report_encryptor import SensitiveField -from monkey_island.cc.server_utils.encryptor import initialize_encryptor MOCK_SENSITIVE_FIELD_CONTENTS = ["the_string", "the_string2"] MOCK_REPORT_DICT = { @@ -13,20 +14,33 @@ MOCK_REPORT_DICT = { "meta_info": {"foo": "bar"}, } -MOCK_SENSITIVE_FIELDS = [SensitiveField("overview.foo.the_key", StringListEncryptor)] + +class MockFieldEncryptor(IFieldEncryptor): + plaintext = [] + + @staticmethod + def encrypt(value: List[str]) -> List[str]: + return [MockFieldEncryptor._encrypt(v) for v in value] + + @staticmethod + def _encrypt(value: str) -> str: + MockFieldEncryptor.plaintext.append(value) + return str(len(MockFieldEncryptor.plaintext) - 1) + + @staticmethod + def decrypt(value: List[str]) -> List[str]: + return [MockFieldEncryptor.plaintext[int(v)] for v in value] + + +MOCK_SENSITIVE_FIELDS = [SensitiveField("overview.foo.the_key", MockFieldEncryptor)] @pytest.mark.usefixtures("uses_database") def test_report_encryption(monkeypatch, data_for_tests_dir): - initialize_encryptor(data_for_tests_dir) - monkeypatch.setattr( "monkey_island.cc.models.utils.report_encryptor.sensitive_fields", MOCK_SENSITIVE_FIELDS ) Report.save_report(MOCK_REPORT_DICT) - assert not Report.objects.first()["overview"]["foo"]["the_key"] == MOCK_SENSITIVE_FIELD_CONTENTS - assert ( - not Report.objects.first()["overview"]["foo"]["the_key"][1] - == MOCK_SENSITIVE_FIELD_CONTENTS[1] - ) + + assert Report.objects.first()["overview"]["foo"]["the_key"] == ["0", "1"] assert Report.get_report()["overview"]["foo"]["the_key"] == MOCK_SENSITIVE_FIELD_CONTENTS