Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions ietf/doc/templatetags/ietf_filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright The IETF Trust 2007-2020, All Rights Reserved
# Copyright The IETF Trust 2007-2023, All Rights Reserved
# -*- coding: utf-8 -*-


Expand Down Expand Up @@ -409,18 +409,18 @@ def startswith(x, y):
return str(x).startswith(y)


@register.filter(name='removesuffix', is_safe=False)
def removesuffix(value, suffix):
"""Remove an exact-match suffix
@register.filter(name='removeprefix', is_safe=False)
def removeprefix(value, prefix):
"""Remove an exact-match prefix

The is_safe flag is False because indiscriminate use of this could result in non-safe output.
See https://docs.djangoproject.com/en/2.2/howto/custom-template-tags/#filters-and-auto-escaping
which describes the possibility that removing characters from an escaped string may introduce
HTML-unsafe output.
"""
base = str(value)
if base.endswith(suffix):
return base[:-len(suffix)]
if base.startswith(prefix):
return base[len(prefix):]
else:
return base

Expand Down
31 changes: 30 additions & 1 deletion ietf/ietfauth/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright The IETF Trust 2009-2022, All Rights Reserved
# Copyright The IETF Trust 2009-2023, All Rights Reserved
# -*- coding: utf-8 -*-


Expand Down Expand Up @@ -111,6 +111,35 @@ def test_login_and_logout(self):
self.assertEqual(r.status_code, 302)
self.assertEqual(urlsplit(r["Location"])[2], "/foobar")

def test_login_button(self):
PersonFactory(user__username='plain')

def _test_login(url):
# try mashing the sign-in button repeatedly
r = self.client.get(url)
if r.status_code == 302:
r = self.client.get(r["Location"])
self.assertEqual(r.status_code, 200)
q = PyQuery(r.content)
login_url = q("a:Contains('Sign in')").attr("href")
self.assertEqual(login_url, "/accounts/login/?next=" + url)
r = self.client.get(login_url)
self.assertEqual(r.status_code, 200)
q = PyQuery(r.content)
login_url = q("a:Contains('Sign in')").attr("href")
self.assertEqual(login_url, "/accounts/login/?next=" + url)

# try logging in with the provided next
r = self.client.post(login_url, {"username":"plain", "password":"plain+password"})
self.assertEqual(r.status_code, 302)
self.assertEqual(urlsplit(r["Location"])[2], url)
self.client.logout()

# try with a trivial next
_test_login("/")
# try with a next that requires login
_test_login(urlreverse(ietf.ietfauth.views.profile))

def test_login_with_different_email(self):
person = PersonFactory(user__username='plain')
email = EmailFactory(person=person)
Expand Down
4 changes: 2 additions & 2 deletions ietf/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{# Copyright The IETF Trust 2015-2022, All Rights Reserved #}
{# Copyright The IETF Trust 2015-2023, All Rights Reserved #}
<!DOCTYPE html>
{% load analytical %}
{% load ietf_filters static %}
Expand Down Expand Up @@ -60,7 +60,7 @@
{% if not user.is_authenticated %}
<a class="btn me-1 {% if server_mode and server_mode == "production" %} btn-warning {% else %} btn-info {% endif %} d-none d-sm-block"
rel="nofollow"
href="{% url 'ietf.ietfauth.views.login' %}?next={{ request.get_full_path|removesuffix:'accounts/logout/'|urlencode }}">
href="{% url 'ietf.ietfauth.views.login' %}?next={{ request.get_full_path|removeprefix:'/accounts/logout'|removeprefix:'/accounts/login/?next='|urlencode }}">
Sign in
</a>
{% endif %}
Expand Down
4 changes: 2 additions & 2 deletions ietf/templates/base/menu_user.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{# Copyright The IETF Trust 2015, All Rights Reserved #}
{# Copyright The IETF Trust 2015-2023, All Rights Reserved #}
{% load origin %}
{% origin %}
{% load ietf_filters %}
Expand Down Expand Up @@ -87,7 +87,7 @@
<li>
<a class="dropdown-item {% if flavor != 'top' %} text-wrap link-primary{% endif %}"
rel="nofollow"
href="{% url 'ietf.ietfauth.views.login' %}?next={{ request.get_full_path|urlencode }}">
href="{% url 'ietf.ietfauth.views.login' %}?next={{ request.get_full_path|removeprefix:'/accounts/login/?next='|urlencode }}">
Sign in
</a>
</li>
Expand Down