from unittest import TestCase
from pygeai.admin.clients import AdminClient
from pygeai.core.common.exceptions import APIResponseError 


class TestAdminValidateApiTokenIntegration(TestCase):
    __token_validation_attrs = [
        'organizationId', 'organizationName', 'scope'
    ]    

    def setUp(self):
        """
        Set up the test environment.
        """
        self.admin_client = AdminClient(alias="org")


    def __validate_api_token(self):
        """
        Helper method to call validate_api_token.
        """
        return self.admin_client.validate_api_token()


    def test_validate_api_token_valid_token(self):
        """Test successful API token validation with valid token."""
        response = self.__validate_api_token()

        # Validate expected attributes are present
        for attr in self.__token_validation_attrs:
            self.assertIn(attr, response, f"Response should have a '{attr}' key")

        # Validate specific data types
        self.assertEqual(response["organizationId"], "400413a7-3dd2-4273-91c3-6bd2158b3a87", "Organization ID should be string")
        self.assertEqual(response["organizationName"], "TestOrg", "Organization name should be string")
        self.assertEqual(response["scope"], "Pia.Data.Organization", "scope should be string")


    def test_validate_api_token_invalid_credentials(self):
        """Test error when using invalid API token."""
        # Create a client with invalid credentials
        invalid_client = AdminClient(api_key="invalid_token_12345", base_url="https://api.beta.saia.ai")
        
        with self.assertRaises(APIResponseError) as exception:
            invalid_client.validate_api_token()
        
        self.assertIn(
            "Unauthorized",
            str(exception.exception),
            "Expected authorization-related error message for invalid token"
        )