mirror of
				https://gitlab.com/animath/si/plateforme-corres2math.git
				synced 2025-11-04 16:02:31 +01:00 
			
		
		
		
	Fully test registration app
This commit is contained in:
		
							
								
								
									
										16
									
								
								apps/api/tests.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								apps/api/tests.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
				
			|||||||
 | 
					from django.contrib.auth.models import User
 | 
				
			||||||
 | 
					from django.test import TestCase
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestAPIPages(TestCase):
 | 
				
			||||||
 | 
					    def setUp(self):
 | 
				
			||||||
 | 
					        self.user = User.objects.create_superuser(
 | 
				
			||||||
 | 
					            username="admin",
 | 
				
			||||||
 | 
					            password="apitest",
 | 
				
			||||||
 | 
					            email="",
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        self.client.force_login(self.user)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_user_page(self):
 | 
				
			||||||
 | 
					        response = self.client.get("/api/user/")
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, 200)
 | 
				
			||||||
@@ -10,7 +10,7 @@ class UserViewSet(ModelViewSet):
 | 
				
			|||||||
    """
 | 
					    """
 | 
				
			||||||
    Display list of users.
 | 
					    Display list of users.
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    queryset = User.objects.all()
 | 
					    queryset = User.objects.order_by("id").all()
 | 
				
			||||||
    serializer_class = UserSerializer
 | 
					    serializer_class = UserSerializer
 | 
				
			||||||
    filter_backends = [DjangoFilterBackend, SearchFilter]
 | 
					    filter_backends = [DjangoFilterBackend, SearchFilter]
 | 
				
			||||||
    filterset_fields = ['id', 'first_name', 'last_name', 'email', 'is_superuser', 'is_staff', 'is_active', ]
 | 
					    filterset_fields = ['id', 'first_name', 'last_name', 'email', 'is_superuser', 'is_staff', 'is_active', ]
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,119 @@
 | 
				
			|||||||
 | 
					from corres2math.tokens import email_validation_token
 | 
				
			||||||
 | 
					from django.contrib.auth.models import User
 | 
				
			||||||
 | 
					from django.test import TestCase
 | 
				
			||||||
 | 
					from django.urls import reverse
 | 
				
			||||||
 | 
					from django.utils.encoding import force_bytes
 | 
				
			||||||
 | 
					from django.utils.http import urlsafe_base64_encode
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from .models import CoachRegistration, Registration, StudentRegistration
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestIndexPage(TestCase):
 | 
				
			||||||
 | 
					    def test_index(self) -> None:
 | 
				
			||||||
 | 
					        response = self.client.get(reverse("index"))
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, 200)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestRegistration(TestCase):
 | 
				
			||||||
 | 
					    def setUp(self) -> None:
 | 
				
			||||||
 | 
					        self.user = User.objects.create_superuser(
 | 
				
			||||||
 | 
					            username="admin",
 | 
				
			||||||
 | 
					            password="admin",
 | 
				
			||||||
 | 
					            email="admin@example.com",
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        self.client.force_login(self.user)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_registration(self):
 | 
				
			||||||
 | 
					        response = self.client.get(reverse("registration:signup"))
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, 200)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Incomplete form
 | 
				
			||||||
 | 
					        response = self.client.post(reverse("registration:signup"), data=dict(
 | 
				
			||||||
 | 
					            last_name="Toto",
 | 
				
			||||||
 | 
					            first_name="Toto",
 | 
				
			||||||
 | 
					            email="toto@example.com",
 | 
				
			||||||
 | 
					            password1="azertyuiopazertyuiop",
 | 
				
			||||||
 | 
					            password2="azertyuiopazertyuiop",
 | 
				
			||||||
 | 
					            role="participant",
 | 
				
			||||||
 | 
					        ))
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, 200)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        response = self.client.post(reverse("registration:signup"), data=dict(
 | 
				
			||||||
 | 
					            last_name="Toto",
 | 
				
			||||||
 | 
					            first_name="Toto",
 | 
				
			||||||
 | 
					            email="toto@example.com",
 | 
				
			||||||
 | 
					            password1="azertyuiopazertyuiop",
 | 
				
			||||||
 | 
					            password2="azertyuiopazertyuiop",
 | 
				
			||||||
 | 
					            role="participant",
 | 
				
			||||||
 | 
					            student_class=12,
 | 
				
			||||||
 | 
					            school="God",
 | 
				
			||||||
 | 
					            give_contact_to_animath=False,
 | 
				
			||||||
 | 
					        ))
 | 
				
			||||||
 | 
					        self.assertRedirects(response, reverse("registration:email_validation_sent"), 302, 200)
 | 
				
			||||||
 | 
					        self.assertTrue(User.objects.filter(email="toto@example.com").exists())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        response = self.client.get(reverse("registration:email_validation_sent"))
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, 200)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        response = self.client.post(reverse("registration:signup"), data=dict(
 | 
				
			||||||
 | 
					            last_name="Toto",
 | 
				
			||||||
 | 
					            first_name="Coach",
 | 
				
			||||||
 | 
					            email="coachtoto@example.com",
 | 
				
			||||||
 | 
					            password1="azertyuiopazertyuiop",
 | 
				
			||||||
 | 
					            password2="azertyuiopazertyuiop",
 | 
				
			||||||
 | 
					            role="coach",
 | 
				
			||||||
 | 
					            professional_activity="God",
 | 
				
			||||||
 | 
					            give_contact_to_animath=True,
 | 
				
			||||||
 | 
					        ))
 | 
				
			||||||
 | 
					        self.assertRedirects(response, reverse("registration:email_validation_sent"), 302, 200)
 | 
				
			||||||
 | 
					        self.assertTrue(User.objects.filter(email="coachtoto@example.com").exists())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        user = User.objects.get(email="coachtoto@example.com")
 | 
				
			||||||
 | 
					        token = email_validation_token.make_token(user)
 | 
				
			||||||
 | 
					        uid = urlsafe_base64_encode(force_bytes(user.pk))
 | 
				
			||||||
 | 
					        response = self.client.get(reverse("registration:email_validation", kwargs=dict(uidb64=uid, token=token)))
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, 200)
 | 
				
			||||||
 | 
					        user.registration.refresh_from_db()
 | 
				
			||||||
 | 
					        self.assertTrue(user.registration.email_confirmed)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Token has expired
 | 
				
			||||||
 | 
					        response = self.client.get(reverse("registration:email_validation", kwargs=dict(uidb64=uid, token=token)))
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, 400)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Uid does not exist
 | 
				
			||||||
 | 
					        response = self.client.get(reverse("registration:email_validation", kwargs=dict(uidb64=0, token="toto")))
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, 400)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        response = self.client.get(reverse("registration:email_validation_resend", args=(user.pk,)))
 | 
				
			||||||
 | 
					        self.assertRedirects(response, reverse("registration:email_validation_sent"), 302, 200)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_login(self):
 | 
				
			||||||
 | 
					        response = self.client.get(reverse("login"))
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, 200)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.client.logout()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        response = self.client.post(reverse("login"), data=dict(
 | 
				
			||||||
 | 
					            username="admin",
 | 
				
			||||||
 | 
					            password="toto",
 | 
				
			||||||
 | 
					        ))
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, 200)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        response = self.client.post(reverse("login"), data=dict(
 | 
				
			||||||
 | 
					            username="admin@example.com",
 | 
				
			||||||
 | 
					            password="admin",
 | 
				
			||||||
 | 
					        ))
 | 
				
			||||||
 | 
					        self.assertRedirects(response, reverse("index"), 302, 200)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_change_email(self):
 | 
				
			||||||
 | 
					        self.user.email = "newaddressmail@example.com"
 | 
				
			||||||
 | 
					        self.user.save()
 | 
				
			||||||
 | 
					        self.assertEqual(self.user.email, self.user.username)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_string_render(self):
 | 
				
			||||||
 | 
					        # TODO These string field tests will be removed when used in a template
 | 
				
			||||||
 | 
					        str(self.user.registration)
 | 
				
			||||||
 | 
					        self.assertRaises(NotImplementedError, lambda: Registration().type)
 | 
				
			||||||
 | 
					        str(StudentRegistration().type)
 | 
				
			||||||
 | 
					        str(CoachRegistration().type)
 | 
				
			||||||
 | 
					        str(self.user.registration.type)    # AdminRegistration
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -45,7 +45,7 @@ class SignupView(CreateView):
 | 
				
			|||||||
        return ret
 | 
					        return ret
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_success_url(self):
 | 
					    def get_success_url(self):
 | 
				
			||||||
        return reverse_lazy("index")
 | 
					        return reverse_lazy("registration:email_validation_sent")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class UserValidateView(TemplateView):
 | 
					class UserValidateView(TemplateView):
 | 
				
			||||||
