From db5158a3098c88619487ef7b85647206b753d75c Mon Sep 17 00:00:00 2001 From: Marion Deveaud Date: Wed, 14 Feb 2024 18:30:53 +0100 Subject: [PATCH] fix(token): call sys.exit if server is not reachable Properly test all reponse scenarios Closes #122 --- fossology/__init__.py | 3 ++- tests/test_users.py | 28 +++++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/fossology/__init__.py b/fossology/__init__.py index 68c0aa0..26e6a13 100644 --- a/fossology/__init__.py +++ b/fossology/__init__.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: MIT import logging +import sys from datetime import date, timedelta import requests @@ -77,7 +78,7 @@ def fossology_token( description = "Error while generating new token" raise FossologyApiError(description, response) except requests.exceptions.ConnectionError as error: - exit(f"Server {url} does not seem to be running or is unreachable: {error}") + sys.exit(f"Server {url} does not seem to be running or is unreachable: {error}") class Fossology( diff --git a/tests/test_users.py b/tests/test_users.py index 359285e..a60beb9 100644 --- a/tests/test_users.py +++ b/tests/test_users.py @@ -47,16 +47,11 @@ def test_generate_token_too_long(foss_server: str): @responses.activate -def test_generate_token_errors(foss_server: str): +def test_generate_token_if_receiving_connection_error_exits(foss_server: str): responses.add( responses.POST, f"{foss_server}/api/v1/tokens", - body=requests.exceptions.ConnectionError(), - ) - responses.add( - responses.POST, - f"{foss_server}/api/v1/tokens", - status=404, + body=requests.exceptions.ConnectionError("Test Exception"), ) with pytest.raises(SystemExit) as excinfo: fossology_token( @@ -66,10 +61,21 @@ def test_generate_token_errors(foss_server: str): secrets.token_urlsafe(8), token_expire=str(date.today() - timedelta(days=1)), ) - assert ( - f"Server {foss_server} does not seem to be running or is unreachable" - in str(excinfo.value) - ) + assert ( + f"Server {foss_server} does not seem to be running or is unreachable: Test Exception" + in str(excinfo.value) + ) + + +@responses.activate +def test_generate_token_if_receiving_authentication_error_raises_api_error_( + foss_server: str, +): + responses.add( + responses.POST, + f"{foss_server}/api/v1/tokens", + status=404, + ) with pytest.raises(AuthenticationError) as excinfo: fossology_token( foss_server,