You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by vi...@apache.org on 2026/07/29 19:53:31 UTC

(airflow) branch main updated: Add tests for Keycloak token models (#70689)

This is an automated email from the ASF dual-hosted git repository.

vincbeck pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 658bd8a2885 Add tests for Keycloak token models (#70689)
658bd8a2885 is described below

commit 658bd8a28856fdd3e879ec2eb80fa635e51396a0
Author: D.H.KIM <60...@users.noreply.github.com>
AuthorDate: Thu Jul 30 04:53:23 2026 +0900

    Add tests for Keycloak token models (#70689)
    
    The token request models are exempted from provider test-structure enforcement, leaving grant discrimination and validation behavior without dedicated coverage.
---
 .../tests/unit/always/test_project_structure.py    |   1 -
 .../keycloak/auth_manager/datamodels/__init__.py   |  16 +++
 .../keycloak/auth_manager/datamodels/test_token.py | 120 +++++++++++++++++++++
 3 files changed, 136 insertions(+), 1 deletion(-)

diff --git a/airflow-core/tests/unit/always/test_project_structure.py b/airflow-core/tests/unit/always/test_project_structure.py
index 2a599f9759e..0d42a2201ca 100644
--- a/airflow-core/tests/unit/always/test_project_structure.py
+++ b/airflow-core/tests/unit/always/test_project_structure.py
@@ -172,7 +172,6 @@ class TestProjectStructure:
             "providers/google/tests/unit/google/common/hooks/test_operation_helpers.py",
             "providers/google/tests/unit/google/test_go_module_utils.py",
             "providers/http/tests/unit/http/test_exceptions.py",
-            "providers/keycloak/tests/unit/keycloak/auth_manager/datamodels/test_token.py",
             "providers/microsoft/azure/tests/unit/microsoft/azure/operators/test_adls.py",
             "providers/snowflake/tests/unit/snowflake/triggers/test_snowflake_trigger.py",
             "providers/standard/tests/unit/standard/operators/test_branch.py",
diff --git a/providers/keycloak/tests/unit/keycloak/auth_manager/datamodels/__init__.py b/providers/keycloak/tests/unit/keycloak/auth_manager/datamodels/__init__.py
new file mode 100644
index 00000000000..13a83393a91
--- /dev/null
+++ b/providers/keycloak/tests/unit/keycloak/auth_manager/datamodels/__init__.py
@@ -0,0 +1,16 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
diff --git a/providers/keycloak/tests/unit/keycloak/auth_manager/datamodels/test_token.py b/providers/keycloak/tests/unit/keycloak/auth_manager/datamodels/test_token.py
new file mode 100644
index 00000000000..378a5d7da55
--- /dev/null
+++ b/providers/keycloak/tests/unit/keycloak/auth_manager/datamodels/test_token.py
@@ -0,0 +1,120 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+from unittest import mock
+
+import pytest
+from pydantic import ValidationError
+
+from airflow.providers.keycloak.auth_manager.datamodels.token import (
+    TokenBody,
+    TokenClientCredentialsBody,
+    TokenPasswordBody,
+    TokenResponse,
+)
+
+
+class TestTokenResponse:
+    def test_model_validate_and_dump(self):
+        token = TokenResponse.model_validate({"access_token": "token"})
+
+        assert token.access_token == "token"
+        assert token.model_dump() == {"access_token": "token"}
+
+    def test_requires_access_token(self):
+        with pytest.raises(ValidationError):
+            TokenResponse.model_validate({})
+
+
+class TestTokenBody:
+    @pytest.mark.parametrize(
+        ("payload", "expected_type", "expected_dump"),
+        [
+            (
+                {"username": "username", "password": "password"},
+                TokenPasswordBody,
+                {"grant_type": "password", "username": "username", "password": "password"},
+            ),
+            (
+                {"grant_type": "password", "username": "username", "password": "password"},
+                TokenPasswordBody,
+                {"grant_type": "password", "username": "username", "password": "password"},
+            ),
+            (
+                {
+                    "grant_type": "client_credentials",
+                    "client_id": "client_id",
+                    "client_secret": "client_secret",
+                },
+                TokenClientCredentialsBody,
+                {
+                    "grant_type": "client_credentials",
+                    "client_id": "client_id",
+                    "client_secret": "client_secret",
+                },
+            ),
+        ],
+    )
+    def test_model_validate_and_dump(self, payload, expected_type, expected_dump):
+        token_body = TokenBody.model_validate(payload)
+
+        assert isinstance(token_body.root, expected_type)
+        assert token_body.model_dump() == expected_dump
+
+    @pytest.mark.parametrize(
+        "payload",
+        [
+            {},
+            {"username": None, "password": "password"},
+            {"grant_type": "unsupported", "username": "username", "password": "password"},
+            {"username": "username", "password": "password", "extra": "value"},
+            {"grant_type": "client_credentials", "client_secret": "client_secret"},
+        ],
+    )
+    def test_rejects_invalid_payload(self, payload):
+        with pytest.raises(ValidationError):
+            TokenBody.model_validate(payload)
+
+
+class TestTokenPasswordBody:
+    @mock.patch("airflow.providers.keycloak.auth_manager.datamodels.token.create_token_for", autospec=True)
+    def test_create_token(self, mock_create_token_for):
+        mock_create_token_for.return_value = "token"
+        body = TokenPasswordBody(username="username", password="password")
+
+        assert body.create_token(expiration_time_in_seconds=60) == "token"
+        mock_create_token_for.assert_called_once_with("username", "password", expiration_time_in_seconds=60)
+
+
+class TestTokenClientCredentialsBody:
+    @mock.patch(
+        "airflow.providers.keycloak.auth_manager.datamodels.token.create_client_credentials_token",
+        autospec=True,
+    )
+    def test_create_token(self, mock_create_client_credentials_token):
+        mock_create_client_credentials_token.return_value = "token"
+        body = TokenClientCredentialsBody(
+            grant_type="client_credentials",
+            client_id="client_id",
+            client_secret="client_secret",
+        )
+
+        assert body.create_token(expiration_time_in_seconds=60) == "token"
+        mock_create_client_credentials_token.assert_called_once_with(
+            "client_id", "client_secret", expiration_time_in_seconds=60
+        )