<commit_before>"""Acceptance tests for LMS-hosted Programs pages"""
from __future__ import absolute_import

from common.test.acceptance.fixtures.catalog import CatalogFixture, CatalogIntegrationMixin
from common.test.acceptance.fixtures.course import CourseFixture
from common.test.acceptance.fixtures.programs import ProgramsConfigMixin
from common.test.acceptance.pages.common.auto_auth import AutoAuthPage
from common.test.acceptance.pages.lms.catalog import CacheProgramsPage
from common.test.acceptance.pages.lms.programs import ProgramDetailsPage, ProgramListingPage
from common.test.acceptance.tests.helpers import UniqueCourseTest
from openedx.core.djangoapps.catalog.tests.factories import (
    CourseFactory,
    CourseRunFactory,
    PathwayFactory,
    ProgramFactory,
    ProgramTypeFactory
)


class ProgramPageBase(ProgramsConfigMixin, CatalogIntegrationMixin, UniqueCourseTest):
    """Base class used for program listing page tests."""
    def setUp(self):
        super(ProgramPageBase, self).setUp()

        self.set_programs_api_configuration(is_enabled=True)

        self.programs = ProgramFactory.create_batch(3)
        self.pathways = PathwayFactory.create_batch(3)
        for pathway in self.pathways:
            self.programs += pathway['programs']

        # add some of the previously created programs to some pathways
        self.pathways[0]['programs'].extend([self.programs[0], self.programs[1]])
        self.pathways[1]['programs'].append(self.programs[0])

        self.username = None

    def auth(self, enroll=True):
        """Authenticate, enrolling the user in the configured course if requested."""
        CourseFixture(**self.course_info).install()

        course_id = self.course_id if enroll else None
        auth_page = AutoAuthPage(self.browser, course_id=course_id)
        auth_page.visit()

        self.username = auth_page.user_info['username']

    def create_program(self):
        """DRY helper for creating test program data."""
        course_run = CourseRunFactory(key=self.course_id)
        course = CourseFactory(course_runs=[course_run])

        program_type = ProgramTypeFactory()
        return ProgramFactory(courses=[course], type=program_type['name'])

    def stub_catalog_api(self, programs, pathways):
        """
        Stub the discovery service's program list and detail API endpoints, as well as
        the credit pathway list endpoint.
        """
        self.set_catalog_integration(is_enabled=True, service_username=self.username)
        CatalogFixture().install_programs(programs)

        program_types = [program['type'] for program in programs]
        CatalogFixture().install_program_types(program_types)

        CatalogFixture().install_pathways(pathways)

    def cache_programs(self):
        """
        Populate the LMS' cache of program data.
        """
        cache_programs_page = CacheProgramsPage(self.browser)
        cache_programs_page.visit()


class ProgramListingPageTest(ProgramPageBase):
    """Verify user-facing behavior of the program listing page."""
    shard = 21

    def setUp(self):
        super(ProgramListingPageTest, self).setUp()

        self.listing_page = ProgramListingPage(self.browser)

    def test_no_enrollments(self):
        """Verify that no cards appear when the user has no enrollments."""
        self.auth(enroll=False)
        self.stub_catalog_api(self.programs, self.pathways)
        self.cache_programs()

        self.listing_page.visit()

        self.assertTrue(self.listing_page.is_sidebar_present)
        self.assertFalse(self.listing_page.are_cards_present)

    def test_no_programs(self):
        """
        Verify that no cards appear when the user has enrollments
        but none are included in an active program.
        """
        self.auth()
        self.stub_catalog_api(self.programs, self.pathways)
        self.cache_programs()

        self.listing_page.visit()

        self.assertTrue(self.listing_page.is_sidebar_present)
        self.assertFalse(self.listing_page.are_cards_present)


class ProgramListingPageA11yTest(ProgramPageBase):
    """Test program listing page accessibility."""
    a11y = True

    def setUp(self):
        super(ProgramListingPageA11yTest, self).setUp()
