diff --git a/.coverage b/.coverage index 54b2bef..c5c9742 100644 Binary files a/.coverage and b/.coverage differ diff --git a/account/tests.py b/account/tests.py index a58f7a3..9b8af76 100644 --- a/account/tests.py +++ b/account/tests.py @@ -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) diff --git a/coverage-reports/coverage.xml b/coverage-reports/coverage.xml index dbf7607..d63768a 100644 --- a/coverage-reports/coverage.xml +++ b/coverage-reports/coverage.xml @@ -1,5 +1,5 @@ - + @@ -29,7 +29,7 @@ - + @@ -509,24 +509,27 @@ - - - + + + - - - - + - - + + - + + + + + + + @@ -566,7 +569,7 @@ - + @@ -673,11 +676,11 @@ - + - + - + @@ -1421,7 +1424,7 @@ - + @@ -1867,13 +1870,13 @@ - + - - + + @@ -2108,66 +2111,67 @@ - - - - + + + + + - - + + - - - - - - + + + + + + - - + + - - + + - - + + - - - - - + + + + + - - + + - - + + - + - - - - + + + + - - + + @@ -2179,43 +2183,42 @@ - - + + - + - - + + - - + + - + - + - + - - + + - - + + - - + + - @@ -2416,13 +2419,22 @@ + + + + + + + + + - + @@ -3006,6 +3018,7 @@ + @@ -3045,7 +3058,7 @@ - + @@ -3139,401 +3152,411 @@ - - - - + + + + + - + - - - - - - - - - - - - - - + + + + + + + - + - - - - - - - + - + - - + - - - - - - + + + + + + + + + + + + - + + + - - - - + + + + - - - - + + + + + + + + + + + - - - + + + - - - - - - - - - - - - - + + + + + + - - + + + - - - - - - - - - - - - - - + + + + + + + + + + + - + + + + + - - - - - - - - - - + + + + + + + + - + + - - - + + - - - - - - - - - + + + + + + + - + - - - - - - + + + + + + + - - - - - - - - - - + + + + + + + + + + + - - - + + + - - - - - - - - - - + + + + + + + + + + + - - + + + - - - + + + - - - - + + + - - - + + - - - - - + - - - - + + + + + + + + + + - + - - - - - - + + + + + - - + - - + + - - - + + + - - - - + + + + - + + + - - - - + + + - - - - + + + - - - - - + + + + + + - - - - - + + + + + - - - - + + - - - - - - - - - + + + + + + + + + + + + + - - - - + + + - - + + - - - - + - - - + + + - - - + + + + + - + - - - - - + + - - - - + + + + + + - + + + + + - - - - - - - - + + + + + + + + + + + + + + + + + + + @@ -3837,7 +3860,7 @@ - + @@ -3953,7 +3976,7 @@ - + @@ -4016,37 +4039,34 @@ - + - - - - - + + + + + - - - + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + @@ -4318,7 +4338,7 @@ - + @@ -4606,7 +4626,7 @@ - + @@ -4625,40 +4645,40 @@ - - - - + + + + - - + + - - - - - - - - + + + + + + + + - - - - - - + + + + + + - + - - - + + + @@ -4667,18 +4687,18 @@ - - - - + + + + - - - - + + + + - - + + @@ -4843,120 +4863,120 @@ - - - + - - - - - - - - - - - + - - - - - + - - - - + + - - + - + - - - - - - - - + + - - - - + - - + - - + - - + - - - + + - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -4964,27 +4984,108 @@ - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + - - - + + + + + + + + + + + + + + + + + + + + + + + - + @@ -5216,7 +5317,7 @@ - + @@ -5231,26 +5332,26 @@ - - - - - + + + + + - - - - + + + + - - - - - - - + + + + + + + diff --git a/web_admin/tests/test_article.py b/web_admin/tests/test_article.py index 728907c..f77b26b 100644 --- a/web_admin/tests/test_article.py +++ b/web_admin/tests/test_article.py @@ -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", diff --git a/web_admin/tests/test_auth.py b/web_admin/tests/test_auth.py index 6033af1..fa16ced 100644 --- a/web_admin/tests/test_auth.py +++ b/web_admin/tests/test_auth.py @@ -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) diff --git a/web_admin/tests/test_set_up.py b/web_admin/tests/test_set_up.py index f5cf1f6..5225969 100644 --- a/web_admin/tests/test_set_up.py +++ b/web_admin/tests/test_set_up.py @@ -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 }