Skip to content
Snippets Groups Projects
Commit a2d890f4 authored by Sebastian Mohr's avatar Sebastian Mohr
Browse files

Added a test for tokens creation and is_valid check

parent e51af73d
Branches
No related tags found
No related merge requests found
Pipeline #510502 passed
......@@ -22,7 +22,6 @@ lint:
- docker
cache:
<<: *pnpm_cache
rules:
- changes:
- /apps/fullstack/**/*
......
No preview for this file type
......@@ -154,7 +154,7 @@ def add(
book_id = typer.prompt("Book ID for the token", type=int)
# Save token
t = Token.from_unsafe(str(name), str(book_id), token, deployment_url)
t = Token(str(name), str(book_id), token, deployment_url)
# Save token
keyring_store.save_token(t, selected_keyring)
......
......@@ -19,10 +19,7 @@ class Token:
def from_unsafe(
cls, name: str, book_id: str, token: str, deployment_url: Optional[str] = None
) -> "Token":
"""Create a token object from unsafe input.
@deprecated is not really needed anymore with dataclasses
"""
"""Create a token object from unsafe input. I.e. optional deployment_url."""
if deployment_url is None:
deployment_url = cls.deployment_url
......@@ -54,16 +51,15 @@ class Token:
)
# Check if reponse is json type
if not response.headers.get("content-type") != "application/json":
log.warning("Token is not valid.")
return False
data = None
if response.headers.get("content-type") == "application/json":
data = response.json()
# Check if response is not 200
if response.status_code != 200:
log.debug(
f"Got status code {response.status_code} with {response.json()}"
)
log.debug(f"Got status code {response.status_code} with {data}")
return False
return True
except requests.exceptions.RequestException as e:
log.warning("Request failed. Deployment might be inaccessible.")
......
import pytest
import requests
from snip.token.storage import file_store, keyring_store
from snip.token.token import Token
class TestFileStore:
......@@ -113,3 +115,82 @@ class TestKeyringStore:
for token in tokens:
with pytest.raises(ValueError):
keyring_store.save_token(token, dummy_keyring)
class TestToken:
def test_create_token(self):
token = Token("test", "123", "asd")
assert token.name == "test"
assert token.book_id == "123"
assert token.token == "asd"
assert token.deployment_url == Token.deployment_url
def test_create_with_deployment_url(self):
token = Token("test", "123", "asd", "https://test.de")
assert token.deployment_url == "https://test.de"
def test_from_unsafe(self):
token = Token.from_unsafe("test", "123", "asd")
assert token.deployment_url == Token.deployment_url
token = Token.from_unsafe("test", "123", "asd", None)
assert token.deployment_url == Token.deployment_url
token = Token.from_unsafe("test", "123", "asd", "https://test.de")
assert token.deployment_url == "https://test.de"
def test_is_valid(self, monkeypatch):
# Mock requests
def mock_post(url, headers, verify):
return type("Response", (), {"status_code": 200, "headers": {}})
monkeypatch.setattr("requests.post", mock_post)
token = Token("test", "123", "asd")
assert token.is_valid
def test_is_invalid(self, monkeypatch):
# Invalid request i.e. 400/500
def mock_post(url, headers, verify):
class MockResponse:
def __init__(self, status_code, headers):
self.status_code = status_code
self.headers = headers
def json(self):
return {"some_key": "some_value"} # Return a mock JSON response
return MockResponse(
status_code=500, headers={"content-type": "application/json"}
)
monkeypatch.setattr("requests.post", mock_post)
token = Token("test", "123", "asd")
assert not token.is_valid
# Not a JSON response
def mock_post_not_json(url, headers, verify):
class MockResponse:
def __init__(self, status_code, headers):
self.status_code = status_code
self.headers = headers
def json(self):
raise ValueError("Not a JSON response")
return MockResponse(status_code=500, headers={"content-type": "text/html"})
monkeypatch.setattr("requests.post", mock_post_not_json)
token = Token("test", "123", "asd")
assert not token.is_valid
# Raise requests exception
def mock_post_exception(url, headers, verify):
raise requests.exceptions.RequestException("Some error")
monkeypatch.setattr("requests.post", mock_post_exception)
token = Token("test", "123", "asd")
assert not token.is_valid
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment