Skip to content

Commit

Permalink
test: Fix bucket resource tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm committed Oct 24, 2022
1 parent 72b8a92 commit 90e35dd
Showing 1 changed file with 74 additions and 38 deletions.
112 changes: 74 additions & 38 deletions tests/resources/test_buckets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
#
from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch, AsyncMock

from nitricapi.nitric.storage.v1 import StorageWriteRequest

from nitric.resources import bucket

from nitricapi.nitric.resource.v1 import Action
from nitricapi.nitric.resource.v1 import Action, ResourceDeclareRequest, Resource, ResourceType, PolicyResource


class Object(object):
Expand All @@ -37,9 +40,14 @@ async def test_create_allow_writing(self):
await bucket("test-bucket").allow(["writing"])

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

async def test_create_allow_reading(self):
mock_declare = AsyncMock()
Expand All @@ -50,10 +58,14 @@ async def test_create_allow_reading(self):
await bucket("test-bucket").allow(["reading"])

# Check expected values were passed to Stub
mock_declare.assert_called()

self.assertEqual(mock_declare.call_args.kwargs["policy"].resources[0].name, "test-bucket")
self.assertEqual(mock_declare.call_args.kwargs["policy"].actions, [Action.BucketFileGet, Action.BucketFileList])
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
resource=Resource(type=ResourceType.Policy),
policy=PolicyResource(
principals=[Resource(type=ResourceType.Function)],
actions=[Action.BucketFileGet, Action.BucketFileList],
resources=[Resource(type=ResourceType.Bucket, name="test-bucket")]
)
))

async def test_create_allow_deleting(self):
mock_declare = AsyncMock()
Expand All @@ -64,9 +76,14 @@ async def test_create_allow_deleting(self):
await bucket("test-bucket").allow(["deleting"])

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

async def test_create_allow_all(self):
mock_declare = AsyncMock()
Expand All @@ -77,17 +94,19 @@ async def test_create_allow_all(self):
await bucket("test-bucket").allow(["deleting", "reading", "writing"])

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

async def test_create_allow_all_reversed_policy(self):
mock_declare = AsyncMock()
Expand All @@ -98,17 +117,20 @@ async def test_create_allow_all_reversed_policy(self):
await bucket("test-bucket").allow(["writing", "reading", "deleting"])

# Check expected values were passed to Stub
mock_declare.assert_called()
self.assertEqual(mock_declare.call_args.kwargs["policy"].resources[0].name, "test-bucket")
self.assertLessEqual(
mock_declare.call_args.kwargs["policy"].actions,
[
Action.BucketFilePut,
Action.BucketFileGet,
Action.BucketFileList,
Action.BucketFileDelete,
],
)
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
resource=Resource(type=ResourceType.Policy),
policy=PolicyResource(
principals=[Resource(type=ResourceType.Function)],
actions=[
Action.BucketFilePut,
Action.BucketFileGet,
Action.BucketFileList,
Action.BucketFileDelete,
],
resources=[Resource(type=ResourceType.Bucket, name="test-bucket")]
)
))


async def test_write(self):
mock_declare = AsyncMock()
Expand All @@ -124,8 +146,22 @@ async def test_write(self):
file = b.file("test-file")
await file.write(contents)

# Check expected values were passed to Stub
print(f"num calls: {len(mock_declare.mock_calls)}")
self.assertEqual(mock_write.call_args.kwargs["bucket_name"], "test-bucket")
self.assertEqual(mock_write.call_args.kwargs["key"], "test-file")
self.assertEqual(mock_write.call_args.kwargs["body"], contents)
# Check expected resources were declared
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
resource=Resource(type=ResourceType.Policy),
policy=PolicyResource(
principals=[Resource(type=ResourceType.Function)],
actions=[
Action.BucketFilePut,
],
resources=[Resource(type=ResourceType.Bucket, name="test-bucket")]
)
))

# Check correct data was written
mock_write.assert_called_with(storage_write_request=StorageWriteRequest(
bucket_name="test-bucket",
key="test-file",
body=contents

))

0 comments on commit 90e35dd

Please sign in to comment.