Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /tests /test_api_auth_simple.py
| """Simple API Auth Tests. | |
| Target: +5% Coverage durch Auth-Tests. | |
| """ | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| from api.auth import create_access_token | |
| from api.main import app | |
| from database.models import init_db | |
| init_db() | |
| client = TestClient(app) | |
| class TestTokenCreation: | |
| """Tests for token creation.""" | |
| def test_create_access_token(self): | |
| """Test creating an access token.""" | |
| token = create_access_token({"sub": "test@example.com"}) | |
| assert token is not None | |
| assert isinstance(token, str) | |
| assert len(token) > 0 | |
| def test_create_access_token_with_data(self): | |
| """Test creating token with user data.""" | |
| data = {"sub": "user@example.com", "role": "admin"} | |
| token = create_access_token(data) | |
| assert token is not None | |
| class TestAuthEndpoints: | |
| """Tests for auth endpoints.""" | |
| def test_login_without_credentials(self): | |
| """Test login without credentials returns error.""" | |
| response = client.post("/auth/login", json={}) | |
| # Should return 400, 401, 403, or 422 | |
| assert response.status_code in [400, 401, 403, 404, 422] | |
| def test_login_with_invalid_credentials(self): | |
| """Test login with invalid credentials.""" | |
| response = client.post("/auth/login", json={"username": "invalid", "password": "wrong"}) | |
| assert response.status_code in [400, 401, 403, 404, 422] | |
| class TestProtectedEndpoints: | |
| """Tests for protected endpoints behavior.""" | |
| def test_scans_without_auth(self): | |
| """Test /scans without auth returns 401.""" | |
| response = client.get("/scans") | |
| assert response.status_code in [200, 401, 403, 404] | |
| def test_findings_without_auth(self): | |
| """Test /findings without auth returns 401.""" | |
| response = client.get("/findings") | |
| assert response.status_code in [200, 401, 403, 404] | |
| def test_users_me_without_auth(self): | |
| """Test /users/me without auth returns 401.""" | |
| response = client.get("/users/me") | |
| assert response.status_code in [200, 401, 403, 404] | |
| class TestWithAuthToken: | |
| """Tests with valid auth token.""" | |
| def test_token_structure(self): | |
| """Test token has correct structure (JWT).""" | |
| token = create_access_token({"sub": "test@example.com"}) | |
| # JWT tokens have 3 parts separated by dots | |
| parts = token.split(".") | |
| assert len(parts) == 3 | |
| def test_request_with_auth_header(self): | |
| """Test request with Authorization header.""" | |
| token = create_access_token({"sub": "test@example.com"}) | |
| headers = {"Authorization": f"Bearer {token}"} | |
| response = client.get("/scans", headers=headers) | |
| # Token may be valid but user might not exist in DB; rate limit may also apply | |
| assert response.status_code in [200, 401, 403, 404, 429] | |
Xet Storage Details
- Size:
- 2.86 kB
- Xet hash:
- fdb885bee0063f833fbe9f6d565e33bed734728495f7313b9b5cc1083bfa4546
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.