# 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, IAMGroupControllerApiCreateGroupRequest, ApiIds, \
    IAMUserControllerApiCreateOrUpdateUserRequest, ApiAutocomplete, IAMGroupControllerApiUpdateGroupRequest, \
    GroupIdentifierMembership

class TestGroupsApi(unittest.TestCase):
    """GroupsApi 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_add_user_to_group(self) -> None:
        """Test case for add_user_to_group

        Add a user to a group
        """
        group = IAMGroupControllerApiCreateGroupRequest(
            name="test_add_user_to_group",
            description="An example group"
        )

        created_group = self.kestra_client.groups.create_group(
            tenant=self.tenant,
            iam_group_controller_api_create_group_request=group
        )

        user = IAMUserControllerApiCreateOrUpdateUserRequest(
            email="test_add_user_to_group@kestra.io"
        )

        created_user = self.kestra_client.users.create_user(
            iam_user_controller_api_create_or_update_user_request=user
        )

        member = self.kestra_client.groups.add_user_to_group(
            tenant=self.tenant,
            id=created_group.id,
            user_id=created_user.id
        )

        assert any(getattr(g, 'id', None) == created_group.id for g in member.groups)

    def test_autocomplete_groups(self) -> None:
        """Test case for autocomplete_groups

        List groups for autocomplete
        """
        prefix="test_auto"
        group = IAMGroupControllerApiCreateGroupRequest(
            name=prefix + "complete_groups",
            description="An example group"
        )

        created = self.kestra_client.groups.create_group(
            tenant=self.tenant,
            iam_group_controller_api_create_group_request=group
        )

        apiAutocomplete = ApiAutocomplete(q=prefix)

        results = self.kestra_client.groups.autocomplete_groups(
            tenant=self.tenant,
            api_autocomplete=apiAutocomplete
        )

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

    def test_create_group(self) -> None:
        """Test case for create_group

        Create a group
        """
        group = IAMGroupControllerApiCreateGroupRequest(
            name="test_create_group",
            description="An example group"
        )

        created = self.kestra_client.groups.create_group(
            tenant=self.tenant,
            iam_group_controller_api_create_group_request=group
        )
        assert created.name == group.name

    def test_delete_group(self) -> None:
        group = IAMGroupControllerApiCreateGroupRequest(
            name="test_delete_group",
            description="An example group"
        )

        created = self.kestra_client.groups.create_group(
            tenant=self.tenant,
            iam_group_controller_api_create_group_request=group
        )

        self.kestra_client.groups.delete_group(
            tenant=self.tenant,
            id=created.id
        )

    def test_delete_user_from_group(self) -> None:
        """Test case for delete_user_from_group

        Remove a user from a group
        """
        group = IAMGroupControllerApiCreateGroupRequest(
            name="test_delete_user_from_group",
            description="An example group"
        )

        created_group = self.kestra_client.groups.create_group(
            tenant=self.tenant,
            iam_group_controller_api_create_group_request=group
        )

        user = IAMUserControllerApiCreateOrUpdateUserRequest(
            email="test_delete_user_from_group@kestra.io"
        )

        created_user = self.kestra_client.users.create_user(
            iam_user_controller_api_create_or_update_user_request=user
        )

        self.kestra_client.groups.add_user_to_group(
            tenant=self.tenant,
            id=created_group.id,
            user_id=created_user.id
        )

        iAMGroupControllerApiGroupMember = self.kestra_client.groups.delete_user_from_group(
            tenant=self.tenant,
            id=created_group.id,
            user_id=created_user.id
        )
        assert iAMGroupControllerApiGroupMember.groups.__contains__(created_group.id) is False

    def test_get_group(self) -> None:
        """Test case for get_group

        Retrieve a group
        """
        group = IAMGroupControllerApiCreateGroupRequest(
            name="test_get_group",
            description="An example group"
        )

        created = self.kestra_client.groups.create_group(
            tenant=self.tenant,
            iam_group_controller_api_create_group_request=group
        )

        fetched = self.kestra_client.groups.group(
            tenant=self.tenant,
            id=created.id
        )

        assert fetched.id == created.id

    def test_list_group_ids(self) -> None:
        """Test case for list_group_ids

        List groups by ids
        """
        group = IAMGroupControllerApiCreateGroupRequest(
            name="test_list_group_ids",
            description="An example group"
        )

        created = self.kestra_client.groups.create_group(
            tenant=self.tenant,
            iam_group_controller_api_create_group_request=group
        )

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

        fetched = self.kestra_client.groups.list_group_ids(
            tenant=self.tenant,
            api_ids=apiIds
        )

        assert len(fetched) > 0

    def test_search_group_members(self) -> None:
        """Test case for search_group_members

        Search for users in a group
        """
        group = IAMGroupControllerApiCreateGroupRequest(
            name="test_search_group_members",
            description="An example group"
        )

        created_group = self.kestra_client.groups.create_group(
            tenant=self.tenant,
            iam_group_controller_api_create_group_request=group
        )

        user = IAMUserControllerApiCreateOrUpdateUserRequest(
            email="test_search_group_members@kestra.io"
        )

        created_user = self.kestra_client.users.create_user(
            iam_user_controller_api_create_or_update_user_request=user
        )

        self.kestra_client.groups.add_user_to_group(
            tenant=self.tenant,
            id=created_group.id,
            user_id=created_user.id
        )

        members = self.kestra_client.groups.search_group_members(
            tenant=self.tenant,
            id=created_group.id,
            q=created_user.email,
            page=1,
            size=10
        )

        assert any(getattr(m, 'id', None) == created_user.id for m in members.results)

    def test_search_groups(self) -> None:
        """Test case for search_groups

        Search for groups
        """
        name = "test_search_groups"
        group = IAMGroupControllerApiCreateGroupRequest(
            name=name,
            description="An example group"
        )

        created = self.kestra_client.groups.create_group(
            tenant=self.tenant,
            iam_group_controller_api_create_group_request=group
        )

        results = self.kestra_client.groups.search_groups(
            tenant=self.tenant,
            page=1,
            size=10,
            q=name
        )

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

    def test_set_user_membership_for_group(self) -> None:
        """Test case for set_user_membership_for_group

        Update a user's membership type in a group
        """
        group = IAMGroupControllerApiCreateGroupRequest(
            name="test_set_user_membership_for_group",
            description="An example group"
        )

        created_group = self.kestra_client.groups.create_group(
            tenant=self.tenant,
            iam_group_controller_api_create_group_request=group
        )

        user = IAMUserControllerApiCreateOrUpdateUserRequest(
            email="test_set_user_membership_for_group@kestra.io"
        )

        created_user = self.kestra_client.users.create_user(
            iam_user_controller_api_create_or_update_user_request=user
        )

        self.kestra_client.groups.add_user_to_group(
            tenant=self.tenant,
            id=created_group.id,
            user_id=created_user.id
        )

        member = self.kestra_client.groups.set_user_membership_for_group(
            tenant=self.tenant,
            id=created_group.id,
            user_id=created_user.id,
            membership=GroupIdentifierMembership.OWNER
        )

        assert any(getattr(g, 'id', None) == created_group.id for g in member.groups)

    def test_update_group(self) -> None:
        """Test case for update_group

        Update a group
        """
        group = IAMGroupControllerApiCreateGroupRequest(
            name="test_update_group",
            description="An example group"
        )

        created = self.kestra_client.groups.create_group(
            tenant=self.tenant,
            iam_group_controller_api_create_group_request=group
        )

        update_desc = "Updated description"

        updatedRequest = IAMGroupControllerApiUpdateGroupRequest(
            name=created.name,
            description = update_desc
        )

        updated = self.kestra_client.groups.update_group(
            tenant=self.tenant,
            id=created.id,
            iam_group_controller_api_update_group_request=updatedRequest
        )

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


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