Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions airflow/cli/commands/connection_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ def _is_stdout(fileio: io.TextIOWrapper) -> bool:


def _valid_uri(uri: str) -> bool:
"""Check if a URI is valid, by checking if both scheme and netloc are available."""
uri_parts = urlsplit(uri)
return uri_parts.scheme != "" and uri_parts.netloc != ""
"""Check if a URI is valid, by checking if scheme (conn_type) provided."""
return urlsplit(uri).scheme != ""


@cache
Expand Down
67 changes: 57 additions & 10 deletions tests/cli/commands/test_connection_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io
import json
import re
import shlex
import warnings
from contextlib import redirect_stdout
from unittest import mock
Expand Down Expand Up @@ -353,7 +354,7 @@ def setup_method(self):
@pytest.mark.parametrize(
"cmd, expected_output, expected_conn",
[
(
pytest.param(
[
"connections",
"add",
Expand All @@ -371,8 +372,9 @@ def setup_method(self):
"port": 5432,
"schema": "airflow",
},
id="json-connection",
),
(
pytest.param(
[
"connections",
"add",
Expand All @@ -391,8 +393,9 @@ def setup_method(self):
"port": 5432,
"schema": "airflow",
},
id="uri-connection-with-description",
),
(
pytest.param(
[
"connections",
"add",
Expand All @@ -411,8 +414,9 @@ def setup_method(self):
"port": 5432,
"schema": "airflow",
},
id="uri-connection-with-description-2",
),
(
pytest.param(
[
"connections",
"add",
Expand All @@ -432,8 +436,9 @@ def setup_method(self):
"port": 5432,
"schema": "airflow",
},
id="uri-connection-with-extra",
),
(
pytest.param(
[
"connections",
"add",
Expand All @@ -455,8 +460,9 @@ def setup_method(self):
"port": 5432,
"schema": "airflow",
},
id="uri-connection-with-extra-and-description",
),
(
pytest.param(
[
"connections",
"add",
Expand All @@ -480,8 +486,9 @@ def setup_method(self):
"port": 9083,
"schema": "airflow",
},
id="individual-parts",
),
(
pytest.param(
[
"connections",
"add",
Expand All @@ -504,6 +511,37 @@ def setup_method(self):
"port": None,
"schema": None,
},
id="empty-uri-with-conn-type-and-extra",
),
pytest.param(
["connections", "add", "new6", "--conn-uri", "aws://?region_name=foo-bar-1"],
"Successfully added `conn_id`=new6 : aws://?region_name=foo-bar-1",
{
"conn_type": "aws",
"description": None,
"host": "",
"is_encrypted": False,
"is_extra_encrypted": True,
"login": None,
"port": None,
"schema": "",
},
id="uri-without-authority-and-host-blocks",
),
pytest.param(
["connections", "add", "new7", "--conn-uri", "aws://@/?region_name=foo-bar-1"],
"Successfully added `conn_id`=new7 : aws://@/?region_name=foo-bar-1",
{
"conn_type": "aws",
"description": None,
"host": "",
"is_encrypted": False,
"is_extra_encrypted": True,
"login": "",
"port": None,
"schema": "",
},
id="uri-with-@-instead-authority-and-host-blocks",
),
],
)
Expand Down Expand Up @@ -572,11 +610,20 @@ def test_cli_connections_add_json_and_uri(self):
)
)

def test_cli_connections_add_invalid_uri(self):
@pytest.mark.parametrize(
"invalid_uri",
[
pytest.param("nonsense_uri", id="word"),
pytest.param("://password:type@host:42/schema", id="missing-conn-type"),
],
)
def test_cli_connections_add_invalid_uri(self, invalid_uri):
# Attempt to add with invalid uri
with pytest.raises(SystemExit, match=r"The URI provided to --conn-uri is invalid: nonsense_uri"):
with pytest.raises(SystemExit, match=r"The URI provided to --conn-uri is invalid: .*"):
connection_command.connections_add(
self.parser.parse_args(["connections", "add", "new1", f"--conn-uri={'nonsense_uri'}"])
self.parser.parse_args(
["connections", "add", "new1", f"--conn-uri={shlex.quote(invalid_uri)}"]
)
)

def test_cli_connections_add_invalid_type(self):
Expand Down