Python 3: multi-line regex replacement of assertIn and assertNotIn

This commit is contained in:
Nimisha Asthagiri
2019-09-29 18:27:52 -04:00
parent 4914c42364
commit 8cfc33c0f1
11 changed files with 80 additions and 83 deletions

View File

@@ -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

View File

@@ -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):

View File

@@ -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')

View File

@@ -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, '<span class="important-dates-item-text">$10</span>')
self.assertNotContains(resp, '<span class="important-dates-item-text">$10</span>')
@ddt.ddt
@@ -461,7 +461,7 @@ class AboutSidebarHTMLTestCase(SharedModuleStoreTestCase):
self.assertContains(resp, '<section class="about-sidebar-html">')
self.assertContains(resp, itemfactory_data)
else:
self.assertNotContains(response, '<section class="about-sidebar-html">')
self.assertNotContains(resp, '<section class="about-sidebar-html">')
@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 <span>($10 USD)</span>")
self.assertNotContains(resp, "Add buyme to Cart <span>($10 USD)</span>")
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 <span>($10 USD)</span>")
self.assertNotContains(resp, "Add buyme to Cart <span>($10 USD)</span>")
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 <span>($10 USD)</span>")
self.assertNotContains(resp, "Add closed to Cart <span>($10 USD)</span>")
# 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, '<p class="important-dates-item-title">Price</p>')
self.assertNotContains(resp, "Add free to Cart (Free)")
self.assertNotContains(resp, '<p class="important-dates-item-title">Price</p>')
class CourseAboutTestCaseCCX(SharedModuleStoreTestCase, LoginEnrollmentTestCase):

View File

@@ -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))

View File

@@ -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,
)

View File

@@ -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):

View File

@@ -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,
)

View File

@@ -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)

View File

@@ -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')

View File

@@ -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):