Ejemplo n.º 1
0
 def expired():
     raise adal.AdalError("Too late",
                          {'error_description': "AADSTS70008: Expired"})
Ejemplo n.º 2
0
    def test_user_pass_credentials(self, adal_context):

        adal_context.acquire_token_with_username_password = mock.Mock()

        # Basic with parameters

        mock_proxies = {
            'http': 'http://myproxy:8080',
            'https': 'https://myproxy:8080',
        }

        creds = UserPassCredentials('user',
                                    'pass',
                                    'id',
                                    resource="resource",
                                    timeout=12,
                                    verify=True,
                                    validate_authority=True,
                                    cache=None,
                                    proxies=mock_proxies)

        adal_context.assert_called_with(
            "https://login.microsoftonline.com/common",
            timeout=12,
            verify_ssl=True,
            proxies=mock_proxies,
            validate_authority=True,
            cache=None,
            api_version=None)

        creds.set_token()

        creds._context.acquire_token_with_username_password.assert_called_with(
            "resource", "user", "pass", "id")

        # Using default

        creds = UserPassCredentials(
            'user',
            'pass',
        )

        adal_context.assert_called_with(
            "https://login.microsoftonline.com/common",
            timeout=None,
            verify_ssl=None,
            proxies=None,
            validate_authority=True,
            cache=None,
            api_version=None)

        creds.set_token()

        creds._context.acquire_token_with_username_password.assert_called_with(
            "https://management.core.windows.net/", "user", "pass",
            "04b07795-8ddb-461a-bbee-02f9e1bf7b46")

        # Testing cloud_environment

        creds = UserPassCredentials('user',
                                    'pass',
                                    cloud_environment=AZURE_CHINA_CLOUD)

        adal_context.assert_called_with(
            "https://login.chinacloudapi.cn/common",
            timeout=None,
            verify_ssl=None,
            proxies=None,
            validate_authority=True,
            cache=None,
            api_version=None)
        creds.set_token()
        creds._context.acquire_token_with_username_password.assert_called_with(
            "https://management.core.chinacloudapi.cn/", "user", "pass",
            "04b07795-8ddb-461a-bbee-02f9e1bf7b46")

        # Testing china=True

        creds = UserPassCredentials('user', 'pass', china=True)

        adal_context.assert_called_with(
            "https://login.chinacloudapi.cn/common",
            timeout=None,
            verify_ssl=None,
            proxies=None,
            validate_authority=True,
            cache=None,
            api_version=None)
        creds.set_token()
        creds._context.acquire_token_with_username_password.assert_called_with(
            "https://management.core.chinacloudapi.cn/", "user", "pass",
            "04b07795-8ddb-461a-bbee-02f9e1bf7b46")

        # ADAL boom

        creds._context.acquire_token_with_username_password.side_effect = adal.AdalError(
            "Boom")

        with self.assertRaises(AuthenticationError):
            creds.set_token()
Ejemplo n.º 3
0
 def error():
     raise adal.AdalError("You hacker", {})
Ejemplo n.º 4
0
    def test_service_principal(self, adal_context):

        adal_context.acquire_token_with_client_credentials = mock.Mock()

        # Basic with parameters

        mock_proxies = {
            'http': 'http://myproxy:8080',
            'https': 'https://myproxy:8080',
        }

        creds = ServicePrincipalCredentials(123,
                                            'secret',
                                            resource="resource",
                                            timeout=12,
                                            verify=True,
                                            proxies=mock_proxies)

        adal_context.assert_called_with(
            "https://login.microsoftonline.com/common",
            timeout=12,
            verify_ssl=True,
            proxies=mock_proxies,
            validate_authority=True,
            cache=None,
            api_version=None)

        creds.set_token()

        creds._context.acquire_token_with_client_credentials.assert_called_with(
            "resource", 123, "secret")

        # Using default

        creds = ServicePrincipalCredentials(123, 'secret', tenant="private")

        adal_context.assert_called_with(
            "https://login.microsoftonline.com/private",
            timeout=None,
            verify_ssl=None,
            proxies=None,
            validate_authority=True,
            cache=None,
            api_version=None)
        creds.set_token()
        creds._context.acquire_token_with_client_credentials.assert_called_with(
            "https://management.core.windows.net/", 123, "secret")

        # Testing cloud_environment

        creds = ServicePrincipalCredentials(
            123, 'secret', cloud_environment=AZURE_CHINA_CLOUD)

        adal_context.assert_called_with(
            "https://login.chinacloudapi.cn/common",
            timeout=None,
            verify_ssl=None,
            proxies=None,
            validate_authority=True,
            cache=None,
            api_version=None)
        creds.set_token()
        creds._context.acquire_token_with_client_credentials.assert_called_with(
            "https://management.core.chinacloudapi.cn/", 123, "secret")

        # Testing china=True

        creds = ServicePrincipalCredentials(123, 'secret', china=True)

        adal_context.assert_called_with(
            "https://login.chinacloudapi.cn/common",
            timeout=None,
            verify_ssl=None,
            proxies=None,
            validate_authority=True,
            cache=None,
            api_version=None)
        creds.set_token()
        creds._context.acquire_token_with_client_credentials.assert_called_with(
            "https://management.core.chinacloudapi.cn/", 123, "secret")

        # ADAL boom

        creds._context.acquire_token_with_client_credentials.side_effect = adal.AdalError(
            "Boom")

        with self.assertRaises(AuthenticationError):
            creds.set_token()