Refactored test_new_user_communication, mostly separated to functions

This commit is contained in:
Shay Nehmad 2019-09-16 15:04:22 +03:00
parent 4330a39725
commit dd9a4b2d10
1 changed files with 25 additions and 21 deletions

View File

@ -5,31 +5,35 @@ from monkey_island.cc.models.zero_trust.event import Event
def test_new_user_communication(current_monkey, success, message):
AggregateFinding.create_or_add_to_existing(
test=TEST_COMMUNICATE_AS_NEW_USER,
status=STATUS_PASSED if success else STATUS_FAILED,
events=[
get_attempt_event(current_monkey),
get_result_event(current_monkey, message, success)
]
)
def get_attempt_event(current_monkey):
tried_to_communicate_event = Event.create_event(
title="Communicate as new user",
message="Monkey on {} tried to create a new user and communicate from it.".format(current_monkey.hostname),
event_type=EVENT_TYPE_MONKEY_NETWORK)
events = [tried_to_communicate_event]
return tried_to_communicate_event
def get_result_event(current_monkey, message, success):
if success:
events.append(
Event.create_event(
title="Communicate as new user",
message="New user created by Monkey on {} successfully tried to communicate with the internet. "
"Details: {}".format(current_monkey.hostname, message),
event_type=EVENT_TYPE_MONKEY_NETWORK)
)
test_status = STATUS_FAILED
event_to_append = Event.create_event(
title="Communicate as new user",
message="New user created by Monkey on {} successfully tried to communicate with the internet. "
"Details: {}".format(current_monkey.hostname, message),
event_type=EVENT_TYPE_MONKEY_NETWORK)
else:
events.append(
Event.create_event(
title="Communicate as new user",
message="Monkey on {} couldn't communicate as new user. Details: {}".format(
current_monkey.hostname, message),
event_type=EVENT_TYPE_MONKEY_NETWORK)
)
test_status = STATUS_PASSED
AggregateFinding.create_or_add_to_existing(
test=TEST_COMMUNICATE_AS_NEW_USER, status=test_status, events=events
)
event_to_append = Event.create_event(
title="Communicate as new user",
message="Monkey on {} couldn't communicate as new user. Details: {}".format(
current_monkey.hostname, message),
event_type=EVENT_TYPE_MONKEY_NETWORK)
return event_to_append