Skip to content

Commit

Permalink
test: update secret resource tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm committed Oct 25, 2022
1 parent 0d62fd6 commit 803477a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
2 changes: 1 addition & 1 deletion nitric/resources/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async def allow(self, permissions: List[Union[SecretPermission, str]]) -> Secret
resources=[_to_resource(self)],
)
try:
await self._resources_stub.declare(ResourceDeclareRequest(resource=Resource(type=ResourceType.Policy), policy=policy))
await self._resources_stub.declare(resource_declare_request=ResourceDeclareRequest(resource=Resource(type=ResourceType.Policy), policy=policy))
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

Expand Down
47 changes: 22 additions & 25 deletions tests/resources/test_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from unittest.mock import patch, AsyncMock
from nitric.resources import secret

from nitricapi.nitric.resource.v1 import Action
from nitricapi.nitric.resource.v1 import Action, ResourceDeclareRequest, Resource, ResourceType, PolicyResource
from nitricapi.nitric.secret.v1 import SecretPutResponse, SecretVersion, Secret


Expand All @@ -38,9 +38,17 @@ async def test_allow_put(self):
await secret("test-secret").allow(["putting"])

# Check expected values were passed to Stub
mock_declare.assert_called()
self.assertEqual(mock_declare.call_args.kwargs["policy"].resources[0].name, "test-secret")
self.assertListEqual(mock_declare.call_args.kwargs["policy"].actions, [Action.SecretPut])
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
resource=Resource(type=ResourceType.Policy),
policy=PolicyResource(
principals=[Resource(type=ResourceType.Function)],
actions=[
Action.SecretPut
],
resources=[Resource(type=ResourceType.Secret, name="test-secret")]
)
))


async def test_allow_access(self):
mock_declare = AsyncMock()
Expand All @@ -51,24 +59,13 @@ async def test_allow_access(self):
await secret("test-secret").allow(["accessing"])

# Check expected values were passed to Stub
mock_declare.assert_called()
self.assertEqual(mock_declare.call_args.kwargs["policy"].resources[0].name, "test-secret")
self.assertListEqual(mock_declare.call_args.kwargs["policy"].actions, [Action.SecretAccess])

async def test_put_string(self):
mock_put = AsyncMock()
mock_declare = AsyncMock()

mock_response = SecretPutResponse(
secret_version=SecretVersion(secret=Secret(name="test-secret"), version="test-version")
)
mock_put.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
with patch("nitricapi.nitric.secret.v1.SecretServiceStub.put", mock_put):
s = await secret("test-secret").allow(["accessing"])
await s.put("a test secret value") # string, not bytes

# Check expected values were passed to Stub
mock_put.assert_called_once()
assert mock_put.call_args.kwargs["value"] == b"a test secret value" # value should still be bytes when sent.
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
resource=Resource(type=ResourceType.Policy),
policy=PolicyResource(
principals=[Resource(type=ResourceType.Function)],
actions=[
Action.SecretAccess
],
resources=[Resource(type=ResourceType.Secret, name="test-secret")]
)
))

0 comments on commit 803477a

Please sign in to comment.