Commit d417dbe3 authored by Flavio TARSETTI's avatar Flavio TARSETTI
Browse files

[beat][web][accounts/ui] account yearly re-validation possible rejection fix URL_PREFIX usage

Part of #558
parent 94f5959f
......@@ -30,10 +30,14 @@ from datetime import timedelta
from django.conf import settings
from django.contrib.auth.models import User
from django.core import mail
from django.test import Client
from django.test import TestCase
from django.test import override_settings
from django.urls import reverse
from rest_framework import status
from ...utils.tests.helpers import reload_urlconf
from ..models import SupervisionTrack
from ..models import TemporaryUrl
from .core import AccountTestMixin
......@@ -317,3 +321,78 @@ class TemporaryUrlTestCase(ViewTestCase):
.count()
)
self.assertEqual(count, 0)
# ----------------------------------------------------------
class EmailSendingTestCase(ViewTestCase, AccountTestMixin):
def setUp(self):
super().setUp()
def run_email_check_yearly_revalidation_possible_rejection(
self, year_revalidation_url
):
client = self.yearrevalidationuser
logged_in = self.client.login(username="yearrevalidationuser", password="1234")
self.assertTrue(logged_in)
self.assertEqual(client.username, "yearrevalidationuser")
revalidation_url = reverse("api_accounts:revalidate_account")
response = self.client.put(revalidation_url, format="json")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.client.logout()
self.assertEqual(len(mail.outbox), 1)
text_lines = mail.outbox[0].body.split("\n")
self.assertTrue(
text_lines[8].startswith(
year_revalidation_url.split("/settings/")[0] + "/rejection/"
)
)
client = Client()
response = client.get(text_lines[8])
self.assertEqual(response.status_code, 200)
self.assertEqual(len(mail.outbox), 2)
def test_email_for_yearly_revalidation_possible_rejection(self):
for prefix in ["", "/platform"]:
with self.subTest(url_prefix=prefix):
mail.outbox = []
# YearRevalidation user
# Create and assign key
now = datetime.datetime.now()
expiration_date_delta = datetime.timedelta(
days=settings.ACCOUNT_BLOCKAGE_AFTER_FIRST_REJECTION_DAYS
)
self.yearrevalidationuser.profile.supervision_key = (
self.yearrevalidationuser.profile._generate_current_supervision_key()
)
supervisiontrack = SupervisionTrack.objects.create(
supervisee=self.yearrevalidationuser,
supervisor=self.firstsupervisor,
is_valid=True,
start_date=now,
expiration_date=now + expiration_date_delta,
last_validation_date=now,
)
# Assign key to supervision track
supervisiontrack.supervision_key = (
self.yearrevalidationuser.profile.supervision_key
)
supervisiontrack.save()
self.yearrevalidationuser.profile.supervision.add(supervisiontrack)
self.yearrevalidationuser.profile.save()
self.yearrevalidationuser.save()
with override_settings(URL_PREFIX=prefix):
reload_urlconf()
self.run_email_check_yearly_revalidation_possible_rejection(
"http://testserver" + prefix + "/accounts/settings/"
)
......@@ -36,6 +36,7 @@ from django.contrib.auth.models import User
from django.db import models
from django.shortcuts import get_object_or_404
from django.shortcuts import render
from django.urls import reverse
from rest_framework.authtoken.models import Token
from ..utils import mail
......@@ -245,18 +246,22 @@ def perform_revalidation(request, supervisiontrack, supervisee, now):
# Inform supervisor about supervisee revalidation
# Possible supervisor rejection available
parsed_url = urlparse(settings.URL_PREFIX)
server_address = "%s://%s" % (parsed_url.scheme, parsed_url.hostname,)
temp_url_rejection = TemporaryUrl.objects.create_temporary_url(
TemporaryUrl.REJECTION, supervisiontrack
)
supervisor_rejection_url = request.build_absolute_uri(
reverse(
"accounts:temp_url_rejection",
kwargs={"hash_url": temp_url_rejection.url_hash},
)
)
context = {
"supervisor": supervisiontrack.supervisor,
"supervisee": supervisee,
"prefix": server_address,
"temp_url": temp_url_rejection.url_hash,
"supervisor_rejection_url": supervisor_rejection_url,
}
mail.send_email(
......
......@@ -6,6 +6,6 @@ If he's still your current supervisee, you can drop this message.
However if he's not your supervisee anymore, you can immediately revoke your supervision by clicking on the following link:
{{ prefix }}/accounts/rejection/{{ temp_url }}
{{ supervisor_rejection_url }}
BEAT Administrators at the Idiap Research Institute
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