164 lines
4.8 KiB
Python
164 lines
4.8 KiB
Python
"""Tests for instance activation endpoint."""
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
import pytest
|
|
from httpx import AsyncClient
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_activate_success(client: AsyncClient, admin_headers: dict):
|
|
"""Test successful activation."""
|
|
# Create client and instance
|
|
client_response = await client.post(
|
|
"/api/v1/admin/clients",
|
|
json={"name": "Activation Test Corp"},
|
|
headers=admin_headers,
|
|
)
|
|
client_id = client_response.json()["id"]
|
|
|
|
instance_response = await client.post(
|
|
f"/api/v1/admin/clients/{client_id}/instances",
|
|
json={"instance_id": "activation-test"},
|
|
headers=admin_headers,
|
|
)
|
|
license_key = instance_response.json()["license_key"]
|
|
|
|
# Activate
|
|
response = await client.post(
|
|
"/api/v1/instances/activate",
|
|
json={
|
|
"license_key": license_key,
|
|
"instance_id": "activation-test",
|
|
},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["status"] == "ok"
|
|
assert data["instance_id"] == "activation-test"
|
|
# Should return USE_EXISTING since key was pre-generated
|
|
assert data["hub_api_key"] == "USE_EXISTING"
|
|
assert "config" in data
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_activate_increments_count(client: AsyncClient, admin_headers: dict):
|
|
"""Test that activation increments count."""
|
|
# Create client and instance
|
|
client_response = await client.post(
|
|
"/api/v1/admin/clients",
|
|
json={"name": "Count Test Corp"},
|
|
headers=admin_headers,
|
|
)
|
|
client_id = client_response.json()["id"]
|
|
|
|
instance_response = await client.post(
|
|
f"/api/v1/admin/clients/{client_id}/instances",
|
|
json={"instance_id": "count-test"},
|
|
headers=admin_headers,
|
|
)
|
|
license_key = instance_response.json()["license_key"]
|
|
|
|
# Activate multiple times
|
|
for i in range(3):
|
|
await client.post(
|
|
"/api/v1/instances/activate",
|
|
json={
|
|
"license_key": license_key,
|
|
"instance_id": "count-test",
|
|
},
|
|
)
|
|
|
|
# Check count
|
|
get_response = await client.get(
|
|
"/api/v1/admin/instances/count-test",
|
|
headers=admin_headers,
|
|
)
|
|
assert get_response.json()["activation_count"] == 3
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_activate_invalid_license(client: AsyncClient, admin_headers: dict):
|
|
"""Test activation with invalid license key."""
|
|
# Create client and instance
|
|
client_response = await client.post(
|
|
"/api/v1/admin/clients",
|
|
json={"name": "Invalid Test Corp"},
|
|
headers=admin_headers,
|
|
)
|
|
client_id = client_response.json()["id"]
|
|
|
|
await client.post(
|
|
f"/api/v1/admin/clients/{client_id}/instances",
|
|
json={"instance_id": "invalid-license-test"},
|
|
headers=admin_headers,
|
|
)
|
|
|
|
# Try with wrong license
|
|
response = await client.post(
|
|
"/api/v1/instances/activate",
|
|
json={
|
|
"license_key": "lb_inst_wrongkey123456789012345678901234",
|
|
"instance_id": "invalid-license-test",
|
|
},
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
data = response.json()["detail"]
|
|
assert data["code"] == "invalid_license"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_activate_unknown_instance(client: AsyncClient):
|
|
"""Test activation with unknown instance_id."""
|
|
response = await client.post(
|
|
"/api/v1/instances/activate",
|
|
json={
|
|
"license_key": "lb_inst_somekey1234567890123456789012",
|
|
"instance_id": "unknown-instance",
|
|
},
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
data = response.json()["detail"]
|
|
assert data["code"] == "instance_not_found"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_activate_suspended_license(client: AsyncClient, admin_headers: dict):
|
|
"""Test activation with suspended license."""
|
|
# Create client and instance
|
|
client_response = await client.post(
|
|
"/api/v1/admin/clients",
|
|
json={"name": "Suspended Test Corp"},
|
|
headers=admin_headers,
|
|
)
|
|
client_id = client_response.json()["id"]
|
|
|
|
instance_response = await client.post(
|
|
f"/api/v1/admin/clients/{client_id}/instances",
|
|
json={"instance_id": "suspended-license-test"},
|
|
headers=admin_headers,
|
|
)
|
|
license_key = instance_response.json()["license_key"]
|
|
|
|
# Suspend instance
|
|
await client.post(
|
|
"/api/v1/admin/instances/suspended-license-test/suspend",
|
|
headers=admin_headers,
|
|
)
|
|
|
|
# Try to activate
|
|
response = await client.post(
|
|
"/api/v1/instances/activate",
|
|
json={
|
|
"license_key": license_key,
|
|
"instance_id": "suspended-license-test",
|
|
},
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
data = response.json()["detail"]
|
|
assert data["code"] == "suspended"
|