# coding: utf-8
"""
   Python SDK for OpenFGA

   API version: 0.1
   Website: https://openfga.dev
   Documentation: https://openfga.dev/docs
   Support: https://discord.gg/8naAwJfWN6
   License: [Apache-2.0](https://github.com/openfga/python-sdk/blob/main/LICENSE)

   NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.
"""

from unittest.mock import ANY
from unittest import IsolatedAsyncioTestCase
from mock import patch
from datetime import datetime

import urllib3

from openfga_sdk.sync.client.client import OpenFgaClient
from openfga_sdk.sync import rest
from openfga_sdk.client import ClientConfiguration
from openfga_sdk.client.models.assertion import ClientAssertion
from openfga_sdk.client.models.check_request import ClientCheckRequest
from openfga_sdk.client.models.tuple import ClientTuple
from openfga_sdk.client.models.write_request import ClientWriteRequest
from openfga_sdk.client.models.expand_request import ClientExpandRequest
from openfga_sdk.client.models.list_objects_request import ClientListObjectsRequest
from openfga_sdk.client.models.list_relations_request import ClientListRelationsRequest
from openfga_sdk.client.models.read_changes_request import ClientReadChangesRequest
from openfga_sdk.client.models.write_single_response import ClientWriteSingleResponse
from openfga_sdk.client.models.write_transaction_opts import WriteTransactionOpts
from openfga_sdk.exceptions import ValidationException, FgaValidationException, UnauthorizedException
from openfga_sdk.models.assertion import Assertion
from openfga_sdk.models.authorization_model import AuthorizationModel
from openfga_sdk.models.check_response import CheckResponse
from openfga_sdk.models.create_store_request import CreateStoreRequest
from openfga_sdk.models.create_store_response import CreateStoreResponse
from openfga_sdk.models.expand_response import ExpandResponse
from openfga_sdk.models.get_store_response import GetStoreResponse
from openfga_sdk.models.leaf import Leaf
from openfga_sdk.models.list_objects_response import ListObjectsResponse
from openfga_sdk.models.list_stores_response import ListStoresResponse
from openfga_sdk.models.node import Node
from openfga_sdk.models.object_relation import ObjectRelation
from openfga_sdk.models.read_assertions_response import ReadAssertionsResponse
from openfga_sdk.models.read_authorization_model_response import ReadAuthorizationModelResponse
from openfga_sdk.models.read_authorization_models_response import ReadAuthorizationModelsResponse
from openfga_sdk.models.read_changes_response import ReadChangesResponse
from openfga_sdk.models.read_response import ReadResponse
from openfga_sdk.models.store import Store
from openfga_sdk.models.tuple import Tuple
from openfga_sdk.models.tuple_change import TupleChange
from openfga_sdk.models.tuple_key import TupleKey
from openfga_sdk.models.tuple_operation import TupleOperation
from openfga_sdk.models.type_definition import TypeDefinition
from openfga_sdk.models.users import Users
from openfga_sdk.models.userset import Userset
from openfga_sdk.models.usersets import Usersets
from openfga_sdk.models.userset_tree import UsersetTree
from openfga_sdk.models.validation_error_message_response import ValidationErrorMessageResponse
from openfga_sdk.models.write_authorization_model_request import WriteAuthorizationModelRequest
from openfga_sdk.models.write_authorization_model_response import WriteAuthorizationModelResponse


store_id = '01YCP46JKYM8FJCQ37NMBYHE5X'
request_id = 'x1y2z3'

# Helper function to construct mock response


def http_mock_response(body, status):
    headers = urllib3.response.HTTPHeaderDict({
        'content-type': 'application/json',
        'Fga-Request-Id': request_id
    })
    return urllib3.HTTPResponse(
        body.encode('utf-8'),
        headers,
        status,
        preload_content=False
    )


def mock_response(body, status):
    obj = http_mock_response(body, status)
    return rest.RESTResponse(obj, obj.data)


