From 8cfc33c0f1c99cdd5ebfc14d350294c6107cff87 Mon Sep 17 00:00:00 2001 From: Nimisha Asthagiri Date: Sun, 29 Sep 2019 18:27:52 -0400 Subject: [PATCH] Python 3: multi-line regex replacement of assertIn and assertNotIn --- cms/djangoapps/contentstore/tests/tests.py | 18 ++--- .../third_party_auth/tests/specs/base.py | 6 +- .../certificates/tests/test_webview_views.py | 1 - lms/djangoapps/courseware/tests/test_about.py | 20 ++--- .../courseware/tests/test_entrance_exam.py | 5 +- lms/djangoapps/courseware/tests/test_views.py | 6 +- .../dashboard/tests/test_sysadmin.py | 4 +- lms/djangoapps/instructor/tests/test_api.py | 21 +++--- .../instructor/tests/test_ecommerce.py | 4 +- .../tests/views/test_instructor_dashboard.py | 4 +- .../shoppingcart/tests/test_views.py | 74 ++++++++++--------- 11 files changed, 80 insertions(+), 83 deletions(-) diff --git a/cms/djangoapps/contentstore/tests/tests.py b/cms/djangoapps/contentstore/tests/tests.py index 71ded8ff60..d8a9f1b410 100644 --- a/cms/djangoapps/contentstore/tests/tests.py +++ b/cms/djangoapps/contentstore/tests/tests.py @@ -202,19 +202,19 @@ class AuthTestCase(ContentStoreTestCase): for i in range(3): resp = self._login(self.email, 'wrong_password{0}'.format(i)) - self.assertEqual(resp.status_code, 403) - self.assertIn( + self.assertContains( + resp, 'Email or password is incorrect.', - resp.content + status_code=403, ) # now the account should be locked resp = self._login(self.email, 'wrong_password') - self.assertEqual(resp.status_code, 403) - self.assertIn( + self.assertContains( + resp, 'This account has been temporarily locked due to excessive login failures.', - resp.content + status_code=403, ) with freeze_time('2100-01-01'): @@ -222,10 +222,10 @@ class AuthTestCase(ContentStoreTestCase): # make sure the failed attempt counter gets reset on successful login resp = self._login(self.email, 'wrong_password') - self.assertEqual(resp.status_code, 403) - self.assertIn( + self.assertContains( + resp, 'Email or password is incorrect.', - resp.content + status_code=403, ) # account should not be locked out after just one attempt diff --git a/common/djangoapps/third_party_auth/tests/specs/base.py b/common/djangoapps/third_party_auth/tests/specs/base.py index a83af01ac6..a1a54a52bf 100644 --- a/common/djangoapps/third_party_auth/tests/specs/base.py +++ b/common/djangoapps/third_party_auth/tests/specs/base.py @@ -121,10 +121,10 @@ class HelperMixin(object): def assert_json_failure_response_is_missing_social_auth(self, response): """Asserts failure on /login for missing social auth looks right.""" - self.assertEqual(403, response.status_code) - self.assertIn( + self.assertContains( + response, u"successfully signed in to your %s account, but this account isn't linked" % self.provider.name, - response.content.decode(response.charset) + status_code=403, ) def assert_json_failure_response_is_username_collision(self, response): diff --git a/lms/djangoapps/certificates/tests/test_webview_views.py b/lms/djangoapps/certificates/tests/test_webview_views.py index 6ee2732072..c00fc7ce40 100644 --- a/lms/djangoapps/certificates/tests/test_webview_views.py +++ b/lms/djangoapps/certificates/tests/test_webview_views.py @@ -318,7 +318,6 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) self._add_course_certificates(count=1, signatory_count=1, is_active=True) test_url = get_certificate_url(course_id=self.cert.course_id, uuid=self.cert.verify_uuid) response = self.client.get(test_url, HTTP_HOST='test.localhost') - self.assertEqual(response.status_code, 200) self.assertContains(response, "Post on Facebook") self.assertContains(response, 'test_facebook_my_site') diff --git a/lms/djangoapps/courseware/tests/test_about.py b/lms/djangoapps/courseware/tests/test_about.py index 4e44338f64..d7a6b03d42 100644 --- a/lms/djangoapps/courseware/tests/test_about.py +++ b/lms/djangoapps/courseware/tests/test_about.py @@ -362,7 +362,7 @@ class AboutWithInvitationOnly(SharedModuleStoreTestCase): self.assertContains(resp, "Enrollment in this course is by invitation only") # Check that registration button is not present - self.assertNotContains(response, REG_STR) + self.assertNotContains(resp, REG_STR) def test_invitation_only_but_allowed(self): """ @@ -412,14 +412,14 @@ class AboutWithClosedEnrollment(ModuleStoreTestCase): self.assertContains(resp, "Enrollment is Closed") # Check that registration button is not present - self.assertNotContains(response, REG_STR) + self.assertNotContains(resp, REG_STR) def test_course_price_is_not_visble_in_sidebar(self): url = reverse('about_course', args=[text_type(self.course.id)]) resp = self.client.get(url) # course price is not visible ihe course_about page when the course # mode is not set to honor - self.assertNotContains(response, '$10') + self.assertNotContains(resp, '$10') @ddt.ddt @@ -461,7 +461,7 @@ class AboutSidebarHTMLTestCase(SharedModuleStoreTestCase): self.assertContains(resp, '
') self.assertContains(resp, itemfactory_data) else: - self.assertNotContains(response, '
') + self.assertNotContains(resp, '
') @patch.dict(settings.FEATURES, {'ENABLE_SHOPPING_CART': True}) @@ -534,7 +534,7 @@ class AboutPurchaseCourseTestCase(LoginEnrollmentTestCase, SharedModuleStoreTest url = reverse('about_course', args=[text_type(self.course.id)]) resp = self.client.get(url) self.assertContains(resp, "This course is in your") - self.assertNotContains(response, "Add buyme to Cart ($10 USD)") + self.assertNotContains(resp, "Add buyme to Cart ($10 USD)") def test_already_enrolled(self): """ @@ -552,7 +552,7 @@ class AboutPurchaseCourseTestCase(LoginEnrollmentTestCase, SharedModuleStoreTest resp = self.client.get(url) self.assertContains(resp, "You are enrolled in this course") self.assertContains(resp, "View Course") - self.assertNotContains(response, "Add buyme to Cart ($10 USD)") + self.assertNotContains(resp, "Add buyme to Cart ($10 USD)") def test_closed_enrollment(self): """ @@ -564,7 +564,7 @@ class AboutPurchaseCourseTestCase(LoginEnrollmentTestCase, SharedModuleStoreTest url = reverse('about_course', args=[text_type(self.closed_course.id)]) resp = self.client.get(url) self.assertContains(resp, "Enrollment is Closed") - self.assertNotContains(response, "Add closed to Cart ($10 USD)") + self.assertNotContains(resp, "Add closed to Cart ($10 USD)") # course price is visible ihe course_about page when the course # mode is set to honor and it's price is set @@ -618,7 +618,7 @@ class AboutPurchaseCourseTestCase(LoginEnrollmentTestCase, SharedModuleStoreTest # Get the about page again and make sure that the page says that the course is full resp = self.client.get(url) self.assertContains(resp, "Course is full") - self.assertNotContains(response, "Add buyme to Cart ($10)") + self.assertNotContains(resp, "Add buyme to Cart ($10)") def test_free_course_display(self): """ @@ -630,8 +630,8 @@ class AboutPurchaseCourseTestCase(LoginEnrollmentTestCase, SharedModuleStoreTest url = reverse('about_course', args=[text_type(course.id)]) resp = self.client.get(url) - self.assertNotContains(response, "Add free to Cart (Free)") - self.assertNotContains(response, '

Price

') + self.assertNotContains(resp, "Add free to Cart (Free)") + self.assertNotContains(resp, '

Price

') class CourseAboutTestCaseCCX(SharedModuleStoreTestCase, LoginEnrollmentTestCase): diff --git a/lms/djangoapps/courseware/tests/test_entrance_exam.py b/lms/djangoapps/courseware/tests/test_entrance_exam.py index 538a3f6b9e..346074eb0c 100644 --- a/lms/djangoapps/courseware/tests/test_entrance_exam.py +++ b/lms/djangoapps/courseware/tests/test_entrance_exam.py @@ -327,10 +327,9 @@ class EntranceExamTestCases(LoginEnrollmentTestCase, ModuleStoreTestCase, Milest } ) resp = self.client.get(url) - self.assertEqual(resp.status_code, 200) - self.assertIn( + self.assertContains( + resp, u'To access course materials, you must score {}% or higher'.format(minimum_score_pct), - resp.content.decode(resp.charset) ) self.assertIn(u'Your current score is 20%.', resp.content.decode(resp.charset)) diff --git a/lms/djangoapps/courseware/tests/test_views.py b/lms/djangoapps/courseware/tests/test_views.py index 8144631c8a..b9f18d34a7 100644 --- a/lms/djangoapps/courseware/tests/test_views.py +++ b/lms/djangoapps/courseware/tests/test_views.py @@ -2270,12 +2270,12 @@ class GenerateUserCertTests(ModuleStoreTestCase): # If user try to access without login should see a bad request status code with message self.client.logout() resp = self.client.post(self.url) - self.assertEqual(resp.status_code, HttpResponseBadRequest.status_code) self.assertContains( resp, u"You must be signed in to {platform_name} to create a certificate.".format( - platform_name=settings.PLATFORM_NAME - ) + platform_name=settings.PLATFORM_NAME + ), + status_code=HttpResponseBadRequest.status_code, ) diff --git a/lms/djangoapps/dashboard/tests/test_sysadmin.py b/lms/djangoapps/dashboard/tests/test_sysadmin.py index 9a4a05d345..9b17e763c1 100644 --- a/lms/djangoapps/dashboard/tests/test_sysadmin.py +++ b/lms/djangoapps/dashboard/tests/test_sysadmin.py @@ -258,9 +258,9 @@ class TestSysAdminMongoCourseImport(SysadminBaseTestCase): response = self.client.get( reverse('gitlogs_detail', kwargs={ 'course_id': 'Not/Real/Testing'})) - self.assertIn( + self.assertContains( + response, 'No git import logs have been recorded for this course.', - response.content ) def test_gitlog_no_logs(self): diff --git a/lms/djangoapps/instructor/tests/test_api.py b/lms/djangoapps/instructor/tests/test_api.py index 6dd7a4361e..101e0fd226 100644 --- a/lms/djangoapps/instructor/tests/test_api.py +++ b/lms/djangoapps/instructor/tests/test_api.py @@ -293,30 +293,27 @@ class TestCommonExceptions400(TestCase): def test_user_doesnotexist(self): self.request.is_ajax.return_value = False resp = view_user_doesnotexist(self.request) # pylint: disable=assignment-from-no-return - self.assertEqual(resp.status_code, 400) - self.assertIn("User does not exist", resp.content.decode("utf-8")) + self.assertContains(resp, "User does not exist", status_code=400) def test_user_doesnotexist_ajax(self): self.request.is_ajax.return_value = True resp = view_user_doesnotexist(self.request) # pylint: disable=assignment-from-no-return - self.assertEqual(resp.status_code, 400) - self.assertIn("User does not exist", resp.content.decode("utf-8")) + self.assertContains(resp, "User does not exist", status_code=400) @ddt.data(True, False) def test_alreadyrunningerror(self, is_ajax): self.request.is_ajax.return_value = is_ajax resp = view_alreadyrunningerror(self.request) # pylint: disable=assignment-from-no-return - self.assertEqual(resp.status_code, 400) - self.assertIn("Requested task is already running", resp.content.decode("utf-8")) + self.assertContains(resp, "Requested task is already running", status_code=400) @ddt.data(True, False) def test_alreadyrunningerror_with_unicode(self, is_ajax): self.request.is_ajax.return_value = is_ajax resp = view_alreadyrunningerror_unicode(self.request) # pylint: disable=assignment-from-no-return - self.assertEqual(resp.status_code, 400) - self.assertIn( + self.assertContains( + resp, u'Text with unicode chárácters', - resp.content.decode('utf-8') + status_code=400, ) @ddt.data(True, False) @@ -326,10 +323,10 @@ class TestCommonExceptions400(TestCase): """ self.request.is_ajax.return_value = is_ajax resp = view_queue_connection_error(self.request) # pylint: disable=assignment-from-no-return - self.assertEqual(resp.status_code, 400) - self.assertIn( + self.assertContains( + resp, 'Error occured. Please try again later', - resp.content.decode('utf-8') + status_code=400, ) diff --git a/lms/djangoapps/instructor/tests/test_ecommerce.py b/lms/djangoapps/instructor/tests/test_ecommerce.py index 3318f8ce9a..715a435ed0 100644 --- a/lms/djangoapps/instructor/tests/test_ecommerce.py +++ b/lms/djangoapps/instructor/tests/test_ecommerce.py @@ -341,9 +341,9 @@ class TestECommerceDashboardViews(SiteMixin, SharedModuleStoreTestCase): # URL for update_coupon update_coupon_url = reverse('update_coupon', kwargs={'course_id': text_type(self.course.id)}) response = self.client.post(update_coupon_url, data=data) - self.assertIn( + self.assertContains( + response, u'coupon with the coupon id ({coupon_id}) updated Successfully'.format(coupon_id=coupon.id), - response.content ) response = self.client.post(self.url) diff --git a/lms/djangoapps/instructor/tests/views/test_instructor_dashboard.py b/lms/djangoapps/instructor/tests/views/test_instructor_dashboard.py index 882e7b6fb4..4c01c878cb 100644 --- a/lms/djangoapps/instructor/tests/views/test_instructor_dashboard.py +++ b/lms/djangoapps/instructor/tests/views/test_instructor_dashboard.py @@ -246,9 +246,9 @@ class TestInstructorDashboard(ModuleStoreTestCase, LoginEnrollmentTestCase, XssT numbers of learners. """ response = self.client.get(self.url) - self.assertIn( + self.assertContains( + response, self.GRADEBOOK_LEARNER_COUNT_MESSAGE, - response.content ) self.assertContains(response, 'View Gradebook') diff --git a/lms/djangoapps/shoppingcart/tests/test_views.py b/lms/djangoapps/shoppingcart/tests/test_views.py index ffd720db50..25327ea6d8 100644 --- a/lms/djangoapps/shoppingcart/tests/test_views.py +++ b/lms/djangoapps/shoppingcart/tests/test_views.py @@ -289,10 +289,10 @@ class ShoppingCartViewsTests(SharedModuleStoreTestCase, XssTestMixin): PaidCourseRegistration.add_to_order(self.cart, self.course_key) self.login_user() resp = self.client.post(reverse('add_course_to_cart', args=[text_type(self.course_key)])) - self.assertEqual(resp.status_code, 400) - self.assertIn( + self.assertContains( + resp, u'The course {0} is already in your cart.'.format(text_type(self.course_key)), - resp.content.decode('utf-8') + status_code=400, ) def test_course_discount_invalid_coupon(self): @@ -300,10 +300,10 @@ class ShoppingCartViewsTests(SharedModuleStoreTestCase, XssTestMixin): self.add_course_to_user_cart(self.course_key) non_existing_code = "non_existing_code" resp = self.client.post(reverse('shoppingcart.views.use_code'), {'code': non_existing_code}) - self.assertEqual(resp.status_code, 404) - self.assertIn( + self.assertContains( + resp, u"Discount does not exist against code '{0}'.".format(non_existing_code), - resp.content.decode('utf-8') + status_code=404, ) def test_valid_qty_greater_then_one_and_purchase_type_should_business(self): @@ -422,20 +422,20 @@ class ShoppingCartViewsTests(SharedModuleStoreTestCase, XssTestMixin): self.add_course_to_user_cart(self.course_key) non_existing_code = "non_existing_code" resp = self.client.post(reverse('shoppingcart.views.use_code'), {'code': non_existing_code}) - self.assertEqual(resp.status_code, 404) - self.assertIn( + self.assertContains( + resp, u"Discount does not exist against code '{0}'.".format(non_existing_code), - resp.content.decode('utf-8') + status_code=404, ) def test_course_discount_inactive_coupon(self): self.add_coupon(self.course_key, False, self.coupon_code) self.add_course_to_user_cart(self.course_key) resp = self.client.post(reverse('shoppingcart.views.use_code'), {'code': self.coupon_code}) - self.assertEqual(resp.status_code, 404) - self.assertIn( + self.assertContains( + resp, u"Discount does not exist against code '{0}'.".format(self.coupon_code), - resp.content.decode('utf-8') + status_code=404, ) def test_course_does_not_exist_in_cart_against_valid_coupon(self): @@ -444,10 +444,10 @@ class ShoppingCartViewsTests(SharedModuleStoreTestCase, XssTestMixin): self.add_course_to_user_cart(self.course_key) resp = self.client.post(reverse('shoppingcart.views.use_code'), {'code': self.coupon_code}) - self.assertEqual(resp.status_code, 404) - self.assertIn( + self.assertContains( + resp, u"Discount does not exist against code '{0}'.".format(self.coupon_code), - resp.content.decode('utf-8') + status_code=404, ) def test_inactive_registration_code_returns_error(self): @@ -462,10 +462,12 @@ class ShoppingCartViewsTests(SharedModuleStoreTestCase, XssTestMixin): # now apply the inactive registration code # it will raise an exception resp = self.client.post(reverse('shoppingcart.views.use_code'), {'code': self.reg_code}) - self.assertEqual(resp.status_code, 400) - self.assertIn( + self.assertContains( + resp, u"This enrollment code ({enrollment_code}) is no longer valid.".format( - enrollment_code=self.reg_code), resp.content.decode('utf-8')) + enrollment_code=self.reg_code), + status_code=400, + ) def test_course_does_not_exist_in_cart_against_valid_reg_code(self): course_key = text_type(self.course_key) + 'testing' @@ -473,10 +475,10 @@ class ShoppingCartViewsTests(SharedModuleStoreTestCase, XssTestMixin): self.add_course_to_user_cart(self.course_key) resp = self.client.post(reverse('shoppingcart.views.use_code'), {'code': self.reg_code}) - self.assertEqual(resp.status_code, 404) - self.assertIn( + self.assertContains( + resp, u"Code '{0}' is not valid for any course in the shopping cart.".format(self.reg_code), - resp.content.decode('utf-8') + status_code=404, ) def test_cart_item_qty_greater_than_1_against_valid_reg_code(self): @@ -488,10 +490,10 @@ class ShoppingCartViewsTests(SharedModuleStoreTestCase, XssTestMixin): # now update the cart item quantity and then apply the registration code # it will raise an exception resp = self.client.post(reverse('shoppingcart.views.use_code'), {'code': self.reg_code}) - self.assertEqual(resp.status_code, 404) - self.assertIn( + self.assertContains( + resp, "Cart item quantity should not be greater than 1 when applying activation code", - resp.content.decode('utf-8') + status_code=404, ) @ddt.data(True, False) @@ -535,10 +537,10 @@ class ShoppingCartViewsTests(SharedModuleStoreTestCase, XssTestMixin): # now using the same coupon code against the same order. # Only one coupon redemption should be allowed per order. resp = self.client.post(reverse('shoppingcart.views.use_code'), {'code': self.coupon_code}) - self.assertEqual(resp.status_code, 400) - self.assertIn( + self.assertContains( + resp, "Only one coupon redemption is allowed against an order", - resp.content.decode('utf-8') + status_code=400, ) def test_course_discount_against_two_distinct_coupon_codes(self): @@ -557,10 +559,10 @@ class ShoppingCartViewsTests(SharedModuleStoreTestCase, XssTestMixin): # Only one coupon redemption should be allowed per order. self.add_coupon(self.course_key, True, 'abxyz') resp = self.client.post(reverse('shoppingcart.views.use_code'), {'code': 'abxyz'}) - self.assertEqual(resp.status_code, 400) - self.assertIn( + self.assertContains( + resp, "Only one coupon redemption is allowed against an order", - resp.content.decode('utf-8') + status_code=400, ) def test_same_coupons_code_on_multiple_courses(self): @@ -1272,9 +1274,9 @@ class ShoppingCartViewsTests(SharedModuleStoreTestCase, XssTestMixin): self.assertContains(resp, 'FirstNameTesting123') self.assertContains(resp, '80.00') # check for the enrollment codes content - self.assertIn( + self.assertContains( + resp, 'Please send each professional one of these unique registration codes to enroll into the course.', - resp.content.decode('utf-8') ) # fetch the newly generated registration codes @@ -1291,9 +1293,9 @@ class ShoppingCartViewsTests(SharedModuleStoreTestCase, XssTestMixin): self.assertFalse(context['reg_code_info_list'][0]['is_redeemed']) self.assertFalse(context['reg_code_info_list'][1]['is_redeemed']) - self.assertIn( + self.assertContains( + resp, self.cart.purchase_time.strftime(u"%B %d, %Y"), - resp.content.decode('utf-8') ) self.assertContains(resp, self.cart.company_name) self.assertContains(resp, self.cart.company_contact_name) @@ -2158,9 +2160,9 @@ class CSVReportViewsTest(SharedModuleStoreTestCase): self.assertEqual(response['Content-Type'], 'text/csv') report = initialize_report(report_type, start_date, end_date) self.assertContains(response, ",".join(report.header())) - self.assertIn( + self.assertContains( + response, ",1,purchased,1,40.00,40.00,usd,Registration for Course: Robot Super Course,", - response.content ) def test_report_csv_university_revenue_share(self):