Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ dev = [
"pytest-cov>=4",
"pytest-mock>=3.10.0,<4",
"responses>=0.23.1",
"parameterized>=0.9.0,<1",
"mypy>=1.5.1,<2",
"types-requests>=2.29.0.0,<3",
]
Expand Down Expand Up @@ -118,5 +117,11 @@ namespace_packages = true
explicit_package_bases = true

[[tool.mypy.overrides]]
module = ["parameterized", "rich", "attr"]
module = ["rich", "attr"]
ignore_missing_imports = true

[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = "--cov -rs"
filterwarnings = ["ignore:.*:DeprecationWarning:.*"]
python_classes = ["Test*", "*Tests", "*Test"]
9 changes: 0 additions & 9 deletions pytest.ini

This file was deleted.

19 changes: 6 additions & 13 deletions tests/asm/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import pytest
import responses
from parameterized import parameterized
from pytest_mock import MockerFixture
from requests.models import Response

Expand All @@ -20,12 +19,6 @@
class CensysAPIBaseTestsNoAsmEnv(unittest.TestCase):
@pytest.fixture(autouse=True)
def __inject_fixtures(self, mocker: MockerFixture):
"""Injects fixtures into the test case.

Args:
mocker (MockerFixture): pytest-mock fixture.
"""
# Inject mocker fixture
self.mocker = mocker

def setUp(self):
Expand All @@ -43,14 +36,14 @@ def test_no_env(self):
CensysAsmAPI()


class CensysAsmAPITests(CensysTestCase):
AsmExceptionParams = [(code, exception) for code, exception in CensysExceptionMapper.ASM_EXCEPTIONS.items()]
AsmExceptionParams = [(code, exception) for code, exception in CensysExceptionMapper.ASM_EXCEPTIONS.items()]

def setUp(self):
super().setUp()

class CensysAsmAPITests(CensysTestCase):
def setup_method(self):
self.setUpApi(CensysAsmAPI(self.api_id))

@parameterized.expand(AsmExceptionParams)
@pytest.mark.parametrize(("status_code", "exception"), AsmExceptionParams)
def test_get_exception_class(self, status_code, exception):
# Mock
mock_response = self.mocker.patch("requests.models.Response.json")
Expand All @@ -73,7 +66,7 @@ def test_exception_repr(self):
repr(exception) == "404 (Error Code: 10014), Unable to Find Seed. [{id: 999}]" # noqa: FS003
)

