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