From 6483e0d1a90886d2c195aad1e423288930110225 Mon Sep 17 00:00:00 2001 From: Luke Tainton Date: Sun, 26 Jun 2022 00:07:14 +0100 Subject: [PATCH] Add tests for broken IP/prefix API responses --- requirements-dev.txt | 1 + tests/test_ip_info.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/requirements-dev.txt b/requirements-dev.txt index fcf0dd6..df0ec7f 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -2,3 +2,4 @@ black coverage pylint pytest +requests-mock diff --git a/tests/test_ip_info.py b/tests/test_ip_info.py index ea84846..a1a8dcd 100644 --- a/tests/test_ip_info.py +++ b/tests/test_ip_info.py @@ -2,6 +2,8 @@ """MODULE: Provides test cases for app/ip_info.py.""" +import requests_mock + from app.ip_info import ( # pragma: no cover get_ip_information, get_autonomous_system_number, @@ -16,6 +18,15 @@ def test_get_ip_information() -> None: assert ip_info.get("status") == "success" and ip_info.get("query") == test_query +def test_get_ip_information_broken_api() -> None: + """TEST: ensure that None is returned if the IP API response is broken.""" + test_query = "1.2.3.4" + with requests_mock.Mocker() as mocker: + mocker.get(f"http://ip-api.com/json/{test_query}", text="error") + resp = get_ip_information(test_query) + assert not resp + + def test_get_autonomous_system_number() -> None: """TEST: ensure that AS information is parsed into AS number correctly.""" as_info = "AS5089 Virgin Media Limited" @@ -28,3 +39,15 @@ def test_get_prefix_information() -> None: autonomous_system = "AS109" prefixes = get_prefix_information(autonomous_system) assert "144.254.0.0/16" in prefixes + + +def test_get_prefix_information_broken_api() -> None: + """TEST: ensure that None is returned if the prefix API response is broken.""" + autonomous_system = "AS109" + with requests_mock.Mocker() as mocker: + mocker.get( + f"https://api.hackertarget.com/aslookup/?q={str(autonomous_system)}", + text="error", + ) + resp = get_prefix_information(autonomous_system) + assert not resp