Skip to content

Test success depends on result order #654

@regulartim

Description

@regulartim

In our tests for the API, we depend on the API returning IoCs in the right order. For example:

def test_200_all_feeds(self):
        response = self.client.get("/api/feeds/advanced/")
        self.assertEqual(response.status_code, 200)
        if settings.FEEDS_LICENSE:
            self.assertEqual(response.json()["license"], settings.FEEDS_LICENSE)
        else:
            self.assertNotIn("license", response.json())

        iocs = response.json()["iocs"]
        target_ioc = next((i for i in iocs if i["value"] == self.ioc.name), None)
        self.assertIsNotNone(target_ioc)

        self.assertEqual(target_ioc["feed_type"], ["log4j", "cowrie", "heralding", "ciscoasa"])
        self.assertEqual(target_ioc["attack_count"], 1)
        self.assertEqual(target_ioc["scanner"], True)
        self.assertEqual(target_ioc["payload_request"], True)
        self.assertEqual(target_ioc["recurrence_probability"], self.ioc.recurrence_probability)
        self.assertEqual(target_ioc["expected_interactions"], self.ioc.expected_interactions)

Here the API returns all 4 IoCs we defined in ./tests/__init__.py. The success of the test now depends on which IoC is returned first, as this will be assigned to target_ioc. This is usually not a problem, as the order should not change, but I think we can find a way to do this more reliably.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomerspythonPull requests that update Python codetestsAdd or improve test cases

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions