69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
import pytest
|
|
from fastapi import status
|
|
|
|
|
|
def test_create_group(client, auth_headers):
|
|
"""Test creating a new group."""
|
|
group_data = {"name": "New Test Group"}
|
|
|
|
response = client.post("/groups/", json=group_data, headers=auth_headers)
|
|
assert response.status_code == 200
|
|
|
|
data = response.json()
|
|
assert data["name"] == "New Test Group"
|
|
assert "id" in data
|
|
|
|
|
|
def test_create_duplicate_group(client, auth_headers, test_group):
|
|
"""Test creating a group with a duplicate name."""
|
|
group_data = {"name": test_group.name}
|
|
|
|
response = client.post("/groups/", json=group_data, headers=auth_headers)
|
|
# This should fail due to unique constraint
|
|
assert response.status_code == 422 # Validation error
|
|
|
|
|
|
def test_get_groups(client, auth_headers, test_group):
|
|
"""Test retrieving all groups."""
|
|
response = client.get("/groups/", headers=auth_headers)
|
|
assert response.status_code == 200
|
|
|
|
groups = response.json()
|
|
assert len(groups) >= 1
|
|
|
|
group_names = [group["name"] for group in groups]
|
|
assert test_group.name in group_names
|
|
|
|
|
|
def test_delete_group(client, auth_headers, test_group):
|
|
"""Test deleting a group."""
|
|
response = client.delete(f"/groups/{test_group.id}", headers=auth_headers)
|
|
assert response.status_code == 200
|
|
assert "deleted successfully" in response.json()["message"].lower()
|
|
|
|
# Verify group is deleted
|
|
response = client.get("/groups/", headers=auth_headers)
|
|
groups = response.json()
|
|
assert not any(group["id"] == test_group.id for group in groups)
|
|
|
|
|
|
def test_delete_nonexistent_group(client, auth_headers):
|
|
"""Test deleting a non-existent group."""
|
|
response = client.delete("/groups/99999", headers=auth_headers)
|
|
assert response.status_code == 404
|
|
|
|
|
|
def test_group_operations_by_non_admin(client, user_auth_headers):
|
|
"""Test that non-admin users cannot perform group operations."""
|
|
# Try to create a group
|
|
response = client.post(
|
|
"/groups/",
|
|
json={"name": "test"},
|
|
headers=user_auth_headers
|
|
)
|
|
assert response.status_code == 403
|
|
|
|
# Try to get groups
|
|
response = client.get("/groups/", headers=user_auth_headers)
|
|
assert response.status_code == 403
|