From cd1a6e5cf4174276991b253ca7f5eebede08aa42 Mon Sep 17 00:00:00 2001 From: Serghei Mihai Date: Tue, 15 Nov 2022 18:58:38 +0100 Subject: [PATCH] misc: render authenticators names in their own templates (#53264) --- .../authentic2/login_password_form.html | 47 ++++++++++--------- .../login_password_registration_form.html | 22 +++++---- .../registration/registration_form.html | 7 +-- src/authentic2_auth_fc/models.py | 2 +- .../templates/authentic2_auth_fc/login.html | 7 ++- .../authentic2_auth_fc/registration.html | 7 +++ src/authentic2_auth_fc/views.py | 7 ++- .../templates/authentic2_auth_saml/login.html | 3 ++ tests/conftest.py | 12 +++++ tests/settings.py | 2 +- .../authentic2/login_password_form.html | 3 ++ .../login_password_registration_form.html | 4 ++ tests/templates/authentic2_auth_fc/login.html | 5 ++ .../authentic2_auth_fc/registration.html | 5 ++ tests/test_template.py | 16 +++++++ 15 files changed, 108 insertions(+), 41 deletions(-) create mode 100644 src/authentic2_auth_fc/templates/authentic2_auth_fc/registration.html create mode 100644 tests/templates/authentic2/login_password_form.html create mode 100644 tests/templates/authentic2/login_password_registration_form.html create mode 100644 tests/templates/authentic2_auth_fc/login.html create mode 100644 tests/templates/authentic2_auth_fc/registration.html diff --git a/src/authentic2/templates/authentic2/login_password_form.html b/src/authentic2/templates/authentic2/login_password_form.html index cf406dc3e..49e7b76f3 100644 --- a/src/authentic2/templates/authentic2/login_password_form.html +++ b/src/authentic2/templates/authentic2/login_password_form.html @@ -1,13 +1,15 @@ {% load i18n static gadjo %} +{% block login-block-title %} +{% endblock %} + {% block login %} +
+ {% block form %} + {% if authenticator.button_description %} +

{{ authenticator.button_description }}

+ {% endif %} - {% block form %} - {% if authenticator.button_description %} -

{{ authenticator.button_description }}

- {% endif %} - -
{% csrf_token %} {{ form|with_template }} @@ -21,22 +23,21 @@ {% endblock %}
{{ form.media }} -
- {% endblock %} - - {% block actions %} - {% if can_reset_password or registration_authorized %} - - {% endif %} - {% endblock %} + {% endblock %} + {% block actions %} + {% if can_reset_password or registration_authorized %} + + {% endif %} + {% endblock %} +
{% endblock %} diff --git a/src/authentic2/templates/authentic2/login_password_registration_form.html b/src/authentic2/templates/authentic2/login_password_registration_form.html index 108df7f49..91fe3ff6c 100644 --- a/src/authentic2/templates/authentic2/login_password_registration_form.html +++ b/src/authentic2/templates/authentic2/login_password_registration_form.html @@ -1,11 +1,17 @@ {% load i18n gadjo %} -{% block registration %} -
- {% csrf_token %} - {{ form|with_template }} -
- -
-
+{% block registration-block-title %} +

{% trans "Registration" %}

+{% endblock %} + +{% block registration %} +

+

+ {% csrf_token %} + {{ form|with_template }} +
+ +
+
+

{% endblock %} diff --git a/src/authentic2/templates/registration/registration_form.html b/src/authentic2/templates/registration/registration_form.html index 35d4d0bda..ae6830245 100644 --- a/src/authentic2/templates/registration/registration_form.html +++ b/src/authentic2/templates/registration/registration_form.html @@ -7,16 +7,11 @@ {% block content %} -

{{ view.title }}

- {% include "authentic2/service_info_fragment.html" %} {% for id, block in frontends.items %}
-

{{ block.name }}

-

- {{ block.content|safe }} -

+ {{ block.content|safe }}
{% endfor %} diff --git a/src/authentic2_auth_fc/models.py b/src/authentic2_auth_fc/models.py index b1aa13386..93a639433 100644 --- a/src/authentic2_auth_fc/models.py +++ b/src/authentic2_auth_fc/models.py @@ -136,7 +136,7 @@ class FcAuthenticator(BaseAuthenticator): return views.profile(request, *args, **kwargs) def registration(self, request, *args, **kwargs): - return self.login(request, *args, **kwargs) + return views.registration(request, *args, **kwargs) class FcAccount(models.Model): diff --git a/src/authentic2_auth_fc/templates/authentic2_auth_fc/login.html b/src/authentic2_auth_fc/templates/authentic2_auth_fc/login.html index 16bba7495..753433c7f 100644 --- a/src/authentic2_auth_fc/templates/authentic2_auth_fc/login.html +++ b/src/authentic2_auth_fc/templates/authentic2_auth_fc/login.html @@ -1,7 +1,12 @@ {% load static %} {% load i18n %} -{% block login %} +{% block title %} + {% block login-block-title %} + {% endblock %} +{% endblock %} + +{% block content %} {% include "authentic2_auth_fc/explanation.html" %}
diff --git a/src/authentic2_auth_fc/templates/authentic2_auth_fc/registration.html b/src/authentic2_auth_fc/templates/authentic2_auth_fc/registration.html new file mode 100644 index 000000000..21abd6c8f --- /dev/null +++ b/src/authentic2_auth_fc/templates/authentic2_auth_fc/registration.html @@ -0,0 +1,7 @@ +{% extends "authentic2_auth_fc/login.html" %} + +{% block title %} + {% block registration-block-title %} +

FranceConnect

+ {% endblock %} +{% endblock %} diff --git a/src/authentic2_auth_fc/views.py b/src/authentic2_auth_fc/views.py index cfc124c10..5c1bc476a 100644 --- a/src/authentic2_auth_fc/views.py +++ b/src/authentic2_auth_fc/views.py @@ -77,10 +77,15 @@ def login(request, *args, **kwargs): ) context['login_url'] = utils_misc.make_url('fc-login-or-link', keep_params=True, request=request) context['block-extra-css-class'] = 'fc-login' - template = 'authentic2_auth_fc/login.html' + template = kwargs.get('template', 'authentic2_auth_fc/login.html') return TemplateResponse(request, template, context) +def registration(request, *args, **kwargs): + kwargs['template'] = 'authentic2_auth_fc/registration.html' + return login(request, *args, **kwargs) + + def profile(request, *args, **kwargs): # We prevent unlinking if the user has no usable password and can't change it # because we assume that the password is the unique other mean of authentication diff --git a/src/authentic2_auth_saml/templates/authentic2_auth_saml/login.html b/src/authentic2_auth_saml/templates/authentic2_auth_saml/login.html index a170849ab..37ef41db1 100644 --- a/src/authentic2_auth_saml/templates/authentic2_auth_saml/login.html +++ b/src/authentic2_auth_saml/templates/authentic2_auth_saml/login.html @@ -1,5 +1,8 @@ {% load i18n %} +{% block login-block-title %} +{% endblock %} + {% block before-login %} {% if authenticator.button_description %}

