def setup(self) -> None:
     log = logging.getLogger("Test")
     test_conn = Connection()
     test_action = AgentsAction()
     test_conn.logger = log
     test_action.logger = log
     try:
         with open("../tests/agents_action.json") as file:
             test_json = json.loads(file.read()).get("body")
             connection_params = test_json.get("connection")
             action_params = test_json.get("input")
     except Exception as e:
         self.fail(
             "Likely could not find tests in test directory. Generate and fill out samples to fix this."
         )
     return action_params, connection_params, test_action, test_conn, log
    def test_integration_agents_summary(self):
        """
        This is an integration test that will connect to the services your plugin uses. It should be used
        as the basis for tests below that can run independent of a "live" connection.

        This test assumes a normal plugin structure with a /tests directory. In that /tests directory should
        be json samples that contain all the data needed to run this test. To generate samples run:

        icon-plugin generate samples

        """

        log = logging.getLogger("Test")
        test_conn = Connection()
        test_action = AgentsSummary()

        test_conn.logger = log
        test_action.logger = log

        try:
            with open("../tests/agents_summary.json") as file:
                test_json = json.loads(file.read()).get("body")
                connection_params = test_json.get("connection")
                action_params = test_json.get("input")
        except Exception as e:
            message = """
            Could not find or read sample tests from /tests directory
            
            An exception here likely means you didn't fill out your samples correctly in the /tests directory 
            Please use 'icon-plugin generate samples', and fill out the resulting test files in the /tests directory
            """
            self.fail(message)

        test_conn.connect(connection_params)
        test_action.connection = test_conn
        results = test_action.run(action_params)

        self.assertEqual([
            "decommissioned", "infected", "out_of_date", "online", "total",
            "up_to_date"
        ], list(results.keys()))
Exemplo n.º 3
0
    def test_integration_threats_fetch_file(self):
        """
        This is an integration test that will connect to the services your plugin uses. It should be used
        as the basis for tests below that can run independent of a "live" connection.

        This test assumes a normal plugin structure with a /tests directory. In that /tests directory should
        be json samples that contain all the data needed to run this test. To generate samples run:

        icon-plugin generate samples

        """

        log = logging.getLogger("Test")
        test_conn = Connection()
        test_action = ThreatsFetchFile()

        test_conn.logger = log
        test_action.logger = log

        try:
            with open("../tests/threats_fetch_file.json") as file:
                test_json = json.loads(file.read()).get("body")
                connection_params = test_json.get("connection")
                action_params = test_json.get("input")
        except Exception as e:
            message = """
            Could not find or read sample tests from /tests directory
            
            An exception here likely means you didn't fill out your samples correctly in the /tests directory 
            Please use 'icon-plugin generate samples', and fill out the resulting test files in the /tests directory
            """
            self.fail(message)


        test_conn.connect(connection_params)
        test_action.connection = test_conn
        results = test_action.run(action_params)

        self.assertIsNotNone(results.get("file"))
    def test_integration_get_threats(self, mockSend):
        """
        TODO: Manually validate results

        Because the send function is essentially an endless loop, there's no way to validate the output from
        that in an elegant way. Really this test is just making sure no exceptions are thrown.

        The bulk of your logic for your trigger should not be in the run loop and should be tested with subsequent
        tests.
        """
        log = logging.getLogger("Test")

        try:
            with open("../tests/get_threats.json") as f:
                data = json.load(f)
                connection_params = data.get("body").get("connection")
                trigger_params = data.get("body").get("input")
        except Exception as e:
            message = """
            Could not find or read sample tests from /tests directory
            
            An exception here likely means you didn't fill out your samples correctly in the /tests directory 
            Please use 'icon-plugin generate samples', and fill out the resulting test files in the /tests directory
            """
            self.fail(message)

        test_connection = Connection()
        test_connection.logger = log
        test_connection.connect(connection_params)

        test_email_received = GetThreats()
        test_email_received.connection = test_connection
        test_email_received.logger = log

        test_email_received.run(trigger_params)

        self.fail()  # If we made it this far, the run loop failed somehow