Skip to content

Commit

Permalink
test: Update collection resource tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm committed Oct 25, 2022
1 parent 90e35dd commit 463abe1
Showing 1 changed file with 66 additions and 117 deletions.
183 changes: 66 additions & 117 deletions tests/resources/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from nitric.resources import collection

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

from betterproto.lib.google.protobuf import Struct, Value

Expand All @@ -43,15 +43,17 @@ async def test_create_allow_writing(self):
await collection("test-collection").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-collection")
self.assertListEqual(
mock_declare.call_args.kwargs["policy"].actions,
[
Action.CollectionDocumentWrite,
Action.CollectionList,
],
)
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
resource=Resource(type=ResourceType.Policy),
policy=PolicyResource(
principals=[Resource(type=ResourceType.Function)],
actions=[
Action.CollectionDocumentWrite,
Action.CollectionList,
],
resources=[Resource(type=ResourceType.Collection, name="test-collection")]
)
))

async def test_create_allow_reading(self):
mock_declare = AsyncMock()
Expand All @@ -62,17 +64,18 @@ async def test_create_allow_reading(self):
await collection("test-collection").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-collection")
self.assertListEqual(
mock_declare.call_args.kwargs["policy"].actions,
[
Action.CollectionDocumentRead,
Action.CollectionQuery,
Action.CollectionList,
],
)
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
resource=Resource(type=ResourceType.Policy),
policy=PolicyResource(
principals=[Resource(type=ResourceType.Function)],
actions=[
Action.CollectionDocumentRead,
Action.CollectionQuery,
Action.CollectionList,
],
resources=[Resource(type=ResourceType.Collection, name="test-collection")]
)
))

async def test_create_allow_deleting(self):
mock_declare = AsyncMock()
Expand All @@ -83,15 +86,17 @@ async def test_create_allow_deleting(self):
await collection("test-collection").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-collection")
self.assertListEqual(
mock_declare.call_args.kwargs["policy"].actions,
[
Action.CollectionDocumentDelete,
Action.CollectionList,
],
)
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
resource=Resource(type=ResourceType.Policy),
policy=PolicyResource(
principals=[Resource(type=ResourceType.Function)],
actions=[
Action.CollectionDocumentDelete,
Action.CollectionList,
],
resources=[Resource(type=ResourceType.Collection, name="test-collection")]
)
))

async def test_create_allow_all(self):
mock_declare = AsyncMock()
Expand All @@ -102,20 +107,22 @@ async def test_create_allow_all(self):
await collection("test-collection").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-collection")
self.assertListEqual(
mock_declare.call_args.kwargs["policy"].actions,
[
Action.CollectionDocumentDelete,
Action.CollectionList,
Action.CollectionDocumentRead,
Action.CollectionQuery,
Action.CollectionList,
Action.CollectionDocumentWrite,
Action.CollectionList,
],
)
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
resource=Resource(type=ResourceType.Policy),
policy=PolicyResource(
principals=[Resource(type=ResourceType.Function)],
actions=[
Action.CollectionDocumentDelete,
Action.CollectionList,
Action.CollectionDocumentRead,
Action.CollectionQuery,
Action.CollectionList,
Action.CollectionDocumentWrite,
Action.CollectionList,
],
resources=[Resource(type=ResourceType.Collection, name="test-collection")]
)
))

async def test_create_allow_all_reversed_policy(self):
mock_declare = AsyncMock()
Expand All @@ -126,77 +133,19 @@ async def test_create_allow_all_reversed_policy(self):
await collection("test-collection").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-collection")
self.assertListEqual(
mock_declare.call_args.kwargs["policy"].actions,
[
Action.CollectionDocumentWrite,
Action.CollectionList,
Action.CollectionDocumentRead,
Action.CollectionQuery,
Action.CollectionList,
Action.CollectionDocumentDelete,
Action.CollectionList,
],
)

async def test_set_document(self):
mock_set = AsyncMock()
mock_declare = AsyncMock()
mock_response = Object()
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.set", mock_set):
collection_a = await collection("a").allow(["writing"])
await collection_a.doc("b").set({"a": 1})

mock_set.assert_called_once_with(
key=Key(
collection=DocumentCollection(name="a"),
id="b",
),
content=Struct(
fields={
"a": Value(number_value=1.0),
},
),
)
self.assertListEqual(
mock_declare.call_args.kwargs["policy"].actions,
[
Action.CollectionDocumentWrite,
Action.CollectionList,
],
)

async def test_get_document(self):
mock_declare = AsyncMock()
mock_response = Object()
mock_declare.return_value = mock_response

mock_get = AsyncMock()
mock_get.return_value = DocumentGetResponse(
document=Document(
key=Key(id="b", collection=DocumentCollection(name="a")),
content=Struct(
fields={
"a": Value(number_value=1.0),
},
),
),
)

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.get", mock_get):
collection_a = await collection("a").allow(["reading"])
response = await collection_a.doc("b").get()

mock_get.assert_called_once_with(
key=Key(
collection=DocumentCollection(name="a"),
id="b",
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
resource=Resource(type=ResourceType.Policy),
policy=PolicyResource(
principals=[Resource(type=ResourceType.Function)],
actions=[
Action.CollectionDocumentWrite,
Action.CollectionList,
Action.CollectionDocumentRead,
Action.CollectionQuery,
Action.CollectionList,
Action.CollectionDocumentDelete,
Action.CollectionList,
],
resources=[Resource(type=ResourceType.Collection, name="test-collection")]
)
)
self.assertEqual(1.0, response.content["a"])
))

0 comments on commit 463abe1

Please sign in to comment.