mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-14 17:45:46 +00:00
161 lines
5.0 KiB
Python
161 lines
5.0 KiB
Python
"""
|
|
web admin test auth file
|
|
"""
|
|
from datetime import datetime
|
|
from django.utils import timezone
|
|
from django.urls import reverse
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from rest_framework.test import APITestCase, APIClient
|
|
from rest_framework import status
|
|
|
|
from account.models import UserEmailOtp
|
|
from base.constants import USER_TYPE
|
|
from guardian.tasks import generate_otp
|
|
from web_admin.tests.test_set_up import BaseSetUp
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
class AdminOTPTestCase(BaseSetUp):
|
|
"""
|
|
test case to send otp to admin email
|
|
"""
|
|
|
|
def setUp(self):
|
|
"""
|
|
inherit data here
|
|
:return:
|
|
"""
|
|
super(AdminOTPTestCase, self).setUp()
|
|
self.url = reverse('web_admin:admin-otp')
|
|
|
|
def test_admin_otp_for_valid_email(self):
|
|
"""
|
|
test admin otp for valid email
|
|
:return:
|
|
"""
|
|
data = {
|
|
'email': self.admin_email
|
|
}
|
|
response = self.client.post(self.url, data, format='json')
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
self.assertEqual(UserEmailOtp.objects.count(), 1)
|
|
|
|
def test_admin_otp_for_invalid_email(self):
|
|
"""
|
|
test admin otp for invalid email
|
|
:return:
|
|
"""
|
|
data = {
|
|
'email': 'notadmin@example.com'
|
|
}
|
|
response = self.client.post(self.url, data, format='json')
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
class AdminVerifyOTPTestCase(BaseSetUp):
|
|
"""
|
|
test case to verify otp for admin email
|
|
"""
|
|
|
|
def setUp(self):
|
|
"""
|
|
inherit data here
|
|
:return:
|
|
"""
|
|
super(AdminVerifyOTPTestCase, self).setUp()
|
|
self.verification_code = generate_otp()
|
|
expiry = timezone.now() + timezone.timedelta(days=1)
|
|
self.user_email_otp = UserEmailOtp.objects.create(email=self.admin_email,
|
|
otp=self.verification_code,
|
|
expired_at=expiry,
|
|
user_type=dict(USER_TYPE).get('3'),
|
|
)
|
|
self.url = reverse('web_admin:admin-verify-otp')
|
|
|
|
def test_admin_verify_otp_with_valid_otp(self):
|
|
"""
|
|
test admin verify otp with valid otp
|
|
:return:
|
|
"""
|
|
|
|
data = {
|
|
'email': self.admin_email,
|
|
"otp": self.verification_code
|
|
}
|
|
|
|
response = self.client.post(self.url, data)
|
|
self.user_email_otp.refresh_from_db()
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
self.assertEqual(self.user_email_otp.is_verified, True)
|
|
|
|
def test_admin_verify_otp_with_invalid_otp(self):
|
|
"""
|
|
test admin verify otp with invalid otp
|
|
:return:
|
|
"""
|
|
data = {
|
|
'email': self.admin_email,
|
|
"otp": generate_otp()
|
|
}
|
|
|
|
response = self.client.post(self.url, data)
|
|
self.user_email_otp.refresh_from_db()
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
|
self.assertEqual(self.user_email_otp.is_verified, False)
|
|
|
|
|
|
class AdminCreateNewPassword(BaseSetUp):
|
|
"""
|
|
test case to create new password for admin email
|
|
"""
|
|
|
|
def setUp(self):
|
|
"""
|
|
inherit data here
|
|
:return:
|
|
"""
|
|
super(AdminCreateNewPassword, self).setUp()
|
|
self.verification_code = generate_otp()
|
|
expiry = timezone.now() + timezone.timedelta(days=1)
|
|
self.user_email_otp = UserEmailOtp.objects.create(email=self.admin_email,
|
|
otp=self.verification_code,
|
|
expired_at=expiry,
|
|
user_type=dict(USER_TYPE).get('3'),
|
|
)
|
|
self.url = reverse('web_admin:admin-create-password')
|
|
|
|
def test_admin_create_new_password_after_verification(self):
|
|
"""
|
|
test admin create new password
|
|
:return:
|
|
"""
|
|
self.user_email_otp.is_verified = True
|
|
self.user_email_otp.save()
|
|
|
|
data = {
|
|
'email': self.admin_email,
|
|
"new_password": "New@1234",
|
|
"confirm_password": "New@1234"
|
|
}
|
|
|
|
response = self.client.post(self.url, data)
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
self.assertEqual(UserEmailOtp.objects.count(), 0)
|
|
|
|
def test_admin_create_new_password_without_verification(self):
|
|
"""
|
|
test admin create new password
|
|
:return:
|
|
"""
|
|
data = {
|
|
'email': self.admin_email,
|
|
"new_password": "Some@1234",
|
|
"confirm_password": "Some@1234"
|
|
}
|
|
|
|
response = self.client.post(self.url, data)
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
|
self.assertEqual(UserEmailOtp.objects.count(), 1)
|