test: add tests for platform assignment in user/vendor creation

Unit tests (test_admin_service.py):
- Vendor creation without platforms
- Vendor creation with single platform
- Vendor creation with multiple platforms
- Vendor creation with invalid platform ID (ignored)
- Vendor creation with duplicate code/subdomain (fails)
- Vendor creation with invalid company ID (fails)

Integration tests (test_vendors.py):
- Create vendor via API without platforms
- Create vendor via API with platforms
- Create vendor with duplicate code fails (409)
- Non-admin cannot create vendors (403)

Auth tests (test_auth.py):
- Super admin login includes is_super_admin=true
- Platform admin login includes is_super_admin=false
- Get current super admin info includes is_super_admin

Total: 69 admin platform tests passing

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-24 20:17:36 +01:00
parent 3cbe7e2979
commit 9d28210cf1
3 changed files with 317 additions and 0 deletions

View File

@@ -175,3 +175,48 @@ class TestAdminAuthAPI:
assert response.status_code == 200
data = response.json()
assert data["message"] == "Logged out successfully"
def test_super_admin_login_includes_is_super_admin(
self, client, test_super_admin
):
"""Test super admin login includes is_super_admin in response."""
response = client.post(
"/api/v1/admin/auth/login",
json={
"email_or_username": test_super_admin.username,
"password": "superadminpass123",
},
)
assert response.status_code == 200
data = response.json()
assert "user" in data
assert "is_super_admin" in data["user"]
assert data["user"]["is_super_admin"] is True
def test_platform_admin_login_includes_is_super_admin(
self, client, test_platform_admin
):
"""Test platform admin login includes is_super_admin=False in response."""
response = client.post(
"/api/v1/admin/auth/login",
json={
"email_or_username": test_platform_admin.username,
"password": "platformadminpass123",
},
)
assert response.status_code == 200
data = response.json()
assert "user" in data
assert "is_super_admin" in data["user"]
assert data["user"]["is_super_admin"] is False
def test_get_current_super_admin_info(self, client, super_admin_headers, test_super_admin):
"""Test getting current super admin user info includes is_super_admin."""
response = client.get("/api/v1/admin/auth/me", headers=super_admin_headers)
assert response.status_code == 200
data = response.json()
assert data["username"] == test_super_admin.username
assert data["is_super_admin"] is True

View File

@@ -115,3 +115,95 @@ class TestAdminVendorsAPI:
assert len(data["vendors"]) >= 0
assert "skip" in data
assert "limit" in data
@pytest.mark.integration
@pytest.mark.api
@pytest.mark.admin
class TestAdminVendorCreationAPI:
"""Test admin vendor creation endpoints with platform assignment."""
def test_create_vendor_without_platforms(
self, client, admin_headers, test_company
):
"""Test creating a vendor without platform assignments."""
import uuid
unique_id = str(uuid.uuid4())[:8]
response = client.post(
"/api/v1/admin/vendors",
headers=admin_headers,
json={
"company_id": test_company.id,
"vendor_code": f"NOPLAT_{unique_id}",
"subdomain": f"noplat{unique_id}",
"name": f"No Platform Vendor {unique_id}",
},
)
assert response.status_code == 200
data = response.json()
assert data["vendor_code"] == f"NOPLAT_{unique_id}".upper()
assert data["company_id"] == test_company.id
def test_create_vendor_with_platforms(
self, client, admin_headers, test_company, test_platform, another_platform
):
"""Test creating a vendor with platform assignments."""
import uuid
unique_id = str(uuid.uuid4())[:8]
response = client.post(
"/api/v1/admin/vendors",
headers=admin_headers,
json={
"company_id": test_company.id,
"vendor_code": f"WITHPLAT_{unique_id}",
"subdomain": f"withplat{unique_id}",
"name": f"With Platform Vendor {unique_id}",
"platform_ids": [test_platform.id, another_platform.id],
},
)
assert response.status_code == 200
data = response.json()
assert data["vendor_code"] == f"WITHPLAT_{unique_id}".upper()
def test_create_vendor_duplicate_code_fails(
self, client, admin_headers, test_company, test_vendor
):
"""Test creating a vendor with duplicate code fails."""
response = client.post(
"/api/v1/admin/vendors",
headers=admin_headers,
json={
"company_id": test_company.id,
"vendor_code": test_vendor.vendor_code,
"subdomain": "uniquesubdomain123",
"name": "Duplicate Code Vendor",
},
)
assert response.status_code == 409 # Conflict
data = response.json()
assert data["error_code"] == "VENDOR_ALREADY_EXISTS"
def test_create_vendor_non_admin_fails(
self, client, auth_headers, test_company
):
"""Test non-admin cannot create vendors."""
import uuid
unique_id = str(uuid.uuid4())[:8]
response = client.post(
"/api/v1/admin/vendors",
headers=auth_headers,
json={
"company_id": test_company.id,
"vendor_code": f"NONADMIN_{unique_id}",
"subdomain": f"nonadmin{unique_id}",
"name": "Non Admin Vendor",
},
)
assert response.status_code == 403