Unverified Commit 85feadbc authored by Thom Wiggers's avatar Thom Wiggers 📐
Browse files

Add some tests for utilities

parent 84402470
......@@ -5,6 +5,23 @@ from django.utils.six.moves.urllib.parse import unquote
def datetime_to_lectureyear(date):
"""Convert a date to the start of the lectureyear
>>> from datetime import date, datetime, timezone
>>> nov_23 = date(1990, 11, 7)
>>> datetime_to_lectureyear(nov_23)
1990
>>> mar_2 = date(1993, 3, 2)
>>> datetime_to_lectureyear(mar_2)
1992
Also works on ``datetimes``, but they need to be tz-aware:
>>> new_year = datetime(2000, 1, 1, tzinfo=timezone.utc)
>>> datetime_to_lectureyear(new_year)
1999
"""
if isinstance(date, timezone.datetime):
date = timezone.localtime(date).date()
sept_1 = timezone.make_aware(timezone.datetime(date.year, 9, 1))
......@@ -14,8 +31,23 @@ def datetime_to_lectureyear(date):
def sanitize_path(path):
"""Cleans up an insecure path, i.e. against directory traversal.
This code is partially copied from django.views.static"""
r"""
Cleans up an insecure path, i.e. against directory traversal.
This code is partially copied from ``django.views.static``.
>>> sanitize_path('//////')
''
>>> sanitize_path('////test//')
'test'
>>> sanitize_path('../../../test/')
'test'
>>> sanitize_path('../.././test/')
'test'
>>> sanitize_path(r'..\..\..\test')
'test/'
"""
path = os.path.normpath(unquote(path))
path = path.lstrip('/')
newpath = ''
......@@ -30,3 +62,8 @@ def sanitize_path(path):
continue
newpath = os.path.join(newpath, part).replace('\\', '/')
return newpath
if __name__ == "__main__":
import doctest
doctest.testmod()
import doctest
from django.core.exceptions import FieldError
from django.db import models
from django.test import TestCase, override_settings
from django.utils import translation
from utils.translation import ModelTranslateMeta, MultilingualField
from utils import snippets, validators
LANGUAGES = [
('en', 'English'),
......@@ -12,6 +15,17 @@ LANGUAGES = [
]
def load_tests(loader, tests, ignore):
"""
Load all tests in this module
"""
# Adds the doctests in snippets
tests.addTests(doctest.DocTestSuite(snippets))
# Adds the doctests in validators
tests.addTests(doctest.DocTestSuite(validators))
return tests
@override_settings(LANGUAGES=LANGUAGES)
class TestTranslateMeta(TestCase):
......
......@@ -3,7 +3,24 @@ import os
from django.core.exceptions import ValidationError
def validate_file_extension(file, exts=['.txt', '.pdf', '.jpg', '.png']):
def validate_file_extension(file,
exts=['.txt', '.pdf', '.jpg', '.jpeg', '.png']):
"""
Checks if a file has a certain allowed extension. Raises a
``ValidationError`` if that's not the case.
>>> class File(object):
... pass
>>> f = File()
>>> f.name = 'foo.jpeg'
>>> validate_file_extension(f)
>>> f.name = 'foo.exe'
>>> validate_file_extension(f)
Traceback (most recent call last):
...
django.core.exceptions.ValidationError: ['File extension not allowed.']
>>> validate_file_extension(f, ['.exe'])
"""
ext = os.path.splitext(file.name)[1]
if not ext.lower() in exts:
raise ValidationError("File extension not allowed.")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment