added test cases for auth

This commit is contained in:
abutalib-kiwi
2023-09-11 14:39:01 +05:30
parent d937c1bb92
commit bf1004696a
6 changed files with 730 additions and 633 deletions

BIN
.coverage

Binary file not shown.

View File

@ -20,11 +20,10 @@ class UserLoginTestCase(TestCase):
:return:
"""
self.client = APIClient()
self.user = User.objects.create_superuser(
username='admin@example.com',
email='admin@example.com',
password='admin@1234'
)
self.user_email = 'user@example.com'
self.user = User.objects.create_superuser(username=self.user_email, email=self.user_email)
self.user.set_password('user@1234')
self.user.save()
def test_admin_login_success(self):
"""
@ -33,8 +32,8 @@ class UserLoginTestCase(TestCase):
"""
url = reverse('account:admin-login')
data = {
'email': 'admin@example.com',
'password': 'admin@1234',
'email': self.user_email,
'password': 'user@1234',
}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
@ -49,8 +48,8 @@ class UserLoginTestCase(TestCase):
"""
url = reverse('account:admin-login')
data = {
'email': 'admin@example.com',
'password': 'admin@1235',
'email': self.user_email,
'password': 'user@1235',
}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

File diff suppressed because it is too large Load Diff

View File

@ -36,7 +36,7 @@ class ArticleViewSetTestCase(ArticleTestSetUp):
test article create with default card_image
:return:
"""
url = reverse('web_admin:article-list')
url = reverse(self.article_list_url)
response = self.client.post(url, self.article_data_with_default_card_image, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
# Check that a new article was created
@ -48,7 +48,7 @@ class ArticleViewSetTestCase(ArticleTestSetUp):
:return:
"""
self.client.force_authenticate(user=self.admin_user)
url = reverse('web_admin:article-list')
url = reverse(self.article_list_url)
response = self.client.post(url, self.article_data_with_base64_card_image, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
# Check that a new article was created
@ -60,13 +60,13 @@ class ArticleViewSetTestCase(ArticleTestSetUp):
:return:
"""
self.client.force_authenticate(user=self.admin_user)
url = reverse('web_admin:article-detail', kwargs={'pk': self.article.id})
url = reverse(self.article_detail_url, kwargs={'pk': self.article.id})
response = self.client.put(url, self.article_update_data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.article.refresh_from_db()
self.assertEqual(self.article.title, self.article_update_data['title'])
self.assertEqual(self.article.article_cards.count(), 1)
self.assertEqual(self.article.article_survey.count(), 5)
self.assertEqual(self.article.article_survey.count(), 6)
self.assertEqual(self.article.article_survey.first().options.count(), 3)
def test_articles_list(self):
@ -74,7 +74,7 @@ class ArticleViewSetTestCase(ArticleTestSetUp):
test articles list
:return:
"""
url = reverse('web_admin:article-list')
url = reverse(self.article_list_url)
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
# Assuming only one article exists in the database
@ -85,7 +85,7 @@ class ArticleViewSetTestCase(ArticleTestSetUp):
test article retrieve
:return:
"""
url = reverse('web_admin:article-detail', kwargs={'pk': self.article.id})
url = reverse(self.article_detail_url, kwargs={'pk': self.article.id})
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
@ -94,7 +94,7 @@ class ArticleViewSetTestCase(ArticleTestSetUp):
test article delete
:return:
"""
url = reverse('web_admin:article-detail', kwargs={'pk': self.article.id})
url = reverse(self.article_detail_url, kwargs={'pk': self.article.id})
response = self.client.delete(url)
self.article.refresh_from_db()
self.assertEqual(response.status_code, status.HTTP_200_OK)
@ -105,7 +105,7 @@ class ArticleViewSetTestCase(ArticleTestSetUp):
test article create with invalid data
:return:
"""
url = reverse('web_admin:article-list')
url = reverse(self.article_list_url)
# Missing article_cards
invalid_data = {
"title": "Invalid Article",

View File

@ -19,7 +19,7 @@ User = get_user_model()
class AdminOTPTestCase(BaseSetUp):
"""
test case to send otp to admin email
"""
def setUp(self):
@ -28,36 +28,35 @@ class AdminOTPTestCase(BaseSetUp):
: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:
"""
url = reverse('web_admin:admin-otp')
data = {
'email': 'admin@example.com'
'email': self.admin_email
}
response = self.client.post(url, data, format='json')
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:
"""
url = reverse('web_admin:admin-otp')
data = {
'email': 'notadmin@example.com'
}
response = self.client.post(url, data, format='json')
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):
@ -68,32 +67,94 @@ class AdminVerifyOTPTestCase(BaseSetUp):
super(AdminVerifyOTPTestCase, self).setUp()
self.verification_code = generate_otp()
expiry = timezone.now() + timezone.timedelta(days=1)
self.user_email_otp = UserEmailOtp.objects.create(email='admin@example.com',
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):
url = reverse('web_admin:admin-verify-otp')
"""
test admin verify otp with valid otp
:return:
"""
data = {
'email': 'admin@example.com',
'email': self.admin_email,
"otp": self.verification_code
}
response = self.client.post(url, data)
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):
url = reverse('web_admin:admin-verify-otp')
"""
test admin verify otp with invalid otp
:return:
"""
data = {
'email': 'admin@example.com',
'email': self.admin_email,
"otp": generate_otp()
}
response = self.client.post(url, data)
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)

View File

@ -105,23 +105,31 @@ base64_image = ("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCBIS
class BaseSetUp(APITestCase):
"""
basic setup
"""
def setUp(self) -> None:
"""
user data
:return:
"""
self.user_email = 'user@example.com'
self.admin_email = 'admin@example.com'
self.client = APIClient()
self.user = User.objects.create_user(username='user@example.com', password='user@1234')
self.admin_user = User.objects.create_user(username='admin@example.com', email='admin@example.com',
password='admin@1234', is_staff=True, is_superuser=True)
self.user = User.objects.create_user(username=self.user_email, email=self.user_email)
self.user.set_password('user@1234')
self.user.save()
self.admin_user = User.objects.create_user(username=self.admin_email, email=self.admin_email,
is_staff=True, is_superuser=True)
self.admin_user.set_password('admin@1234')
self.admin_user.save()
class ArticleTestSetUp(BaseSetUp):
"""
test cases data set up for article create, update
"""
def setUp(self):
"""
set up data for test
@ -131,63 +139,71 @@ class ArticleTestSetUp(BaseSetUp):
self.article = Article.objects.create(title="Existing Article", description="Existing Description",
is_published=True)
self.article_card = ArticleCard.objects.create(article=self.article, title="Card 1",
description="Card 1 Description")
self.article_card = ArticleCard.objects.create(article=self.article, title="Existing Card 1",
description="Existing Card 1 Description")
self.article_survey = ArticleSurvey.objects.create(article=self.article, points=5,
question="Survey Question 1")
SurveyOption.objects.create(survey=self.article_survey, option="Option 1", is_answer=True)
SurveyOption.objects.create(survey=self.article_survey, option="Option 2", is_answer=False)
question="Existing Survey Question 1")
SurveyOption.objects.create(survey=self.article_survey, option="Existing Option 1", is_answer=True)
SurveyOption.objects.create(survey=self.article_survey, option="Existing Option 2", is_answer=False)
self.article_list_url = 'web_admin:article-list'
self.article_detail_url = 'web_admin:article-detail'
# article card data with default card image
self.article_card_data_with_default_card_image = {
"title": "Card 1",
"description": "Card 1 Description",
"image_name": "card1.jpg",
"image_url": "https://example.com/card1.jpg"
}
# article card data with base64 image
self.article_card_data_with_base64_image = {
"title": "Card base64",
"description": "Card base64 Description",
"image_name": "base64_image.jpg",
"image_url": base64_image
}
# article survey option data
self.article_survey_option_data = [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
# article survey data
self.article_survey_data = [
{
"question": "Survey Question 1",
"options": self.article_survey_option_data
},
{
"question": "Survey Question 2",
"options": self.article_survey_option_data
},
{
"question": "Survey Question 3",
"options": self.article_survey_option_data
},
{
"question": "Survey Question 4",
"options": self.article_survey_option_data
},
{
"question": "Survey Question 5",
"options": self.article_survey_option_data
},
]
# article data with default card image
self.article_data_with_default_card_image = {
"title": "Test Article",
"description": "Test Description",
"article_cards": [
{
"title": "Card 1",
"description": "Card 1 Description",
"image_name": "card1.jpg",
"image_url": "https://example.com/updated_card1.jpg"
}
self.article_card_data_with_default_card_image
],
# minimum 5 article survey needed
"article_survey": [
{
"question": "Survey Question 1",
"options": [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
},
{
"question": "Survey Question 2",
"options": [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
},
{
"question": "Survey Question 3",
"options": [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
},
{
"question": "Survey Question 4",
"options": [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
},
{
"question": "Survey Question 5",
"options": [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
},
]
"article_survey": self.article_survey_data
}
# article data with base64 card image
@ -195,51 +211,10 @@ class ArticleTestSetUp(BaseSetUp):
"title": "Test Article",
"description": "Test Description",
"article_cards": [
{
"title": "Card 1",
"description": "Card 1 Description",
"image_name": "card1.jpg",
"image_url": base64_image
}
self.article_card_data_with_base64_image
],
# minimum 5 article survey needed
"article_survey": [
{
"question": "Survey Question 1",
"options": [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
},
{
"question": "Survey Question 2",
"options": [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
},
{
"question": "Survey Question 3",
"options": [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
},
{
"question": "Survey Question 4",
"options": [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
},
{
"question": "Survey Question 5",
"options": [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
}
]
"article_survey": self.article_survey_data
}
# article update data
@ -259,55 +234,16 @@ class ArticleTestSetUp(BaseSetUp):
],
# updated article survey
"article_survey": [
# updated article survey
{
"id": self.article_survey.id,
"question": "Updated Survey Question 1",
"options": [
{"id": self.article_survey.options.first().id,
"option": "Updated Option 1", "is_answer": False},
# New option
{"option": "Option 3", "is_answer": True}
]
},
# new article survey
{
"question": "Survey Question 2",
"options": [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
},
# new article survey
{
"question": "Survey Question 3",
"options": [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
},
# new article survey
{
"question": "Survey Question 4",
"options": [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
},
# new article survey
{
"question": "Survey Question 5",
"options": [
{"option": "Option 1", "is_answer": True},
{"option": "Option 2", "is_answer": False}
]
}
]
}
# article card data with default card image
self.article_card_data_with_default_card_image = {
"title": "Card 1",
"description": "Card 1 Description",
"image_name": "card1.jpg",
"image_url": "https://example.com/card2.jpg"
# updated article survey
{
"id": self.article_survey.id,
"question": "Updated Survey Question 1",
"options": [
{"id": self.article_survey.options.first().id,
"option": "Updated Option 1", "is_answer": False},
# New option
{"option": "New Option 3", "is_answer": True}
]
# added new articles
}] + self.article_survey_data
}