Skip to content
Snippets Groups Projects
Commit f24c098e authored by Karel van Klink's avatar Karel van Klink :smiley_cat: Committed by Neda Moeini
Browse files

Update LibreNMS unit tests

parent f759a865
No related branches found
No related tags found
1 merge request!253LibreNMS retries
Pipeline #88383 passed
......@@ -10,7 +10,7 @@ from gso.utils.helpers import SNMPVersion
@pytest.fixture()
def mock_get_device_success(faker):
with patch("gso.services.librenms_client.requests.get") as mock_get_device:
with patch("gso.services.librenms_client.LibreNMSClient._send_request") as mock_get_device:
mock_get_device().status_code = HTTPStatus.OK
mock_get_device().json.return_value = {
"status": "ok",
......@@ -81,14 +81,14 @@ def mock_get_device_success(faker):
@pytest.fixture()
def mock_get_device_not_found():
with patch("gso.services.librenms_client.requests.get") as mock_get_not_found:
with patch("gso.services.librenms_client.LibreNMSClient._send_request") as mock_get_not_found:
mock_get_not_found().status_code = HTTPStatus.NOT_FOUND
mock_get_not_found().json.return_value = {
"status": "error",
"message": "Device non-existent-url does not exist",
}
mock_get_not_found().raise_for_status.side_effect = HTTPError(
"404 Client Error: Not Found for url: http://librenms/devices/non-existent-url",
"404 Client Error: Not Found for url: https://librenms/devices/non-existent-url",
response=mock_get_not_found(),
)
......@@ -97,7 +97,7 @@ def mock_get_device_not_found():
@pytest.fixture()
def mock_get_device_misconfigured(faker):
with patch("gso.services.librenms_client.requests.get") as mock_get_device:
with patch("gso.services.librenms_client.LibreNMSClient._send_request") as mock_get_device:
mock_get_device().status_code = HTTPStatus.OK
mock_get_device().json.return_value = {
"status": "ok",
......@@ -169,7 +169,7 @@ def mock_get_device_misconfigured(faker):
@pytest.fixture()
def mock_get_device_unauthenticated():
with (
patch("gso.services.librenms_client.requests.get") as mock_get_unauthorized,
patch("gso.services.librenms_client.LibreNMSClient._send_request") as mock_get_unauthorized,
patch(
"gso.services.librenms_client.LibreNMSClient.get_device",
) as mock_get_device,
......@@ -177,7 +177,7 @@ def mock_get_device_unauthenticated():
mock_get_unauthorized().status_code = HTTPStatus.UNAUTHORIZED
mock_get_unauthorized().json.return_value = {"message": "Unauthenticated."}
mock_get_device.side_effect = HTTPError(
"401 Client Error: Unauthorized for url: http://librenms/devices/naughty-url",
"401 Client Error: Unauthorized for url: https://librenms/devices/naughty-url",
response=mock_get_unauthorized(),
)
......@@ -186,7 +186,7 @@ def mock_get_device_unauthenticated():
@pytest.fixture()
def mock_add_device_success():
with patch("gso.services.librenms_client.requests.post") as mock_post_device:
with patch("gso.services.librenms_client.LibreNMSClient._send_request") as mock_post_device:
mock_post_device().status_code = HTTPStatus.OK
mock_post_device().json.return_value = {
"status": "ok",
......@@ -222,14 +222,14 @@ def mock_add_device_success():
@pytest.fixture()
def mock_add_device_bad_url():
with patch("gso.services.librenms_client.requests.post") as mock_post_device:
with patch("gso.services.librenms_client.LibreNMSClient._send_request") as mock_post_device:
mock_post_device().status_code = HTTPStatus.INTERNAL_SERVER_ERROR
mock_post_device().json.return_value = {
"status": "error",
"message": "Could not ping non-existent-url (Hostname did not resolve to IP)",
}
mock_post_device().raise_for_status.side_effect = HTTPError(
"500 Server Error: Internal server error for url: http://librenms/devices",
"500 Server Error: Internal server error for url: https://librenms/devices",
response=mock_post_device(),
)
......@@ -238,14 +238,14 @@ def mock_add_device_bad_url():
@pytest.fixture()
def mock_add_device_unreachable():
with patch("gso.services.librenms_client.requests.post") as mock_post_device:
with patch("gso.services.librenms_client.LibreNMSClient._send_request") as mock_post_device:
mock_post_device().status_code = HTTPStatus.INTERNAL_SERVER_ERROR
mock_post_device().json.return_value = {
"status": "error",
"message": "Could not connect to non-existent-url, please check the snmp details and snmp reachability",
}
mock_post_device().raise_for_status.side_effect = HTTPError(
"500 Server Error: Internal server error for url: http://librenms/devices",
"500 Server Error: Internal server error for url: https://librenms/devices",
response=mock_post_device(),
)
......@@ -254,7 +254,7 @@ def mock_add_device_unreachable():
@pytest.fixture()
def mock_remove_device_success(faker):
with patch("gso.services.librenms_client.requests.delete") as mock_remove_device:
with patch("gso.services.librenms_client.LibreNMSClient._send_request") as mock_remove_device:
mock_remove_device().status_code = HTTPStatus.OK
mock_remove_device().json.return_value = {
"status": "ok",
......@@ -326,11 +326,11 @@ def mock_remove_device_success(faker):
@pytest.fixture()
def mock_remove_device_non_existent(faker):
with patch("gso.services.librenms_client.requests.delete") as mock_remove_device:
with patch("gso.services.librenms_client.LibreNMSClient._send_request") as mock_remove_device:
mock_remove_device().status_code = HTTPStatus.NOT_FOUND
mock_remove_device().json.return_value = {"status": "error", "message": "Device non-existent-url not found"}
mock_remove_device().raise_for_status.side_effect = HTTPError(
"404 Client Error: Not Found for url: http://librenms/devices/non-existent-url",
"404 Client Error: Not Found for url: https://librenms/devices/non-existent-url",
response=mock_remove_device(),
)
......@@ -353,7 +353,7 @@ def test_get_device_not_found(mock_get_device_not_found):
assert e.value.response.status_code == HTTPStatus.NOT_FOUND
assert e.value.response.json() == {"status": "error", "message": "Device non-existent-url does not exist"}
assert e.value.args[0] == "404 Client Error: Not Found for url: http://librenms/devices/non-existent-url"
assert e.value.args[0] == "404 Client Error: Not Found for url: https://librenms/devices/non-existent-url"
def test_device_exists_true(mock_get_device_success):
......@@ -376,7 +376,7 @@ def test_device_exists_bad_request(mock_get_device_unauthenticated):
assert e.value.response.status_code == HTTPStatus.UNAUTHORIZED
assert e.value.response.json() == {"message": "Unauthenticated."}
assert e.value.args[0] == "401 Client Error: Unauthorized for url: http://librenms/devices/naughty-url"
assert e.value.args[0] == "401 Client Error: Unauthorized for url: https://librenms/devices/naughty-url"
def test_add_device_success(mock_add_device_success):
......@@ -401,7 +401,7 @@ def test_add_device_bad_fqdn(mock_add_device_bad_url):
"status": "error",
"message": "Could not ping non-existent-url (Hostname did not resolve to IP)",
}
assert e.value.args[0] == "500 Server Error: Internal server error for url: http://librenms/devices"
assert e.value.args[0] == "500 Server Error: Internal server error for url: https://librenms/devices"
def test_add_device_no_ping(mock_add_device_unreachable):
......@@ -416,7 +416,7 @@ def test_add_device_no_ping(mock_add_device_unreachable):
"status": "error",
"message": "Could not connect to non-existent-url, please check the snmp details and snmp reachability",
}
assert e.value.args[0] == "500 Server Error: Internal server error for url: http://librenms/devices"
assert e.value.args[0] == "500 Server Error: Internal server error for url: https://librenms/devices"
def test_remove_device_success(mock_remove_device_success):
......@@ -435,7 +435,7 @@ def test_remove_non_existent_device(mock_remove_device_non_existent):
assert e.value.response.status_code == HTTPStatus.NOT_FOUND
assert e.value.response.json() == {"status": "error", "message": "Device non-existent-url not found"}
assert e.value.args[0] == "404 Client Error: Not Found for url: http://librenms/devices/non-existent-url"
assert e.value.args[0] == "404 Client Error: Not Found for url: https://librenms/devices/non-existent-url"
def test_validate_device_success(mock_get_device_success):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment