Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix registration flow (2) #75

Merged
merged 7 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions authentication/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,22 @@ def test_missing_fields(self):
class LoginTest(BaseTestCase):

def test_login_successful(self):
data = {'username': 'testuser', 'password': self.password}
self.user2 = AppUser.objects.create_user(email='[email protected]',username='testuser2',password=self.password, is_verified_user=True)
data = {'username': 'testuser2', 'password': self.password}
response = self.client.post(LOGIN_LINK, json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 200)

def test_login_failed(self):
data = {'username': 'testuser', 'password': 'testwrongpassword'}
response = self.client.post(LOGIN_LINK, json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()['msg'],"Wrong username/password!")
self.assertEqual(response.json()['msg'],"Wrong username/password!")

def test_login_failed_not_verfied(self):
data = {'username': 'testuser', 'password': self.password}
response = self.client.post(LOGIN_LINK, json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()['msg'],"You have not verified yet. Please register again and verify your account!")

def test_missing_fields(self):
data = {
Expand All @@ -119,6 +126,8 @@ def test_sent_email(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].to, ['[email protected]'])
response = self.client.get(EMAIL_VERIFICATION_LINK)
self.assertEqual(response.status_code, 200)

def test_valid_verification_token(self):
token = account_token.make_token(self.user)
Expand Down Expand Up @@ -158,6 +167,8 @@ def test_sent_email_recover_password(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].to, ['[email protected]'])
response = self.client.post((RECOVER_PASSWORD_LINK), {'email':'[email protected]'})
self.assertEqual(response.status_code, 200)

def test_sent_wrong_email_recover_password(self):
response = self.client.post((RECOVER_PASSWORD_LINK), {'email':'[email protected]'})
Expand Down
16 changes: 14 additions & 2 deletions authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ def post(self, request):
username = request.data.get('username')
password = request.data.get('password')
user = authenticate(request, username=username,password=password)
if user is not None:
if user is not None and AppUser.objects.get(id=user.pk).is_verified_user:
refresh = RefreshToken.for_user(user)
return Response({'refresh': str(refresh),
'access': str(refresh.access_token)})
else:
elif user is None:
return Response({'msg': 'Wrong username/password!'}, status=400)
else:
return Response({'msg': 'You have not verified yet. Please register again and verify your account!'}, status=400)

class SendVerificationEmailView(APIView):

Expand All @@ -96,6 +98,10 @@ class SendVerificationEmailView(APIView):
def get(self, request):
if request.user.is_verified_user != True:
user = request.user
if UserToken.objects.filter(user=user).exists():
token = UserToken.objects.get(user=user).token
if account_token.check_token(user, token):
return Response({'msg': 'Token already delivered!'})
token = create_shortened_token(user)
asyncio.run(send_verification_email(user, token.upper()))
return Response({'msg': 'Email delivered!'})
Expand All @@ -113,6 +119,7 @@ def post(self, request):
if account_token.check_token(user, token):
user.is_verified_user = True
user.save()
UserToken.objects.filter(user=user).delete()
Subscription.objects.create(user=user, plan=Package.objects.get(id=1), start_date=timezone.now(), end_date=timezone.make_aware(datetime(year=9999, month=12, day=31)))
return Response({'message': 'Email verified successfully!'}, status=200)
else:
Expand Down Expand Up @@ -145,6 +152,10 @@ def post(self, request):
is_user_exist = AppUser.objects.filter(email=email).exists()
if is_user_exist:
user = AppUser.objects.get(email=email)
if UserToken.objects.filter(user=user).exists():
token = UserToken.objects.get(user=user).token
if account_token.check_token(user, token):
return Response({'msg': 'Token already delivered!'})
token = create_shortened_token(user).upper()
asyncio.run(send_recover_account_email(user, token))
return Response({'msg': 'Email delivered!'})
Expand All @@ -165,6 +176,7 @@ def put(self, request):
if account_token.check_token(user, token):
user.set_password(new_password)
user.save()
UserToken.objects.filter(user=user).delete()
return Response({'msg': 'Password changes successfully!'}, status=200)
else:
return Response({'msg': 'Expired token!'}, status=400)
Expand Down
2 changes: 1 addition & 1 deletion revelio/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = os.getenv('DEFAULT_FROM_EMAIL')

PASSWORD_RESET_TIMEOUT = 300
PASSWORD_RESET_TIMEOUT = 1500

# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/
Expand Down
2 changes: 1 addition & 1 deletion revelio/settings_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = os.getenv('DEFAULT_FROM_EMAIL')

PASSWORD_RESET_TIMEOUT = 300
PASSWORD_RESET_TIMEOUT = 1500

# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/
Expand Down
Loading