Skip to content

Commit

Permalink
euth, euth_wagtail: disable registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
m4ra committed Jan 10, 2024
1 parent 9a655dd commit 8423894
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 97 deletions.
11 changes: 11 additions & 0 deletions euth/users/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ def template_name(self):
class EuthAccountAdapter(DefaultAccountAdapter):
username_regex = re.compile(USERNAME_REGEX)

def is_open_for_signup(self, request):
"""
Checks whether or not the site is open for signups.
Next to simply returning True/False you can also intervene the
regular flow by raising an ImmediateHttpResponse
(Comment reproduced from the overridden method.)
"""
return False

def get_email_confirmation_url(self, request, emailconfirmation):
url = super().get_email_confirmation_url(request, emailconfirmation)
if 'next' in request.POST \
Expand Down
5 changes: 3 additions & 2 deletions euth/users/templates/euth_users/indicator_menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@
</ul>
</div>
{% else %}
<a href="#" class="dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="inner">{% trans "Login / Register"%}<i class="fa fa-caret-down ps-1" aria-hidden="true"></i></span>
<a href="#" class="dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="inner">{% trans "Login"%}<i class="fa fa-caret-down ps-1" aria-hidden="true"></i></span>
</a>
<ul class="dropdown-menu">
<li>
<a href="{% url 'account_login' %}?next={{ redirect_field_value|urlencode }}" class="dropdown-item">{% trans "login" %}</a>
</li>
<li>
<!-- <li>
<a href="{% url 'account_signup' %}?next={{ redirect_field_value|urlencode }}" class="dropdown-item">{% trans "register" %}</a>
</li>
-->
</ul>
{% endif %}
</li>
Expand Down
1 change: 1 addition & 0 deletions euth_wagtail/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
'micawber.contrib.mcdjango',

'adhocracy4.administrative_districts',
Expand Down
6 changes: 4 additions & 2 deletions euth_wagtail/templates/account/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
<div class="general-form">
<div class="form-inner pt-3 pt-md-5">
<h1>{% trans "Login" %}</h1>
<p>
<!-- <p>
{% trans "I do not have an account, " %}
<a href="{% url 'account_signup' %}?next={{redirect_field_value|urlencode}}">
{% trans "register" %}
</a>
</p>
-->
<form role="form" name="LoginForm" action="{% url 'account_login' %}" method="post" >
{% csrf_token %}

Expand Down Expand Up @@ -79,7 +80,8 @@ <h1>{% trans "Login" %}</h1>
{% endif %}
</div>
<div class="account__container--right">
{% include 'account/includes/howto.html' %}
<!-- {% include 'account/includes/howto.html' %}
-->
</div>
</div>
{% endblock %}
20 changes: 20 additions & 0 deletions euth_wagtail/templates/account/signup_closed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends 'base.html' %}

{% load i18n %}

{% block head_title %}{% trans "Sign Up Closed" %}{% endblock %}

{% block content %}
<div class="account__container">
<div class="account__container--left">
<div class="general-form">
<header class="form-inner pt-3 pt-md-5">
<h1>{% trans "Sign Up Closed" %}</h1>
<p>
{% trans "We are sorry, but the sign up is currently closed." %}
</p>
</header>
</div>
</div>
{% endblock %}

190 changes: 97 additions & 93 deletions tests/users/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from django.urls import reverse

from euth.users import models
from tests.helpers import redirect_target

# from tests.helpers import redirect_target

User = auth.get_user_model()

Expand Down Expand Up @@ -56,113 +57,116 @@ def test_logout_with_next(user, client, logout_url):
assert '_auth_user_id' not in client.session


@pytest.mark.django_db
def test_register(client, signup_url):
assert EmailAddress.objects.count() == 0
email = 'testuser@liqd.de'
response = client.post(
signup_url, {
'username': 'testuser',
'email': email,
'password1': 'password',
'password2': 'password',
'terms_of_use': 'on',
'captcha': 'testpass:0',
}
)
assert response.status_code == 302
assert EmailAddress.objects.filter(
email=email, verified=False
).count() == 1
assert len(mail.outbox) == 1
confirmation_url = re.search(
r'(http://testserver/.*/)',
str(mail.outbox[0].body)
).group(0)

confirm_email_response = client.get(confirmation_url)
assert confirm_email_response.status_code == 200
assert EmailAddress.objects.filter(
email=email, verified=False
).count() == 1
confirm_email_response = client.post(confirmation_url)
assert confirm_email_response.status_code == 302
assert EmailAddress.objects.filter(
email=email, verified=True
).count() == 1


