Merge pull request #325 from KiwiTechLLC/dev

Dev
This commit is contained in:
Abu Talib
2023-09-11 17:54:18 +05:30
committed by GitHub
8 changed files with 832 additions and 627 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)

View File

@ -300,24 +300,21 @@ def make_special_password(length=10):
lowercase_letters = string.ascii_lowercase
uppercase_letters = string.ascii_uppercase
digits = string.digits
special_characters = '!@#$%^&*()_-+=<>?/[]{}|'
special_characters = '@#$%&*?'
# Combine character sets
all_characters = lowercase_letters + uppercase_letters + digits + special_characters
alphabets = lowercase_letters + uppercase_letters
# Create a password with random characters
password = (
secrets.choice(lowercase_letters) +
password = [
secrets.choice(uppercase_letters) +
secrets.choice(lowercase_letters) +
secrets.choice(digits) +
secrets.choice(special_characters) +
''.join(secrets.choice(all_characters) for _ in range(length - 4))
)
''.join(secrets.choice(alphabets) for _ in range(length - 4))
]
# Shuffle the characters to make it more random
password_list = list(password)
random.shuffle(password_list)
return ''.join(password_list)
return ''.join(password)
def task_status_fun(status_value):
"""task status"""

Binary file not shown.

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

@ -0,0 +1,160 @@
"""
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)

View File

@ -103,79 +103,107 @@ base64_image = ("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCBIS
"GYdEC/arYms/HtkfAS7huhuDXXJmPEkn5whN4xrZ0NkYdPRpIPdLS0kG5QPdCEskKlYcIWHCEJUO3KFuUIQD3QhCB//Z")
class ArticleTestSetUp(APITestCase):
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=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
:return:
"""
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)
super(ArticleTestSetUp, self).setUp()
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
@ -183,51 +211,10 @@ class ArticleTestSetUp(APITestCase):
"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
@ -247,55 +234,16 @@ class ArticleTestSetUp(APITestCase):
],
# 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
}