{{ authenticator.button_description }}

diff --git a/tests/conftest.py b/tests/conftest.py index befe5f4be..8668e37bb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -35,6 +35,7 @@ from authentic2.manager.utils import get_ou_count from authentic2.models import Attribute, Service from authentic2.utils import hooks as a2_hooks from authentic2.utils.evaluate import BaseExpressionValidator +from authentic2_auth_fc.models import FcAuthenticator from authentic2_auth_oidc.utils import get_provider_by_issuer from authentic2_idp_oidc.models import OIDCClient @@ -556,3 +557,14 @@ def scoped_db(django_db_setup, django_db_blocker): transaction.set_rollback(True) return scoped_db + + +@pytest.fixture +def fc(db): + FcAuthenticator.objects.create( + enabled=True, + client_id='xxx', + client_secret='yyy', + platform='test', + scopes=['profile', 'email'], + ) diff --git a/tests/settings.py b/tests/settings.py index 47b9b0d80..9153a4211 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -73,7 +73,7 @@ A2_VALIDATE_EMAIL_DOMAIN = False A2_HOOKS_PROPAGATE_EXCEPTIONS = True -TEMPLATES[0]['DIRS'].append('tests/templates') # pylint: disable=undefined-variable +TEMPLATES[0]['DIRS'].insert(0, 'tests/templates') # pylint: disable=undefined-variable TEMPLATES[0]['OPTIONS']['debug'] = True # pylint: disable=undefined-variable SITE_BASE_URL = 'https://testserver' diff --git a/tests/templates/authentic2/login_password_form.html b/tests/templates/authentic2/login_password_form.html new file mode 100644 index 000000000..afcdeb560 --- /dev/null +++ b/tests/templates/authentic2/login_password_form.html @@ -0,0 +1,3 @@ +{% extends "authentic2/login_password_form.html" %} + +{% block login-block-title %}

Log in with Password

{% endblock %} diff --git a/tests/templates/authentic2/login_password_registration_form.html b/tests/templates/authentic2/login_password_registration_form.html new file mode 100644 index 000000000..cfcae085a --- /dev/null +++ b/tests/templates/authentic2/login_password_registration_form.html @@ -0,0 +1,4 @@ +{% extends "authentic2/login_password_registration_form.html" %} +{% block registration-block-title %} +

Register with Password

+{% endblock %} diff --git a/tests/templates/authentic2_auth_fc/login.html b/tests/templates/authentic2_auth_fc/login.html new file mode 100644 index 000000000..000423f62 --- /dev/null +++ b/tests/templates/authentic2_auth_fc/login.html @@ -0,0 +1,5 @@ +{% extends "authentic2_auth_fc/login.html" %} + +{% block login-block-title %} +

Log in with FC

+{% endblock %} diff --git a/tests/templates/authentic2_auth_fc/registration.html b/tests/templates/authentic2_auth_fc/registration.html new file mode 100644 index 000000000..ab00662d7 --- /dev/null +++ b/tests/templates/authentic2_auth_fc/registration.html @@ -0,0 +1,5 @@ +{% extends "authentic2_auth_fc/registration.html" %} + +{% block registration-block-title %} +

Register with FC

+{% endblock %} diff --git a/tests/test_template.py b/tests/test_template.py index 164a840c8..59ff87424 100644 --- a/tests/test_template.py +++ b/tests/test_template.py @@ -16,6 +16,7 @@ import pytest +from authentic2.utils import misc as utils_misc from authentic2.utils.template import Template, TemplateError pytestmark = pytest.mark.django_db @@ -108,3 +109,18 @@ def test_render_template_missing_variable(): with pytest.raises(TemplateError) as raised: template.render(context=context) assert 'missing template variable' in raised + + +def test_registration_with_custom_titles(app, fc): + url = utils_misc.make_url('registration_register') + response = app.get(url) + assert '

Registration

' not in response.text + assert '

Register with Password

' in response.text + assert '

Register with FC

' in response.text + + +def test_login_with_custom_titles(app, fc): + url = utils_misc.make_url('auth_login') + response = app.get(url) + assert '

Log in with Password

' in response.text + assert '

Log in with FC

' in response.text