Compare commits

..

4 Commits

Author SHA1 Message Date
Agate ab28c31b2d user: allow customization of User.get_full_name() through templates (#72945)
gitea-wip/hobo/pipeline/pr-main This commit looks good Details
2023-02-07 10:04:59 +01:00
Lauréline Guérin 13f0821c66
applications: fix Element type size (#74233)
gitea-wip/hobo/pipeline/pr-main This commit looks good Details
2023-02-06 17:50:05 +01:00
Frédéric Péters 88d1681a12 ci: upgrade isort (#74044) 2023-02-01 09:43:24 +01:00
Benjamin Dauvergne b3520030f5 tests: improve search of a free TCP port (#72645) 2023-01-31 17:36:13 +01:00
12 changed files with 84 additions and 45 deletions

View File

@ -17,7 +17,7 @@ repos:
- id: black
args: ['--target-version', 'py37', '--skip-string-normalization', '--line-length', '110']
- repo: https://github.com/PyCQA/isort
rev: 5.7.0
rev: 5.12.0
hooks:
- id: isort
args: ['--profile', 'black', '--line-length', '110']

View File

@ -0,0 +1,16 @@
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('applications', '0010_relation_error'),
]
operations = [
migrations.AlterField(
model_name='element',
name='type',
field=models.CharField(max_length=100, verbose_name='Type'),
),
]

View File

@ -182,7 +182,7 @@ class Application(models.Model):
class Element(models.Model):
type = models.CharField(max_length=25, verbose_name=_('Type'))
type = models.CharField(max_length=100, verbose_name=_('Type'))
slug = models.SlugField(max_length=500, verbose_name=_('Slug'))
name = models.CharField(max_length=500, verbose_name=_('Name'))
cache = JSONField(blank=True, default=dict)

View File

@ -16,6 +16,9 @@
import hashlib
import os
import socket
import struct
from contextlib import closing
def get_safe_db_name(max_length=53):
@ -47,3 +50,12 @@ def get_safe_db_name(max_length=53):
truncated_db_name = full_db_name[:prefix_length] + '_' + hashcode + '_' + full_db_name[-suffix_length:]
assert len(truncated_db_name) == max_length
return truncated_db_name
def find_free_port():
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(('', 0))
# SO_LINGER (man 7 socket) l_onoff=1 l_linger=0, immediately release
# the port on closing of the socket
s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0))
return s.getsockname()[1]

View File

@ -1,8 +1,13 @@
from django.template.loader import render_to_string
from django.conf import settings
from django.template import engines
from django.template.exceptions import TemplateDoesNotExist
def get_full_name_from_template(user):
template_name = 'includes/user-info-user-name.html'
context = {}
context['user'] = user
return render_to_string(template_name, context)
template_vars = getattr(settings, 'TEMPLATE_VARS', {})
if 'user_full_name_template' not in template_vars:
raise TemplateDoesNotExist('user full name inline template var does not exist')
template = engines['django'].from_string(template_vars['user_full_name_template'])
return template.render(context)

View File

@ -1,12 +1,7 @@
import os
import sys
import hobo.test_utils
SETTINGS_DIR = os.path.dirname(os.path.abspath(__file__))
TESTS_DIR = os.path.join(os.path.dirname(SETTINGS_DIR), 'tests')
sys.path.append(TESTS_DIR)
LANGUAGE_CODE = 'en-us'
BROKER_URL = 'memory://'
@ -37,7 +32,6 @@ DATABASES = {
}
}
TEMPLATES[0]['DIRS'].append('tests/templates') # pylint: disable=undefined-variable
TEMPLATES[0]['OPTIONS'].setdefault('builtins', []).append('hobo.templatetags.hobo')
REST_FRAMEWORK = {

View File

@ -1 +0,0 @@
{{ user.first_name }} {{ user.attributes.foo }}

View File

@ -66,6 +66,13 @@ WCS_AVAILABLE_OBJECTS = {
"minor": True,
"urls": {"list": "https://wcs.example.invalid/api/export-import/mail-templates/"},
},
{
"id": "comment-templates-categories",
"text": "Categories (comment templates)",
"singular": "Category (comment templates)",
"minor": True,
"urls": {"list": "https://wcs.example.invalid/api/export-import/comment-templates-categories/"},
},
{
"id": "wscalls",
"text": "Webservice Calls",
@ -775,6 +782,12 @@ def get_bundle(with_icon=False):
{'type': 'forms', 'slug': 'test', 'name': 'test', 'auto-dependency': False},
{'type': 'blocks', 'slug': 'test', 'name': 'test', 'auto-dependency': True},
{'type': 'workflows', 'slug': 'test', 'name': 'test', 'auto-dependency': True},
{
'type': 'comment-templates-categories',
'slug': 'test',
'name': 'test',
'auto-dependency': True,
},
],
}
manifest_fd = io.BytesIO(json.dumps(manifest_json, indent=2).encode())
@ -833,7 +846,7 @@ def test_deploy_application(app, admin_user, settings, app_bundle, app_bundle_wi
else:
assert version.number == '42.0'
assert version.notes == 'foo bar blah'
assert application.elements.count() == 3
assert application.elements.count() == 4
job = AsyncJob.objects.latest('pk')
assert job.status == 'completed'
assert job.progression_urls == {'wcs': {'Foobar': 'https://wcs.example.invalid/api/jobs/job-uuid/'}}
@ -914,10 +927,11 @@ def test_deploy_application(app, admin_user, settings, app_bundle, app_bundle_wi
resp.form.submit()
application = Application.objects.get(slug='test')
elements = application.elements.all().order_by('type')
assert len(elements) == 3
assert len(elements) == 4
assert elements[0].cache == {}
assert elements[1].cache == form_def
assert elements[2].cache == {}
assert elements[1].cache == {}
assert elements[2].cache == form_def
assert elements[3].cache == {}
def response_content(url, request):
if url.path == '/api/export-import/forms/':

View File

@ -16,17 +16,12 @@ from test_manager import login
from hobo.emails.validators import validate_email_address
from hobo.environment.models import Variable
from hobo.test_utils import find_free_port
@pytest.fixture
def port_available():
errno = 0
while not errno:
port = random.randint(49152, 65534)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
errno = sock.connect_ex(('127.0.0.1', port))
sock.close()
return port
return find_free_port()
@pytest.fixture

View File

@ -1,5 +1,6 @@
import pytest
from django.contrib.auth.models import User
from django.test import override_settings
from hobo.agent.common.models import UserExtraAttributes
from hobo.user_name.utils import get_full_name_from_template
@ -30,8 +31,14 @@ def test_user_cached_extra_attributes_missing_fallbacks_to_empty_dict(user):
def test_user_get_full_name_from_template(user):
assert get_full_name_from_template(user) == 'Jane bar'
with override_settings(
TEMPLATE_VARS={'user_full_name_template': '{{ user.first_name }} {{ user.attributes.foo }}'}
):
assert get_full_name_from_template(user) == 'Jane bar'
def test_user_get_full_name(user):
assert user.get_full_name() == 'Jane bar'
with override_settings(
TEMPLATE_VARS={'user_full_name_template': '{{ user.first_name }} {{ user.attributes.foo }}'}
):
assert user.get_full_name() == 'Jane bar'

View File

@ -1 +0,0 @@
{{ user.first_name }} {{ user.attributes.nicknames.0 }} {{ user.attributes.nicknames.2 }}

View File

@ -1,19 +1,13 @@
import pytest
from authentic2.models import Attribute, AttributeValue
from authentic2.models import Attribute
from django.contrib.auth import get_user_model
from django.test import override_settings
from tenant_schemas.utils import tenant_context
from hobo.agent.common.models import UserExtraAttributes
from hobo.user_name.utils import get_full_name_from_template
User = get_user_model()
TEMPLATES_DIR_CONFIG_INDIVIDUAL = ['tests/templates']
TEMPLATES_DIR_CONFIG_MULTIPLE = ['tests_authentic/custom_templates']
def test_get_full_name_from_template_utils_from_multiple_attrs(db, tenant, settings):
with tenant_context(tenant):
user = User.objects.create(
@ -43,14 +37,16 @@ def test_get_full_name_from_template_utils_from_multiple_attrs(db, tenant, setti
user.save()
user.refresh_from_db()
templates_conf_individual = settings.TEMPLATES.copy()
templates_conf_individual[0]['DIRS'] = TEMPLATES_DIR_CONFIG_INDIVIDUAL
with override_settings(TEMPLATES=templates_conf_individual):
with override_settings(
TEMPLATE_VARS={'user_full_name_template': '{{ user.first_name }} {{ user.attributes.foo }}'}
):
assert get_full_name_from_template(user) == 'Jane bar'
templates_conf_multiple = settings.TEMPLATES.copy()
templates_conf_multiple[0]['DIRS'] = TEMPLATES_DIR_CONFIG_MULTIPLE
with override_settings(TEMPLATES=templates_conf_multiple):
with override_settings(
TEMPLATE_VARS={
'user_full_name_template': '{{ user.first_name }} {{ user.attributes.nicknames.0 }} {{ user.attributes.nicknames.2 }}'
}
):
assert get_full_name_from_template(user) == 'Jane Milly Minnie'
@ -83,12 +79,14 @@ def test_get_full_name_from_template_accessor_from_multiple_attrs(db, tenant, se
user.save()
user.refresh_from_db()
templates_conf_individual = settings.TEMPLATES.copy()
templates_conf_individual[0]['DIRS'] = TEMPLATES_DIR_CONFIG_INDIVIDUAL
with override_settings(TEMPLATES=templates_conf_individual):
with override_settings(
TEMPLATE_VARS={'user_full_name_template': '{{ user.first_name }} {{ user.attributes.foo }}'}
):
assert user.get_full_name() == 'Jane bar'
templates_conf_multiple = settings.TEMPLATES.copy()
templates_conf_multiple[0]['DIRS'] = TEMPLATES_DIR_CONFIG_MULTIPLE
with override_settings(TEMPLATES=templates_conf_multiple):
with override_settings(
TEMPLATE_VARS={
'user_full_name_template': '{{ user.first_name }} {{ user.attributes.nicknames.0 }} {{ user.attributes.nicknames.2 }}'
}
):
assert user.get_full_name() == 'Jane Milly Minnie'