# coding: utf-8

"""
    Kestra EE

    All API operations, except for Superadmin-only endpoints, require a tenant identifier in the HTTP path.<br/> Endpoints designated as Superadmin-only are not tenant-scoped.

    The version of the OpenAPI document: v1
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


import unittest
from kestrapy import (
    Configuration,
    KestraClient,
    IAMRoleControllerApiRoleCreateOrUpdateRequest,
    IAMRoleControllerApiRoleCreateOrUpdateRequestPermissions,
    ApiAutocomplete,
    ApiIds,
)


class TestRolesApi(unittest.TestCase):
    """RolesApi unit test stubs"""

    def setUp(self) -> None:
        configuration = Configuration()
        configuration.host = "http://localhost:9902"
        configuration.username = "root@root.com"
        configuration.password = "Root!1234"

        self.kestra_client = KestraClient(configuration)
        self.tenant = "main"


    def tearDown(self) -> None:
        pass

    def test_autocomplete_roles(self) -> None:
        """Test case for autocomplete_roles

        List roles for autocomplete
        """
        prefix = "test_autocomplete_roles"
        permissions = IAMRoleControllerApiRoleCreateOrUpdateRequestPermissions()
        permissions.flow = ["READ"]

        role_req = IAMRoleControllerApiRoleCreateOrUpdateRequest(
            name=prefix + "_complete_roles",
            permissions=permissions,
            description="An example role"
        )

        created = self.kestra_client.roles.create_role(
            tenant=self.tenant,
            iam_role_controller_api_role_create_or_update_request=role_req
        )

        api_autocomplete = ApiAutocomplete(q=prefix)

        results = self.kestra_client.roles.autocomplete_roles(
            tenant=self.tenant,
            api_autocomplete=api_autocomplete
        )

        assert any(getattr(r, 'id', None) == created.id or getattr(r, 'name', None) == created.name for r in results)

    def test_create_role(self) -> None:
        """Test case for create_role

        Create a role
        """
        name = "test_create_role"
        permissions = IAMRoleControllerApiRoleCreateOrUpdateRequestPermissions()
        permissions.flow = ["READ"]

        role_req = IAMRoleControllerApiRoleCreateOrUpdateRequest(
            name=name,
            permissions=permissions,
            description="An example role"
        )

        created = self.kestra_client.roles.create_role(
            tenant=self.tenant,
            iam_role_controller_api_role_create_or_update_request=role_req
        )

        assert getattr(created, 'name', None) == name


    def test_delete_role(self) -> None:
        """Test case for delete_role

        Delete a role
        """
        name = "test_delete_role"
        permissions = IAMRoleControllerApiRoleCreateOrUpdateRequestPermissions()
        permissions.flow = ["READ"]

        role_req = IAMRoleControllerApiRoleCreateOrUpdateRequest(
            name=name,
            permissions=permissions,
        )

        created = self.kestra_client.roles.create_role(
            tenant=self.tenant,
            iam_role_controller_api_role_create_or_update_request=role_req
        )

        self.kestra_client.roles.delete_role(id=created.id, tenant=self.tenant)

        with self.assertRaises(Exception):
            self.kestra_client.roles.role(id=created.id, tenant=self.tenant)

    def test_get_role(self) -> None:
        """Test case for get_role

        Retrieve a role
        """
        name = "test_get_role"
        permissions = IAMRoleControllerApiRoleCreateOrUpdateRequestPermissions()
        permissions.flow = ["READ"]

        role_req = IAMRoleControllerApiRoleCreateOrUpdateRequest(
            name=name,
            permissions=permissions,
        )

        created = self.kestra_client.roles.create_role(
            tenant=self.tenant,
            iam_role_controller_api_role_create_or_update_request=role_req
        )

        fetched = self.kestra_client.roles.role(id=created.id, tenant=self.tenant)
        assert fetched.id == created.id

    def test_list_roles_from_given_ids(self) -> None:
        """Test case for list_roles_from_given_ids

        List roles by ids
        """
        name = "test_list_roles_from_given_ids"
        permissions = IAMRoleControllerApiRoleCreateOrUpdateRequestPermissions()
        permissions.flow = ["READ"]

        role_req = IAMRoleControllerApiRoleCreateOrUpdateRequest(
            name=name,
            permissions=permissions,
        )

        created = self.kestra_client.roles.create_role(
            tenant=self.tenant,
            iam_role_controller_api_role_create_or_update_request=role_req
        )

        api_ids = ApiIds(ids=[created.id])

        fetched = self.kestra_client.roles.list_roles_from_given_ids(
            tenant=self.tenant,
            api_ids=api_ids
        )

        assert len(fetched) > 0

    def test_search_roles(self) -> None:
        """Test case for search_roles

        Search for roles
        """
        name = "test_search_roles"
        permissions = IAMRoleControllerApiRoleCreateOrUpdateRequestPermissions()
        permissions.flow = ["READ"]

        role_req = IAMRoleControllerApiRoleCreateOrUpdateRequest(
            name=name,
            permissions=permissions,
        )

        created = self.kestra_client.roles.create_role(
            tenant=self.tenant,
            iam_role_controller_api_role_create_or_update_request=role_req
        )

        results = self.kestra_client.roles.search_roles(page=1, size=10, tenant=self.tenant, q=name)

        assert any(getattr(r, 'id', None) == created.id for r in results.results)

    def test_update_role(self) -> None:
        """Test case for update_role

        Update a role
        """
        name = "test_update_role"
        permissions = IAMRoleControllerApiRoleCreateOrUpdateRequestPermissions()
        permissions.flow = ["READ"]

        role_req = IAMRoleControllerApiRoleCreateOrUpdateRequest(
            name=name,
            permissions=permissions,
            description="Before"
        )

        created = self.kestra_client.roles.create_role(
            tenant=self.tenant,
            iam_role_controller_api_role_create_or_update_request=role_req
        )

        update_desc = "Updated description"
        update_req = IAMRoleControllerApiRoleCreateOrUpdateRequest(
            name=created.name,
            permissions=permissions,
            description=update_desc
        )

        updated = self.kestra_client.roles.update_role(
            id=created.id,
            tenant=self.tenant,
            iam_role_controller_api_role_create_or_update_request=update_req
        )

        assert getattr(updated, 'description', None) == update_desc


if __name__ == '__main__':
    unittest.main()