@parameterized.expand([("assets")])
@pytest.mark.parametrize("keyword", ["assets"])
def test_page_keywords(self, keyword):
# Mock
page_json = {
Expand Down
45 changes: 18 additions & 27 deletions tests/asm/test_assets.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import unittest
from urllib.parse import quote

import pytest
from parameterized import parameterized_class
from pytest_mock import MockerFixture

from censys.asm.client import AsmClient
Expand Down Expand Up @@ -32,37 +30,30 @@
TEST_TAG_COLOR = "#4287f5"
TEST_INVALID_TAG_COLOR = "4287f5"

ASSET_PARAMS = [
("hosts", "3.12.122.3"),
(
"certificates",
"0006afc1ddc8431aa57c812adf028ab4f168b25bf5f06e94af86edbafa88dfe0",
),
("domains", "amazonaws.com"),
("subdomains", "s3.amazonaws.com"),
("web_entities", "www.amazon.com:443"),
("object_storages", "https://censys-python.s3.us-east-2.amazonaws.com/"),
]

@parameterized_class(
("asset_type", "test_asset_id"),
[
("hosts", "3.12.122.3"),
(
"certificates",
"0006afc1ddc8431aa57c812adf028ab4f168b25bf5f06e94af86edbafa88dfe0",
),
("domains", "amazonaws.com"),
("subdomains", "s3.amazonaws.com"),
("web_entities", "www.amazon.com:443"),
("object_storages", "https://censys-python.s3.us-east-2.amazonaws.com/"),
],
)
class AssetsUnitTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def __inject_fixtures(self, mocker: MockerFixture):
"""Injects fixtures into the test case.

Args:
mocker (MockerFixture): pytest-mock fixture.
"""
# Inject mocker fixture
self.mocker = mocker

class AssetsUnitTest:
"""Unit tests for Host, Certificate, and Domain APIs."""

def setUp(self):
@pytest.fixture(autouse=True, params=ASSET_PARAMS, ids=[p[0] for p in ASSET_PARAMS])
def _setup(self, request, mocker: MockerFixture):
self.asset_type, self.test_asset_id = request.param
self.mocker = mocker
mocker.patch("time.sleep", return_value=None)
self.client = AsmClient()
self.resource_type = ASSET_TYPE if self.asset_type != "subdomains" else SUBDOMAIN_ASSET_TYPE
return

def get_asset_accessor(self):
return getattr(self.client, self.asset_type)
Expand Down
3 changes: 1 addition & 2 deletions tests/asm/test_beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@


class BetaUnitTest(CensysTestCase):
def setUp(self):
super().setUp()
def setup_method(self):
self.client = Beta(self.api_key)

def test_get_logbook_data(self):
Expand Down
3 changes: 1 addition & 2 deletions tests/asm/test_clouds.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@


class CloudsUnitTest(CensysTestCase):
def setUp(self):
super().setUp()
def setup_method(self):
self.client = AsmClient(self.api_key)

def test_get_host_counts(self):
Expand Down
40 changes: 20 additions & 20 deletions tests/asm/test_inventory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
import responses
from parameterized import parameterized

from censys.asm.inventory import InventorySearch

Expand Down Expand Up @@ -36,11 +36,11 @@
class InventoryTests(CensysTestCase):
api: InventorySearch

def setUp(self):
super().setUp()
def setup_method(self):
self.setUpApi(InventorySearch(self.api_key))

@parameterized.expand(
@pytest.mark.parametrize(
("kwargs", "params"),
[
(
{
Expand All @@ -59,7 +59,7 @@ def setUp(self):
},
"?workspaces=1&workspaces=2&query=test&pageSize=50",
),
]
],
)
def test_search(self, kwargs, params):
mock_request = self.mocker.patch("censys.asm.api.CensysAsmAPI.get_workspace_id")
Expand All @@ -79,21 +79,20 @@ def test_search(self, kwargs, params):
# Assertions
assert res == TEST_INVENTORY_SEARCH_JSON

@parameterized.expand(
@pytest.mark.parametrize(
"kwargs",
[
(
{
"workspaces": ["1", "2"],
"query": "test",
"aggregation": {
"field": "test",
"size": 50,
"sort": "test",
"order": "test",
},
{
"workspaces": ["1", "2"],
"query": "test",
"aggregation": {
"field": "test",
"size": 50,
"sort": "test",
"order": "test",
},
),
]
},
],
)
def test_aggregate(self, kwargs):
# Setup response
Expand All @@ -111,15 +110,16 @@ def test_aggregate(self, kwargs):
# Assertions
assert res == TEST_INVENTORY_AGGREGATE_JSON

@parameterized.expand(
@pytest.mark.parametrize(
("kwargs", "params"),
[
(
{
"fields": ["host.services.name", "host.services.port"],
},
"?fields=host.services.name&fields=host.services.port",
),
]
],
)
def test_fields(self, kwargs, params):
# Setup response
Expand Down
30 changes: 17 additions & 13 deletions tests/asm/test_risks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import urllib.parse

import pytest
import responses
from parameterized import parameterized
from responses import matchers

from censys.asm.risks import Risks
Expand Down Expand Up @@ -76,11 +76,11 @@
class RisksTests(CensysTestCase):
api: Risks

def setUp(self):
super().setUp()
def setup_method(self):
self.setUpApi(Risks(self.api_key))

@parameterized.expand(
@pytest.mark.parametrize(
("kwargs", "params"),
[
({}, ""),
(
Expand All @@ -92,7 +92,7 @@ def setUp(self):
{"cursor": "eyJhZnRlcklEIjo3NzQwLCJsaW1pdCI6MTAwfQ=="},
"?cursor=eyJhZnRlcklEIjo3NzQwLCJsaW1pdCI6MTAwfQ==",
),
]
],
)
def test_get_risk_events(self, kwargs, params):
# Setup response
Expand All @@ -107,12 +107,13 @@ def test_get_risk_events(self, kwargs, params):
# Assertions
assert res == TEST_RISK_EVENTS_JSON

@parameterized.expand(
@pytest.mark.parametrize(
("kwargs", "params"),
[
({}, ""),
({"include_events": True}, "?includeEvents=True"),
({"include_events": False}, "?includeEvents=False"),
]
],
)
def test_get_risk_instances(self, kwargs, params):
# Setup response
Expand Down Expand Up @@ -174,12 +175,13 @@ def test_search_risk_instances(self):
# Assertions
assert res == TEST_RISK_TYPE_JSON

@parameterized.expand(
@pytest.mark.parametrize(
("kwargs", "params"),
[
({"risk_instance_id": 0}, "0"),
({"risk_instance_id": 1, "include_events": True}, "1?includeEvents=True"),
({"risk_instance_id": 2, "include_events": False}, "2?includeEvents=False"),
]
],
)
def test_get_risk_instance(self, kwargs, params):
# Setup response
Expand Down Expand Up @@ -215,15 +217,16 @@ def test_patch_risk_instance(self):
# Assertions
assert res == TEST_PATCH_RISK_INSTANCE_JSON

@parameterized.expand(
@pytest.mark.parametrize(
("kwargs", "params"),
[
({}, ""),
({"include_events": True}, "?includeEvents=True"),
({"include_events": False}, "?includeEvents=False"),
({"sort": ["severity", "type:asc"]}, "?sort=severity&sort=type:asc"),
({"page": 1, "limit": 10000}, "?page=1&limit=10000"),
({"page": 10}, "?page=10"),
]
],
)
def test_get_risk_types(self, kwargs, params):
# Setup response
Expand All @@ -238,7 +241,8 @@ def test_get_risk_types(self, kwargs, params):
# Assertions
assert res == TEST_RISK_TYPES_JSON

@parameterized.expand(
@pytest.mark.parametrize(
("kwargs", "params"),
[
({"risk_type": TEST_RISK_TYPE}, ESCAPED_TEST_RISK_TYPE),
(
Expand All @@ -249,7 +253,7 @@ def test_get_risk_types(self, kwargs, params):
{"risk_type": TEST_RISK_TYPE, "include_events": False},
ESCAPED_TEST_RISK_TYPE + "?includeEvents=False",
),
]
],
)
def test_get_risk_type(self, kwargs, params):
# Setup respnonse
Expand Down
Loading
Loading