new user activation working
This commit is contained in:
@@ -7,11 +7,13 @@ import csv
|
|||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.core import mail
|
from django.core import mail
|
||||||
|
from django.utils.http import urlsafe_base64_encode
|
||||||
|
from django.utils.encoding import force_bytes
|
||||||
|
|
||||||
from rest_framework.test import APITestCase
|
from rest_framework.test import APITestCase
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
|
||||||
from core.utils import get_tokens_for_user
|
from core.utils import get_tokens_for_user, account_activation_token
|
||||||
|
|
||||||
from companies.models import Company
|
from companies.models import Company
|
||||||
|
|
||||||
@@ -539,3 +541,42 @@ class MyUserViewTest(APITestCase):
|
|||||||
# check response
|
# check response
|
||||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
|
|
||||||
|
class ActivateUserTest(APITestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.endpoint = 'activate/<uidb64>/<token>/'
|
||||||
|
self.factory = factories.CustomUserFactory
|
||||||
|
self.model = models.CustomUser
|
||||||
|
# create user
|
||||||
|
self.email = f"user@mail.com"
|
||||||
|
self.password = ''.join(random.choices(string.ascii_uppercase, k = 10))
|
||||||
|
self.user = self.factory(email=self.email, is_active=False)
|
||||||
|
self.user.set_password(self.password)
|
||||||
|
self.user.save()
|
||||||
|
|
||||||
|
def test_correct_activation(self):
|
||||||
|
# create values
|
||||||
|
uid = urlsafe_base64_encode(force_bytes(self.user.pk))
|
||||||
|
token = account_activation_token.make_token(self.user)
|
||||||
|
|
||||||
|
url = f'/activate/{uid}/{token}/'
|
||||||
|
|
||||||
|
response = self.client.get(url)
|
||||||
|
|
||||||
|
# assertions
|
||||||
|
self.assertEquals(response.status_code, 200)
|
||||||
|
self.assertTrue(self.user.email in str(response.content))
|
||||||
|
|
||||||
|
def test_bad_activation(self):
|
||||||
|
# create values
|
||||||
|
uid = urlsafe_base64_encode(force_bytes(self.user.pk))[:-1]
|
||||||
|
token = account_activation_token.make_token(self.user)[:-1]
|
||||||
|
|
||||||
|
url = f'/activate/{uid}/{token}/'
|
||||||
|
|
||||||
|
response = self.client.get(url)
|
||||||
|
|
||||||
|
# assertions
|
||||||
|
self.assertEquals(response.status_code, 406)
|
||||||
|
self.assertTrue('error' in response.json())
|
||||||
|
|||||||
@@ -202,10 +202,10 @@ def activate_user(request, uidb64, token):
|
|||||||
except (TypeError, ValueError, OverflowError, User.DoesNotExist):
|
except (TypeError, ValueError, OverflowError, User.DoesNotExist):
|
||||||
user = None
|
user = None
|
||||||
|
|
||||||
if user is not None and account_activation_token.check_token(user, token):
|
if user is not None and utils.account_activation_token.check_token(user, token):
|
||||||
# activate user
|
# activate user
|
||||||
user.is_active = True
|
user.is_active = True
|
||||||
user.save()
|
user.save()
|
||||||
return HttpResponse(f"Tu cuenta de usuario {request.user.email} ha sido activada")
|
return Response(f"Tu cuenta de usuario {user.email} ha sido activada")
|
||||||
else:
|
else:
|
||||||
return HttpResponse(f"Tu token de verificacion no coincide con ningún usuario registrado")
|
return Response({"error": f"Tu token de verificacion no coincide con ningún usuario registrado"}, status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||||
|
|||||||
Reference in New Issue
Block a user