<commit_msg>Remove this test because it is flaky.<commit_after>"""Acceptance tests for LMS-hosted Programs pages"""
from __future__ import absolute_import

from common.test.acceptance.fixtures.catalog import CatalogFixture, CatalogIntegrationMixin
from common.test.acceptance.fixtures.course import CourseFixture
from common.test.acceptance.fixtures.programs import ProgramsConfigMixin
from common.test.acceptance.pages.common.auto_auth import AutoAuthPage
from common.test.acceptance.pages.lms.catalog import CacheProgramsPage
from common.test.acceptance.pages.lms.programs import ProgramDetailsPage, ProgramListingPage
from common.test.acceptance.tests.helpers import UniqueCourseTest
from openedx.core.djangoapps.catalog.tests.factories import (
    CourseFactory,
    CourseRunFactory,
    PathwayFactory,
    ProgramFactory,
    ProgramTypeFactory
)


class ProgramPageBase(ProgramsConfigMixin, CatalogIntegrationMixin, UniqueCourseTest):
    """Base class used for program listing page tests."""
    def setUp(self):
        super(ProgramPageBase, self).setUp()

        self.set_programs_api_configuration(is_enabled=True)

        self.programs = ProgramFactory.create_batch(3)
        self.pathways = PathwayFactory.create_batch(3)
        for pathway in self.pathways:
            self.programs += pathway['programs']

        # add some of the previously created programs to some pathways
        self.pathways[0]['programs'].extend([self.programs[0], self.programs[1]])
        self.pathways[1]['programs'].append(self.programs[0])

        self.username = None

    def auth(self, enroll=True):
        """Authenticate, enrolling the user in the configured course if requested."""
        CourseFixture(**self.course_info).install()

        course_id = self.course_id if enroll else None
        auth_page = AutoAuthPage(self.browser, course_id=course_id)
        auth_page.visit()

        self.username = auth_page.user_info['username']

    def create_program(self):
        """DRY helper for creating test program data."""
        course_run = CourseRunFactory(key=self.course_id)
        course = CourseFactory(course_runs=[course_run])

        program_type = ProgramTypeFactory()
        return ProgramFactory(courses=[course], type=program_type['name'])

    def stub_catalog_api(self, programs, pathways):
        """
        Stub the discovery service's program list and detail API endpoints, as well as
        the credit pathway list endpoint.
        """
        self.set_catalog_integration(is_enabled=True, service_username=self.username)
        CatalogFixture().install_programs(programs)

        program_types = [program['type'] for program in programs]
        CatalogFixture().install_program_types(program_types)

        CatalogFixture().install_pathways(pathways)

    def cache_programs(self):
        """
        Populate the LMS' cache of program data.
        """
        cache_programs_page = CacheProgramsPage(self.browser)
        cache_programs_page.visit()


class ProgramListingPageTest(ProgramPageBase):
    """Verify user-facing behavior of the program listing page."""
    shard = 21

    def setUp(self):
        super(ProgramListingPageTest, self).setUp()

        self.listing_page = ProgramListingPage(self.browser)

    def test_no_programs(self):
        """
        Verify that no cards appear when the user has enrollments
        but none are included in an active program.
        """
        self.auth()
        self.stub_catalog_api(self.programs, self.pathways)
        self.cache_programs()

        self.listing_page.visit()

        self.assertTrue(self.listing_page.is_sidebar_present)
        self.assertFalse(self.listing_page.are_cards_present)


class ProgramListingPageA11yTest(ProgramPageBase):
    """Test program listing page accessibility."""
    a11y = True

    def setUp(self):
        super(ProgramListingPageA11yTest, self).setUp()

{'fraction_non_alphanumeric': 0.09028627355028138, 'fraction_numerical': 0.0036701737215561535, 'mean_word_length': 3.5747062115277, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6264728', 'n_tokens_mistral': 2345, 'n_tokens_neox': 2244, 'n_words': 500}