diff --git a/projectroles/tests/test_utils.py b/projectroles/tests/test_utils.py index 8af0eeee..207905f7 100644 --- a/projectroles/tests/test_utils.py +++ b/projectroles/tests/test_utils.py @@ -1,13 +1,23 @@ """Utils tests for the projectroles app""" +import re + +from django.conf import settings from django.contrib.auth.models import AnonymousUser from django.test import override_settings from django.urls import reverse +from test_plus import TestCase + from projectroles.models import SODAR_CONSTANTS from projectroles.tests.test_models import ProjectMixin, RoleAssignmentMixin from projectroles.tests.test_views import ViewTestBase -from projectroles.utils import AppLinkContent +from projectroles.utils import ( + AppLinkContent, + get_display_name, + build_secret, + get_app_names, +) app_links = AppLinkContent() @@ -18,6 +28,64 @@ PROJECT_TYPE_PROJECT = SODAR_CONSTANTS['PROJECT_TYPE_PROJECT'] SITE_MODE_TARGET = SODAR_CONSTANTS['SITE_MODE_TARGET'] +# Local constants +CONSTANTS_OVERRIDE = { + 'DISPLAY_NAMES': { + 'CATEGORY': {'default': 'bar', 'plural': 'bars'}, + 'PROJECT': {'default': 'foo', 'plural': 'foos'}, + } +} + + +class TestUtils(TestCase): + """Tests for general utilities""" + + def test_get_display_name(self): + """Test get_display_name()""" + self.assertEqual(get_display_name(PROJECT_TYPE_PROJECT), 'project') + self.assertEqual( + get_display_name(PROJECT_TYPE_PROJECT, title=True), 'Project' + ) + self.assertEqual( + get_display_name(PROJECT_TYPE_PROJECT, count=3), 'projects' + ) + self.assertEqual( + get_display_name(PROJECT_TYPE_PROJECT, title=True, count=3), + 'Projects', + ) + self.assertEqual(get_display_name(PROJECT_TYPE_CATEGORY), 'category') + self.assertEqual( + get_display_name(PROJECT_TYPE_CATEGORY, title=True), 'Category' + ) + self.assertEqual( + get_display_name(PROJECT_TYPE_CATEGORY, count=3), 'categories' + ) + self.assertEqual( + get_display_name(PROJECT_TYPE_CATEGORY, title=True, count=3), + 'Categories', + ) + + # TODO: Test with override + + def test_build_secret(self): + """Test build_secret()""" + secret = build_secret() + self.assertEqual(re.match(r'[a-z\d]{32}', secret).string, secret) + self.assertEqual(len(build_secret(16)), 16) + + @override_settings(PROJECTROLES_SECRET_LENGTH=16) + def test_build_secret_override(self): + """Test build_secret() with default length setting override""" + self.assertEqual(len(build_secret()), 16) + + def test_get_app_names(self): + """Test get_app_names()""" + app_names = get_app_names() + self.assertNotEqual(len(app_names), 0) + self.assertFalse(any([a.startswith('django.') for a in app_names])) + self.assertFalse(any(['.apps.' in a for a in app_names])) + self.assertNotIn(settings.SITE_PACKAGE, app_names) + class TestAppLinkContent(ProjectMixin, RoleAssignmentMixin, ViewTestBase): """Tests for AppLinkContent""" diff --git a/projectroles/utils.py b/projectroles/utils.py index 907f3a16..9f026d7c 100644 --- a/projectroles/utils.py +++ b/projectroles/utils.py @@ -7,8 +7,6 @@ from projectroles.plugins import get_active_plugins from projectroles.models import SODAR_CONSTANTS -# Settings -SECRET_LENGTH = getattr(settings, 'PROJECTROLES_SECRET_LENGTH', 32) # SODAR constants PROJECT_TYPE_PROJECT = SODAR_CONSTANTS['PROJECT_TYPE_PROJECT'] @@ -47,6 +45,7 @@ def get_display_name(key, title=False, count=1, plural=False): return ret.lower() if not title else ret.title() +# TODO: Deprecate (see #1487) def get_user_display_name(user, inc_user=False): """ Return full name of user for displaying. @@ -61,13 +60,15 @@ def get_user_display_name(user, inc_user=False): return user.username -def build_secret(length=SECRET_LENGTH): +def build_secret(length=None): """ Return secret string for e.g. public URLs. - :param length: Length of string if specified, default value from settings + :param length: Length of string, use None for default (integer or None) :return: Randomized secret (string) """ + if not length: + length = getattr(settings, 'PROJECTROLES_SECRET_LENGTH', 32) length = int(length) if int(length) <= 255 else 255 return ''.join( random.SystemRandom().choice(string.ascii_lowercase + string.digits)