Debian Codegraph @

tests/utils.py

raw
1 from typing import Optional
2
3 import pytest
4 import responses
5 from pytest_mock import MockerFixture
6
7 from censys.common.base import CensysAPIBase
8
9 BASE_URL = "https://search.censys.io/api"
10 V1_URL = BASE_URL + "/v1"
11 V2_URL = BASE_URL + "/v2"
12
13
14 class CensysTestCase:
15 api_id = "test-api-id"
16 api_secret = "test-api-secret"
17 api_key = "test-api-key"
18 cli_args = [
19 "--api-id",
20 api_id,
21 "--api-secret",
22 api_secret,
23 ]
24 asm_cli_args = [
25 "--api-key",
26 api_key,
27 ]
28 api: CensysAPIBase
29 mocker: MockerFixture
30
31 @pytest.fixture(autouse=True)
32 def _setup(self, mocker: MockerFixture):
33 self.mocker = mocker
34 mocker.patch("time.sleep", return_value=None)
35 rsps = responses.RequestsMock(assert_all_requests_are_fired=False)
36 rsps.start()
37 self.responses = rsps
38 yield
39 rsps.stop()
40 rsps.reset()
41
42 def setUpApi(self, api: CensysAPIBase): # noqa: N802
43 self.api = api
44 self.base_url = self.api._api_url
45
46 def patch_args(
47 self,
48 args: list[str],
49 search_auth: Optional[bool] = False,
50 asm_auth: Optional[bool] = False,
51 ):
52 if search_auth:
53 args.extend(self.cli_args)
54 if asm_auth:
55 args.extend(self.asm_cli_args)
56
57 self.mocker.patch("argparse._sys.argv", args)