def test_save_finding_sanity(self): assert len( Finding.objects(test=zero_trust_consts.TEST_SEGMENTATION)) == 0 event_example = Event.create_event( title="Event Title", message="event message", event_type=zero_trust_consts.EVENT_TYPE_MONKEY_NETWORK, ) monkey_details_example = MonkeyFindingDetails() monkey_details_example.events.append(event_example) monkey_details_example.save() MonkeyFinding.save_finding( test=zero_trust_consts.TEST_SEGMENTATION, status=zero_trust_consts.STATUS_FAILED, detail_ref=monkey_details_example, ) assert len( MonkeyFinding.objects( test=zero_trust_consts.TEST_SEGMENTATION)) == 1 assert len( MonkeyFinding.objects(status=zero_trust_consts.STATUS_FAILED)) == 1 assert len( Finding.objects(status=zero_trust_consts.STATUS_FAILED)) == 1
def test_save_finding_validation(self): with pytest.raises(ValidationError): _ = MonkeyFinding.save_finding( test="bla bla", status=zero_trust_consts.STATUS_FAILED, detail_ref=MONKEY_FINDING_DETAIL_MOCK, )
def create_or_add_to_existing(test: str, status: str, events: List[Event]): """ Create a new finding or add the events to an existing one if it's the same (same meaning same status and same test). :raises: Assertion error if this is used when there's more then one finding which fits the query - this is not when this function should be used. """ existing_findings = list(MonkeyFinding.objects(test=test, status=status)) assert (len(existing_findings) < 2), "More than one finding exists for {}:{}".format(test, status) if len(existing_findings) == 0: MonkeyZTFindingService.create_new_finding(test, status, events) else: # Now we know for sure this is the only one MonkeyZTFindingService.add_events(existing_findings[0], events)
def test_create_or_add_to_existing_addition(self): # Create new finding MonkeyZTFindingService.create_or_add_to_existing(test=TESTS[0], status=STATUS[0], events=[EVENTS[0]]) # Assert that there's only one finding assert len(Finding.objects()) == 1 # Add events to an existing finding MonkeyZTFindingService.create_or_add_to_existing(test=TESTS[0], status=STATUS[0], events=[EVENTS[1]]) # Assert there's still only one finding, only events got appended assert len(Finding.objects()) == 1 assert len(Finding.objects()[0].details.fetch().events) == 2 # Create new finding MonkeyZTFindingService.create_or_add_to_existing(test=TESTS[1], status=STATUS[1], events=[EVENTS[1]]) # Assert there was a new finding created, because test and status is different assert len(MonkeyFinding.objects()) == 2
def get_monkey_finding_dto() -> Finding: monkey_details = get_monkey_details_dto() monkey_details.save() return MonkeyFinding(test=TEST_ENDPOINT_SECURITY_EXISTS, status=STATUS_PASSED, details=monkey_details)
def create_new_finding(test: str, status: str, events: List[Event]): details = MonkeyFindingDetails() details.events = events details.save() MonkeyFinding.save_finding(test, status, details)