| import pytest |
| from app.services.intent_adapter import to_oss_intent, IntentAdapterError |
|
|
|
|
| def test_unknown_intent_type(): |
| """Unknown intent_type raises IntentAdapterError.""" |
| with pytest.raises(IntentAdapterError, match="Unknown intent_type: UnknownIntent"): |
| to_oss_intent({"intent_type": "UnknownIntent", |
| "environment": "prod", "requester": "alice"}) |
|
|
|
|
| def test_missing_intent_type(): |
| """Missing intent_type raises IntentAdapterError.""" |
| with pytest.raises(IntentAdapterError, match="Missing 'intent_type' in request"): |
| to_oss_intent({}) |
|
|
|
|
| def test_missing_environment(): |
| """Missing environment raises IntentAdapterError.""" |
| payload = {"intent_type": "provision_resource"} |
| with pytest.raises(IntentAdapterError, match="Missing 'environment' field"): |
| to_oss_intent(payload) |
|
|
|
|
| def test_invalid_environment(): |
| """Invalid environment value raises IntentAdapterError.""" |
| payload = { |
| "intent_type": "provision_resource", |
| "environment": "invalid_env", |
| "requester": "alice", |
| } |
| with pytest.raises(IntentAdapterError, match="Invalid environment: invalid_env"): |
| to_oss_intent(payload) |
|
|
|
|
| def test_missing_requester(): |
| """Missing requester raises IntentAdapterError.""" |
| payload = { |
| "intent_type": "provision_resource", |
| "environment": "prod", |
| } |
| with pytest.raises(IntentAdapterError, match="Missing 'requester' field"): |
| to_oss_intent(payload) |
|
|
|
|
| def test_valid_provision_intent(): |
| """Valid provision_resource payload returns ProvisionResourceIntent.""" |
| payload = { |
| "intent_type": "provision_resource", |
| "environment": "prod", |
| "resource_type": "database", |
| "region": "eastus", |
| "size": "Standard", |
| "requester": "alice", |
| "configuration": {"foo": "bar"}, |
| "provenance": {"source": "test"}, |
| } |
| intent = to_oss_intent(payload) |
| |
| assert intent.resource_type == "database" |
| assert intent.region == "eastus" |
| assert intent.size == "Standard" |
| assert intent.environment == "prod" |
| assert intent.requester == "alice" |
| assert intent.configuration == {"foo": "bar"} |
|
|
|
|
| def test_provision_missing_resource_type(): |
| """Missing resource_type raises IntentAdapterError.""" |
| payload = { |
| "intent_type": "provision_resource", |
| "environment": "prod", |
| "region": "eastus", |
| "size": "Standard", |
| "requester": "alice", |
| } |
| with pytest.raises(IntentAdapterError, match="Missing 'resource_type'"): |
| to_oss_intent(payload) |
|
|
|
|
| def test_provision_invalid_resource_type(): |
| """Invalid resource_type raises IntentAdapterError.""" |
| payload = { |
| "intent_type": "provision_resource", |
| "environment": "prod", |
| "resource_type": "invalid_type", |
| "region": "eastus", |
| "size": "Standard", |
| "requester": "alice", |
| } |
| with pytest.raises(IntentAdapterError, match="Invalid resource_type: invalid_type"): |
| to_oss_intent(payload) |
|
|
|
|
| def test_valid_grant_intent(): |
| """Valid grant_access payload returns GrantAccessIntent.""" |
| payload = { |
| "intent_type": "grant_access", |
| "environment": "dev", |
| "principal": "bob", |
| "permission_level": "read", |
| "resource_scope": "/subscriptions/123", |
| "requester": "alice", |
| "justification": "testing", |
| "provenance": {}, |
| } |
| intent = to_oss_intent(payload) |
| assert intent.principal == "bob" |
| assert intent.permission_level == "read" |
| assert intent.resource_scope == "/subscriptions/123" |
|
|
| assert intent.requester == "alice" |
|
|
|
|
| def test_grant_missing_principal(): |
| """Missing principal raises IntentAdapterError.""" |
| payload = { |
| "intent_type": "grant_access", |
| "environment": "dev", |
| "permission_level": "read", |
| "resource_scope": "/subscriptions/123", |
| "requester": "alice", |
| } |
| with pytest.raises(IntentAdapterError, match="Missing 'principal'"): |
| to_oss_intent(payload) |
|
|
|
|
| def test_valid_deploy_intent(): |
| """Valid deploy_config payload returns DeployConfigurationIntent.""" |
| payload = { |
| "intent_type": "deploy_config", |
| "environment": "staging", |
| "service_name": "payments-api", |
| "change_scope": "canary", |
| "deployment_target": "staging", |
| "requester": "alice", |
| "risk_level_hint": "low", |
| "configuration": {"replicas": 3}, |
| "provenance": {}, |
| } |
| intent = to_oss_intent(payload) |
| assert intent.service_name == "payments-api" |
| assert intent.change_scope == "canary" |
| assert intent.deployment_target == "staging" |
|
|
| assert intent.requester == "alice" |
| |
| assert intent.risk_level_hint is None |
|
|
|
|
| def test_deploy_missing_service_name(): |
| """Missing service_name raises IntentAdapterError.""" |
| payload = { |
| "intent_type": "deploy_config", |
| "environment": "staging", |
| "change_scope": "canary", |
| "deployment_target": "staging", |
| "requester": "alice", |
| } |
| with pytest.raises(IntentAdapterError, match="Missing 'service_name'"): |
| to_oss_intent(payload) |
|
|
|
|
| def test_pydantic_model_support(): |
| """Test that Pydantic models are handled correctly (simulated).""" |
| class MockModel: |
| def model_dump(self): |
| return { |
| "intent_type": "provision_resource", |
| "environment": "prod", |
| "resource_type": "database", |
| "region": "eastus", |
| "size": "Standard", |
| "requester": "alice", |
| } |
|
|
| intent = to_oss_intent(MockModel()) |
| assert intent.resource_type == "database" |
|
|