Skip to content

Commit

Permalink
Merge pull request #125 from fossology/fix/sys-exit
Browse files Browse the repository at this point in the history
fix(token): call sys.exit if server is not reachable
  • Loading branch information
deveaud-m authored Feb 15, 2024
2 parents 6211083 + 899c006 commit f8908ee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ Using the API
.. code:: python
from fossology import fossology_token
from fossology.obj import TokenScope
from fossology.enum import TokenScope
FOSSOLOGY_SERVER = "https://fossology.example.com/"
FOSSOLOGY_SERVER = "https://fossology.example.com/repo" # Note the absense of the trailing slash, otherwise the token generation will fail
FOSSOLOGY_USER = "fossy"
FOSSOLOGY_PASSWORD = "fossy"
TOKEN_NAME = "fossy_token"
Expand Down
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 f8908ee

Please sign in to comment.