|
| 1 | +"""Integration tests for ResourcePolicyClient. |
| 2 | +
|
| 3 | +E2e tests against real AWS resources. No mocking. |
| 4 | +
|
| 5 | +Requires: |
| 6 | + RESOURCE_POLICY_TEST_ARN: ARN of an Agent Runtime to attach policies to |
| 7 | + RESOURCE_POLICY_TEST_PRINCIPAL: IAM principal ARN to use in test policies |
| 8 | + BEDROCK_TEST_REGION: AWS region (default: us-west-2) |
| 9 | +
|
| 10 | +Run: pytest -xvs tests_integ/services/test_resource_policy.py |
| 11 | +""" |
| 12 | + |
| 13 | +import json |
| 14 | +import os |
| 15 | + |
| 16 | +import pytest |
| 17 | +from botocore.exceptions import ClientError |
| 18 | + |
| 19 | +from bedrock_agentcore.services.resource_policy import ResourcePolicyClient |
| 20 | + |
| 21 | + |
| 22 | +def _make_policy( |
| 23 | + resource_arn: str, |
| 24 | + principal_arn: str, |
| 25 | + action: str = "bedrock-agentcore:InvokeAgentRuntime", |
| 26 | +) -> dict: |
| 27 | + """Build a minimal valid policy dict for testing.""" |
| 28 | + return { |
| 29 | + "Version": "2012-10-17", |
| 30 | + "Statement": [ |
| 31 | + { |
| 32 | + "Effect": "Allow", |
| 33 | + "Principal": {"AWS": principal_arn}, |
| 34 | + "Action": action, |
| 35 | + "Resource": resource_arn, |
| 36 | + } |
| 37 | + ], |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.integration |
| 42 | +class TestResourcePolicyClient: |
| 43 | + @classmethod |
| 44 | + def setup_class(cls): |
| 45 | + cls.resource_arn = os.environ.get("RESOURCE_POLICY_TEST_ARN") |
| 46 | + cls.principal_arn = os.environ.get("RESOURCE_POLICY_TEST_PRINCIPAL") |
| 47 | + cls.region = os.environ.get("BEDROCK_TEST_REGION", "us-west-2") |
| 48 | + |
| 49 | + if not cls.resource_arn: |
| 50 | + pytest.fail("RESOURCE_POLICY_TEST_ARN env var is required") |
| 51 | + if not cls.principal_arn: |
| 52 | + pytest.fail("RESOURCE_POLICY_TEST_PRINCIPAL env var is required") |
| 53 | + |
| 54 | + cls.client = ResourcePolicyClient(region=cls.region) |
| 55 | + |
| 56 | + def setup_method(self): |
| 57 | + """Runs before each test — ensures no policy is attached.""" |
| 58 | + try: |
| 59 | + self.client.delete_resource_policy(self.resource_arn) |
| 60 | + except Exception: |
| 61 | + pass |
| 62 | + |
| 63 | + @classmethod |
| 64 | + def teardown_class(cls): |
| 65 | + """Remove any policy left by the last test so we don't leave side effects on the resource.""" |
| 66 | + try: |
| 67 | + cls.client.delete_resource_policy(cls.resource_arn) |
| 68 | + except Exception: |
| 69 | + pass |
| 70 | + |
| 71 | + @pytest.mark.order(1) |
| 72 | + def test_get_returns_none_when_no_policy(self): |
| 73 | + """get on a resource with no policy returns None.""" |
| 74 | + result = self.client.get_resource_policy(self.resource_arn) |
| 75 | + assert result is None |
| 76 | + |
| 77 | + @pytest.mark.order(2) |
| 78 | + def test_put_get_round_trip(self): |
| 79 | + """put(policy) then get() returns matching policy as a dict.""" |
| 80 | + policy = _make_policy(self.resource_arn, self.principal_arn) |
| 81 | + |
| 82 | + put_result = self.client.put_resource_policy(self.resource_arn, policy) |
| 83 | + assert isinstance(put_result, dict) |
| 84 | + assert put_result["Version"] == policy["Version"] |
| 85 | + |
| 86 | + result = self.client.get_resource_policy(self.resource_arn) |
| 87 | + assert isinstance(result, dict) |
| 88 | + assert result["Version"] == policy["Version"] |
| 89 | + assert result["Statement"][0]["Effect"] == "Allow" |
| 90 | + assert result["Statement"][0]["Resource"] == self.resource_arn |
| 91 | + |
| 92 | + @pytest.mark.order(3) |
| 93 | + def test_put_overwrites(self): |
| 94 | + """put(A) then put(B) then get() returns B.""" |
| 95 | + policy_a = _make_policy(self.resource_arn, self.principal_arn, action="bedrock-agentcore:InvokeAgentRuntime") |
| 96 | + policy_b = _make_policy(self.resource_arn, self.principal_arn, action="bedrock-agentcore:GetAgentCard") |
| 97 | + |
| 98 | + self.client.put_resource_policy(self.resource_arn, policy_a) |
| 99 | + self.client.put_resource_policy(self.resource_arn, policy_b) |
| 100 | + result = self.client.get_resource_policy(self.resource_arn) |
| 101 | + |
| 102 | + assert result["Statement"][0]["Action"] == "bedrock-agentcore:GetAgentCard" |
| 103 | + |
| 104 | + @pytest.mark.order(4) |
| 105 | + def test_delete_removes_policy(self): |
| 106 | + """put(policy) then delete() then get() returns None.""" |
| 107 | + policy = _make_policy(self.resource_arn, self.principal_arn) |
| 108 | + self.client.put_resource_policy(self.resource_arn, policy) |
| 109 | + self.client.delete_resource_policy(self.resource_arn) |
| 110 | + |
| 111 | + result = self.client.get_resource_policy(self.resource_arn) |
| 112 | + assert result is None |
| 113 | + |
| 114 | + @pytest.mark.order(5) |
| 115 | + def test_delete_on_no_policy_raises(self): |
| 116 | + """delete on a resource with no policy raises ResourceNotFoundException.""" |
| 117 | + with pytest.raises(ClientError) as exc_info: |
| 118 | + self.client.delete_resource_policy(self.resource_arn) |
| 119 | + assert exc_info.value.response["Error"]["Code"] == "ResourceNotFoundException" |
| 120 | + |
| 121 | + @pytest.mark.order(6) |
| 122 | + def test_dict_and_string_equivalence(self): |
| 123 | + """put(dict) and put(json.dumps(dict)) produce the same get() result.""" |
| 124 | + policy = _make_policy(self.resource_arn, self.principal_arn) |
| 125 | + |
| 126 | + self.client.put_resource_policy(self.resource_arn, policy) |
| 127 | + result_from_dict = self.client.get_resource_policy(self.resource_arn) |
| 128 | + |
| 129 | + self.client.put_resource_policy(self.resource_arn, json.dumps(policy)) |
| 130 | + result_from_str = self.client.get_resource_policy(self.resource_arn) |
| 131 | + |
| 132 | + assert result_from_dict == result_from_str |
0 commit comments