Skip to content

Commit

Permalink
temp: sanity confirm
Browse files Browse the repository at this point in the history
  • Loading branch information
zawan-ila committed Dec 3, 2024
1 parent 689e8b3 commit c32aa40
Showing 1 changed file with 176 additions and 176 deletions.
352 changes: 176 additions & 176 deletions course_discovery/apps/course_metadata/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,182 +223,182 @@ def test_new_program_without_courses(self):
assert response.status_code == 200


class ProgramAdminFunctionalTests(SiteMixin, LiveServerTestCase):
""" Functional Tests for Admin page."""
# Required for access to initial data loaded in migrations (e.g., LanguageTags).
serialized_rollback = True

create_view_name = 'admin:course_metadata_program_add'
edit_view_name = 'admin:course_metadata_program_change'

@classmethod
def setUpClass(cls):
super().setUpClass()
opts = Options()
opts.headless = True
cls.browser = webdriver.Firefox(options=opts)
cls.browser.set_window_size(1024, 768)

@classmethod
def tearDownClass(cls):
cls.browser.quit()
super().tearDownClass()

@classmethod
def _build_url(cls, path):
""" Returns a URL for the live test server. """
return cls.live_server_url + path

@classmethod
def _wait_for_page_load(cls, body_class):
""" Wait for the page to load. """
WebDriverWait(cls.browser, 2).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'body.' + body_class))
)

def setUp(self):
super().setUp()
# ContentTypeManager uses a cache to speed up ContentType retrieval. This
# cache persists across tests. This is fine in the context of a regular
# TestCase which uses a transaction to reset the database between tests.
# However, it becomes a problem in subclasses of TransactionTestCase which
# truncate all tables to reset the database between tests. When tables are
# truncated, ContentType objects in the ContentTypeManager's cache become
# stale. Attempting to use these stale objects in tests such as the ones
# below, which create LogEntry objects as a side-effect of interacting with
# the admin, will result in IntegrityErrors on databases that check foreign
# key constraints (e.g., MySQL). Preemptively clearing the cache prevents
# stale ContentType objects from being used.
ContentType.objects.clear_cache()

self.site.domain = self.live_server_url.strip('http://')
self.site.save()

self.course_runs = factories.CourseRunFactory.create_batch(2)
self.courses = [course_run.course for course_run in self.course_runs]

self.excluded_course_run = factories.CourseRunFactory(course=self.courses[0])
self.program = factories.ProgramFactory(
courses=self.courses, excluded_course_runs=[self.excluded_course_run], status=ProgramStatus.Unpublished,
product_source=None
)

self.user = UserFactory(is_staff=True, is_superuser=True)
self.product_source = factories.SourceFactory(name='Test Source')
self._login()

def _login(self):
""" Log into Django admin. """
self.browser.get(self._build_url(reverse('admin:login')))
self.browser.find_element(By.ID, 'id_username').send_keys(self.user.username)
self.browser.find_element(By.ID, 'id_password').send_keys(USER_PASSWORD)
self.browser.find_element(By.CSS_SELECTOR, 'input[type=submit]').click()
self._wait_for_page_load('dashboard')

def _wait_for_add_edit_page_to_load(self):
self._wait_for_page_load('change-form')

def _wait_for_excluded_course_runs_page_to_load(self):
self._wait_for_page_load('change-program-excluded-course-runs-form')

def _navigate_to_edit_page(self):
url = self._build_url(reverse(self.edit_view_name, args=(self.program.id,)))
self.browser.get(url)
self._wait_for_add_edit_page_to_load()

def _select_option(self, select_id, option_value):
select = Select(self.browser.find_element(By.ID, select_id))
select.select_by_value(option_value)

def _submit_program_form(self):
self.browser.find_element(By.CSS_SELECTOR, 'input[type=submit][name=_save]').click()
self._wait_for_excluded_course_runs_page_to_load()

def assert_form_fields_present(self):
""" Asserts the correct fields are rendered on the form. """
# Check the model fields
actual = []
for element in self.browser.find_elements(By.CLASS_NAME, 'form-row'):
actual += [_class for _class in element.get_attribute('class').split(' ') if _class.startswith('field-')]

expected = [
'field-uuid', 'field-title', 'field-subtitle',
'field-marketing_hook', 'field-product_source', 'field-type', 'field-status', 'field-partner',
'field-banner_image', 'field-banner_image_url', 'field-card_image', 'field-marketing_slug',
'field-overview', 'field-credit_redemption_overview', 'field-video', 'field-total_hours_of_effort',
'field-weeks_to_complete', 'field-min_hours_effort_per_week', 'field-max_hours_effort_per_week',
'field-courses', 'field-order_courses_by_start_date', 'field-custom_course_runs_display',
'field-excluded_course_runs', 'field-authoring_organizations', 'field-credit_backing_organizations',
'field-one_click_purchase_enabled', 'field-hidden', 'field-corporate_endorsements', 'field-faq',
'field-individual_endorsements', 'field-job_outlook_items', 'field-expected_learning_items',
'field-instructor_ordering', 'field-enrollment_count', 'field-recent_enrollment_count',
'field-credit_value', 'field-organization_short_code_override', 'field-organization_logo_override',
'field-primary_subject_override', 'field-level_type_override', 'field-language_override',
'field-enterprise_subscription_inclusion', 'field-in_year_value', 'field-labels', 'field-geolocation',
'field-program_duration_override', 'field-has_ofac_restrictions', 'field-ofac_comment',
'field-data_modified_timestamp', 'field-excluded_from_search', 'field-excluded_from_seo'
]
assert actual == expected

