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


##########
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:
   One more thing to consider is including a new field to response data model 
doesn't seem like a breaking change since it can be ignored easily since no one 
is using the field due to not exist in the response, but it could be for a 
couple of users if they are tightly coupled with the response schema somehow 
and persisting somewhere the response.
   
   In any case, maybe we should document this somewhere, either in the common 
api changes issue or so. What do you think?



-- 
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