From dd9a4b2d101b1dcd7a7282c55f4bd6e7a35a57b8 Mon Sep 17 00:00:00 2001
From: Shay Nehmad <shay.nehmad@guardicore.com>
Date: Mon, 16 Sep 2019 15:04:22 +0300
Subject: [PATCH] Refactored test_new_user_communication, mostly separated to
 functions

---
 .../communicate_as_new_user.py                | 46 ++++++++++---------
 1 file changed, 25 insertions(+), 21 deletions(-)

diff --git a/monkey/monkey_island/cc/services/telemetry/zero_trust_tests/communicate_as_new_user.py b/monkey/monkey_island/cc/services/telemetry/zero_trust_tests/communicate_as_new_user.py
index 564ce4d20..0c36b7b94 100644
--- a/monkey/monkey_island/cc/services/telemetry/zero_trust_tests/communicate_as_new_user.py
+++ b/monkey/monkey_island/cc/services/telemetry/zero_trust_tests/communicate_as_new_user.py
@@ -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