|
| 1 | +import os |
| 2 | +import asyncio |
| 3 | +import vulncheck_sdk.aio as vcaio |
| 4 | +from vulncheck_sdk.aio.api.endpoints_api import EndpointsApi |
| 5 | +from vulncheck_sdk.aio.api.indices_api import IndicesApi |
| 6 | +from vulncheck_sdk.aio.exceptions import ApiException, UnauthorizedException |
| 7 | + |
| 8 | +DEFAULT_HOST = "https://api.vulncheck.com" |
| 9 | +DEFAULT_API = DEFAULT_HOST + "/v3" |
| 10 | +API_TOKEN = os.environ.get("VULNCHECK_API_TOKEN", "") |
| 11 | + |
| 12 | +# --- Helpers --- |
| 13 | + |
| 14 | + |
| 15 | +async def _get_http_status(func, *args) -> int: |
| 16 | + """Awaits the SDK call and returns the status code, handling common exceptions.""" |
| 17 | + try: |
| 18 | + api_response = await func(*args) |
| 19 | + return api_response.status_code |
| 20 | + except UnauthorizedException as e: |
| 21 | + print(f"Not authorized for this endpoint: {e}") |
| 22 | + return 401 |
| 23 | + except ApiException as e: |
| 24 | + status = getattr(e, "status", 500) |
| 25 | + print(f"ApiException (Status {status}) when calling {func.__name__}: {e}") |
| 26 | + return status |
| 27 | + except Exception as e: |
| 28 | + print(f"Unexpected error in {func.__name__}: {e}") |
| 29 | + return 500 |
| 30 | + |
| 31 | + |
| 32 | +# --- EndpointsApi Tests --- |
| 33 | + |
| 34 | + |
| 35 | +async def test_openapi_get(api: EndpointsApi): |
| 36 | + print(f"Testing test_openapi_get") |
| 37 | + status = await _get_http_status(api.openapi_get_with_http_info) |
| 38 | + assert status == 200 |
| 39 | + |
| 40 | + |
| 41 | +async def test_entitlements_get(api: EndpointsApi): |
| 42 | + print(f"Testing test_entitlements_get") |
| 43 | + status = await _get_http_status(api.entitlements_get_with_http_info) |
| 44 | + assert status == 200 |
| 45 | + |
| 46 | + |
| 47 | +async def test_purl_get(api: EndpointsApi): |
| 48 | + print(f"Testing test_purl_get") |
| 49 | + status = await _get_http_status( |
| 50 | + api. purl_get_with_http_info, "pkg:hex/[email protected]" |
| 51 | + ) |
| 52 | + assert status == 200 |
| 53 | + |
| 54 | + |
| 55 | +async def test_index_get(api: EndpointsApi): |
| 56 | + print(f"Testing test_index_get") |
| 57 | + status = await _get_http_status(api.index_get_with_http_info) |
| 58 | + assert status == 200 |
| 59 | + |
| 60 | + |
| 61 | +async def test_cpe_get(api: EndpointsApi): |
| 62 | + print(f"Testing test_cpe_get") |
| 63 | + status = await _get_http_status( |
| 64 | + api.cpe_get_with_http_info, "cpe:/a:microsoft:internet_explorer:8.0.6001:beta" |
| 65 | + ) |
| 66 | + assert status == 200 |
| 67 | + |
| 68 | + |
| 69 | +async def test_backup_get(api: EndpointsApi): |
| 70 | + print(f"Testing test_backup_get") |
| 71 | + status = await _get_http_status(api.backup_get_with_http_info) |
| 72 | + assert status == 200 |
| 73 | + |
| 74 | + |
| 75 | +async def test_backup_index_get(api: EndpointsApi): |
| 76 | + print(f"Testing test_backup_index_get") |
| 77 | + status = await _get_http_status(api.backup_index_get_with_http_info, "") |
| 78 | + assert status == 200 |
| 79 | + |
| 80 | + |
| 81 | +async def test_pdns_vulncheck_c2_get(api: EndpointsApi): |
| 82 | + print(f"Testing test_pdns_vulncheck_c2_get") |
| 83 | + status = await _get_http_status(api.pdns_vulncheck_c2_get_with_http_info, "") |
| 84 | + assert status == 200 |
| 85 | + |
| 86 | + |
| 87 | +async def test_rules_initial_access_type_get(api: EndpointsApi): |
| 88 | + print(f"Testing test_rules_initial_access_type_get") |
| 89 | + status = await _get_http_status( |
| 90 | + api.rules_initial_access_type_get_with_http_info, "suricata" |
| 91 | + ) |
| 92 | + assert status == 200 |
| 93 | + |
| 94 | + |
| 95 | +# --- IndicesApi Tests --- |
| 96 | + |
| 97 | + |
| 98 | +async def test_all_indices(indices: IndicesApi): |
| 99 | + targets = [ |
| 100 | + "index_nist_nvd2_get_with_http_info", |
| 101 | + "index_vulncheck_kev_get_with_http_info", |
| 102 | + "index_ipintel3d_get_with_http_info", |
| 103 | + "index_cisa_kev_get_with_http_info", |
| 104 | + ] |
| 105 | + |
| 106 | + for name in targets: |
| 107 | + method = getattr(indices, name) |
| 108 | + # Passing strings for limit/page to satisfy Pydantic validation |
| 109 | + status = await _get_http_status(method, 1, 1) |
| 110 | + print(f"Testing {name}: Result {status}") |
| 111 | + assert status == 200 |
| 112 | + |
| 113 | + |
| 114 | +# --- Orchestrator --- |
| 115 | + |
| 116 | + |
| 117 | +async def main(): |
| 118 | + if not API_TOKEN: |
| 119 | + print("Warning: VULNCHECK_API_TOKEN is not set.") |
| 120 | + |
| 121 | + config = vcaio.Configuration(host=DEFAULT_API) |
| 122 | + config.api_key["Bearer"] = API_TOKEN |
| 123 | + |
| 124 | + # The 'async with' ensures the ClientSession is closed |
| 125 | + async with vcaio.ApiClient(config) as client: |
| 126 | + endpoints_api = vcaio.EndpointsApi(client) |
| 127 | + indices_api = vcaio.IndicesApi(client) |
| 128 | + |
| 129 | + print("--- Running Tests ---") |
| 130 | + asyncio.gather( |
| 131 | + test_openapi_get(endpoints_api), |
| 132 | + test_entitlements_get(endpoints_api), |
| 133 | + test_purl_get(endpoints_api), |
| 134 | + test_index_get(endpoints_api), |
| 135 | + test_cpe_get(endpoints_api), |
| 136 | + test_backup_get(endpoints_api), |
| 137 | + test_backup_index_get(endpoints_api), |
| 138 | + test_pdns_vulncheck_c2_get(endpoints_api), |
| 139 | + test_rules_initial_access_type_get(endpoints_api), |
| 140 | + ) |
| 141 | + |
| 142 | + await test_all_indices(indices_api) |
| 143 | + |
| 144 | + # --- CRITICAL FIX FOR "UNCLOSED CONNECTOR" --- |
| 145 | + # aiohttp requires a small window of time to allow the |
| 146 | + # underlying SSL/TCP transports to finish closing. |
| 147 | + await asyncio.sleep(0.250) |
| 148 | + print("--- All Tests Completed Successfully ---") |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + try: |
| 153 | + asyncio.run(main()) |
| 154 | + except KeyboardInterrupt: |
| 155 | + pass |
0 commit comments