def test_post_course_specific_consent_bad_api_response(
         self, reverse_mock, *args):  # pylint: disable=unused-argument
     self._login()
     course_id = 'course-v1:does+not+exist'
     data_sharing_consent = True
     enterprise_customer = EnterpriseCustomerFactory(
         name='Starfleet Academy',
         enable_data_sharing_consent=True,
         enforce_data_sharing_consent='at_enrollment',
     )
     ecu = EnterpriseCustomerUserFactory(
         user_id=self.user.id, enterprise_customer=enterprise_customer)
     EnterpriseCourseEnrollment.objects.create(enterprise_customer_user=ecu,
                                               course_id=course_id)
     dsc = DataSharingConsentFactory(
         username=self.user.username,
         course_id=course_id,
         enterprise_customer=enterprise_customer,
         granted=False,
     )
     reverse_mock.return_value = '/dashboard'
     resp = self.client.post(
         self.url,
         data={
             'course_id': course_id,
             'data_sharing_consent': data_sharing_consent,
             'redirect_url': '/successful_enrollment',
             'enterprise_customer_uuid':
             'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
         },
     )
     assert resp.status_code == 404
     dsc.refresh_from_db()
     assert dsc.granted is False
 def test_post_course_specific_consent_no_user(self, reverse_mock,
                                               course_api_client_mock,
                                               *args):  # pylint: disable=unused-argument
     course_id = 'course-v1:edX+DemoX+Demo_Course'
     enterprise_customer = EnterpriseCustomerFactory(
         name='Starfleet Academy',
         enable_data_sharing_consent=True,
         enforce_data_sharing_consent='at_enrollment',
     )
     ecu = EnterpriseCustomerUserFactory(
         user_id=self.user.id, enterprise_customer=enterprise_customer)
     EnterpriseCourseEnrollment.objects.create(enterprise_customer_user=ecu,
                                               course_id=course_id)
     DataSharingConsentFactory(
         username=self.user.username,
         course_id=course_id,
         enterprise_customer=enterprise_customer,
     )
     client = course_api_client_mock.return_value
     client.get_course_details.return_value = {
         'name': 'edX Demo Course',
     }
     reverse_mock.return_value = '/dashboard'
     resp = self.client.post(
         self.url,
         data={
             'course_id': course_id,
             'redirect_url': '/successful_enrollment',
         },
     )
     assert resp.status_code == 302
     self.assertRedirects(
         resp,
         '/accounts/login/?next=/enterprise/grant_data_sharing_permissions',
         fetch_redirect_response=False)
 def test_get_course_specific_consent_not_needed(
     self,
     course_api_client_mock,
     course_catalog_api_client_mock,
 ):  # pylint: disable=unused-argument
     self._login()
     course_id = 'course-v1:edX+DemoX+Demo_Course'
     course_catalog_api_client = course_catalog_api_client_mock.return_value
     course_catalog_api_client.is_course_in_catalog.return_value = False
     enterprise_customer = EnterpriseCustomerFactory(
         name='Starfleet Academy',
         enable_data_sharing_consent=True,
         enforce_data_sharing_consent='at_enrollment',
     )
     client = course_api_client_mock.return_value
     client.get_course_details.return_value = {
         'name': 'edX Demo Course',
     }
     ecu = EnterpriseCustomerUserFactory(
         user_id=self.user.id, enterprise_customer=enterprise_customer)
     EnterpriseCourseEnrollment.objects.create(
         enterprise_customer_user=ecu,
         course_id=course_id,
     )
     DataSharingConsentFactory(username=self.user.username,
                               course_id=course_id,
                               enterprise_customer=enterprise_customer,
                               granted=True)
     response = self.client.get(
         self.url +
         '?course_id=course-v1%3AedX%2BDemoX%2BDemo_Course&next=https%3A%2F%2Fgoogle.com',
     )
     assert response.status_code == 404
 def test_get_course_specific_consent_unauthenticated_user(
         self, course_api_client_mock, *args):  # pylint: disable=unused-argument
     course_id = 'course-v1:edX+DemoX+Demo_Course'
     enterprise_customer = EnterpriseCustomerFactory(
         name='Starfleet Academy',
         enable_data_sharing_consent=True,
         enforce_data_sharing_consent='at_enrollment',
     )
     client = course_api_client_mock.return_value
     client.get_course_details.return_value = {
         'name': 'edX Demo Course',
     }
     ecu = EnterpriseCustomerUserFactory(
         user_id=self.user.id, enterprise_customer=enterprise_customer)
     EnterpriseCourseEnrollment.objects.create(enterprise_customer_user=ecu,
                                               course_id=course_id)
     DataSharingConsentFactory(
         username=self.user.username,
         course_id=course_id,
         enterprise_customer=enterprise_customer,
     )
     response = self.client.get(
         self.url +
         '?course_id=course-v1%3AedX%2BDemoX%2BDemo_Course&next=https%3A%2F%2Fgoogle.com'
     )
     assert response.status_code == 302
     self.assertRedirects(response, (
         '/accounts/login/?next=/enterprise/grant_data_sharing_permissions%3Fcourse_id%3Dcourse-v1'
         '%253AedX%252BDemoX%252BDemo_Course%26next%3Dhttps%253A%252F%252Fgoogle.com'
     ),
                          fetch_redirect_response=False)
 def test_get_course_specific_consent_invalid_get_params(
         self, course_api_client_mock, *args):  # pylint: disable=unused-argument
     course_id = 'course-v1:edX+DemoX+Demo_Course'
     client = course_api_client_mock.return_value
     client.get_course_details.return_value = {
         'name': 'edX Demo Course',
     }
     self._login()
     enterprise_customer = EnterpriseCustomerFactory(
         name='Starfleet Academy',
         enable_data_sharing_consent=True,
         enforce_data_sharing_consent='at_enrollment',
     )
     ecu = EnterpriseCustomerUserFactory(
         user_id=self.user.id, enterprise_customer=enterprise_customer)
     EnterpriseCourseEnrollment.objects.create(enterprise_customer_user=ecu,
                                               course_id=course_id)
     DataSharingConsentFactory(
         username=self.user.username,
         course_id=course_id,
         enterprise_customer=enterprise_customer,
     )
     params = {
         'enterprise_customer_uuid': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
         'course_id': 'course-v1:edX+DemoX+Demo_Course',
         'next': 'https://google.com',
         'defer_creation': True,
     }
     response = self.client.get(self.url, data=params)
     assert response.status_code == 404
    def setUp(self):
        super().setUp()

        self.course_ids = [
            'course-v1:edX+DemoX+Demo_Course',
            'course-v1:edX+Python+1T2019',
            'course-v1:edX+React+2T2019',
        ]
        self.enterprise_customer = EnterpriseCustomerFactory(
            name='Starfleet Academy',
            enable_data_sharing_consent=True,
            enforce_data_sharing_consent='at_enrollment',
        )

        learners = []
        for __ in range(5):
            user = UserFactory.create(is_staff=False, is_active=True)
            learners.append(user)

        self.learners_data = []
        for learner in learners:
            course_id = random.choice(self.course_ids)
            self.learners_data.append({
                'ENTERPRISE_UUID':
                self.enterprise_customer.uuid,
                'EMAIL':
                learner.email,
                'USERNAME':
                learner.username,
                'USER_ID':
                learner.id,
                'COURSE_ID':
                course_id
            })

            enterprise_customer_user = EnterpriseCustomerUserFactory(
                user_id=learner.id,
                enterprise_customer=self.enterprise_customer)

            EnterpriseCourseEnrollmentFactory(
                enterprise_customer_user=enterprise_customer_user,
                course_id=course_id,
            )

        self.existing_dsc_record_count = 2
        # create consent records for some learners with `granted` set to `False`
        # this is needed to verify that command is working for existing DSC records
        for learner in self.learners_data[:self.existing_dsc_record_count]:
            DataSharingConsentFactory(
                username=learner['USERNAME'],
                course_id=learner['COURSE_ID'],
                enterprise_customer=self.enterprise_customer,
                granted=False)
