Commit 52d442db authored by Flavio TARSETTI's avatar Flavio TARSETTI

[beat][web][ui] registration/supervisor validation fix URL_PREFIX usage

Part of #558
parent 6d0cff60
......@@ -29,7 +29,6 @@ import datetime
import hashlib
import random
import re
from urllib.parse import urlparse
from django.conf import settings
from django.contrib.auth.models import User
......@@ -57,7 +56,7 @@ class RegistrationManager(models.Manager):
"""
def activate_user(self, activation_key):
def activate_user(self, activation_key, request):
"""
Validate an activation key and activate the corresponding
``User`` if valid.
......@@ -130,21 +129,23 @@ class RegistrationManager(models.Manager):
)
supervisor_user = supervisiontrack.supervisor
parsed_url = urlparse(settings.URL_PREFIX)
server_address = "%s://%s" % (
parsed_url.scheme,
parsed_url.hostname,
)
temp_url = TemporaryUrl.objects.create_temporary_url(
TemporaryUrl.VALIDATION, supervisiontrack
)
supervisor_validation_url = request.build_absolute_uri(
reverse(
"accounts:temp_url_validation",
kwargs={"hash_url": temp_url.url_hash},
)
)
contact_url = request.build_absolute_uri(reverse("contact"))
context = {
"supervisor": supervisor_user,
"supervisee": user,
"prefix": server_address,
"temp_url": temp_url.url_hash,
"supervisor_validation_url": supervisor_validation_url,
"contact_url": contact_url,
}
mail.send_email(
......
......@@ -12,9 +12,11 @@ Username: {{ supervisee.username }}
If you don't do this the supervisee will not be able to use his account and
will be deleted after 7 days.
You can also click on the following direct link to accomplish this action and accept this new supervisee at {{ prefix }}/accounts/validation/{{ temp_url }}
You can also click on the following direct link to accomplish this action and accept this new supervisee at:
{{ supervisor_validation_url }}
If you are having problems to activate your supervisee account, contact a member of our
support staff at {{ prefix }}{% url 'contact' %}.
support staff at {{ contact_url }}.
BEAT Administrators at the Idiap Research Institute
......@@ -40,7 +40,7 @@ class EmailSendingTestCase(ViewTestCase, AccountTestMixin):
def setUp(self):
super().setUp()
def run_email_check(self, reference_url):
def run_email_check_signup(self, reference_url):
client = Client()
response = client.post(
reverse("registration"),
......@@ -69,4 +69,48 @@ class EmailSendingTestCase(ViewTestCase, AccountTestMixin):
User.objects.filter(username="test").delete()
with override_settings(URL_PREFIX=prefix):
reload_urlconf()
self.run_email_check("http://testserver" + prefix + "/signup/")
self.run_email_check_signup(
"http://testserver" + prefix + "/signup/"
)
def run_email_check_activation(self, signup_url):
client = Client()
response = client.post(
reverse("registration"),
dict(
username="test",
first_name="test",
last_name="test",
email="test_email@idiap.ch",
password1="123",
password2="123",
supervisor="firstsupervisor",
tos="on",
),
)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 1)
text_lines = mail.outbox[0].body.split("\n")
self.assertTrue(text_lines[6].startswith(signup_url))
response = client.get(text_lines[6])
self.assertEqual(len(mail.outbox), 2)
text_lines = mail.outbox[1].body.split("\n")
self.assertTrue(
text_lines[16].startswith(
signup_url.split("/signup/")[0] + "/accounts/validation/"
)
)
def test_email_for_activation(self):
for prefix in ["", "/platform"]:
with self.subTest(url_prefix=prefix):
mail.outbox = []
User.objects.filter(username="test").delete()
with override_settings(URL_PREFIX=prefix):
reload_urlconf()
self.run_email_check_activation(
"http://testserver" + prefix + "/signup/"
)
......@@ -91,7 +91,7 @@ def activate(
"""
account = RegistrationProfile.objects.activate_user(activation_key)
account = RegistrationProfile.objects.activate_user(activation_key, request)
context = {"account": account, "expiration_days": settings.ACCOUNT_ACTIVATION_DAYS}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment