bugraoz93 commented on code in PR #43396:
URL: https://github.com/apache/airflow/pull/43396#discussion_r1823054504


##########
tests/api_fastapi/core_api/routes/public/test_connections.py:
##########
@@ -169,3 +169,83 @@ def test_should_respond_200(
         body = response.json()
         assert body["total_entries"] == expected_total_entries
         assert [connection["connection_id"] for connection in 
body["connections"]] == expected_ids
+
+
+class TestPostConnection(TestConnectionEndpoint):
+    @pytest.mark.parametrize(
+        "body",
+        [
+            {"connection_id": "test-connection-id", "conn_type": "test_type"},
+            {"connection_id": "test-connection-id", "conn_type": "test_type", 
"extra": None},
+            {"connection_id": "test-connection-id", "conn_type": "test_type", 
"extra": "{}"},
+            {"connection_id": "test-connection-id", "conn_type": "test_type", 
"extra": '{"key": "value"}'},
+            {
+                "connection_id": "test-connection-id",
+                "conn_type": "test_type",
+                "description": "test_description",
+                "host": "test_host",
+                "login": "test_login",
+                "schema": "test_schema",
+                "port": 8080,
+                "extra": '{"key": "value"}',
+            },
+        ],
+    )
+    def test_post_should_respond_200(self, test_client, session, body):
+        response = test_client.post("/public/connections/", json=body)
+        assert response.status_code == 201
+        connection = session.query(Connection).all()
+        assert len(connection) == 1
+
+    @pytest.mark.parametrize(
+        "body",
+        [
+            {"connection_id": "****", "conn_type": "test_type"},
+            {"connection_id": "test()", "conn_type": "test_type"},
+            {"connection_id": "this_^$#is_invalid", "conn_type": "test_type"},
+            {"connection_id": "iam_not@#$_connection_id", "conn_type": 
"test_type"},
+        ],
+    )
+    def test_post_should_respond_400_for_invalid_conn_id(self, test_client, 
body):
+        response = test_client.post("/public/connections/", json=body)
+        assert response.status_code == 400
+        connection_id = body["connection_id"]
+        assert response.json() == {
+            "detail": f"The key '{connection_id}' has to be made of "
+            "alphanumeric characters, dashes, dots and underscores 
exclusively",
+        }
+
+    @pytest.mark.parametrize(
+        "body",
+        [
+            {"connection_id": "test-connection-id", "conn_type": "test_type"},
+        ],
+    )
+    def test_post_should_respond_already_exist(self, test_client, body):
+        response = test_client.post("/public/connections/", json=body)
+        assert response.status_code == 201
+        # Another request
+        response = test_client.post("/public/connections/", json=body)
+        assert response.status_code == 409
+        assert response.json() == {
+            "detail": "Connection with connection_id: `test-connection-id` 
already exists",
+        }
+
+    @pytest.mark.parametrize(
+        "body",
+        [
+            {"connection_id": "test-connection-id", "conn_type": "test_type", 
"password": "test-password"},
+            {"connection_id": "test-connection-id", "conn_type": "test_type", 
"password": "?>@#+!_%()#"},
+            {
+                "connection_id": "test-connection-id",
+                "conn_type": "test_type",
+                "password": "A!rF|0wi$aw3s0m3",
+                "extra": '{"password": "test-password"}',
+            },
+        ],
+    )
+    def test_post_should_response_201_redacted_password(self, test_client, 
body):
+        response = test_client.post("/public/connections/", json=body)
+        assert response.status_code == 201
+        connection = response.json()
+        assert "password" not in connection

Review Comment:
   I first included it like that will return `***` as redacted using similar 
approaches to the `variables` and `extra` field `connections`. Then I  saw it 
is not even included in the reponse in the legacy api document. 
   
[post_connection](https://airflow.apache.org/docs/apache-airflow/stable/stable-rest-api-ref.html#operation/post_connection).
 In the Patch PR comment, I know you have said we should reduct the returned 
`password`, but the document made me confused, and I pushed this to align with 
you first. I can easily get back to returning the password field and redacting.
   
   Yeah, for the redacted unit test, indeed this should be missing that marking 
definition. Thanks for pointing out!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to