Tests: Decouple test_report_model.py from StringListEncryptor

This commit is contained in:
Mike Salvatore 2021-09-21 12:51:55 -04:00
parent 13ba0b9091
commit f662369a07
1 changed files with 24 additions and 10 deletions

View File

@ -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