|
16 | 16 | # under the License. |
17 | 17 | from __future__ import annotations |
18 | 18 |
|
| 19 | +import json |
19 | 20 | import os |
20 | 21 | from importlib.metadata import PackageNotFoundError, metadata |
21 | 22 | from unittest import mock |
22 | 23 |
|
23 | 24 | import pytest |
24 | | -from sqlalchemy import select |
| 25 | +from sqlalchemy import func, select |
25 | 26 | from sqlalchemy.orm import Session |
26 | 27 |
|
27 | 28 | from airflow.models import Connection |
@@ -1001,6 +1002,140 @@ def test_should_respond_403_by_default(self, test_client, body): |
1001 | 1002 | "Contact your deployment admin to enable it." |
1002 | 1003 | } |
1003 | 1004 |
|
| 1005 | + @mock.patch.dict(os.environ, {"AIRFLOW__CORE__TEST_CONNECTION": "Enabled"}) |
| 1006 | + def test_should_merge_password_with_existing_connection(self, test_client, session): |
| 1007 | + connection = Connection( |
| 1008 | + conn_id=TEST_CONN_ID, |
| 1009 | + conn_type="sqlite", |
| 1010 | + password="existing_password", |
| 1011 | + ) |
| 1012 | + session.add(connection) |
| 1013 | + session.commit() |
| 1014 | + initial_count = session.scalar(select(func.count()).select_from(Connection)) |
| 1015 | + |
| 1016 | + captured_value = {} |
| 1017 | + |
| 1018 | + def mock_test_connection(self): |
| 1019 | + captured_value["password"] = self.password |
| 1020 | + captured_value["conn_type"] = self.conn_type |
| 1021 | + return True, "mocked" |
| 1022 | + |
| 1023 | + body = { |
| 1024 | + "connection_id": TEST_CONN_ID, |
| 1025 | + "conn_type": "new_sqlite", |
| 1026 | + "password": "***", |
| 1027 | + } |
| 1028 | + |
| 1029 | + with mock.patch.object(Connection, "test_connection", mock_test_connection): |
| 1030 | + response = test_client.post("/connections/test", json=body) |
| 1031 | + |
| 1032 | + assert response.status_code == 200 |
| 1033 | + assert response.json()["status"] is True |
| 1034 | + # Verify that the existing password was used, not "***" |
| 1035 | + assert captured_value["password"] == "existing_password" |
| 1036 | + # Verify that payload info were used for other fields |
| 1037 | + assert captured_value["conn_type"] == "new_sqlite" |
| 1038 | + |
| 1039 | + # Verify DB was not mutated |
| 1040 | + session.expire_all() |
| 1041 | + db_conn = session.scalar(select(Connection).filter_by(conn_id=TEST_CONN_ID)) |
| 1042 | + assert db_conn.password == "existing_password" |
| 1043 | + assert session.scalar(select(func.count()).select_from(Connection)) == initial_count |
| 1044 | + |
| 1045 | + @mock.patch.dict(os.environ, {"AIRFLOW__CORE__TEST_CONNECTION": "Enabled"}) |
| 1046 | + def test_should_merge_extra_with_existing_connection(self, test_client, session): |
| 1047 | + connection = Connection( |
| 1048 | + conn_id=TEST_CONN_ID, |
| 1049 | + conn_type="fs", |
| 1050 | + extra='{"path": "/", "existing_key": "existing_value"}', |
| 1051 | + ) |
| 1052 | + session.add(connection) |
| 1053 | + session.commit() |
| 1054 | + initial_count = session.scalar(select(func.count()).select_from(Connection)) |
| 1055 | + |
| 1056 | + captured_extra = {} |
| 1057 | + |
| 1058 | + def mock_test_connection(self): |
| 1059 | + captured_extra["value"] = self.extra |
| 1060 | + return True, "mocked" |
| 1061 | + |
| 1062 | + body = { |
| 1063 | + "connection_id": TEST_CONN_ID, |
| 1064 | + "conn_type": "fs", |
| 1065 | + "extra": '{"path": "/", "new_key": "new_value"}', |
| 1066 | + } |
| 1067 | + |
| 1068 | + with mock.patch.object(Connection, "test_connection", mock_test_connection): |
| 1069 | + response = test_client.post("/connections/test", json=body) |
| 1070 | + |
| 1071 | + assert response.status_code == 200 |
| 1072 | + assert response.json()["status"] is True |
| 1073 | + # Verify that new_key is reflected in the merged extra |
| 1074 | + merged_extra = json.loads(captured_extra["value"]) |
| 1075 | + assert merged_extra["new_key"] == "new_value" |
| 1076 | + assert merged_extra["path"] == "/" |
| 1077 | + |
| 1078 | + # Verify DB was not mutated |
| 1079 | + session.expire_all() |
| 1080 | + db_conn = session.scalar(select(Connection).filter_by(conn_id=TEST_CONN_ID)) |
| 1081 | + assert json.loads(db_conn.extra) == {"path": "/", "existing_key": "existing_value"} |
| 1082 | + assert session.scalar(select(func.count()).select_from(Connection)) == initial_count |
| 1083 | + |
| 1084 | + @mock.patch.dict(os.environ, {"AIRFLOW__CORE__TEST_CONNECTION": "Enabled"}) |
| 1085 | + def test_should_merge_both_password_and_extra(self, test_client, session): |
| 1086 | + connection = Connection( |
| 1087 | + conn_id=TEST_CONN_ID, |
| 1088 | + conn_type="fs", |
| 1089 | + password="existing_password", |
| 1090 | + extra='{"path": "/", "existing_key": "existing_value"}', |
| 1091 | + ) |
| 1092 | + session.add(connection) |
| 1093 | + session.commit() |
| 1094 | + initial_count = session.scalar(select(func.count()).select_from(Connection)) |
| 1095 | + |
| 1096 | + captured_values = {} |
| 1097 | + |
| 1098 | + def mock_test_connection(self): |
| 1099 | + captured_values["password"] = self.password |
| 1100 | + captured_values["extra"] = self.extra |
| 1101 | + return True, "mocked" |
| 1102 | + |
| 1103 | + body = { |
| 1104 | + "connection_id": TEST_CONN_ID, |
| 1105 | + "conn_type": "fs", |
| 1106 | + "password": "***", |
| 1107 | + "extra": '{"path": "/", "new_key": "new_value"}', |
| 1108 | + } |
| 1109 | + |
| 1110 | + with mock.patch.object(Connection, "test_connection", mock_test_connection): |
| 1111 | + response = test_client.post("/connections/test", json=body) |
| 1112 | + |
| 1113 | + assert response.status_code == 200 |
| 1114 | + assert response.json()["status"] is True |
| 1115 | + # Verify that the existing password was used, not "***" |
| 1116 | + assert captured_values["password"] == "existing_password" |
| 1117 | + # Verify that new_key is reflected in the merged extra |
| 1118 | + merged_extra = json.loads(captured_values["extra"]) |
| 1119 | + assert merged_extra["new_key"] == "new_value" |
| 1120 | + assert merged_extra["path"] == "/" |
| 1121 | + |
| 1122 | + # Verify DB was not mutated |
| 1123 | + session.expire_all() |
| 1124 | + db_conn = session.scalar(select(Connection).filter_by(conn_id=TEST_CONN_ID)) |
| 1125 | + assert db_conn.password == "existing_password" |
| 1126 | + assert json.loads(db_conn.extra) == {"path": "/", "existing_key": "existing_value"} |
| 1127 | + assert session.scalar(select(func.count()).select_from(Connection)) == initial_count |
| 1128 | + |
| 1129 | + @mock.patch.dict(os.environ, {"AIRFLOW__CORE__TEST_CONNECTION": "Enabled"}) |
| 1130 | + def test_should_test_new_connection_without_existing(self, test_client): |
| 1131 | + body = { |
| 1132 | + "connection_id": "non_existent_conn", |
| 1133 | + "conn_type": "sqlite", |
| 1134 | + } |
| 1135 | + response = test_client.post("/connections/test", json=body) |
| 1136 | + assert response.status_code == 200 |
| 1137 | + assert response.json()["status"] is True |
| 1138 | + |
1004 | 1139 |
|
1005 | 1140 | class TestCreateDefaultConnections(TestConnectionEndpoint): |
1006 | 1141 | def test_should_respond_204(self, test_client, session): |
|
0 commit comments