| 1 |
import unittest |
| 2 |
from unittest.mock import mock_open, patch |
| 3 |
|
| 4 |
import pytest |
| 5 |
import responses |
| 6 |
from requests.models import Response |
| 7 |
|
| 8 |
from censys.common.exceptions import CensysException, CensysExceptionMapper |
| 9 |
from censys.search.v2.api import CensysSearchAPIv2 |
| 10 |
from tests.search.v1.test_api import ACCOUNT_JSON |
| 11 |
from tests.utils import V1_URL, CensysTestCase |
| 12 |
|
| 13 |
SearchExceptionParams = [(code, exception) for code, exception in CensysExceptionMapper.SEARCH_EXCEPTIONS.items()] |
| 14 |
|
| 15 |
|
| 16 |
class CensysSearchAPITests(CensysTestCase): |
| 17 |
api: CensysSearchAPIv2 |
| 18 |
|
| 19 |
def setup_method(self): |
| 20 |
self.setUpApi(CensysSearchAPIv2(self.api_id, self.api_secret)) |
| 21 |
|
| 22 |
@pytest.mark.parametrize(("status_code", "exception"), SearchExceptionParams) |
| 23 |
def test_get_exception_class(self, status_code, exception): |
| 24 |
response = Response() |
| 25 |
response.status_code = status_code |
| 26 |
|
| 27 |
assert self.api._get_exception_class(response) == exception |
| 28 |
|
| 29 |
def test_account_and_quota(self): |
| 30 |
self.responses.add( |
| 31 |
responses.GET, |
| 32 |
f"{V1_URL}/account", |
| 33 |
status=200, |
| 34 |
json=ACCOUNT_JSON, |
| 35 |
) |
| 36 |
results = self.api.account() |
| 37 |
assert results == ACCOUNT_JSON |
| 38 |
|
| 39 |
results = self.api.quota() |
| 40 |
assert results == ACCOUNT_JSON["quota"] |
| 41 |
|
| 42 |
|
| 43 |
@patch.dict("os.environ", {"CENSYS_API_ID": "", "CENSYS_API_SECRET": ""}) |
| 44 |
class CensysAPIBaseTestsNoSearchEnv(unittest.TestCase): |
| 45 |
@patch("builtins.open", new_callable=mock_open, read_data="[DEFAULT]") |
| 46 |
def test_no_env(self, mock_file): |
| 47 |
with pytest.raises(CensysException, match="No API ID or API secret configured."): |
| 48 |
CensysSearchAPIv2() |