@pytest.mark.django_db
def test_register_with_next(client, signup_url):
assert EmailAddress.objects.count() == 0
email = 'testuser2@liqd.de'
response = client.post(
signup_url, {
'username': 'testuser2',
'email': email,
'password1': 'password',
'password2': 'password',
'terms_of_use': 'on',
'next': '/en/projects/pppp/',
'captcha': 'testpass:0',
}
)
assert response.status_code == 302
assert EmailAddress.objects.filter(
email=email, verified=False
).count() == 1
assert len(mail.outbox) == 1
confirmation_url = re.search(
r'(http://testserver/.*/?next=/en/projects/pppp/)',
str(mail.outbox[0].body)
).group(0)
confirm_email_response = client.get(confirmation_url)
assert confirm_email_response.status_code == 200
assert EmailAddress.objects.filter(
email=email, verified=False
).count() == 1
confirm_email_response = client.post(confirmation_url)
assert confirm_email_response.status_code == 302
assert redirect_target(confirm_email_response) == "project-detail"
assert EmailAddress.objects.filter(
email=email, verified=True
).count() == 1


@pytest.mark.django_db
def test_reregister_same_username(client, signup_url):
assert EmailAddress.objects.count() == 0
data = {
'username': 'testuser3',
'email': 'testuser3@liqd.de',
'password1': 'password',
'password2': 'password',
'terms_of_use': 'on',
'captcha': 'testpass:0',
}
response = client.post(signup_url, data)
assert response.status_code == 302
assert EmailAddress.objects.count() == 1
data['email'] = 'anotheremail@liqd.de'
response = client.post(signup_url, data)
assert response.status_code == 302
assert EmailAddress.objects.count() == 1
# @pytest.mark.django_db
# def test_register(client, signup_url):
# assert EmailAddress.objects.count() == 0
# email = 'testuser@liqd.de'
# response = client.post(
# signup_url, {
# 'username': 'testuser',
# 'email': email,
# 'password1': 'password',
# 'password2': 'password',
# 'terms_of_use': 'on',
# 'captcha': 'testpass:0',
# }
# )
# assert response.status_code == 302
# assert EmailAddress.objects.filter(
# email=email, verified=False
# ).count() == 1
# assert len(mail.outbox) == 1
# confirmation_url = re.search(
# r'(http://testserver/.*/)',
# str(mail.outbox[0].body)
# ).group(0)
# confirm_email_response = client.get(confirmation_url)
# assert confirm_email_response.status_code == 200
# assert EmailAddress.objects.filter(
# email=email, verified=False
# ).count() == 1
# confirm_email_response = client.post(confirmation_url)
# assert confirm_email_response.status_code == 302
# assert EmailAddress.objects.filter(
# email=email, verified=True
# ).count() == 1


# @pytest.mark.django_db
# def test_register_with_next(client, signup_url):
# assert EmailAddress.objects.count() == 0
# email = 'testuser2@liqd.de'
# response = client.post(
# signup_url, {
# 'username': 'testuser2',
# 'email': email,
# 'password1': 'password',
# 'password2': 'password',
# 'terms_of_use': 'on',
# 'next': '/en/projects/pppp/',
# 'captcha': 'testpass:0',
# }
# )
# assert response.status_code == 302
# assert EmailAddress.objects.filter(
# email=email, verified=False
# ).count() == 1
# assert len(mail.outbox) == 1
# confirmation_url = re.search(
# r'(http://testserver/.*/?next=/en/projects/pppp/)',
# str(mail.outbox[0].body)
# ).group(0)
# confirm_email_response = client.get(confirmation_url)
# assert confirm_email_response.status_code == 200
# assert EmailAddress.objects.filter(
# email=email, verified=False
# ).count() == 1
# confirm_email_response = client.post(confirmation_url)
# assert confirm_email_response.status_code == 302
# assert redirect_target(confirm_email_response) == "project-detail"
# assert EmailAddress.objects.filter(
# email=email, verified=True
# ).count() == 1


# @pytest.mark.django_db
# def test_reregister_same_username(client, signup_url):
# assert EmailAddress.objects.count() == 0
# data = {
# 'username': 'testuser3',
# 'email': 'testuser3@liqd.de',
# 'password1': 'password',
# 'password2': 'password',
# 'terms_of_use': 'on',
# 'captcha': 'testpass:0',
# }
# response = client.post(signup_url, data)
# assert response.status_code == 302
# assert EmailAddress.objects.count() == 1
# data['email'] = 'anotheremail@liqd.de'
# response = client.post(signup_url, data)
# assert response.status_code == 302
# assert EmailAddress.objects.count() == 1


@pytest.mark.django_db
def test_register_invalid(client, signup_url):
# check registration is disabled
# uncomment wrong password if registration is enabled
username = 'testuser4'
response = client.post(
signup_url + '?next=/', {
'username': username,
'email': 'testuser4@liqd.de',
'password1': 'password',
'password2': 'wrong_password',
'password2': 'password',
# 'password2': 'wrong_password',
'terms_of_use': 'on'
}
)
assert response.status_code == 200
assert models.User.objects.filter(username=username).count() == 0
assert EmailAddress.objects.count() == 0


@pytest.mark.django_db
Expand Down

0 comments on commit 8423894

Please sign in to comment.