def test_program_creation(self):
url = self._build_url(reverse(self.create_view_name))
self.browser.get(url)
self._wait_for_add_edit_page_to_load()
self.assert_form_fields_present()

program = factories.ProgramFactory.build(
partner=Partner.objects.first(),
status=ProgramStatus.Unpublished,
type=ProgramType.objects.first(),
marketing_slug='foo',
product_source=Source.objects.first(),
)
self.browser.find_element(By.ID, 'id_title').send_keys(program.title)
self.browser.find_element(By.ID, 'id_subtitle').send_keys(program.subtitle)
self.browser.find_element(By.ID, 'id_marketing_slug').send_keys(program.marketing_slug)
self.browser.find_element(By.ID, 'id_program_duration_override').send_keys(program.program_duration_override)
self._select_option('id_status', program.status)
self._select_option('id_type', str(program.type.id))
self._select_option('id_partner', str(program.partner.id))
self._select_option('id_product_source', str(program.product_source.id))
self._submit_program_form()

actual = Program.objects.latest()
assert actual.title == program.title
assert actual.subtitle == program.subtitle
assert actual.marketing_slug == program.marketing_slug
assert actual.status == program.status
assert actual.type == program.type
assert actual.partner == program.partner
assert actual.program_duration_override == program.program_duration_override
assert actual.product_source == program.product_source

def test_program_update(self):
self._navigate_to_edit_page()
self.assert_form_fields_present()
title = 'Test Program'
subtitle = 'This is a test.'

assert self.program.product_source is None

# Update the program
data = (
('title', title),
('subtitle', subtitle),
)

for field, value in data:
element = self.browser.find_element(By.ID, 'id_' + field)
element.clear()
element.send_keys(value)
self._select_option('id_product_source', str(self.product_source.id))
self._submit_program_form()

# Verify the program was updated
self.program = Program.objects.get(pk=self.program.pk)
assert self.program.title == title
assert self.program.subtitle == subtitle
assert self.program.product_source == self.product_source
# class ProgramAdminFunctionalTests(SiteMixin, LiveServerTestCase):
# """ Functional Tests for Admin page."""
# # Required for access to initial data loaded in migrations (e.g., LanguageTags).
# serialized_rollback = True

# create_view_name = 'admin:course_metadata_program_add'
# edit_view_name = 'admin:course_metadata_program_change'

# @classmethod
# def setUpClass(cls):
# super().setUpClass()
# opts = Options()
# opts.headless = True
# cls.browser = webdriver.Firefox(options=opts)
# cls.browser.set_window_size(1024, 768)

# @classmethod
# def tearDownClass(cls):
# cls.browser.quit()
# super().tearDownClass()

# @classmethod
# def _build_url(cls, path):
# """ Returns a URL for the live test server. """
# return cls.live_server_url + path

# @classmethod
# def _wait_for_page_load(cls, body_class):
# """ Wait for the page to load. """
# WebDriverWait(cls.browser, 2).until(
# EC.presence_of_element_located((By.CSS_SELECTOR, 'body.' + body_class))
# )

# def setUp(self):
# super().setUp()
# # ContentTypeManager uses a cache to speed up ContentType retrieval. This
# # cache persists across tests. This is fine in the context of a regular
# # TestCase which uses a transaction to reset the database between tests.
# # However, it becomes a problem in subclasses of TransactionTestCase which
# # truncate all tables to reset the database between tests. When tables are
# # truncated, ContentType objects in the ContentTypeManager's cache become
# # stale. Attempting to use these stale objects in tests such as the ones
# # below, which create LogEntry objects as a side-effect of interacting with
# # the admin, will result in IntegrityErrors on databases that check foreign
# # key constraints (e.g., MySQL). Preemptively clearing the cache prevents
# # stale ContentType objects from being used.
# ContentType.objects.clear_cache()

# self.site.domain = self.live_server_url.strip('http://')
# self.site.save()

# self.course_runs = factories.CourseRunFactory.create_batch(2)
# self.courses = [course_run.course for course_run in self.course_runs]

# self.excluded_course_run = factories.CourseRunFactory(course=self.courses[0])
# self.program = factories.ProgramFactory(
# courses=self.courses, excluded_course_runs=[self.excluded_course_run], status=ProgramStatus.Unpublished,
# product_source=None
# )

# self.user = UserFactory(is_staff=True, is_superuser=True)
# self.product_source = factories.SourceFactory(name='Test Source')
# self._login()

