Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor Python fixes #446

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ from {{packageName}} import (
TypeDefinition,
Userset,
Usersets,
UserTypeFilter,
WriteAuthorizationModelRequest,
)
from {{packageName}}.client.models import (
Expand Down Expand Up @@ -306,16 +307,17 @@ async def main():
# ListUsers
print("Listing user who have access to object")

response = await fga_client.list_objects(
response = await fga_client.list_users(
ClientListUsersRequest(
relation="viewer",
object=FgaObject(type="document", id="roadmap"),
user_filters=[
FgaObject(type="user"),
UserTypeFilter(type="user"),
],
context=dict(ViewCount=100),
)
)
print(f"Users: {response.objects}")
print(f"Users: {response.users}")

# WriteAssertions
await fga_client.write_assertions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ class OpenFgaClient:
options = set_heading_if_not_set(options, CLIENT_METHOD_HEADER, "ReadLatestAuthorizationModel")
options["page_size"] = 1
api_response = {{#asyncio}}await {{/asyncio}}self.read_authorization_models(options)
return ReadAuthorizationModelResponse(api_response.authorization_models[0])
model = api_response.authorization_models[0] if len(api_response.authorization_models) > 0 else None
return ReadAuthorizationModelResponse(model)

#######################
# Relationship Tuples
Expand Down Expand Up @@ -683,9 +684,7 @@ class OpenFgaClient:
)

if body.contextual_tuples:
req_body.contextual_tuples = ContextualTupleKeys(
tuple_keys=convert_tuple_keys(body.contextual_tuples)
)
req_body.contextual_tuples = convert_tuple_keys(body.contextual_tuples)

api_response = {{#asyncio}}await {{/asyncio}}self._api.list_users(body=req_body, **kwargs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ class OpenFgaClient:
options = set_heading_if_not_set(options, CLIENT_METHOD_HEADER, "ReadLatestAuthorizationModel")
options["page_size"] = 1
api_response = self.read_authorization_models(options)
return ReadAuthorizationModelResponse(api_response.authorization_models[0])
model = api_response.authorization_models[0] if len(api_response.authorization_models) > 0 else None
return ReadAuthorizationModelResponse(model)

#######################
# Relationship Tuples
Expand Down Expand Up @@ -653,9 +654,7 @@ class OpenFgaClient:
)

if body.contextual_tuples:
req_body.contextual_tuples = ContextualTupleKeys(
tuple_keys=convert_tuple_keys(body.contextual_tuples)
)
req_body.contextual_tuples = convert_tuple_keys(body.contextual_tuples)

api_response = self._api.list_users(body=req_body, **kwargs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,39 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
)



@patch.object(rest.RESTClientObject, 'request')
{{#asyncio}}async {{/asyncio}}def test_read_latest_authorization_model_with_no_models(self, mock_request):
"""Test case for read_latest_authorization_model when no models are in the store

Return the latest authorization models configured for the store
"""
response_body = '''
{
"authorization_models": []
}
'''
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
# Enter a context with an instance of the API client
{{#asyncio}}async {{/asyncio}}with OpenFgaClient(configuration) as api_client:

# Return a particular version of an authorization model
api_response = {{#asyncio}}await {{/asyncio}}api_client.read_latest_authorization_model(
options={}
)
self.assertIsInstance(api_response, ReadAuthorizationModelResponse)
self.assertIsNone(api_response.authorization_model)
mock_request.assert_called_once_with(
'GET',
'http://api.{{sampleApiDomain}}/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models',
headers=ANY,
query_params=[('page_size', 1)],
_preload_content=ANY,
_request_timeout=None
)

@patch.object(rest.RESTClientObject, 'request')
{{#asyncio}}async {{/asyncio}}def test_read_changes(self, mock_request):
"""Test case for read_changes
Expand Down Expand Up @@ -2076,7 +2109,6 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
relation="can_read",
user_filters=[
UserTypeFilter(type="user"),
UserTypeFilter(type="team", relation="member"),
],
context={},
contextual_tuples=[
Expand Down Expand Up @@ -2137,22 +2169,19 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
"relation": "can_read",
"user_filters": [
{"type": "user"},
{"type": "team", "relation": "member"},
],
"contextual_tuples": {
"tuple_keys": [
{
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
"relation": "editor",
"object": "folder:product",
},
{
"user": "folder:product",
"relation": "parent",
"object": "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
},
]
},
"contextual_tuples": [
{
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
"relation": "editor",
"object": "folder:product",
},
{
"user": "folder:product",
"relation": "parent",
"object": "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
},
],
"context": {},
"consistency": "MINIMIZE_LATENCY",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,38 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
)


@patch.object(rest.RESTClientObject, 'request')
def test_read_latest_authorization_model_with_no_models(self, mock_request):
"""Test case for read_latest_authorization_model when no models are in the store

Return the latest authorization models configured for the store
"""
response_body = '''
{
"authorization_models": []
}
'''
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
# Enter a context with an instance of the API client
with OpenFgaClient(configuration) as api_client:

# Return a particular version of an authorization model
api_response = api_client.read_latest_authorization_model(
options={}
)
self.assertIsInstance(api_response, ReadAuthorizationModelResponse)
self.assertIsNone(api_response.authorization_model)
mock_request.assert_called_once_with(
'GET',
'http://api.{{sampleApiDomain}}/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models',
headers=ANY,
query_params=[('page_size', 1)],
_preload_content=ANY,
_request_timeout=None
)

@patch.object(rest.RESTClientObject, 'request')
def test_read_changes(self, mock_request):
"""Test case for read_changes
Expand Down Expand Up @@ -2085,7 +2117,6 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
body.relation = "can_read"
body.user_filters = [
UserTypeFilter(type="user"),
UserTypeFilter(type="team", relation="member"),
]
body.context = {}
body.contextual_tuples = [
Expand Down Expand Up @@ -2145,22 +2176,19 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
"relation": "can_read",
"user_filters": [
{"type": "user"},
{"type": "team", "relation": "member"},
],
"contextual_tuples": {
"tuple_keys": [
{
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
"relation": "editor",
"object": "folder:product",
},
{
"user": "folder:product",
"relation": "parent",
"object": "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
},
]
},
"contextual_tuples": [
{
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
"relation": "editor",
"object": "folder:product",
},
{
"user": "folder:product",
"relation": "parent",
"object": "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
},
],
"context": {},
"consistency": "MINIMIZE_LATENCY",
},
Expand Down
Loading