From 859d26d07364d9fbbd233f44601d14580894cac7 Mon Sep 17 00:00:00 2001 From: abutalib-kiwi Date: Fri, 8 Sep 2023 19:07:06 +0530 Subject: [PATCH 1/2] test cases for web admin auth --- celerybeat-schedule | Bin 20480 -> 20480 bytes web_admin/tests/test_auth.py | 99 +++++++++++++++++++++++++++++++++ web_admin/tests/test_set_up.py | 22 ++++++-- 3 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 web_admin/tests/test_auth.py diff --git a/celerybeat-schedule b/celerybeat-schedule index be892edea3778b38a0afe5c729adddc781b59706..7638e5252440a76df3a3df6b009f2805ccced487 100644 GIT binary patch delta 74 zcmZozz}T>Wal>2(b`B0XMov@S$qUQ_8QCWr*l13ccT5)mi+0sa$q;Ls5;Vn|ck(pH VVgU{=1_s9DYM9`H&5};fc>xRT7ApV% delta 74 zcmZozz}T>Wal>2(c2*ufMZVII$qUQ_8QCTq*l13ccT5)mi_SeaB}1%jO3)N None: + """ + + :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) + + +class ArticleTestSetUp(BaseSetUp): """ test cases data set up for article create, update """ @@ -112,10 +127,7 @@ class ArticleTestSetUp(APITestCase): 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) From bf1004696ae09d098908033703f85129793a1cba Mon Sep 17 00:00:00 2001 From: abutalib-kiwi Date: Mon, 11 Sep 2023 14:39:01 +0530 Subject: [PATCH 2/2] added test cases for auth --- .coverage | Bin 86016 -> 86016 bytes account/tests.py | 17 +- coverage-reports/coverage.xml | 1007 +++++++++++++++++-------------- web_admin/tests/test_article.py | 16 +- web_admin/tests/test_auth.py | 93 ++- web_admin/tests/test_set_up.py | 230 +++---- 6 files changed, 730 insertions(+), 633 deletions(-) diff --git a/.coverage b/.coverage index 54b2bef06e7247a33767c8d51e38ad691ba51e31..c5c9742d688286628453e0f8b84e1ae94c05c24b 100644 GIT binary patch delta 5795 zcmZ8ld3;n=vOabDu6^(APC__6)1vCAQhD&mc3^GMkuujZ9C zjqNclUEXNoH0=BqKe{*)=FtiX<|~VkbgbizcJs}T7n_&M$HTIhD)Ro!XUZWT5nfhEkPLI;Z z>3+Jd+q8Syx7w%LMeVHimUc{gN&8Rj32mRYL))yiYc8!^E7CHx6fI7R)Iv2sP1kJd zkLoS;Gxd^sPW`KTyk0%3{F-8 z#q;7DqAB(q>0KrSdKgZgQ$om|f#4oea$F6Sxk-itseH2Ayzo$1mX3Hb`OS?pDQyF> zJwzs{g!F*Fp&>toJTmU)J2SqDzWmdvuTLI3eL3ohW8gGYR0WZTZzSw@yuP)wPnwJ| z;FLnL%bdSGTv8BEBRkF3?LOWzVsElz+^A8u35JA12H9>tzCEN`v=k3j#T|F)hJeBt z^5D4XopAT9B&Wfn97MM5OGvSiDTIhl!)7sCZ}gvuB3cD;C`#oB zl<<4AQfC?j#d^|Xj@}U~H+Ku57-ijN-VUGcrThs$%nm|;kP~GdoiaAoYB1&;h44a za{g^!=XUT}gHqI*fs!I1+_rFXWfb8<$P}LrMt9u4+nKccllX7r@yqeogquO_NGG@d z_Tc1!!NE&+|FEg)=fSHF1tA)WXD9Am z(!-)eAT=Z-1b_K4t@uD(>y3BIIUE*ka3cw4*l-CI)h%oR=`H3PGbqv;J^dFbxT4q|C(eJRYLgc&ZvD1TZ*yI|<)}X6yc_o7@nb&HDs?k)c zLgZ1E(JxmrsuGn-1tRYX%Vjwu%QO0u)G|c=WjN&EHIP>@dIfW|oT23ml`>Sy&@zUW zF|?GSr3@|MpKOD%UG=+aJGyK4da&(Le>^Jf4FF!_5P5ohVE1Chnj?G29yi!cb{|!r_+h$} z+eRYrxI15Tm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#45=_ql&?ksc4j&~d$N09!JzD@VX?;K6q81--Xv0|M4 zPxgA{7vs}pMCYok z=CHK0)4atX(ytQ11$LHCwSs~Iu~v7pSFZ1@Lr1MJWL6SZfyAoiuo&u^D2Ns->wEOw zWN_jQGJaqQ)97c*F?g&f!@MnXuq@b^VXUao_ve6*z@g|^v-h-HWmiac9EAq3TC$8b z%>}!FZ>)HRNA4{dDI&RER;Vn6qt&Obn}*K=4{XDdvyeU;1G2bmD+-2S(M_|Wk;~m; ziH+WGxtuOm>F6I)(5B6-F0pH={-9D7ZDKpZw3^RubWRK!rWoT#5jI!`F}*BWa#(_m zx0Mp-LGfK@vDEA&Qjp_yM~Oq#|68A>y`wEuud1f-do@8=CcF3u|axI zd0%w$KkzTuPU34qg#5AGeWcfEqstdSySS&4Hx_Wr{Us9FU5nAqjUJv(dlrHp?4m~& zK?ZZPqz@ku*|`eyz>>-Ai7ltc7DMXD!TXDkCQ_7QP-J&%^c+3Lci7YoY$_${ff9Xf`WOvTv0RWhC(< J&6mrT|36O - + @@ -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 }