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


##########
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:
   This test is not correct. This will only assert that "password" key is not 
in the response dict this is not enough. We actually want password and `extra` 
in the response, just the values should be redacted.
   
   You need to check that the response has `"password": "***"` and `"extra": 
{"password": "***"}`
   
   Also you might be missing a `@pytest.mark.enable_redact` in your test.
   
   Cf `variables`.



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