# def _login(self):
# """ Log into Django admin. """
# self.browser.get(self._build_url(reverse('admin:login')))
# self.browser.find_element(By.ID, 'id_username').send_keys(self.user.username)
# self.browser.find_element(By.ID, 'id_password').send_keys(USER_PASSWORD)
# self.browser.find_element(By.CSS_SELECTOR, 'input[type=submit]').click()
# self._wait_for_page_load('dashboard')

# def _wait_for_add_edit_page_to_load(self):
# self._wait_for_page_load('change-form')

# def _wait_for_excluded_course_runs_page_to_load(self):
# self._wait_for_page_load('change-program-excluded-course-runs-form')

# def _navigate_to_edit_page(self):
# url = self._build_url(reverse(self.edit_view_name, args=(self.program.id,)))
# self.browser.get(url)
# self._wait_for_add_edit_page_to_load()

# def _select_option(self, select_id, option_value):
# select = Select(self.browser.find_element(By.ID, select_id))
# select.select_by_value(option_value)

# def _submit_program_form(self):
# self.browser.find_element(By.CSS_SELECTOR, 'input[type=submit][name=_save]').click()
# self._wait_for_excluded_course_runs_page_to_load()

# def assert_form_fields_present(self):
# """ Asserts the correct fields are rendered on the form. """
# # Check the model fields
# actual = []
# for element in self.browser.find_elements(By.CLASS_NAME, 'form-row'):
# actual += [_class for _class in element.get_attribute('class').split(' ') if _class.startswith('field-')]

# expected = [
# 'field-uuid', 'field-title', 'field-subtitle',
# 'field-marketing_hook', 'field-product_source', 'field-type', 'field-status', 'field-partner',
# 'field-banner_image', 'field-banner_image_url', 'field-card_image', 'field-marketing_slug',
# 'field-overview', 'field-credit_redemption_overview', 'field-video', 'field-total_hours_of_effort',
# 'field-weeks_to_complete', 'field-min_hours_effort_per_week', 'field-max_hours_effort_per_week',
# 'field-courses', 'field-order_courses_by_start_date', 'field-custom_course_runs_display',
# 'field-excluded_course_runs', 'field-authoring_organizations', 'field-credit_backing_organizations',
# 'field-one_click_purchase_enabled', 'field-hidden', 'field-corporate_endorsements', 'field-faq',
# 'field-individual_endorsements', 'field-job_outlook_items', 'field-expected_learning_items',
# 'field-instructor_ordering', 'field-enrollment_count', 'field-recent_enrollment_count',
# 'field-credit_value', 'field-organization_short_code_override', 'field-organization_logo_override',
# 'field-primary_subject_override', 'field-level_type_override', 'field-language_override',
# 'field-enterprise_subscription_inclusion', 'field-in_year_value', 'field-labels', 'field-geolocation',
# 'field-program_duration_override', 'field-has_ofac_restrictions', 'field-ofac_comment',
# 'field-data_modified_timestamp', 'field-excluded_from_search', 'field-excluded_from_seo'
# ]
# assert actual == expected

# def test_program_creation(self):
# url = self._build_url(reverse(self.create_view_name))
# self.browser.get(url)
# self._wait_for_add_edit_page_to_load()
# self.assert_form_fields_present()

# program = factories.ProgramFactory.build(
# partner=Partner.objects.first(),
# status=ProgramStatus.Unpublished,
# type=ProgramType.objects.first(),
# marketing_slug='foo',
# product_source=Source.objects.first(),
# )
# self.browser.find_element(By.ID, 'id_title').send_keys(program.title)
# self.browser.find_element(By.ID, 'id_subtitle').send_keys(program.subtitle)
# self.browser.find_element(By.ID, 'id_marketing_slug').send_keys(program.marketing_slug)
# self.browser.find_element(By.ID, 'id_program_duration_override').send_keys(program.program_duration_override)
# self._select_option('id_status', program.status)
# self._select_option('id_type', str(program.type.id))
# self._select_option('id_partner', str(program.partner.id))
# self._select_option('id_product_source', str(program.product_source.id))
# self._submit_program_form()

# actual = Program.objects.latest()
# assert actual.title == program.title
# assert actual.subtitle == program.subtitle
# assert actual.marketing_slug == program.marketing_slug
# assert actual.status == program.status
# assert actual.type == program.type
# assert actual.partner == program.partner
# assert actual.program_duration_override == program.program_duration_override
# assert actual.product_source == program.product_source

# def test_program_update(self):
# self._navigate_to_edit_page()
# self.assert_form_fields_present()
# title = 'Test Program'
# subtitle = 'This is a test.'

# assert self.program.product_source is None

# # Update the program
# data = (
# ('title', title),
# ('subtitle', subtitle),
# )

# for field, value in data:
# element = self.browser.find_element(By.ID, 'id_' + field)
# element.clear()
# element.send_keys(value)
# self._select_option('id_product_source', str(self.product_source.id))
# self._submit_program_form()

# # Verify the program was updated
# self.program = Program.objects.get(pk=self.program.pk)
# assert self.program.title == title
# assert self.program.subtitle == subtitle
# assert self.program.product_source == self.product_source


class ProgramEligibilityFilterTests(SiteMixin, TestCase):
Expand Down

0 comments on commit c32aa40

Please sign in to comment.