Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(token): call sys.exit if server is not reachable #125

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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