import pytest from app.models import Agent, User from app import db import uuid def test_delete_user_success(client, admin_headers): """ Verify DELETE /api/admin/users/ works (after fix). """ # Create a user to delete user = User(username=f"del_{uuid.uuid4().hex[:8]}", email=f"del_{uuid.uuid4().hex[:8]}@test.com", role="user") user.set_password("password") db.session.add(user) db.session.commit() user_id = user.id print(f"\nCreated user {user_id} for DELETE test.") # Test DELETE Endpoint res = client.delete(f'/api/admin/users/{user_id}', headers=admin_headers) print(f"DELETE /users/{user_id} status: {res.status_code}") if res.status_code == 200: print("DELETE /api/admin/users/ success.") # Verify user is gone assert User.query.get(user_id) is None else: print(f"DELETE failed with {res.status_code}: {res.text}") pytest.fail(f"DELETE failed: {res.status_code}") def test_update_agent_api_key_scenarios(client, admin_headers): """ Test various Agent API Key update scenarios. """ # Create an agent app_id = f"app-{uuid.uuid4().hex[:8]}" agent = Agent( name="UpdateKeyTest", type="api", dify_app_id=app_id, dify_api_key="orig_key", is_active=True ) db.session.add(agent) db.session.commit() agent_id = agent.id print(f"\nCreated agent {agent_id} for update test.") # Scenario 1: Update Key Success new_key = "new_secret_key" data = { "dify_api_key": new_key } res = client.put(f'/api/admin/agents/{agent_id}', json=data, headers=admin_headers) assert res.status_code == 200 updated_agent = Agent.query.get(agent_id) assert updated_agent.dify_api_key == new_key print("Scenario 1 (Success): Key updated.") # Scenario 2: Update with Empty String (Should allow it? Or ignore?) # Current behavior: updates to empty string. data = { "dify_api_key": "" } res = client.put(f'/api/admin/agents/{agent_id}', json=data, headers=admin_headers) assert res.status_code == 200 updated_agent = Agent.query.get(agent_id) assert updated_agent.dify_api_key == "" print("Scenario 2 (Empty String): Key updated to empty string.") # Scenario 3: Update with Duplicate App ID (Should fail) # Create another agent first other_app_id = f"app-{uuid.uuid4().hex[:8]}" other_agent = Agent( name="OtherAgent", type="api", dify_app_id=other_app_id, dify_api_key="key2", is_active=True ) db.session.add(other_agent) db.session.commit() # Try to update first agent to use second agent's App ID data = { "dify_app_id": other_app_id } res = client.put(f'/api/admin/agents/{agent_id}', json=data, headers=admin_headers) print(f"Scenario 3 (Duplicate App ID) status: {res.status_code}") # This should fail with 400 (caught exception) or 500 # Our code catches Exception and returns 400 assert res.status_code == 400 assert "error" in res.json print("Scenario 3 (Duplicate App ID): Correctly failed.") # Scenario 4: Update with Very Long Key (Should fail if > 100) long_key = "a" * 101 data = { "dify_api_key": long_key } res = client.put(f'/api/admin/agents/{agent_id}', json=data, headers=admin_headers) print(f"Scenario 4 (Long Key) status: {res.status_code}") # Should fail due to DB constraint (String(100)) # Caught by try-except block -> 400 if res.status_code == 400: print("Scenario 4 (Long Key): Correctly failed.") else: print(f"Scenario 4 (Long Key): Unexpected success? Status: {res.status_code}") # If SQLite, it might not enforce length. If Postgres, it will. # We are using SQLite (implied by file structure/lack of PG setup in context, but wait, env says postgres?) # "mcp_PostgreSQL_query" tool suggests Postgres. # But locally it might be SQLite. pass