Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4f5e757

Browse files
committed
generated file: tests/integration/test_user_routes.py
1 parent 6877114 commit 4f5e757

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import pytest
2+
from fastapi.testclient import TestClient
3+
from unittest.mock import patch, MagicMock
4+
5+
from openai_api_client.main import app
6+
from openai_api_client.dependencies.openai import OpenAIService
7+
from openai_api_client.schemas.openai import OpenAIRequest, OpenAIResponse
8+
from openai_api_client.services.openai import openai_service
9+
from openai_api_client.models.user import User
10+
from openai_api_client.schemas.user import UserCreate
11+
12+
13+
@pytest.fixture
14+
def client():
15+
return TestClient(app)
16+
17+
18+
@pytest.fixture
19+
def mock_openai():
20+
with patch("openai_api_client.dependencies.openai.openai") as mock_openai:
21+
yield mock_openai
22+
23+
24+
@pytest.fixture
25+
async def test_user(mock_user_service):
26+
user_data = UserCreate(
27+
username="testuser",
28+
email="test@example.com",
29+
password="testpassword",
30+
)
31+
user = await mock_user_service.create_user(user_data)
32+
yield user
33+
await mock_user_service.delete_user(user.id)
34+
35+
36+
class TestUserRoutes:
37+
def test_register_user_success(self, client):
38+
user_data = {
39+
"username": "newuser",
40+
"email": "newuser@example.com",
41+
"password": "testpassword",
42+
}
43+
response = client.post("/api/v1/users/register", json=user_data)
44+
assert response.status_code == 201
45+
assert response.json()["username"] == "newuser"
46+
assert response.json()["email"] == "newuser@example.com"
47+
assert response.json()["api_key"] is not None
48+
49+
def test_register_user_username_exists(self, client):
50+
user_data = {
51+
"username": "testuser",
52+
"email": "newuser@example.com",
53+
"password": "testpassword",
54+
}
55+
response = client.post("/api/v1/users/register", json=user_data)
56+
assert response.status_code == 400
57+
assert "Username already exists" in response.json()["detail"]
58+
59+
def test_register_user_email_exists(self, client):
60+
user_data = {
61+
"username": "newuser",
62+
"email": "test@example.com",
63+
"password": "testpassword",
64+
}
65+
response = client.post("/api/v1/users/register", json=user_data)
66+
assert response.status_code == 400
67+
assert "Email already exists" in response.json()["detail"]
68+
69+
def test_login_user_success(self, client, test_user):
70+
login_data = {"email": "test@example.com", "password": "testpassword"}
71+
response = client.post("/api/v1/users/login", json=login_data)
72+
assert response.status_code == 200
73+
assert response.json()["access_token"] is not None
74+
assert response.json()["token_type"] == "bearer"
75+
76+
def test_login_user_invalid_credentials(self, client, test_user):
77+
login_data = {"email": "test@example.com", "password": "wrongpassword"}
78+
response = client.post("/api/v1/users/login", json=login_data)
79+
assert response.status_code == 401
80+
assert "Invalid credentials" in response.json()["detail"]
81+
82+
def test_get_current_user_success(self, client, test_user):
83+
response = client.get(
84+
"/api/v1/users/me", headers={"Authorization": f"Bearer {test_user.api_key}"}
85+
)
86+
assert response.status_code == 200
87+
assert response.json()["username"] == "testuser"
88+
assert response.json()["email"] == "test@example.com"
89+
assert response.json()["api_key"] is not None

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /