コード例 #1
0
def test_user_agent():
    transport = validating_transport(
        requests=[Request(required_headers={"User-Agent": USER_AGENT})],
        responses=[
            mock_response(json_payload=build_aad_response(access_token="**"))
        ],
    )

    with mock.patch(VSCodeCredential.__module__ + ".get_credentials",
                    return_value="VALUE"):
        credential = VSCodeCredential(transport=transport)
        credential.get_token("scope")
コード例 #2
0
def test_policies_configurable():
    policy = mock.Mock(spec_set=SansIOHTTPPolicy, on_request=mock.Mock())

    def send(*_, **__):
        return mock_response(json_payload=build_aad_response(
            access_token="**"))

    with mock.patch(VSCodeCredential.__module__ + ".get_credentials",
                    return_value="VALUE"):
        credential = VSCodeCredential(policies=[policy],
                                      transport=mock.Mock(send=send))
        credential.get_token("scope")
        assert policy.on_request.called
コード例 #3
0
def test_distro():
    mock_client = mock.Mock(spec=object)
    mock_client.obtain_token_by_refresh_token = mock.Mock(return_value=None)
    mock_client.get_cached_access_token = mock.Mock(return_value=None)

    with pytest.raises(CredentialUnavailableError):
        credential = VSCodeCredential(_client=mock_client)
        token = credential.get_token("scope")
コード例 #4
0
def test_cache_refresh_token():
    expected_token = AccessToken("token", 42)

    mock_client = mock.Mock(spec=object)
    mock_client.obtain_token_by_refresh_token = mock.Mock(
        return_value=expected_token)
    mock_client.get_cached_access_token = mock.Mock(return_value=None)
    mock_get_credentials = mock.Mock(return_value="VALUE")

    with mock.patch(VSCodeCredential.__module__ + ".get_credentials",
                    mock_get_credentials):
        credential = VSCodeCredential(_client=mock_client)
        token = credential.get_token("scope")
        assert token is expected_token
        assert mock_get_credentials.call_count == 1
        token = credential.get_token("scope")
        assert token is expected_token
        assert mock_get_credentials.call_count == 1
コード例 #5
0
def test_mac_keychain_error():
    from msal_extensions.osx import Keychain, KeychainError

    with mock.patch.object(Keychain,
                           "get_generic_password",
                           side_effect=KeychainError(-1)):
        credential = VSCodeCredential()
        with pytest.raises(CredentialUnavailableError):
            token = credential.get_token("scope")
コード例 #6
0
def test_request_url(authority):
    """the credential should accept an authority, with or without scheme, as an argument or environment variable"""

    tenant_id = "expected_tenant"
    access_token = "***"
    parsed_authority = urlparse(authority)
    expected_netloc = parsed_authority.netloc or authority  # "localhost" parses to netloc "", path "localhost"
    expected_refresh_token = "refresh-token"

    def mock_send(request, **kwargs):
        actual = urlparse(request.url)
        assert actual.scheme == "https"
        assert actual.netloc == expected_netloc
        assert actual.path.startswith("/" + tenant_id)
        assert request.body["refresh_token"] == expected_refresh_token
        return mock_response(json_payload={
            "token_type": "Bearer",
            "expires_in": 42,
            "access_token": access_token
        })

    credential = VSCodeCredential(tenant_id=tenant_id,
                                  transport=mock.Mock(send=mock_send),
                                  authority=authority)
    with mock.patch(VSCodeCredential.__module__ + ".get_credentials",
                    return_value=expected_refresh_token):
        token = credential.get_token("scope")
    assert token.token == access_token

    # authority can be configured via environment variable
    with mock.patch.dict(
            "os.environ",
        {EnvironmentVariables.AZURE_AUTHORITY_HOST: authority},
            clear=True):
        credential = VSCodeCredential(tenant_id=tenant_id,
                                      transport=mock.Mock(send=mock_send))
        with mock.patch(VSCodeCredential.__module__ + ".get_credentials",
                        return_value=expected_refresh_token):
            credential.get_token("scope")
    assert token.token == access_token
コード例 #7
0
def test_no_obtain_token_if_cached():
    expected_token = AccessToken("token", 42)

    mock_client = mock.Mock(should_refresh=lambda _: False)
    mock_client.obtain_token_by_refresh_token = mock.Mock(
        return_value=expected_token)
    mock_client.get_cached_access_token = mock.Mock(return_value="VALUE")

    with mock.patch(VSCodeCredential.__module__ + ".get_credentials",
                    return_value="VALUE"):
        credential = VSCodeCredential(_client=mock_client)
        token = credential.get_token("scope")
        assert mock_client.obtain_token_by_refresh_token.call_count == 0
コード例 #8
0
def test_redeem_token():
    expected_token = AccessToken("token", 42)
    expected_value = "value"

    mock_client = mock.Mock(spec=object)
    mock_client.obtain_token_by_refresh_token = mock.Mock(
        return_value=expected_token)
    mock_client.get_cached_access_token = mock.Mock(return_value=None)

    with mock.patch(VSCodeCredential.__module__ + ".get_credentials",
                    return_value=expected_value):
        credential = VSCodeCredential(_client=mock_client)
        token = credential.get_token("scope")
        assert token is expected_token
        mock_client.obtain_token_by_refresh_token.assert_called_with(
            ("scope", ), expected_value)
        assert mock_client.obtain_token_by_refresh_token.call_count == 1
コード例 #9
0
def test_credential_unavailable_error():
    with mock.patch(VSCodeCredential.__module__ + ".get_credentials",
                    return_value=None):
        credential = VSCodeCredential()
        with pytest.raises(CredentialUnavailableError):
            token = credential.get_token("scope")
コード例 #10
0
def test_no_scopes():
    """The credential should raise ValueError when get_token is called with no scopes"""

    credential = VSCodeCredential()
    with pytest.raises(ValueError):
        credential.get_token()
コード例 #11
0
def test_live():
    credential = VSCodeCredential()
    token=credential.get_token('https://vault.azure.net/.default')
    print(token)