diff --git a/storages/backends/ftp.py b/storages/backends/ftp.py index ddcb87f9..2596a07f 100644 --- a/storages/backends/ftp.py +++ b/storages/backends/ftp.py @@ -60,7 +60,7 @@ def _decode_location(self, location): """Return splitted configuration data from location.""" splitted_url = re.search( r"^(?P.+)://(?P.+):(?P.+)@" - r"(?P.+):(?P\d+)/(?P.*)$", + r"(?P.+):(?P\d+)(?P/.*)?$", location, ) @@ -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"] @@ -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 diff --git a/tests/test_ftp.py b/tests/test_ftp.py index 5ef97d98..3d8eb6f9 100644 --- a/tests/test_ftp.py +++ b/tests/test_ftp.py @@ -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", @@ -59,6 +92,19 @@ 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 = { @@ -66,7 +112,7 @@ def test_decode_location(self): "host": "localhost", "user": "foo", "active": True, - "path": "/", + "path": "", "port": 2121, "secure": False, } @@ -277,7 +323,7 @@ def test_decode_location(self): "host": "localhost", "user": "foo", "active": False, - "path": "/", + "path": "", "port": 2121, "secure": True, }