Files
novalon-manage-system/e2e_tests/tests/test_oauth2.py
T

128 lines
5.2 KiB
Python

"""
OAuth2客户端管理测试用例
"""
import pytest
from httpx import AsyncClient
@pytest.mark.oauth2
@pytest.mark.regression
class TestOAuth2:
"""OAuth2客户端管理测试类"""
@pytest.fixture
def test_oauth2_client_data(self):
"""测试OAuth2客户端数据"""
import time
timestamp = int(time.time() * 1000)
return {
"clientId": f"test-client-{timestamp}",
"clientSecret": "secret123",
"clientName": "Test Client",
"webServerRedirectUri": "http://localhost:8080/callback",
"scope": "read,write",
"authorizedGrantTypes": "authorization_code,refresh_token",
"accessTokenValiditySeconds": 7200,
"refreshTokenValiditySeconds": 2592000,
"autoApprove": False,
"enabled": True
}
@pytest.fixture
async def cleanup_oauth2_client(self, authenticated_client: AsyncClient):
"""清理测试OAuth2客户端"""
client_ids = []
yield client_ids
for client_id in client_ids:
try:
await authenticated_client.delete(f"/api/oauth2/clients/{client_id}")
except Exception:
pass
@pytest.mark.asyncio
async def test_create_oauth2_client_success(self, authenticated_client, test_oauth2_client_data, cleanup_oauth2_client):
"""测试创建OAuth2客户端成功"""
response = await authenticated_client.post("/api/oauth2/clients", json=test_oauth2_client_data)
assert response.status_code == 201
data = response.json()
assert "id" in data
assert data["clientId"] == test_oauth2_client_data["clientId"]
assert data["clientName"] == test_oauth2_client_data["clientName"]
assert "clientSecret" not in data or data["clientSecret"] != test_oauth2_client_data["clientSecret"]
cleanup_oauth2_client.append(data["id"])
@pytest.mark.asyncio
async def test_get_oauth2_client_by_id_success(self, authenticated_client, test_oauth2_client_data, cleanup_oauth2_client):
"""测试根据ID获取OAuth2客户端成功"""
create_response = await authenticated_client.post("/api/oauth2/clients", json=test_oauth2_client_data)
client_id = create_response.json()["id"]
response = await authenticated_client.get(f"/api/oauth2/clients/{client_id}")
assert response.status_code == 200
data = response.json()
assert data["id"] == client_id
assert data["clientId"] == test_oauth2_client_data["clientId"]
cleanup_oauth2_client.append(client_id)
@pytest.mark.asyncio
async def test_get_oauth2_client_by_id_not_found(self, authenticated_client):
"""测试获取不存在的OAuth2客户端"""
response = await authenticated_client.get("/api/oauth2/clients/999999")
assert response.status_code == 404
@pytest.mark.asyncio
async def test_get_oauth2_client_by_client_id_success(self, authenticated_client, test_oauth2_client_data, cleanup_oauth2_client):
"""测试根据clientId获取OAuth2客户端成功"""
create_response = await authenticated_client.post("/api/oauth2/clients", json=test_oauth2_client_data)
client_id = create_response.json()["id"]
response = await authenticated_client.get(f"/api/oauth2/clients/client-id/{test_oauth2_client_data['clientId']}")
assert response.status_code == 200
data = response.json()
assert data["clientId"] == test_oauth2_client_data["clientId"]
cleanup_oauth2_client.append(client_id)
@pytest.mark.asyncio
async def test_get_all_oauth2_clients_success(self, authenticated_client):
"""测试获取所有OAuth2客户端成功"""
response = await authenticated_client.get("/api/oauth2/clients")
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
@pytest.mark.asyncio
async def test_update_oauth2_client_success(self, authenticated_client, test_oauth2_client_data, cleanup_oauth2_client):
"""测试更新OAuth2客户端成功"""
create_response = await authenticated_client.post("/api/oauth2/clients", json=test_oauth2_client_data)
client_id = create_response.json()["id"]
update_data = {"clientName": "Updated Client Name"}
response = await authenticated_client.put(f"/api/oauth2/clients/{client_id}", json=update_data)
assert response.status_code == 200
data = response.json()
assert data["clientName"] == "Updated Client Name"
cleanup_oauth2_client.append(client_id)
@pytest.mark.asyncio
async def test_delete_oauth2_client_success(self, authenticated_client, test_oauth2_client_data, cleanup_oauth2_client):
"""测试删除OAuth2客户端成功"""
create_response = await authenticated_client.post("/api/oauth2/clients", json=test_oauth2_client_data)
client_id = create_response.json()["id"]
response = await authenticated_client.delete(f"/api/oauth2/clients/{client_id}")
assert response.status_code == 204