def test_check_and_compile_query(self):
        log = logging.getLogger("Test")
        nif = NewIncidentFound()
        nif.connection = MockConnection()
        nif.logger = log

        actual = nif._check_and_compile_query(".*")
        expected = re.compile(".*")

        self.assertEqual(actual, expected)
    def test_get_initial_incident_info(self, mock_get):
        log = logging.getLogger("Test")
        nif = NewIncidentFound()
        nif.connection = MockConnection()
        nif.logger = log

        actual = nif._get_initial_incident_info()
        expected = maya.parse("Tue, 24 Sep 2019 18:08:11 GMT")

        self.assertEqual(actual, expected)
    def test_check_new_incidents_and_send(self):
        log = logging.getLogger("Test")
        nif = NewIncidentFound()
        nif.logger = log

        new_incidents_json = json.loads(
            read_file_to_string("./payloads/get_new_incidents.json"))

        nif._check_new_incidents_and_send("", maya.now(), new_incidents_json)
        pass  # We are just making sure the last call didn't throw an exception.
    def test_get_new_incidents(self, mock_get):
        log = logging.getLogger("Test")
        nif = NewIncidentFound()
        nif.connection = MockConnection()
        nif.logger = log

        actual = nif._get_new_incidents()

        self.assertEqual(
            actual.get("entries")[0].get("values").get("Entry ID"),
            "INC000000000109")
    def test_check_new_incidents_and_send_with_query(self, mockSend):
        log = logging.getLogger("Test")
        nif = NewIncidentFound()
        nif.logger = log

        new_incidents_json = json.loads(
            read_file_to_string("./payloads/get_new_incidents.json"))
        test_for_time = maya.parse("2019-09-19T15:00:00.000+0000")

        compiled_query = re.compile("Testing Incident Trigger for Query")

        nif._check_new_incidents_and_send(compiled_query, test_for_time,
                                          new_incidents_json)

        self.assertEqual(mockSend.call_count, 1)

        actual_call = mockSend.call_args_list
        incident1 = actual_call[0][0][0].get("incident").get("values")
        self.assertEqual(incident1.get("Entry ID"), "INC000000000109")
    def test_check_new_incidents_and_send_valid_return(self, mockSend):
        log = logging.getLogger("Test")
        nif = NewIncidentFound()
        nif.logger = log

        new_incidents_json = json.loads(
            read_file_to_string("./payloads/get_new_incidents.json"))
        test_for_time = maya.parse("2019-09-19T15:00:00.000+0000")
        nif._check_new_incidents_and_send(None, test_for_time,
                                          new_incidents_json)

        self.assertEqual(mockSend.call_count, 2)

        actual_call = mockSend.call_args_list
        incident1 = actual_call[0][0][0].get("incident").get("values")
        incident2 = actual_call[1][0][0].get("incident").get("values")

        self.assertEqual(incident1.get("Entry ID"), "INC000000000109")
        self.assertEqual(incident2.get("Entry ID"), "INC000000000108")
    def test_check_and_compile_query_bad_regex(self):
        log = logging.getLogger("Test")
        nif = NewIncidentFound()
        nif.connection = MockConnection()
        nif.logger = log

        with self.assertRaises(PluginException):
            nif._check_and_compile_query("[")