Skip to content

Commit

Permalink
test: add testcases for bootcamp slug formats
Browse files Browse the repository at this point in the history
  • Loading branch information
AfaqShuaib09 committed Sep 8, 2023
1 parent cbe00f8 commit 0f09daa
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
53 changes: 51 additions & 2 deletions course_discovery/apps/course_metadata/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,12 +1194,16 @@ class ValidateSlugFormatTest(TestCase):
def setUp(self):
self.product_source = SourceFactory(slug=settings.DEFAULT_PRODUCT_SOURCE_SLUG)
self.external_product_source = SourceFactory(slug=settings.EXTERNAL_PRODUCT_SOURCE_SLUG)
self.course_type = CourseTypeFactory(slug=CourseType.EXECUTIVE_EDUCATION_2U)
self.bootcamp_course_type = CourseTypeFactory(slug=CourseType.BOOTCAMP_2U)
self.exec_ed_course_type = CourseTypeFactory(slug=CourseType.EXECUTIVE_EDUCATION_2U)
self.test_course_1 = CourseFactory(title='test-title', product_source=self.product_source)
self.test_course_2 = CourseFactory(title='test-title-2')
self.test_course_3 = CourseFactory(title='test-title-3', product_source=self.product_source)
self.test_course_4 = CourseFactory(
title='test-title-4', product_source=self.external_product_source, type=self.course_type
title='test-title-4', product_source=self.external_product_source, type=self.exec_ed_course_type
)
self.bootcamp_course = CourseFactory(
title='bootcamp-course', product_source=self.external_product_source, type=self.bootcamp_course_type
)

CourseRunFactory(course=self.test_course_1, status=CourseRunStatus.Published)
Expand Down Expand Up @@ -1352,3 +1356,48 @@ def test_validate_slug_format__raise_exception_for_for_exec_ed_course(
expected_error_message = expected_error_message.format(url_slug=slug)
actual_error_message = str(context.exception)
self.assertIn(expected_error_message, actual_error_message)

@ddt.data(
('boot-camps/physics/edx-applied-physics', True),
('boot-camps/python/harvard-python-for-beginners', True),
('custom-slug', True),
('custom-slug', False),
)
@ddt.unpack
def test_validate_slug_format__for_bootcamps(self, slug, is_subdirectory_slug_format_active):
"""
Test that validate_slug_format to check if the slug is in correct format for bootcamps
"""
with override_waffle_switch(IS_SUBDIRECTORY_SLUG_FORMAT_ENABLED, active=is_subdirectory_slug_format_active):
assert validate_slug_format(slug, self.bootcamp_course) is None

@ddt.data(
('boot-camps/primary-subject/org-name-course-name', False),
('boot-camps/primary-subject/org-name-course-name/', True),
('boot-camps/primary-subject/org-name-course-name/', False),
('boot-camps/org-name-course-name', True),
('learn/test-course', True),
)
@ddt.unpack
def test_validate_slug_format__raise_exception_for_bootcamp_course(self, slug, is_subdirectory_slug_format_active):
"""
Test that validate_slug_format raises exception if the slug is not in the correct format
for bootcamp courses
"""
expected_error_message = None

if is_subdirectory_slug_format_active:
expected_error_message = (
settings.COURSE_URL_SLUGS_PATTERN[settings.EXTERNAL_PRODUCT_SOURCE_SLUG]
.get('bootcamp-2u').get('error_msg')
)
else:
expected_error_message = DEFAULT_SLUG_FORMAT_ERROR_MSG

with override_waffle_switch(IS_SUBDIRECTORY_SLUG_FORMAT_ENABLED, active=is_subdirectory_slug_format_active):
with self.assertRaises(ValidationError) as context:
validate_slug_format(slug, self.bootcamp_course)

expected_error_message = expected_error_message.format(url_slug=slug)
actual_error_message = str(context.exception)
self.assertIn(expected_error_message, actual_error_message)
8 changes: 8 additions & 0 deletions course_discovery/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,10 @@
{'default': {
'slug_format': '',
'error_msg': '',
},
'bootcamp-2u': {
'slug_format': '',
'error_msg': '',
}},
'ext-source':
{'default': {
Expand All @@ -719,6 +723,10 @@
'executive-education-2u': {
'slug_format': '',
'error_msg': '',
},
'bootcamp-2u': {
'slug_format': '',
'error_msg': '',
}}
}

Expand Down
11 changes: 10 additions & 1 deletion course_discovery/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,17 @@
SLUG_FORMAT_REGEX = '[a-zA-Z0-9-_]+$'
SUBDIRECTORY_SLUG_FORMAT_REGEX = 'learn/[a-zA-Z0-9-_]+/[a-zA-Z0-9-_]+$'
EXEC_ED_SLUG_FORMAT_REGEX = 'executive-education/[a-zA-Z0-9-_]+$'
BOOTCAMP_SLUG_FORMAT_REGEX = 'boot-camps/[a-zA-Z0-9-_]+/[a-zA-Z0-9-_]+$'

COURSE_URL_SLUGS_PATTERN = {
'test-source':
{'default': {
'slug_format': f'{SLUG_FORMAT_REGEX}|{SUBDIRECTORY_SLUG_FORMAT_REGEX}',
'error_msg': 'Course edit was unsuccessful. The course URL slug "[{url_slug}]" is an invalid format. Please ensure that the slug is in the format `learn/<primary_subject>/<organization_name>-<course_title>`',
},
'bootcamp-2u': {
'slug_format': f'{SLUG_FORMAT_REGEX}|{BOOTCAMP_SLUG_FORMAT_REGEX}',
'error_msg': 'Course edit was unsuccessful. The course URL slug "[{url_slug}]" is an invalid format. Please ensure that the slug is in the format `boot-camps/<primary_subject>/<organization_name>-<course_title>`',
}},
'external-test-source':
{'default': {
Expand All @@ -141,5 +146,9 @@
'executive-education-2u': {
'slug_format': f'{SLUG_FORMAT_REGEX}|{EXEC_ED_SLUG_FORMAT_REGEX}',
'error_msg': 'Course edit was unsuccessful. The course URL slug "[{url_slug}]" is an invalid format. Please ensure that the slug is in the format `executive-education/<organization_name>-<course_title>`',
}}
},
'bootcamp-2u': {
'slug_format': f'{SLUG_FORMAT_REGEX}|{BOOTCAMP_SLUG_FORMAT_REGEX}',
'error_msg': 'Course edit was unsuccessful. The course URL slug "[{url_slug}]" is an invalid format. Please ensure that the slug is in the format `boot-camps/<primary_subject>/<organization_name>-<course_title>`',
}},
}

0 comments on commit 0f09daa

Please sign in to comment.