@@ -70,7 +70,7 @@ class UserValidateView(TemplateView):
 | 
				
			|||||||
        if user is not None and email_validation_token.check_token(user, token):
 | 
					        if user is not None and email_validation_token.check_token(user, token):
 | 
				
			||||||
            self.validlink = True
 | 
					            self.validlink = True
 | 
				
			||||||
            user.registration.email_confirmed = True
 | 
					            user.registration.email_confirmed = True
 | 
				
			||||||
            user.save()
 | 
					            user.registration.save()
 | 
				
			||||||
        return self.render_to_response(self.get_context_data(), status=200 if self.validlink else 400)
 | 
					        return self.render_to_response(self.get_context_data(), status=200 if self.validlink else 400)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_user(self, uidb64):
 | 
					    def get_user(self, uidb64):
 | 
				
			||||||
@@ -116,8 +116,5 @@ class UserResendValidationEmailView(LoginRequiredMixin, DetailView):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def get(self, request, *args, **kwargs):
 | 
					    def get(self, request, *args, **kwargs):
 | 
				
			||||||
        user = self.get_object()
 | 
					        user = self.get_object()
 | 
				
			||||||
 | 
					        user.registration.send_email_validation_link()
 | 
				
			||||||
        user.profile.send_email_validation_link()
 | 
					        return redirect('registration:email_validation_sent')
 | 
				
			||||||
 | 
					 | 
				
			||||||
        # TODO Change URL
 | 
					 | 
				
			||||||
        return redirect('index')
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -60,6 +60,7 @@ INSTALLED_APPS = [
 | 
				
			|||||||
    'rest_framework',
 | 
					    'rest_framework',
 | 
				
			||||||
    'rest_framework.authtoken',
 | 
					    'rest_framework.authtoken',
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    'api',
 | 
				
			||||||
    'eastereggs',
 | 
					    'eastereggs',
 | 
				
			||||||
    'registration',
 | 
					    'registration',
 | 
				
			||||||
    'participation',
 | 
					    'participation',
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,7 +20,8 @@ class AccountActivationTokenGenerator(PasswordResetTokenGenerator):
 | 
				
			|||||||
        # Truncate microseconds so that tokens are consistent even if the
 | 
					        # Truncate microseconds so that tokens are consistent even if the
 | 
				
			||||||
        # database doesn't support microseconds.
 | 
					        # database doesn't support microseconds.
 | 
				
			||||||
        login_timestamp = '' if user.last_login is None else user.last_login.replace(microsecond=0, tzinfo=None)
 | 
					        login_timestamp = '' if user.last_login is None else user.last_login.replace(microsecond=0, tzinfo=None)
 | 
				
			||||||
        return str(user.pk) + str(user.email) + str(login_timestamp) + str(timestamp)
 | 
					        return str(user.pk) + str(user.email) + str(user.registration.email_confirmed)\
 | 
				
			||||||
 | 
					            + str(login_timestamp) + str(timestamp)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
email_validation_token = AccountActivationTokenGenerator()
 | 
					email_validation_token = AccountActivationTokenGenerator()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user