示例#7
0
    def test_post_course_specific_consent(
            self,
            defer_creation,
            consent_provided,
            expected_redirect_url,
            reverse_mock,
            course_catalog_api_client_mock_1,
            course_catalog_api_client_mock_2,
            course_api_client_mock,
            *args
    ):  # pylint: disable=unused-argument,invalid-name
        self._login()
        course_id = 'course-v1:edX+DemoX+Demo_Course'
        enterprise_customer = EnterpriseCustomerFactory(
            name='Starfleet Academy',
            enable_data_sharing_consent=True,
            enforce_data_sharing_consent='at_enrollment',
        )
        ecu = EnterpriseCustomerUserFactory(
            user_id=self.user.id,
            enterprise_customer=enterprise_customer
        )
        EnterpriseCourseEnrollment.objects.create(
            enterprise_customer_user=ecu,
            course_id=course_id,
        )
        dsc = DataSharingConsentFactory(
            username=self.user.username,
            course_id=course_id,
            enterprise_customer=enterprise_customer,
            granted=consent_provided
        )
        course_catalog_api_client_mock_1.return_value.program_exists.return_value = True
        course_catalog_api_client_mock_2.return_value.is_course_in_catalog = True
        course_api_client_mock.return_value.get_course_details.return_value = {'name': 'edX Demo Course'}
        reverse_mock.return_value = '/dashboard'
        post_data = {
            'enterprise_customer_uuid': enterprise_customer.uuid,
            'course_id': course_id,
            'redirect_url': '/successful_enrollment',
            'failure_url': '/failure_url',
        }
        if defer_creation:
            post_data['defer_creation'] = True
        if consent_provided:
            post_data['data_sharing_consent'] = consent_provided

        resp = self.client.post(self.url, post_data)

        assert resp.url.endswith(expected_redirect_url)  # pylint: disable=no-member
        assert resp.status_code == 302
        if not defer_creation:
            assert dsc.granted is consent_provided
    def test_get_program_enrollment_page_consent_message(
            self,
            consent_granted,
            program_data_extender_mock,
            course_catalog_api_client_mock,
            embargo_api_mock,
            *args
    ):  # pylint: disable=unused-argument,invalid-name
        """
        The DSC-declined message is rendered if DSC is not given.
        """
        self._setup_embargo_api(embargo_api_mock)
        self._setup_program_data_extender(program_data_extender_mock)
        setup_course_catalog_api_client_mock(course_catalog_api_client_mock)
        enterprise_customer = EnterpriseCustomerFactory(name='Starfleet Academy')
        enterprise_customer_user = EnterpriseCustomerUserFactory(
            enterprise_customer=enterprise_customer,
            user_id=self.user.id
        )
        for dummy_course_id in self.demo_course_ids:
            DataSharingConsentFactory(
                course_id=dummy_course_id,
                granted=consent_granted,
                enterprise_customer=enterprise_customer,
                username=enterprise_customer_user.username,
            )
        program_enrollment_page_url = reverse(
            'enterprise_program_enrollment_page',
            args=[enterprise_customer.uuid, self.dummy_program_uuid],
        )

        self._login()
        response = self.client.get(program_enrollment_page_url)
        messages = self._get_messages_from_response_cookies(response)
        if consent_granted:
            assert not messages
        else:
            assert messages
            self._assert_request_message(
                messages[0],
                'warning',
                (
                    '<strong>We could not enroll you in <em>Program Title 1</em>.</strong> '
                    '<span>If you have questions or concerns about sharing your data, please '
                    'contact your learning manager at Starfleet Academy, or contact '
                    '<a href="{enterprise_support_link}" target="_blank">{platform_name} support</a>.</span>'
                ).format(
                    enterprise_support_link=settings.ENTERPRISE_SUPPORT_URL,
                    platform_name=settings.PLATFORM_NAME,
                )
            )
 def test_get_course_specific_consent_bad_api_response(self, *args):  # pylint: disable=unused-argument
     self._login()
     course_id = 'course-v1:edX+DemoX+Demo_Course'
     enterprise_customer = EnterpriseCustomerFactory(
         name='Starfleet Academy',
         enable_data_sharing_consent=True,
         enforce_data_sharing_consent='at_enrollment',
     )
     ecu = EnterpriseCustomerUserFactory(
         user_id=self.user.id, enterprise_customer=enterprise_customer)
     EnterpriseCourseEnrollment.objects.create(enterprise_customer_user=ecu,
                                               course_id=course_id)
     DataSharingConsentFactory(
         username=self.user.username,
         course_id=course_id,
         enterprise_customer=enterprise_customer,
     )
     response = self.client.get(
         self.url +
         '?course_id=course-v1%3AedX%2BDemoX%2BDemo_Course&next=https%3A%2F%2Fgoogle.com',
     )
     assert response.status_code == 404
    def test_post_course_specific_consent(
            self, defer_creation, consent_provided, expected_redirect_url,
            course_id, license_uuid, reverse_mock,
            course_catalog_api_client_mock, enterprise_catalog_api_client_mock,
            mock_enrollment_api_client, *args):  # pylint: disable=unused-argument,invalid-name
        self._login()
        enterprise_customer = EnterpriseCustomerFactory(
            name='Starfleet Academy',
            enable_data_sharing_consent=True,
            enforce_data_sharing_consent='at_enrollment',
        )
        content_filter = {
            'key': [
                course_id,
            ]
        }
        EnterpriseCustomerCatalogFactory(
            enterprise_customer=enterprise_customer,
            content_filter=content_filter)
        ecu = EnterpriseCustomerUserFactory(
            user_id=self.user.id, enterprise_customer=enterprise_customer)
        enterprise_enrollment = EnterpriseCourseEnrollment.objects.create(
            enterprise_customer_user=ecu,
            course_id=course_id,
        )
        dsc = DataSharingConsentFactory(
            username=self.user.username,
            course_id=course_id,
            enterprise_customer=enterprise_customer,
            granted=consent_provided)

        course_catalog_api_client_mock.return_value.program_exists.return_value = True
        course_catalog_api_client_mock.return_value.get_course_id.return_value = 'edX+DemoX'

        mock_enterprise_catalog_api_client = enterprise_catalog_api_client_mock.return_value
        mock_enterprise_catalog_api_client.enterprise_contains_content_items.return_value = True

        reverse_mock.return_value = '/dashboard'
        course_mode = 'verified'
        mock_enrollment_api_client.return_value.get_course_modes.return_value = [
            course_mode
        ]
        post_data = {
            'enterprise_customer_uuid': enterprise_customer.uuid,
            'course_id': course_id,
            'redirect_url': '/successful_enrollment',
            'failure_url': '/failure_url',
        }
        if defer_creation:
            post_data['defer_creation'] = True
        if consent_provided:
            post_data['data_sharing_consent'] = consent_provided
        if license_uuid:
            post_data['license_uuid'] = license_uuid

        resp = self.client.post(self.url, post_data)

        assert resp.url.endswith(expected_redirect_url)  # pylint: disable=no-member
        assert resp.status_code == 302
        if not defer_creation:
            dsc.refresh_from_db()
            assert dsc.granted is consent_provided

        # we'll only create an enrollment record if (1) creation is not deferred, (2) consent has not been provided,
        # (3) we provide a license_uuid, and (4) we provide a course _run_ id
        if not defer_creation and not consent_provided and license_uuid and course_id.endswith(
                'Demo_Course'):
            assert LicensedEnterpriseCourseEnrollment.objects.filter(
                enterprise_course_enrollment=enterprise_enrollment,
                license_uuid=license_uuid,
            ).exists() is True

            mock_enrollment_api_client.return_value.enroll_user_in_course.assert_called_once_with(
                self.user.username, course_id, course_mode)

        if not license_uuid:
            assert not mock_enrollment_api_client.return_value.enroll_user_in_course.called