Skip to content

Commit

Permalink
fix(token): call sys.exit if server is not reachable
Browse files Browse the repository at this point in the history
Properly test all reponse scenarios
Closes #122
  • Loading branch information
deveaud-m committed Feb 14, 2024
1 parent 6211083 commit db5158a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
3 changes: 2 additions & 1 deletion fossology/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: MIT

import logging
import sys
from datetime import date, timedelta

import requests
Expand Down Expand Up @@ -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(
Expand Down
28 changes: 17 additions & 11 deletions tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down

0 comments on commit db5158a

Please sign in to comment.