2012-09-10 03:07:04 +08:00
|
|
|
import os
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2012-09-10 03:07:04 +08:00
|
|
|
|
2014-11-12 01:59:49 +08:00
|
|
|
from django.core.exceptions import SuspiciousFileOperation
|
2012-09-08 04:49:22 +08:00
|
|
|
from django.utils._os import safe_join
|
|
|
|
|
|
|
|
|
|
|
|
class SafeJoinTests(unittest.TestCase):
|
|
|
|
def test_base_path_ends_with_sep(self):
|
2012-09-10 03:07:04 +08:00
|
|
|
drive, path = os.path.splitdrive(safe_join("/abc/", "abc"))
|
2012-09-08 04:49:22 +08:00
|
|
|
self.assertEqual(
|
2012-09-10 03:07:04 +08:00
|
|
|
path,
|
|
|
|
"{0}abc{0}abc".format(os.path.sep)
|
2012-09-08 04:49:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_root_path(self):
|
2012-09-10 03:07:04 +08:00
|
|
|
drive, path = os.path.splitdrive(safe_join("/", "path"))
|
2012-09-08 04:49:22 +08:00
|
|
|
self.assertEqual(
|
2012-09-10 03:07:04 +08:00
|
|
|
path,
|
|
|
|
"{0}path".format(os.path.sep),
|
2012-09-08 04:49:22 +08:00
|
|
|
)
|
|
|
|
|
2012-09-10 03:07:04 +08:00
|
|
|
drive, path = os.path.splitdrive(safe_join("/", ""))
|
2012-09-08 04:49:22 +08:00
|
|
|
self.assertEqual(
|
2012-09-10 03:07:04 +08:00
|
|
|
path,
|
|
|
|
os.path.sep,
|
2012-09-08 04:49:22 +08:00
|
|
|
)
|
2014-11-12 01:59:49 +08:00
|
|
|
|
|
|
|
def test_parent_path(self):
|
|
|
|
with self.assertRaises(SuspiciousFileOperation):
|
|
|
|
safe_join("/abc/", "../def")
|