Debian Codegraph @

tests/asm/test_api.py

raw
1 import json
2 import unittest
3
4 import pytest
5 import responses
6 from pytest_mock import MockerFixture
7 from requests.models import Response
8
9 from censys.asm.api import CensysAsmAPI
10 from censys.common.exceptions import (
11 CensysAsmException,
12 CensysException,
13 CensysExceptionMapper,
14 )
15
16 from ..utils import CensysTestCase
17
18
19 class CensysAPIBaseTestsNoAsmEnv(unittest.TestCase):
20 @pytest.fixture(autouse=True)
21 def __inject_fixtures(self, mocker: MockerFixture):
22 self.mocker = mocker
23
24 def setUp(self):
25 self.responses = responses.RequestsMock()
26 self.responses.start()
27 self.mocker.patch.dict("os.environ", {"CENSYS_ASM_API_KEY": ""})
28 self.mock_open = self.mocker.patch("builtins.open", new_callable=self.mocker.mock_open, read_data="[DEFAULT]")
29
30 self.addCleanup(self.responses.stop)
31 self.addCleanup(self.responses.reset)
32
33 def test_no_env(self):
34 self.mocker.patch("builtins.open", new_callable=self.mock_open, read_data="[DEFAULT]")
35 with pytest.raises(CensysException, match="No ASM API key configured."):
36 CensysAsmAPI()
37
38
39 AsmExceptionParams = [(code, exception) for code, exception in CensysExceptionMapper.ASM_EXCEPTIONS.items()]
40
41
42 class CensysAsmAPITests(CensysTestCase):
43 def setup_method(self):
44 self.setUpApi(CensysAsmAPI(self.api_id))
45
46 @pytest.mark.parametrize(("status_code", "exception"), AsmExceptionParams)
47 def test_get_exception_class(self, status_code, exception):
48 # Mock
49 mock_response = self.mocker.patch("requests.models.Response.json")
50
51 response = Response()
52 mock_response.return_value = {"errorCode": status_code}
53 # Actual call/assertion
54 assert CensysAsmAPI()._get_exception_class(response) == exception
55
56 def test_exception_repr(self):
57 # Actual call
58 exception = CensysAsmException(
59 404,
60 "Unable to Find Seed",
61 error_code=10014,
62 details="[{id: 999}]", # noqa: FS003
63 )
64 # Assertion
65 assert (
66 repr(exception) == "404 (Error Code: 10014), Unable to Find Seed. [{id: 999}]" # noqa: FS003
67 )
68
69 @pytest.mark.parametrize("keyword", ["assets"])
70 def test_page_keywords(self, keyword):
71 # Mock
72 page_json = {
73 "pageNumber": 1,
74 "totalPages": 2,
75 keyword: ["test1", "test2", "test3"],
76 }
77 second_page = ["test4", "test5"]
78
79 def keyword_callback(request):
80 temp_json = page_json.copy()
81 page_number = int(request.params.get("pageNumber"))
82 temp_json["pageNumber"] = page_number
83 if page_number > 1:
84 temp_json[keyword] = second_page
85 return (200, {}, json.dumps(temp_json))
86
87 # Actual call
88 self.responses.add_callback(
89 responses.GET,
90 f"{self.base_url}/{keyword}?pageNumber=",
91 callback=keyword_callback,
92 )
93
94 res = list(self.api._get_page(f"/{keyword}"))
95 # Assertions
96 assert res == page_json[keyword] + second_page
97
98 def test_get_workspace_id(self):
99 self.responses.add(
100 responses.GET,
101 f"{self.base_url}/integrations/v1/account",
102 status=200,
103 json={"workspaceId": "test-workspace-id"},
104 )
105
106 # Actual call
107 res = self.api.get_workspace_id()
108
109 # Assertions
110 assert res == "test-workspace-id"