Skip to content

Commit 9f098fd

Browse files
Feat: First version of tests
1 parent a823b55 commit 9f098fd

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

tests/pytest.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import pytest
2+
import os
3+
from isduba import Client
4+
from isduba.api.default import get_about, get_documents
5+
from isduba.models import WebAboutInfo
6+
from isduba.types import Response as ISDuBAResponse
7+
8+
# Configuration (ToDo: Make configurable)
9+
BASE_URL = "http://localhost:5173"
10+
TEST_USERNAME = "test-user"
11+
TEST_PASSWORD = "test-user"
12+
13+
@pytest.fixture
14+
def client():
15+
"""Fixture to create a client instance."""
16+
return Client(base_url=BASE_URL)
17+
18+
@pytest.fixture
19+
def authenticated_client(client):
20+
"""Fixture to create an authenticated client instance."""
21+
client.login(username=TEST_USERNAME, password=TEST_PASSWORD)
22+
yield client
23+
24+
def test_get_about_sync(authenticated_client):
25+
"""Test the synchronous get_about endpoint."""
26+
result = get_about.sync(client=authenticated_client)
27+
28+
assert isinstance(result, WebAboutInfo) # Validate result type (WebAboutInfo)
29+
assert hasattr(result, "version") # Ensure version exists
30+
assert isinstance(result.version, str) # Validate version type (string)
31+
32+
def test_get_about_sync_detailed(authenticated_client):
33+
"""Test the synchronous detailed get_about endpoint."""
34+
response = get_about.sync_detailed(client=authenticated_client)
35+
36+
assert isinstance(response, ISDuBAResponse) # Validate response type (ISDuBAResponse)
37+
assert response.status_code == 200 # Ensure request was successful
38+
assert isinstance(response.parsed, WebAboutInfo) # Validate parsed type (WebAboutInfo)
39+
assert hasattr(response.parsed, "version") # Ensure version exists
40+
@pytest.mark.asyncio
41+
42+
async def test_get_about_asyncio(authenticated_client):
43+
"""Test the asynchronous get_about endpoint."""
44+
result = await get_about.asyncio(client=authenticated_client) # log in as user
45+
46+
assert isinstance(result, WebAboutInfo) # validate result type (WebAboutInfo)
47+
assert hasattr(result, "version") # Ensure version exists
48+
assert isinstance(result.version, str) # Validate version type (string)
49+
50+
def test_get_documents_sync(authenticated_client):
51+
"""Test the synchronous get_documents endpoint with query parameters."""
52+
result = get_documents.sync(
53+
client=authenticated_client,
54+
advisories=True,
55+
count=1,
56+
orders="-critical",
57+
limit=10,
58+
offset=0
59+
)
60+
61+
assert isinstance(result.documents, list) # Ensure result.documents is a list
62+
63+
if result.documents: # New instance means list is empty!
64+
assert "id" in result.documents[0].additional_properties # Ensure the documents have an id
65+
assert "title" in result.documents[0].additional_properties # Ensure the documents have a title
66+
67+
def test_get_documents_search(authenticated_client):
68+
"""Test get_documents with a search query."""
69+
result = get_documents.sync(
70+
client=authenticated_client,
71+
query='"csaf" search _clientSearch as'
72+
)
73+
74+
assert isinstance(result.documents, list)
75+
if result.documents: # New instance means list is empty
76+
assert "id" in result.documents[0].additional_properties # Ensure the documents have an id
77+
assert "title" in result.documents[0].additional_properties # Ensure the documents have a title
78+
79+
def test_login_failure(client):
80+
"""Test login failure with invalid credentials."""
81+
with pytest.raises(httpx.HTTPStatusError) as exc_info: # Ensure Error is returned
82+
client.login(username=TEST_USERNAME, password="invalid_password")
83+
assert exc_info.value.response.status_code == 401 # Expect unauthorized
84+
85+
def test_get_about_unauthenticated(client):
86+
"""Test get_about endpoint without authentication (expect failure)."""
87+
result = get_about.sync(client=client)
88+
assert result is None # Expect None for unauthorized requests
89+
90+
response = get_about.sync_detailed(client=client)
91+
assert response.status_code == 401 # Expect unauthorized
92+
assert response.parsed is None

0 commit comments

Comments
 (0)