class TestOpenFgaClient(IsolatedAsyncioTestCase):
    """Test for OpenFGA Client"""

    def setUp(self):
        self.configuration = ClientConfiguration(
            api_scheme='http',
            api_host="api.fga.example",
        )

    def tearDown(self):
        pass

    @patch.object(rest.RESTClientObject, 'request')
    def test_list_stores(self, mock_request):
        """Test case for list_stores

        Get all stores  # noqa: E501
        """
        response_body = '''
{
  "stores": [
    {
      "id": "01YCP46JKYM8FJCQ37NMBYHE5X",
      "name": "store1",
      "created_at": "2022-07-25T21:15:37.524Z",
      "updated_at": "2022-07-25T21:15:37.524Z",
      "deleted_at": "2022-07-25T21:15:37.524Z"
    },
    {
      "id": "01YCP46JKYM8FJCQ37NMBYHE6X",
      "name": "store2",
      "created_at": "2022-07-25T21:15:37.524Z",
      "updated_at": "2022-07-25T21:15:37.524Z",
      "deleted_at": "2022-07-25T21:15:37.524Z"
    }
  ],
  "continuation_token": "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ=="
}
            '''
        mock_request.return_value = mock_response(response_body, 200)
        configuration = self.configuration
        with OpenFgaClient(configuration) as api_client:
            api_response = api_client.list_stores(
                options={"page_size": 1, "continuation_token": "continuation_token_example"}
            )
            self.assertIsInstance(api_response, ListStoresResponse)
            self.assertEqual(api_response.continuation_token,
                             "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ==")
            store1 = Store(
                id="01YCP46JKYM8FJCQ37NMBYHE5X",
                name="store1",
                created_at=datetime.fromisoformat("2022-07-25T21:15:37.524+00:00"),
                updated_at=datetime.fromisoformat("2022-07-25T21:15:37.524+00:00"),
                deleted_at=datetime.fromisoformat("2022-07-25T21:15:37.524+00:00"),
            )
            store2 = Store(
                id="01YCP46JKYM8FJCQ37NMBYHE6X",
                name="store2",
                created_at=datetime.fromisoformat("2022-07-25T21:15:37.524+00:00"),
                updated_at=datetime.fromisoformat("2022-07-25T21:15:37.524+00:00"),
                deleted_at=datetime.fromisoformat("2022-07-25T21:15:37.524+00:00"),
            )

            stores = [store1, store2]
            self.assertEqual(api_response.stores, stores)
            mock_request.assert_called_once_with(
                'GET',
                'http://api.fga.example/stores',
                headers=ANY,
                query_params=[('page_size', 1), ('continuation_token',
                                                 'continuation_token_example')],
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_create_store(self, mock_request):
        """Test case for create_store

        Create a store  # noqa: E501
        """
        response_body = '''{
            "id": "01YCP46JKYM8FJCQ37NMBYHE5X",
            "name": "test_store",
            "created_at": "2022-07-25T17:41:26.607Z",
            "updated_at": "2022-07-25T17:41:26.607Z"}
            '''
        mock_request.return_value = mock_response(response_body, 201)
        configuration = self.configuration
        with OpenFgaClient(configuration) as api_client:
            api_response = api_client.create_store(
                CreateStoreRequest(name="test-store"),
                options={}
            )
            self.assertIsInstance(api_response, CreateStoreResponse)
            self.assertEqual(api_response.id, '01YCP46JKYM8FJCQ37NMBYHE5X')
            mock_request.assert_called_once_with(
                'POST',
                'http://api.fga.example/stores',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"name": "test-store"},
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_get_store(self, mock_request):
        """Test case for get_store

        Get all stores  # noqa: E501
        """
        response_body = '''
{
    "id": "01YCP46JKYM8FJCQ37NMBYHE5X",
    "name": "store1",
    "created_at": "2022-07-25T21:15:37.524Z",
    "updated_at": "2022-07-25T21:15:37.524Z"
}
            '''
        mock_request.return_value = mock_response(response_body, 200)
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            api_response = api_client.get_store(
                options={}
            )
            self.assertIsInstance(api_response, GetStoreResponse)
            self.assertEqual(api_response.id, "01YCP46JKYM8FJCQ37NMBYHE5X")
            self.assertEqual(api_response.name, "store1")
            mock_request.assert_called_once_with(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_delete_store(self, mock_request):
        """Test case for delete_store

        Get all stores  # noqa: E501
        """
        mock_request.return_value = mock_response('', 201)
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            api_client.delete_store(
                options={}
            )
            mock_request.assert_called_once_with(
                'DELETE',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X',
                headers=ANY,
                query_params=[],
                body=None,
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_read_authorization_models(self, mock_request):
        """Test case for read_authorization_models

        Return all authorization models configured for the store  # noqa: E501
        """
        response_body = '''
{
  "authorization_models": [{
    "id": "01G5JAVJ41T49E9TT3SKVS7X1J",
    "schema_version":"1.1",
    "type_definitions": [
      {
        "type": "document",
        "relations": {
          "reader": {
            "union": {
              "child": [
                {
                  "this": {}
                },
                {
                  "computedUserset": {
                    "object": "",
                    "relation": "writer"
                  }
                }
              ]
            }
          },
          "writer": {
            "this": {}
          }
        }
      }
    ]
  }],
  "continuation_token": "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ=="
}
        '''
        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_authorization_models(
                options={}
            )
            self.assertIsInstance(api_response, ReadAuthorizationModelsResponse)
            type_definitions = [
                TypeDefinition(
                    type="document",
                    relations=dict(
                        reader=Userset(
                            union=Usersets(
                                child=[
                                    Userset(this=dict()),
                                    Userset(computed_userset=ObjectRelation(
                                        object="",
                                        relation="writer",
                                    )),
                                ],
                            ),
                        ),
                        writer=Userset(
                            this=dict(),
                        ),
                    )
                )
            ]
            authorization_model = AuthorizationModel(id='01G5JAVJ41T49E9TT3SKVS7X1J', schema_version="1.1",
                                                     type_definitions=type_definitions)
            self.assertEqual(api_response.authorization_models, [authorization_model])
            self.assertEqual(api_response.continuation_token,
                             "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ==")
            mock_request.assert_called_once_with(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_write_authorization_model(self, mock_request):
        """Test case for write_authorization_model

        Create a new authorization model  # noqa: E501
        """
        response_body = '{"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"}'
        mock_request.return_value = mock_response(response_body, 201)
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            # example passing only required values which don't have defaults set
            body = WriteAuthorizationModelRequest(
                schema_version="1.1",
                type_definitions=[
                    TypeDefinition(
                        type="document",
                        relations=dict(
                            writer=Userset(
                                this=dict(),
                            ),
                            reader=Userset(
                                union=Usersets(
                                    child=[
                                        Userset(this=dict()),
                                        Userset(computed_userset=ObjectRelation(
                                            object="",
                                            relation="writer",
                                        )),
                                    ],
                                ),
                            ),
                        )
                    ),
                ],
            )
            # Create a new authorization model
            api_response = api_client.write_authorization_model(
                body,
                options={}
            )
            self.assertIsInstance(api_response, WriteAuthorizationModelResponse)
            expected_response = WriteAuthorizationModelResponse(
                authorization_model_id='01G5JAVJ41T49E9TT3SKVS7X1J'
            )
            self.assertEqual(api_response, expected_response)
            mock_request.assert_called_once_with(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"schema_version": "1.1", "type_definitions": [{"type": "document", "relations": {"writer": {"this": {
                }}, "reader": {"union": {"child": [{"this": {}}, {"computedUserset": {"object": "", "relation": "writer"}}]}}}}]},
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_read_authorization_model(self, mock_request):
        """Test case for read_authorization_model

        Return a particular version of an authorization model  # noqa: E501
        """
        response_body = '''
{
  "authorization_model": {
    "id": "01G5JAVJ41T49E9TT3SKVS7X1J",
    "schema_version":"1.1",
    "type_definitions": [
      {
        "type": "document",
        "relations": {
          "reader": {
            "union": {
              "child": [
                {
                  "this": {}
                },
                {
                  "computedUserset": {
                    "object": "",
                    "relation": "writer"
                  }
                }
              ]
            }
          },
          "writer": {
            "this": {}
          }
        }
      }
    ]
  }
}
        '''
        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_authorization_model(
                options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"}
            )
            self.assertIsInstance(api_response, ReadAuthorizationModelResponse)
            type_definitions = [
                TypeDefinition(
                    type="document",
                    relations=dict(
                        reader=Userset(
                            union=Usersets(
                                child=[
                                    Userset(this=dict()),
                                    Userset(computed_userset=ObjectRelation(
                                        object="",
                                        relation="writer",
                                    )),
                                ],
                            ),
                        ),
                        writer=Userset(
                            this=dict(),
                        ),
                    )
                )
            ]
            authorization_model = AuthorizationModel(id='01G5JAVJ41T49E9TT3SKVS7X1J', schema_version="1.1",
                                                     type_definitions=type_definitions)
            self.assertEqual(api_response.authorization_model, authorization_model)
            mock_request.assert_called_once_with(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models/01G5JAVJ41T49E9TT3SKVS7X1J',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_read_latest_authorization_model(self, mock_request):
        """Test case for read_latest_authorization_model

        Return the latest authorization models configured for the store  # noqa: E501
        """
        response_body = '''
{
  "authorization_models": [{
    "id": "01G5JAVJ41T49E9TT3SKVS7X1J",
    "schema_version":"1.1",
    "type_definitions": [
      {
        "type": "document",
        "relations": {
          "reader": {
            "union": {
              "child": [
                {
                  "this": {}
                },
                {
                  "computedUserset": {
                    "object": "",
                    "relation": "writer"
                  }
                }
              ]
            }
          },
          "writer": {
            "this": {}
          }
        }
      }
    ]
  }],
  "continuation_token": "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ=="
}
        '''
        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)
            type_definitions = [
                TypeDefinition(
                    type="document",
                    relations=dict(
                        reader=Userset(
                            union=Usersets(
                                child=[
                                    Userset(this=dict()),
                                    Userset(computed_userset=ObjectRelation(
                                        object="",
                                        relation="writer",
                                    )),
                                ],
                            ),
                        ),
                        writer=Userset(
                            this=dict(),
                        ),
                    )
                )
            ]
            authorization_model = AuthorizationModel(id='01G5JAVJ41T49E9TT3SKVS7X1J', schema_version="1.1",
                                                     type_definitions=type_definitions)
            self.assertEqual(api_response.authorization_model, authorization_model)
            mock_request.assert_called_once_with(
                'GET',
                'http://api.fga.example/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

        Return a list of all the tuple changes  # noqa: E501
        """
        response_body = '''
{
  "changes": [
    {
      "tuple_key": {
        "object": "document:2021-budget",
        "relation": "reader",
        "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"
      },
      "operation": "TUPLE_OPERATION_WRITE",
      "timestamp": "2022-07-26T15:55:55.809Z"
    }
  ],
  "continuation_token": "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ=="
}
        '''
        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_changes(
                ClientReadChangesRequest("document"),
                options={"page_size": 1, "continuation_token": "abcdefg"}
            )

            self.assertIsInstance(api_response, ReadChangesResponse)
            changes = TupleChange(
                tuple_key=TupleKey(object="document:2021-budget", relation="reader",
                                   user="user:81684243-9356-4421-8fbf-a4f8d36aa31b"),
                operation=TupleOperation.WRITE,
                timestamp=datetime.fromisoformat("2022-07-26T15:55:55.809+00:00"))
            read_changes = ReadChangesResponse(
                continuation_token='eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ==',
                changes=[changes])
            self.assertEqual(api_response, read_changes)
            mock_request.assert_called_once_with(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/changes',
                headers=ANY,
                query_params=[('type', 'document'), ('page_size', 1),
                              ('continuation_token', 'abcdefg')],
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_read(self, mock_request):
        """Test case for read

        Get tuples from the store that matches a query, without following userset rewrite rules  # noqa: E501
        """
        response_body = '''
            {
  "tuples": [
    {
      "key": {
        "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
        "relation": "reader",
        "object": "document:2021-budget"
      },
      "timestamp": "2021-10-06T15:32:11.128Z"
    }
  ]
}
        '''
        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:
            body = TupleKey(
                object="document:2021-budget",
                relation="reader",
                user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
            )
            api_response = api_client.read(
                body=body,
                options={"page_size": 50, "continuation_token":
                         "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ=="}
            )
            self.assertIsInstance(api_response, ReadResponse)
            key = TupleKey(user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                           relation="reader", object="document:2021-budget")
            timestamp = datetime.fromisoformat("2021-10-06T15:32:11.128+00:00")
            expected_data = ReadResponse(tuples=[Tuple(key=key, timestamp=timestamp)])
            self.assertEqual(api_response, expected_data)
            mock_request.assert_called_once_with(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/read',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"object": "document:2021-budget", "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"},
                      "page_size": 50, "continuation_token": "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ=="},
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_read_empty_options(self, mock_request):
        """Test case for read with empty options

        Get tuples from the store that matches a query, without following userset rewrite rules  # noqa: E501
        """
        response_body = '''
            {
  "tuples": [
    {
      "key": {
        "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
        "relation": "reader",
        "object": "document:2021-budget"
      },
      "timestamp": "2021-10-06T15:32:11.128Z"
    }
  ]
}
        '''
        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:
            body = TupleKey(
                object="document:2021-budget",
                relation="reader",
                user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
            )
            api_response = api_client.read(
                body=body,
                options={}
            )
            self.assertIsInstance(api_response, ReadResponse)
            key = TupleKey(user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                           relation="reader", object="document:2021-budget")
            timestamp = datetime.fromisoformat("2021-10-06T15:32:11.128+00:00")
            expected_data = ReadResponse(tuples=[Tuple(key=key, timestamp=timestamp)])
            self.assertEqual(api_response, expected_data)
            mock_request.assert_called_once_with(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/read',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"object": "document:2021-budget", "relation": "reader",
                                    "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}},
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_write(self, mock_request):
        """Test case for write

        Add tuples from the store with transaction enabled
        """
        response_body = '{}'
        mock_request.return_value = mock_response(response_body, 200)
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:

            body = ClientWriteRequest(
                writes=[
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                    ),
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31c",
                    ),
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
                    )
                ],
            )
            api_client.write(
                body,
                options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"}
            )
            mock_request.assert_called_once_with(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"writes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}, {"object": "document:2021-budget", "relation": "reader",
                                                                                                                                                                "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31c"}, {"object": "document:2021-budget", "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31d"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_delete(self, mock_request):
        """Test case for delete

        Delete tuples from the store with transaction enabled
        """
        response_body = '{}'
        mock_request.return_value = mock_response(response_body, 200)
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:

            body = ClientWriteRequest(
                deletes=[
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                    )
                ],
            )
            api_client.write(
                body,
                options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"}
            )
            mock_request.assert_called_once_with(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"deletes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader",
                                                  "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_write_batch(self, mock_request):
        """Test case for write

        Add tuples from the store with transaction disabled
        """
        mock_request.side_effect = [
            mock_response('{}', 200),
            mock_response('{}', 200),
            mock_response('{}', 200),
            mock_response('{}', 200),
        ]
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:

            body = ClientWriteRequest(
                writes=[
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                    ),
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31c",
                    ),
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
                    )
                ],
            )
            transaction = WriteTransactionOpts(
                disabled=True, max_per_chunk=1, max_parallel_requests=10)
            response = api_client.write(
                body,
                options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J",
                         "transaction": transaction}
            )

            self.assertEqual(response.deletes, None)
            self.assertEqual(response.writes,
                             [
                                 ClientWriteSingleResponse(
                                     tuple_key=ClientTuple(
                                         object="document:2021-budget",
                                         relation="reader",
                                         user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                                     ),
                                     success=True,
                                     error=None),
                                 ClientWriteSingleResponse(
                                     tuple_key=ClientTuple(
                                         object="document:2021-budget",
                                         relation="reader",
                                         user="user:81684243-9356-4421-8fbf-a4f8d36aa31c",
                                     ),
                                     success=True,
                                     error=None),
                                 ClientWriteSingleResponse(
                                     tuple_key=ClientTuple(
                                         object="document:2021-budget",
                                         relation="reader",
                                         user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
                                     ),
                                     success=True,
                                     error=None)
                             ]
                             )
            self.assertEqual(mock_request.call_count, 4)
            mock_request.assert_any_call(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models/01G5JAVJ41T49E9TT3SKVS7X1J',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"writes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader",
                                                 "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"writes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader",
                                                 "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31c"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"writes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader",
                                                 "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31d"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_write_batch_min_parallel(self, mock_request):
        """Test case for write

        Add tuples from the store with transaction disabled and minimum parallel request
        """
        mock_request.side_effect = [
            mock_response('{}', 200),
            mock_response('{}', 200),
            mock_response('{}', 200),
            mock_response('{}', 200),
        ]
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:

            body = ClientWriteRequest(
                writes=[
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                    ),
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31c",
                    ),
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
                    )
                ],
            )
            transaction = WriteTransactionOpts(
                disabled=True, max_per_chunk=1, max_parallel_requests=1)
            response = api_client.write(
                body,
                options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J",
                         "transaction": transaction}
            )

            self.assertEqual(response.deletes, None)
            self.assertEqual(response.writes,
                             [
                                 ClientWriteSingleResponse(
                                     tuple_key=ClientTuple(
                                         object="document:2021-budget",
                                         relation="reader",
                                         user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                                     ),
                                     success=True,
                                     error=None),
                                 ClientWriteSingleResponse(
                                     tuple_key=ClientTuple(
                                         object="document:2021-budget",
                                         relation="reader",
                                         user="user:81684243-9356-4421-8fbf-a4f8d36aa31c",
                                     ),
                                     success=True,
                                     error=None),
                                 ClientWriteSingleResponse(
                                     tuple_key=ClientTuple(
                                         object="document:2021-budget",
                                         relation="reader",
                                         user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
                                     ),
                                     success=True,
                                     error=None)
                             ]
                             )
            self.assertEqual(mock_request.call_count, 4)
            mock_request.assert_any_call(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models/01G5JAVJ41T49E9TT3SKVS7X1J',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"writes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader",
                                                 "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"writes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader",
                                                 "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31c"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"writes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader",
                                                 "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31d"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_write_batch_larger_chunk(self, mock_request):
        """Test case for write

        Add tuples from the store with transaction disabled and minimum parallel request
        """
        mock_request.side_effect = [
            mock_response('{}', 200),
            mock_response('{}', 200),
            mock_response('{}', 200),
        ]
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:

            body = ClientWriteRequest(
                writes=[
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                    ),
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31c",
                    ),
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
                    )
                ],
            )
            transaction = WriteTransactionOpts(
                disabled=True, max_per_chunk=2, max_parallel_requests=2)
            response = api_client.write(
                body,
                options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J",
                         "transaction": transaction}
            )

            self.assertEqual(response.deletes, None)
            self.assertEqual(response.writes,
                             [
                                 ClientWriteSingleResponse(
                                     tuple_key=ClientTuple(
                                         object="document:2021-budget",
                                         relation="reader",
                                         user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                                     ),
                                     success=True,
                                     error=None),
                                 ClientWriteSingleResponse(
                                     tuple_key=ClientTuple(
                                         object="document:2021-budget",
                                         relation="reader",
                                         user="user:81684243-9356-4421-8fbf-a4f8d36aa31c",
                                     ),
                                     success=True,
                                     error=None),
                                 ClientWriteSingleResponse(
                                     tuple_key=ClientTuple(
                                         object="document:2021-budget",
                                         relation="reader",
                                         user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
                                     ),
                                     success=True,
                                     error=None)
                             ]
                             )
            self.assertEqual(mock_request.call_count, 3)
            mock_request.assert_any_call(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models/01G5JAVJ41T49E9TT3SKVS7X1J',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"writes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}, {
                    "object": "document:2021-budget", "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31c"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"writes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader",
                                                 "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31d"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_write_batch_failed(self, mock_request):
        """Test case for write

        Add tuples from the store with transaction disabled where one of the request failed
        """
        response_body = '''
{
  "code": "validation_error",
  "message": "Generic validation error"
}
        '''

        mock_request.side_effect = [
            mock_response('{}', 200),
            mock_response('{}', 200),
            ValidationException(http_resp=http_mock_response(response_body, 400)),
            mock_response('{}', 200),
        ]
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:

            body = ClientWriteRequest(
                writes=[
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                    ),
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31c",
                    ),
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
                    )
                ],
            )
            transaction = WriteTransactionOpts(
                disabled=True, max_per_chunk=1, max_parallel_requests=10)
            response = api_client.write(
                body,
                options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J",
                         "transaction": transaction}
            )

            self.assertEqual(response.deletes, None)
            self.assertEqual(len(response.writes), 3)
            self.assertEqual(response.writes[0],
                             ClientWriteSingleResponse(
                tuple_key=ClientTuple(
                    object="document:2021-budget",
                    relation="reader",
                    user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                ),
                success=True,
                error=None))
            self.assertEqual(response.writes[1].tuple_key,
                             ClientTuple(
                object="document:2021-budget",
                relation="reader",
                user="user:81684243-9356-4421-8fbf-a4f8d36aa31c",
            ))
            self.assertFalse(response.writes[1].success)
            self.assertIsInstance(response.writes[1].error, ValidationException)
            self.assertIsInstance(
                response.writes[1].error.parsed_exception, ValidationErrorMessageResponse)
            self.assertEqual(response.writes[2],
                             ClientWriteSingleResponse(
                tuple_key=ClientTuple(
                    object="document:2021-budget",
                    relation="reader",
                    user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
                ),
                success=True,
                error=None))
            self.assertEqual(mock_request.call_count, 4)
            mock_request.assert_any_call(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models/01G5JAVJ41T49E9TT3SKVS7X1J',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"writes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader",
                                                 "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"writes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader",
                                                 "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31c"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"writes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader",
                                                 "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31d"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_delete_batch(self, mock_request):
        """Test case for delete

        Delete tuples from the store with transaction disabled but there is only 1 relationship tuple
        """
        mock_request.side_effect = [
            mock_response('{}', 200),
            mock_response('{}', 200),
        ]
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:

            body = ClientWriteRequest(
                deletes=[
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                    )
                ],
            )
            transaction = WriteTransactionOpts(
                disabled=True, max_per_chunk=1, max_parallel_requests=10)
            api_client.write(
                body,
                options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J",
                         "transaction": transaction}
            )
            mock_request.assert_any_call(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models/01G5JAVJ41T49E9TT3SKVS7X1J',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"deletes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader",
                                                  "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_write_tuples(self, mock_request):
        """Test case for write tuples

        Add tuples from the store with transaction enabled
        """
        response_body = '{}'
        mock_request.return_value = mock_response(response_body, 200)
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:

            api_client.write_tuples(
                [
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                    ),
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31c",
                    ),
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
                    )
                ],
                options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"}
            )
            mock_request.assert_called_once_with(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"writes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}, {"object": "document:2021-budget", "relation": "reader",
                                                                                                                                                                "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31c"}, {"object": "document:2021-budget", "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31d"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_delete_tuples(self, mock_request):
        """Test case for delete tuples

        Add tuples from the store with transaction enabled
        """
        response_body = '{}'
        mock_request.return_value = mock_response(response_body, 200)
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:

            api_client.delete_tuples(
                [
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                    ),
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31c",
                    ),
                    ClientTuple(
                        object="document:2021-budget",
                        relation="reader",
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
                    )
                ],
                options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"}
            )
            mock_request.assert_called_once_with(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"deletes": {"tuple_keys": [{"object": "document:2021-budget", "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}, {"object": "document:2021-budget", "relation": "reader",
                                                                                                                                                                 "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31c"}, {"object": "document:2021-budget", "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31d"}]}, "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"},
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_write_batch_unauthorized(self, mock_request):
        """Test case for write with 401 response
        """

        mock_request.side_effect = UnauthorizedException(
            http_resp=http_mock_response('{}', 401)
        )
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            with self.assertRaises(UnauthorizedException) as api_exception:
                body = ClientWriteRequest(
                    writes=[
                        ClientTuple(
                            object="document:2021-budget",
                            relation="reader",
                            user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                        )
                    ],
                )
                transaction = WriteTransactionOpts(
                    disabled=True, max_per_chunk=1, max_parallel_requests=10)
                api_client.write(
                    body,
                    options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J",
                             "transaction": transaction}
                )

            self.assertIsInstance(api_exception.exception, UnauthorizedException)
            mock_request.assert_called()
            self.assertEqual(mock_request.call_count, 1)

            mock_request.assert_called_once_with(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models/01G5JAVJ41T49E9TT3SKVS7X1J',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_check(self, mock_request):
        """Test case for check

        Check whether a user is authorized to access an object  # noqa: E501
        """

        # First, mock the response
        response_body = '{"allowed": true, "resolution": "1234"}'
        mock_request.return_value = mock_response(response_body, 200)
        body = ClientCheckRequest(
            user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
            relation="reader",
            object="document:budget",
            contextual_tuples=[
                ClientTuple(
                    user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                    relation="writer",
                    object="document:budget",
                ),
            ],
        )
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            api_response = api_client.check(
                body=body,
                options={"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"}
            )
            self.assertIsInstance(api_response, CheckResponse)
            self.assertTrue(api_response.allowed)
            # Make sure the API was called with the right data
            mock_request.assert_called_once_with(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/check',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b", "relation": "reader", "object": "document:budget"},
                      "contextual_tuples": {"tuple_keys": [{"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b", "relation": "writer", "object": "document:budget"}]},
                      "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_check_config_auth_model(self, mock_request):
        """Test case for check

        Check whether a user is authorized to access an object and the auth model is already encoded in store  # noqa: E501
        """

        # First, mock the response
        response_body = '{"allowed": true, "resolution": "1234"}'
        mock_request.return_value = mock_response(response_body, 200)
        body = ClientCheckRequest(
            object="document:2021-budget",
            relation="reader",
            user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
        )
        configuration = self.configuration
        configuration.store_id = store_id
        configuration.authorization_model_id = "01GXSA8YR785C4FYS3C0RTG7B1"
        with OpenFgaClient(configuration) as api_client:
            api_response = api_client.check(
                body=body,
                options={}
            )
            self.assertIsInstance(api_response, CheckResponse)
            self.assertTrue(api_response.allowed)
            # Make sure the API was called with the right data
            mock_request.assert_called_once_with(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/check',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"object": "document:2021-budget",
                                    "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}, "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_batch_check_single_request(self, mock_request):
        """Test case for check with single request

        Check whether a user is authorized to access an object  # noqa: E501
        """

        # First, mock the response
        response_body = '{"allowed": true, "resolution": "1234"}'
        mock_request.side_effect = [
            mock_response('{}', 200),
            mock_response(response_body, 200),
        ]
        body = ClientCheckRequest(
            object="document:2021-budget",
            relation="reader",
            user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
        )
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            api_response = api_client.batch_check(
                body=[body],
                options={"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"}
            )
            self.assertIsInstance(api_response, list)
            self.assertEqual(len(api_response), 1)
            self.assertEqual(api_response[0].error, None)
            self.assertTrue(api_response[0].allowed)
            self.assertEqual(api_response[0].request, body)
            # Make sure the API was called with the right data
            mock_request.assert_any_call(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models/01GXSA8YR785C4FYS3C0RTG7B1',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/check',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"object": "document:2021-budget",
                                    "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}, "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_batch_check_multiple_request(self, mock_request):
        """Test case for check with multiple request

        Check whether a user is authorized to access an object  # noqa: E501
        """

        # First, mock the response
        mock_request.side_effect = [
            mock_response('{}', 200),
            mock_response('{"allowed": true, "resolution": "1234"}', 200),
            mock_response('{"allowed": false, "resolution": "1234"}', 200),
            mock_response('{"allowed": true, "resolution": "1234"}', 200),
        ]
        body1 = ClientCheckRequest(
            object="document:2021-budget",
            relation="reader",
            user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
        )
        body2 = ClientCheckRequest(
            object="document:2021-budget",
            relation="reader",
            user="user:81684243-9356-4421-8fbf-a4f8d36aa31c",
        )
        body3 = ClientCheckRequest(
            object="document:2021-budget",
            relation="reader",
            user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
        )
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            api_response = api_client.batch_check(
                body=[body1, body2, body3],
                options={"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1",
                         "max_parallel_requests": 2}
            )
            self.assertIsInstance(api_response, list)
            self.assertEqual(len(api_response), 3)
            self.assertEqual(api_response[0].error, None)
            self.assertTrue(api_response[0].allowed)
            self.assertEqual(api_response[0].request, body1)
            self.assertEqual(api_response[1].error, None)
            self.assertFalse(api_response[1].allowed)
            self.assertEqual(api_response[1].request, body2)
            self.assertEqual(api_response[2].error, None)
            self.assertTrue(api_response[2].allowed)
            self.assertEqual(api_response[2].request, body3)
            # Make sure the API was called with the right data
            mock_request.assert_any_call(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models/01GXSA8YR785C4FYS3C0RTG7B1',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/check',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"object": "document:2021-budget",
                                    "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}, "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/check',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"object": "document:2021-budget",
                                    "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31c"}, "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/check',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"object": "document:2021-budget",
                                    "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31d"}, "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_batch_check_multiple_request_fail(self, mock_request):
        """Test case for check with multiple request with one request failed

        Check whether a user is authorized to access an object  # noqa: E501
        """
        response_body = '''
{
  "code": "validation_error",
  "message": "Generic validation error"
}
        '''

        # First, mock the response
        mock_request.side_effect = [
            mock_response('{}', 200),
            mock_response('{"allowed": true, "resolution": "1234"}', 200),
            ValidationException(http_resp=http_mock_response(response_body, 400)),
            mock_response('{"allowed": false, "resolution": "1234"}', 200),
        ]
        body1 = ClientCheckRequest(
            object="document:2021-budget",
            relation="reader",
            user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
        )
        body2 = ClientCheckRequest(
            object="document:2021-budget",
            relation="reader",
            user="user:81684243-9356-4421-8fbf-a4f8d36aa31c",
        )
        body3 = ClientCheckRequest(
            object="document:2021-budget",
            relation="reader",
            user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
        )
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            api_response = api_client.batch_check(
                body=[body1, body2, body3],
                options={"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1",
                         "max_parallel_requests": 2}
            )
            self.assertIsInstance(api_response, list)
            self.assertEqual(len(api_response), 3)
            self.assertEqual(api_response[0].error, None)
            self.assertTrue(api_response[0].allowed)
            self.assertEqual(api_response[0].request, body1)
            self.assertFalse(api_response[1].allowed)
            self.assertEqual(api_response[1].request, body2)
            self.assertIsInstance(api_response[1].error, ValidationException)
            self.assertIsInstance(
                api_response[1].error.parsed_exception, ValidationErrorMessageResponse)
            self.assertEqual(api_response[2].error, None)
            self.assertFalse(api_response[2].allowed)
            self.assertEqual(api_response[2].request, body3)
            # Make sure the API was called with the right data
            mock_request.assert_any_call(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models/01GXSA8YR785C4FYS3C0RTG7B1',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/check',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"object": "document:2021-budget",
                                    "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}, "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/check',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"object": "document:2021-budget",
                                    "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31c"}, "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/check',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"object": "document:2021-budget",
                                    "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31d"}, "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_expand(self, mock_request):
        """Test case for expand

        Expand all relationships in userset tree format, and following userset rewrite rules.  Useful to reason about and debug a certain relationship  # noqa: E501
        """
        response_body = '''{
            "tree": {"root": {"name": "document:budget#reader", "leaf": {"users": {"users": ["user:81684243-9356-4421-8fbf-a4f8d36aa31b"]}}}}}
            '''
        mock_request.return_value = mock_response(response_body, 200)
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            body = ClientExpandRequest(
                object="document:budget",
                relation="reader",
            )
            api_response = api_client.expand(
                body=body,
                options={"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"}
            )
            self.assertIsInstance(api_response, ExpandResponse)
            curUsers = Users(users=["user:81684243-9356-4421-8fbf-a4f8d36aa31b"])
            leaf = Leaf(users=curUsers)
            node = Node(name="document:budget#reader", leaf=leaf)
            userTree = UsersetTree(node)
            expected_response = ExpandResponse(userTree)
            self.assertEqual(api_response, expected_response)
            mock_request.assert_called_once_with(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/expand',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"object": "document:budget", "relation": "reader"},
                      "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_list_objects(self, mock_request):
        """Test case for list_objects

        List objects  # noqa: E501
        """
        response_body = '''
{
  "objects": [
    "document:abcd1234"
  ]
}
            '''
        mock_request.return_value = mock_response(response_body, 200)
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            body = ClientListObjectsRequest(
                type="document",
                relation="reader",
                user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
            )
            # Get all stores
            api_response = api_client.list_objects(
                body, options={"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"})
            self.assertIsInstance(api_response, ListObjectsResponse)
            self.assertEqual(api_response.objects, ['document:abcd1234'])
            mock_request.assert_called_once_with(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/list-objects',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={'authorization_model_id': '01GXSA8YR785C4FYS3C0RTG7B1',
                      'type': 'document', 'relation': 'reader', 'user': 'user:81684243-9356-4421-8fbf-a4f8d36aa31b'},
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_list_objects_contextual_tuples(self, mock_request):
        """Test case for list_objects

        List objects  # noqa: E501
        """
        response_body = '''
{
  "objects": [
    "document:abcd1234"
  ]
}
            '''
        mock_request.return_value = mock_response(response_body, 200)
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            body = ClientListObjectsRequest(
                type="document",
                relation="reader",
                user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                contextual_tuples=[
                    ClientTuple(
                        user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                        relation="writer",
                        object="document:budget",
                    ),
                ],
            )
            # Get all stores
            api_response = api_client.list_objects(
                body, options={"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"})
            self.assertIsInstance(api_response, ListObjectsResponse)
            self.assertEqual(api_response.objects, ['document:abcd1234'])
            mock_request.assert_called_once_with(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/list-objects',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={'authorization_model_id': '01GXSA8YR785C4FYS3C0RTG7B1',
                      'type': 'document', 'relation': 'reader', 'user': 'user:81684243-9356-4421-8fbf-a4f8d36aa31b',
                      'contextual_tuples': {'tuple_keys': [{'object': 'document:budget', 'relation': 'writer', 'user': 'user:81684243-9356-4421-8fbf-a4f8d36aa31b'}]}},
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_list_relations(self, mock_request):
        """Test case for list relations

        Check whether a user is authorized to access an object  # noqa: E501
        """

        # First, mock the response
        mock_request.side_effect = [
            mock_response('{}', 200),
            mock_response('{"allowed": true, "resolution": "1234"}', 200),
            mock_response('{"allowed": false, "resolution": "1234"}', 200),
            mock_response('{"allowed": true, "resolution": "1234"}', 200),
        ]
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            api_response = api_client.list_relations(
                body=ClientListRelationsRequest(user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                                                relations=["reader", "owner", "viewer"],
                                                object="document:2021-budget"),
                options={"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"}
            )
            self.assertEqual(api_response, ["reader", "viewer"])

            # Make sure the API was called with the right data
            mock_request.assert_any_call(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models/01GXSA8YR785C4FYS3C0RTG7B1',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/check',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"object": "document:2021-budget",
                                    "relation": "reader", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}, "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/check',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"object": "document:2021-budget",
                                    "relation": "owner", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}, "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
                _preload_content=ANY,
                _request_timeout=None
            )
            mock_request.assert_any_call(
                'POST',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/check',
                headers=ANY,
                query_params=[],
                post_params=[],
                body={"tuple_key": {"object": "document:2021-budget",
                                    "relation": "viewer", "user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"}, "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"},
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_list_relations_unauthorized(self, mock_request):
        """Test case for list relations with 401 response
        """

        mock_request.side_effect = UnauthorizedException(
            http_resp=http_mock_response('{}', 401)
        )
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            with self.assertRaises(UnauthorizedException) as api_exception:
                api_client.list_relations(
                    body=ClientListRelationsRequest(user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
                                                    relations=["reader", "owner", "viewer"],
                                                    object="document:2021-budget"),
                    options={"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"}
                )

            self.assertIsInstance(api_exception.exception, UnauthorizedException)
            mock_request.assert_called()
            self.assertEqual(mock_request.call_count, 1)

            mock_request.assert_called_once_with(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/authorization-models/01GXSA8YR785C4FYS3C0RTG7B1',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )
            api_client.close()

    @patch.object(rest.RESTClientObject, 'request')
    def test_read_assertions(self, mock_request):
        """Test case for read assertions

        """
        response_body = '''
{
  "authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J",
  "assertions": [
    {
      "tuple_key": {
        "object": "document:2021-budget",
        "relation": "reader",
        "user": "user:anne"
      },
      "expectation": true
    }
  ]
}
        '''
        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:
            api_response = api_client.read_assertions(
                options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"}
            )
            self.assertEqual(api_response, ReadAssertionsResponse(
                authorization_model_id="01G5JAVJ41T49E9TT3SKVS7X1J",
                assertions=[Assertion(
                    tuple_key=TupleKey(object="document:2021-budget", relation="reader",
                                       user="user:anne"),
                    expectation=True,
                )]
            ))
            mock_request.assert_called_once_with(
                'GET',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/assertions/01G5JAVJ41T49E9TT3SKVS7X1J',
                headers=ANY,
                query_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_write_assertions(self, mock_request):
        """Test case for write assertions

        Get all stores  # noqa: E501
        """
        mock_request.return_value = mock_response('', 204)
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            api_client.write_assertions(
                [ClientAssertion(user="user:anne", relation="reader",
                                 object="document:2021-budget", expectation=True)],
                options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"}
            )
            mock_request.assert_called_once_with(
                'PUT',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/assertions/01G5JAVJ41T49E9TT3SKVS7X1J',
                headers=ANY,
                body={"assertions": [{"tuple_key": {"object": "document:2021-budget",
                                                    "relation": "reader", "user": "user:anne"}, "expectation": True}]},
                query_params=[],
                post_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_set_store_id(self, mock_request):
        """Test case for write assertions

        Get all stores  # noqa: E501
        """
        mock_request.return_value = mock_response('', 204)
        configuration = self.configuration
        configuration.store_id = store_id
        with OpenFgaClient(configuration) as api_client:
            api_client.set_store_id("01YCP46JKYM8FJCQ37NMBYHE5Y")

            api_client.write_assertions(
                [ClientAssertion(user="user:anne", relation="reader",
                                 object="document:2021-budget", expectation=True)],
                options={"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J"}
            )
            self.assertEqual(api_client.get_store_id(), "01YCP46JKYM8FJCQ37NMBYHE5Y")
            mock_request.assert_called_once_with(
                'PUT',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5Y/assertions/01G5JAVJ41T49E9TT3SKVS7X1J',
                headers=ANY,
                body={"assertions": [{"tuple_key": {"object": "document:2021-budget",
                                                    "relation": "reader", "user": "user:anne"}, "expectation": True}]},
                query_params=[],
                post_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_config_auth_model(self, mock_request):
        """Test case for write assertions

        Get all stores  # noqa: E501
        """
        mock_request.return_value = mock_response('', 204)
        configuration = self.configuration
        configuration.store_id = store_id
        configuration.authorization_model_id = "01G5JAVJ41T49E9TT3SKVS7X1J"
        with OpenFgaClient(configuration) as api_client:
            api_client.write_assertions(
                [ClientAssertion(user="user:anne", relation="reader",
                                 object="document:2021-budget", expectation=True)],
                options={}
            )
            self.assertEqual(api_client.get_authorization_model_id(), "01G5JAVJ41T49E9TT3SKVS7X1J")
            mock_request.assert_called_once_with(
                'PUT',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/assertions/01G5JAVJ41T49E9TT3SKVS7X1J',
                headers=ANY,
                body={"assertions": [{"tuple_key": {"object": "document:2021-budget",
                                                    "relation": "reader", "user": "user:anne"}, "expectation": True}]},
                query_params=[],
                post_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )

    @patch.object(rest.RESTClientObject, 'request')
    def test_update_auth_model(self, mock_request):
        """Test case for write assertions

        Get all stores  # noqa: E501
        """
        mock_request.return_value = mock_response('', 204)
        configuration = self.configuration
        configuration.store_id = store_id
        configuration.authorization_model_id = "01G5JAVJ41T49E9TT3SKVS7X1J"
        with OpenFgaClient(configuration) as api_client:
            api_client.set_authorization_model_id("01G5JAVJ41T49E9TT3SKVS7X2J")

            api_client.write_assertions(
                [ClientAssertion(user="user:anne", relation="reader",
                                 object="document:2021-budget", expectation=True)],
                options={}
            )
            mock_request.assert_called_once_with(
                'PUT',
                'http://api.fga.example/stores/01YCP46JKYM8FJCQ37NMBYHE5X/assertions/01G5JAVJ41T49E9TT3SKVS7X2J',
                headers=ANY,
                body={"assertions": [{"tuple_key": {"object": "document:2021-budget",
                                                    "relation": "reader", "user": "user:anne"}, "expectation": True}]},
                query_params=[],
                post_params=[],
                _preload_content=ANY,
                _request_timeout=None
            )

    def test_configuration_store_id_invalid(self):
        """
        Test whether ApiValueError is raised if host has query
        """
        configuration = ClientConfiguration(
            api_host='localhost',
            api_scheme='http',
            store_id="abcd"
        )
        self.assertRaises(FgaValidationException, configuration.is_valid)

    def test_configuration_authorization_model_id_invalid(self):
        """
        Test whether ApiValueError is raised if host has query
        """
        configuration = ClientConfiguration(
            api_host='localhost',
            api_scheme='http',
            store_id="01H15K9J85050XTEDPVM8DJM78",
            authorization_model_id="abcd"
        )
        self.assertRaises(FgaValidationException, configuration.is_valid)
