|
| 1 | +"""Integration tests for OAuth scopes support. |
| 2 | +
|
| 3 | +These tests verify that scopes correctly flow through to token endpoints |
| 4 | +across all OAuth authentication methods (M2M, U2M, WIF/OIDC). |
| 5 | +""" |
| 6 | + |
| 7 | +from typing import Optional |
| 8 | +from urllib.parse import parse_qs |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from databricks.sdk.config import Config |
| 13 | + |
| 14 | +# --- Helper Functions --- |
| 15 | + |
| 16 | + |
| 17 | +def get_scope_from_request(request_text: str) -> Optional[str]: |
| 18 | + """Extract and return the scope value from a URL-encoded request body.""" |
| 19 | + params = parse_qs(request_text) |
| 20 | + scope_list = params.get("scope") |
| 21 | + return scope_list[0] if scope_list else None |
| 22 | + |
| 23 | + |
| 24 | +def get_grant_type_from_request(request_text: str) -> Optional[str]: |
| 25 | + """Extract and return the grant_type value from a URL-encoded request body.""" |
| 26 | + params = parse_qs(request_text) |
| 27 | + grant_type_list = params.get("grant_type") |
| 28 | + return grant_type_list[0] if grant_type_list else None |
| 29 | + |
| 30 | + |
| 31 | +# --- M2M (Machine-to-Machine) Integration Tests --- |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize( |
| 35 | + "scopes_input,expected_scope", |
| 36 | + [ |
| 37 | + (None, "all-apis"), |
| 38 | + ("unity-catalog:read", "unity-catalog:read"), |
| 39 | + ("jobs:read, clusters, mlflow:read", "clusters jobs:read mlflow:read"), |
| 40 | + ], |
| 41 | + ids=[ |
| 42 | + "default_scope", |
| 43 | + "single_custom_scope", |
| 44 | + "multiple_scopes_sorted", |
| 45 | + ], |
| 46 | +) |
| 47 | +def test_m2m_scopes(requests_mock, scopes_input, expected_scope): |
| 48 | + """Test M2M authentication sends correct scopes to token endpoint.""" |
| 49 | + # Mock the well-known endpoint |
| 50 | + requests_mock.get( |
| 51 | + "https://test.databricks.com/oidc/.well-known/oauth-authorization-server", |
| 52 | + json={ |
| 53 | + "authorization_endpoint": "https://test.databricks.com/oidc/v1/authorize", |
| 54 | + "token_endpoint": "https://test.databricks.com/oidc/v1/token", |
| 55 | + }, |
| 56 | + ) |
| 57 | + |
| 58 | + # Mock the token endpoint |
| 59 | + token_mock = requests_mock.post( |
| 60 | + "https://test.databricks.com/oidc/v1/token", |
| 61 | + json={"access_token": "test-token", "token_type": "Bearer", "expires_in": 3600}, |
| 62 | + ) |
| 63 | + |
| 64 | + # Create config with M2M auth |
| 65 | + config = Config( |
| 66 | + host="https://test.databricks.com", |
| 67 | + client_id="test-client-id", |
| 68 | + client_secret="test-client-secret", |
| 69 | + auth_type="oauth-m2m", |
| 70 | + scopes=scopes_input, |
| 71 | + ) |
| 72 | + |
| 73 | + # Authenticate (triggers token request) |
| 74 | + headers = config.authenticate() |
| 75 | + |
| 76 | + # Verify scope was sent correctly |
| 77 | + assert token_mock.called |
| 78 | + assert get_scope_from_request(token_mock.last_request.text) == expected_scope |
| 79 | + assert headers["Authorization"] == "Bearer test-token" |
| 80 | + |
| 81 | + |
| 82 | +# --- WIF/OIDC Integration Tests --- |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize( |
| 86 | + "scopes_input,expected_scope", |
| 87 | + [ |
| 88 | + (None, "all-apis"), |
| 89 | + ("unity-catalog:read, clusters", "clusters unity-catalog:read"), |
| 90 | + ("jobs:read", "jobs:read"), |
| 91 | + ], |
| 92 | + ids=[ |
| 93 | + "default_scope", |
| 94 | + "multiple_scopes", |
| 95 | + "single_scope", |
| 96 | + ], |
| 97 | +) |
| 98 | +def test_oidc_scopes(requests_mock, tmp_path, scopes_input, expected_scope): |
| 99 | + """Test OIDC token exchange sends correct scopes to token endpoint.""" |
| 100 | + # Create a temporary OIDC token file |
| 101 | + oidc_token_file = tmp_path / "oidc_token" |
| 102 | + oidc_token_file.write_text("mock-id-token") |
| 103 | + |
| 104 | + # Mock the well-known endpoint |
| 105 | + requests_mock.get( |
| 106 | + "https://test.databricks.com/oidc/.well-known/oauth-authorization-server", |
| 107 | + json={ |
| 108 | + "authorization_endpoint": "https://test.databricks.com/oidc/v1/authorize", |
| 109 | + "token_endpoint": "https://test.databricks.com/oidc/v1/token", |
| 110 | + }, |
| 111 | + ) |
| 112 | + |
| 113 | + # Mock the token exchange endpoint |
| 114 | + token_mock = requests_mock.post( |
| 115 | + "https://test.databricks.com/oidc/v1/token", |
| 116 | + json={"access_token": "test-token", "token_type": "Bearer", "expires_in": 3600}, |
| 117 | + ) |
| 118 | + |
| 119 | + # Create config with OIDC auth |
| 120 | + config = Config( |
| 121 | + host="https://test.databricks.com", |
| 122 | + oidc_token_filepath=str(oidc_token_file), |
| 123 | + auth_type="file-oidc", |
| 124 | + scopes=scopes_input, |
| 125 | + ) |
| 126 | + |
| 127 | + # Authenticate (triggers token exchange) |
| 128 | + headers = config.authenticate() |
| 129 | + |
| 130 | + # Verify scope and grant_type were sent correctly |
| 131 | + assert token_mock.called |
| 132 | + assert get_scope_from_request(token_mock.last_request.text) == expected_scope |
| 133 | + assert ( |
| 134 | + get_grant_type_from_request(token_mock.last_request.text) == "urn:ietf:params:oauth:grant-type:token-exchange" |
| 135 | + ) |
| 136 | + assert headers["Authorization"] == "Bearer test-token" |
0 commit comments