Skip to content

Commit

Permalink
add missing projectroles.utils tests (#1482)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Sep 5, 2024
1 parent 174907b commit fc7ff02
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
70 changes: 69 additions & 1 deletion projectroles/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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"""
Expand Down
9 changes: 5 additions & 4 deletions projectroles/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down

0 comments on commit fc7ff02

Please sign in to comment.