Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ftp] Extend path handling #1433

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions storages/backends/ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _decode_location(self, location):
"""Return splitted configuration data from location."""
splitted_url = re.search(
r"^(?P<scheme>.+)://(?P<user>.+):(?P<passwd>.+)@"
r"(?P<host>.+):(?P<port>\d+)/(?P<path>.*)$",
r"(?P<host>.+):(?P<port>\d+)(?P<path>/.*)?$",
location,
)

Expand All @@ -75,7 +75,7 @@ def _decode_location(self, location):
config["active"] = splitted_url["scheme"] == "aftp"
config["secure"] = splitted_url["scheme"] == "ftps"

config["path"] = splitted_url["path"] or "/"
config["path"] = splitted_url["path"][1:] if splitted_url["path"] else None
config["host"] = splitted_url["host"]
config["user"] = splitted_url["user"]
config["passwd"] = splitted_url["passwd"]
Expand All @@ -102,7 +102,7 @@ def _start_connection(self):
ftp.prot_p()
if self._config["active"]:
ftp.set_pasv(False)
if self._config["path"] != "":
if self._config["path"]:
ftp.cwd(self._config["path"])
self._connection = ftp
return
Expand Down
50 changes: 48 additions & 2 deletions tests/test_ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,39 @@ def test_init_location_from_setting(self, mock_setting):

def test_decode_location(self):
config = self.storage._decode_location(URL)
wanted_config = {
"passwd": "b@r",
"host": "localhost",
"user": "foo",
"active": False,
"path": "",
"port": 2121,
"secure": False,
}
self.assertEqual(config, wanted_config)
config = self.storage._decode_location("ftp://foo:b@r@localhost:2121")
wanted_config = {
"passwd": "b@r",
"host": "localhost",
"user": "foo",
"active": False,
"path": None,
"port": 2121,
"secure": False,
}
self.assertEqual(config, wanted_config)
config = self.storage._decode_location("ftp://foo:b@r@localhost:2121/test/dir")
wanted_config = {
"passwd": "b@r",
"host": "localhost",
"user": "foo",
"active": False,
"path": "test/dir",
"port": 2121,
"secure": False,
}
self.assertEqual(config, wanted_config)
config = self.storage._decode_location("ftp://foo:b@r@localhost:2121//")
wanted_config = {
"passwd": "b@r",
"host": "localhost",
Expand All @@ -59,14 +92,27 @@ def test_decode_location(self):
"secure": False,
}
self.assertEqual(config, wanted_config)
config = self.storage._decode_location(
"ftp://foo:b@r@localhost:2121//root/test/dir"
)
wanted_config = {
"passwd": "b@r",
"host": "localhost",
"user": "foo",
"active": False,
"path": "/root/test/dir",
"port": 2121,
"secure": False,
}
self.assertEqual(config, wanted_config)
# Test active FTP
config = self.storage._decode_location("a" + URL)
wanted_config = {
"passwd": "b@r",
"host": "localhost",
"user": "foo",
"active": True,
"path": "/",
"path": "",
"port": 2121,
"secure": False,
}
Expand Down Expand Up @@ -277,7 +323,7 @@ def test_decode_location(self):
"host": "localhost",
"user": "foo",
"active": False,
"path": "/",
"path": "",
"port": 2121,
"secure": True,
}
Expand Down
Loading