def test_unlink_user_existing_user(self, email): other_email = "*****@*****.**" user1, user2 = UserFactory(email=email), UserFactory(email=other_email) enterprise_customer1, enterprise_customer2 = EnterpriseCustomerFactory( ), EnterpriseCustomerFactory() EnterpriseCustomerUserFactory(enterprise_customer=enterprise_customer1, user_id=user1.id) EnterpriseCustomerUserFactory(enterprise_customer=enterprise_customer1, user_id=user2.id) EnterpriseCustomerUserFactory(enterprise_customer=enterprise_customer2, user_id=user1.id) assert len(EnterpriseCustomerUser.objects.all()) == 3 query_method = EnterpriseCustomerUser.objects.filter EnterpriseCustomerUser.objects.unlink_user(enterprise_customer1, email) # removes what was asked assert len( query_method(enterprise_customer=enterprise_customer1, user_id=user1.id)) == 0 # keeps records of the same user with different EC (though it shouldn't be the case) assert len( query_method(enterprise_customer=enterprise_customer2, user_id=user1.id)) == 1 # keeps records of other users assert len(query_method(user_id=user2.id)) == 1
def test_get_enterprise_customer_for_user(self): """ Test `get_enterprise_customer_for_user` helper method. """ faker = FakerFactory.create() provider_id = faker.slug() user = UserFactory() ecu = EnterpriseCustomerUserFactory(user_id=user.id, ) EnterpriseCustomerIdentityProviderFactory( enterprise_customer=ecu.enterprise_customer, provider_id=provider_id, ) # Assert that correct enterprise customer is returned self.assertEqual( utils.get_enterprise_customer_for_user(auth_user=user), ecu.enterprise_customer, ) # Assert that None is returned if user is not associated with any enterprise customer self.assertEqual( utils.get_enterprise_customer_for_user(auth_user=UserFactory()), None, )
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 setUp(self): self.api_user = UserFactory(username='******') self.user = UserFactory() self.course_id = COURSE_ID self.enterprise_customer = EnterpriseCustomerFactory() self.identity_provider = FakerFactory.create().slug() EnterpriseCustomerIdentityProviderFactory( provider_id=self.identity_provider, enterprise_customer=self.enterprise_customer) self.enterprise_customer_user = EnterpriseCustomerUserFactory( user_id=self.user.id, enterprise_customer=self.enterprise_customer, ) self.enrollment = EnterpriseCourseEnrollmentFactory( enterprise_customer_user=self.enterprise_customer_user, course_id=self.course_id, consent_granted=True, ) self.integrated_channel = SAPSuccessFactorsEnterpriseCustomerConfiguration( enterprise_customer=self.enterprise_customer, sapsf_base_url='enterprise.successfactors.com', key='key', secret='secret', ) super(TestTransmitLearnerData, self).setUp()
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_improperly_configured_course_catalog( self, course_catalog_api_client_mock, *args): # pylint: disable=unused-argument,invalid-name course_id = 'course-v1:edX+DemoX+Demo_Course' course_catalog_api_client_mock.side_effect = ImproperlyConfigured( "There is no active CatalogIntegration.") 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) EnterpriseCourseEnrollment.objects.create(enterprise_customer_user=ecu, course_id=course_id) params = { 'course_id': course_id, 'enterprise_customer_uuid': str(enterprise_customer.uuid), 'next': 'https://google.com', 'failure_url': 'https://facebook.com', 'defer_creation': True, } with mock.patch('enterprise.views.render') as mock_render: mock_render.return_value = HttpResponse() self.client.get(self.url, data=params) assert mock_render.call_args_list[0][1]['status'] == 404
def test_get_course_specific_consent_not_needed( self, course_api_client_mock, render_mock, # pylint: disable=unused-argument mock_config, *args # pylint: disable=unused-argument ): self._login() course_id = 'course-v1:edX+DemoX+Demo_Course' mock_config.get_value.return_value = 'My Platform' 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, consent_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_handle_user_post_save_modified_user_already_linked(self): email = "*****@*****.**" user = UserFactory(id=1, email=email) enterprise_customer1, enterprise_customer2 = EnterpriseCustomerFactory( ), EnterpriseCustomerFactory() existing_link = EnterpriseCustomerUserFactory( enterprise_customer=enterprise_customer1, user_id=user.id) PendingEnterpriseCustomerUserFactory( enterprise_customer=enterprise_customer2, user_email=email) assert len(EnterpriseCustomerUser.objects.filter( user_id=user.id)) == 1, "Precondition check: links exists" assert len(PendingEnterpriseCustomerUser.objects.filter(user_email=email)) == 1, \ "Precondition check: pending link exists" parameters = {"instance": user, "created": False} handle_user_post_save(mock.Mock(), **parameters) link = EnterpriseCustomerUser.objects.get(user_id=user.id) # TODO: remove suppression when https://github.com/landscapeio/pylint-django/issues/78 is fixed assert link.id == existing_link.id, "Should keep existing link intact" # pylint: disable=no-member assert link.enterprise_customer == enterprise_customer1, "Should keep existing link intact" assert len(PendingEnterpriseCustomerUser.objects.all() ) == 0, "Should delete pending link"
def test_create_tableau_user_success(self, mock_create_tableau_user, has_pending_admin_user): """ Test that when a new `EnterpriseCustomerUser` record is created, a tableau user is also created, assuming a `PendingEnterpriseCustomerAdminUser` record exists. """ if has_pending_admin_user: PendingEnterpriseCustomerAdminUserFactory( user_email=self.admin_user.email, enterprise_customer=self.enterprise_customer, ) # verify that no EnterpriseCustomerUser exists. enterprise_customer_user = EnterpriseCustomerUser.objects.filter( user_id=self.admin_user.id, ) self.assertFalse(enterprise_customer_user.exists()) # create a new EnterpriseCustomerUser record. EnterpriseCustomerUserFactory( user_id=self.admin_user.id, enterprise_customer=self.enterprise_customer, ) if has_pending_admin_user: mock_create_tableau_user.assert_called_once() else: mock_create_tableau_user.assert_not_called()
def test_delete_enterprise_admin_role_assignment_success( self, mock_create_tableau_user, # pylint: disable=unused-argument ): """ Test that when `EnterpriseCustomerUser` record is deleted, the associated enterprise admin user role assignment is also deleted. """ # create new PendingEnterpriseCustomerAdminUser and EnterpriseCustomerUser records. PendingEnterpriseCustomerAdminUserFactory( user_email=self.admin_user.email, enterprise_customer=self.enterprise_customer, ) EnterpriseCustomerUserFactory( user_id=self.admin_user.id, enterprise_customer=self.enterprise_customer, ) # verify that a new admin role assignment is created. admin_role_assignments = SystemWideEnterpriseUserRoleAssignment.objects.filter( user=self.admin_user, role=self.enterprise_admin_role, ) self.assertTrue(admin_role_assignments.exists()) # delete EnterpriseCustomerUser record and verify that admin role assignment is deleted as well. EnterpriseCustomerUser.objects.filter(user_id=self.admin_user.id).delete() admin_role_assignments = SystemWideEnterpriseUserRoleAssignment.objects.filter( user=self.admin_user, role=self.enterprise_admin_role, ) self.assertFalse(admin_role_assignments.exists())
def test_handle_user_post_save_modified_user_already_linked(self): email = "*****@*****.**" user = UserFactory(id=1, email=email) enterprise_customer1, enterprise_customer2 = EnterpriseCustomerFactory(), EnterpriseCustomerFactory() EnterpriseCustomerUserFactory(enterprise_customer=enterprise_customer1, user_id=user.id) PendingEnterpriseCustomerUserFactory(enterprise_customer=enterprise_customer2, user_email=email) assert EnterpriseCustomerUser.objects.filter(user_id=user.id).count() == 1, "Precondition check: links exists" assert PendingEnterpriseCustomerUser.objects.filter(user_email=email).count() == 1, \ "Precondition check: pending link exists" parameters = {"instance": user, "created": False} handle_user_post_save(mock.Mock(), **parameters) assert EnterpriseCustomerUser.objects.filter(user_id=user.id).count() == 2, "Should return 2 existing links" link_1 = EnterpriseCustomerUser.objects.get( user_id=user.id, enterprise_customer=enterprise_customer1, ) link_2 = EnterpriseCustomerUser.objects.get( user_id=user.id, enterprise_customer=enterprise_customer2, ) assert link_1.enterprise_customer == enterprise_customer1 assert link_2.enterprise_customer == enterprise_customer2 assert PendingEnterpriseCustomerUser.objects.count() == 0, "Should delete pending link"
def test_handle_consent_enrollment_with_professional_course_mode( self, registry_mock, enrollment_api_client_mock, track_enrollment_mock, *args ): # pylint: disable=unused-argument """ Verify that user is redirected to course in case the provided course mode is audit track. """ course_id = self.demo_course_id enterprise_customer = EnterpriseCustomerFactory( name='Starfleet Academy', enable_data_sharing_consent=True, enforce_data_sharing_consent='at_enrollment', enable_audit_enrollment=True, ) enterprise_catalog = EnterpriseCustomerCatalogFactory(enterprise_customer=enterprise_customer) faker = FakerFactory.create() provider_id = faker.slug() # pylint: disable=no-member self._setup_registry_mock(registry_mock, provider_id) EnterpriseCustomerIdentityProviderFactory(provider_id=provider_id, enterprise_customer=enterprise_customer) enterprise_customer_user = EnterpriseCustomerUserFactory( user_id=self.user.id, enterprise_customer=enterprise_customer ) enrollment_client = enrollment_api_client_mock.return_value enrollment_client.get_course_modes.return_value = self.dummy_demo_course_modes self._login() handle_consent_enrollment_url = self._append_fresh_login_param( '{consent_enrollment_url}?{params}'.format( consent_enrollment_url=reverse( 'enterprise_handle_consent_enrollment', args=[enterprise_customer.uuid, course_id] ), params=urlencode({ 'course_mode': 'professional', 'catalog': enterprise_catalog.uuid }) ) ) response = self.client.get(handle_consent_enrollment_url) redirect_url = LMS_START_PREMIUM_COURSE_FLOW_URL.format(course_id=course_id) redirect_url += '?catalog={catalog_uuid}'.format( catalog_uuid=enterprise_catalog.uuid ) self.assertRedirects(response, redirect_url, fetch_redirect_response=False) self.assertTrue(EnterpriseCourseEnrollment.objects.filter( enterprise_customer_user__enterprise_customer=enterprise_customer, enterprise_customer_user__user_id=enterprise_customer_user.user_id, course_id=course_id ).exists()) track_enrollment_mock.assert_called_once_with( 'course-landing-page-enrollment', enterprise_customer_user.user_id, course_id, handle_consent_enrollment_url, )
def test_get_existing_links_only(self): assert self.client.login( username=self.user.username, password="******") # make sure we've logged in users = [ EnterpriseCustomerUserFactory( enterprise_customer=self.enterprise_customer), EnterpriseCustomerUserFactory( enterprise_customer=self.enterprise_customer), EnterpriseCustomerUserFactory( enterprise_customer=self.enterprise_customer), ] response = self.client.get(self.view_url) self._test_get_response(response, users, [])
def test_post_course_specific_consent_bad_api_response( self, reverse_mock, course_api_client_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) enrollment = EnterpriseCourseEnrollment.objects.create( enterprise_customer_user=ecu, course_id=course_id) client = course_api_client_mock.return_value client.get_course_details.side_effect = HttpClientError 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' }, ) assert resp.status_code == 404 enrollment.refresh_from_db() assert enrollment.consent_granted is None
def test_assign_enterprise_learner_role_success(self): """ Test that when a new `EnterpriseCustomerUser` record is created, `assign_enterprise_learner_role` assigns an enterprise learner role to it. """ enterprise_customer_user = EnterpriseCustomerUser.objects.filter( user_id=self.learner_user.id) self.assertFalse(enterprise_customer_user.exists()) # Verify that no learner role assignment exists. learner_role_assignment = SystemWideEnterpriseUserRoleAssignment.objects.filter( user=self.learner_user, role=self.enterprise_learner_role) self.assertFalse(learner_role_assignment.exists()) # Create a new EnterpriseCustomerUser record. EnterpriseCustomerUserFactory( user_id=self.learner_user.id, enterprise_customer=self.enterprise_customer, ) enterprise_customer_user = EnterpriseCustomerUser.objects.filter( user_id=self.learner_user.id) self.assertTrue(enterprise_customer_user.exists()) # Verify that now a new learner role assignment is created. learner_role_assignment = SystemWideEnterpriseUserRoleAssignment.objects.filter( user=self.learner_user, role=self.enterprise_learner_role) self.assertTrue(learner_role_assignment.exists())
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_assign_enterprise_learner_role_on_update(self): """ Test that `assign_enterprise_learner_role` does not do anything on `EnterpriseCustomerUser` update operation. """ # Create a new EnterpriseCustomerUser record. EnterpriseCustomerUserFactory( user_id=self.learner_user.id, enterprise_customer=self.enterprise_customer, ) learner_role_assignment = SystemWideEnterpriseUserRoleAssignment.objects.get( user=self.learner_user, role=self.enterprise_learner_role) modified_datetime_at_create = learner_role_assignment.modified # Update EnterpriseCustomerUser record. enterprise_customer_user = EnterpriseCustomerUser.objects.get( user_id=self.learner_user.id) enterprise_customer_user.active = False enterprise_customer_user.save() # Verify that learner_role_assignment is not modified again # i.e it is still the same time when the object was created. learner_role_assignment = SystemWideEnterpriseUserRoleAssignment.objects.get( user=self.learner_user, role=self.enterprise_learner_role) self.assertEqual(learner_role_assignment.modified, modified_datetime_at_create)
def test_get_course_specific_consent( self, enrollment_deferred, supply_customer_uuid, course_api_client_mock, render_mock, # pylint: disable=unused-argument mock_config, *args # pylint: disable=unused-argument ): course_id = 'course-v1:edX+DemoX+Demo_Course' mock_config.get_value.return_value = 'My Platform' 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 ) params = { 'course_id': 'course-v1:edX+DemoX+Demo_Course', 'next': 'https://google.com' } if enrollment_deferred: params['enrollment_deferred'] = True if supply_customer_uuid: params['enterprise_id'] = str(enterprise_customer.uuid) response = self.client.get(self.url, data=params) assert response.status_code == 200 for key, value in { "platform_name": "My Platform", "data_sharing_consent": "required", "messages": { "note": ( "Courses from Starfleet Academy require data sharing consent. If you do not agree to " "share your data, you will be redirected to your dashboard." ), "warning": ( "Are you sure? If you do not agree to share your data " "with Starfleet Academy, you cannot access edX Demo Course." ), }, "course_id": "course-v1:edX+DemoX+Demo_Course", "course_name": "edX Demo Course", "redirect_url": "https://google.com", "enterprise_customer_name": ecu.enterprise_customer.name, "course_specific": True, "enrollment_deferred": enrollment_deferred, }.items(): assert response.context[key] == value # pylint:disable=no-member
def test_delete_enterprise_learner_role_assignment_success(self): """ Test that when `EnterpriseCustomerUser` record is deleted, `delete_enterprise_learner_role_assignment` also deletes the enterprise learner role assignment associated with it. """ # Create a new EnterpriseCustomerUser record. EnterpriseCustomerUserFactory( user_id=self.learner_user.id, enterprise_customer=self.enterprise_customer, ) enterprise_customer_user = EnterpriseCustomerUser.objects.filter( user_id=self.learner_user.id) self.assertTrue(enterprise_customer_user.exists()) # Verify that now a new learner role assignment is created. learner_role_assignment = SystemWideEnterpriseUserRoleAssignment.objects.filter( user=self.learner_user, role=self.enterprise_learner_role) self.assertTrue(learner_role_assignment.exists()) # Delete EnterpriseCustomerUser record. enterprise_customer_user.delete() # Verify that enterprise_customer_user is deleted enterprise_customer_user = EnterpriseCustomerUser.objects.filter( user_id=self.learner_user.id) self.assertFalse(enterprise_customer_user.exists()) # Also verify that learner role assignment is deleted as well. learner_role_assignment = SystemWideEnterpriseUserRoleAssignment.objects.filter( user=self.learner_user, role=self.enterprise_learner_role) self.assertFalse(learner_role_assignment.exists())
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 ) 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 == 404
def test_delete_enterprise_learner_role_assignment_no_user_associated( self): """ Test that when if no user is associated with a deleted `EnterpriseCustomerUser` record, `delete_enterprise_learner_role_assignment` does nothing. """ # Create a new EnterpriseCustomerUser with no user associated. EnterpriseCustomerUserFactory( enterprise_customer=self.enterprise_customer, ) enterprise_customer_user = EnterpriseCustomerUser.objects.filter( enterprise_customer=self.enterprise_customer) self.assertTrue(enterprise_customer_user.exists()) # Verify that no new learner role assignment is created. learner_role_assignment = SystemWideEnterpriseUserRoleAssignment.objects.filter( user=self.learner_user, role=self.enterprise_learner_role) self.assertFalse(learner_role_assignment.exists()) # Delete EnterpriseCustomerUser record. enterprise_customer_user.delete() # Verify that enterprise_customer_user is deleted enterprise_customer_user = EnterpriseCustomerUser.objects.filter( enterprise_customer=self.enterprise_customer) self.assertFalse(enterprise_customer_user.exists()) # Also verify that no new learner role assignment is created hence won't be deleted. learner_role_assignment = SystemWideEnterpriseUserRoleAssignment.objects.filter( user=self.learner_user, role=self.enterprise_learner_role) self.assertFalse(learner_role_assignment.exists())
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_consent_necessary_for_course(self, consent_provided_state, ec_consent_enabled, ec_consent_enforcement, expected_result): user = UserFactory() enterprise_customer = EnterpriseCustomerFactory( enable_data_sharing_consent=ec_consent_enabled, enforce_data_sharing_consent=ec_consent_enforcement, ) enterprise_user = EnterpriseCustomerUserFactory( user_id=user.id, enterprise_customer=enterprise_customer) course_id = 'course-v1:edX+DemoX+Demo_Course' enrollment = EnterpriseCourseEnrollment.objects.create( enterprise_customer_user=enterprise_user, consent_granted=consent_provided_state, course_id=course_id) assert consent_necessary_for_course(user, course_id) is expected_result account_consent = UserDataSharingConsentAuditFactory( user=enterprise_user, state=UserDataSharingConsentAudit.ENABLED, ) assert consent_necessary_for_course(user, course_id) is False account_consent.delete() # pylint: disable=no-member enrollment.delete() assert consent_necessary_for_course(user, course_id) is 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): self.user = UserFactory.create(username='******', is_staff=True, is_active=True) self.user.set_password("QWERTY") self.user.save() self.client = Client() get_dsc = mock.patch('enterprise.views.get_data_sharing_consent') self.get_data_sharing_consent = get_dsc.start() self.addCleanup(get_dsc.stop) course_catalog_api_client = mock.patch( 'enterprise.api_client.discovery.CourseCatalogApiServiceClient') self.course_catalog_api_client = course_catalog_api_client.start() self.addCleanup(course_catalog_api_client.stop) self.enterprise_customer = EnterpriseCustomerFactory( name='Starfleet Academy', enable_data_sharing_consent=True, enforce_data_sharing_consent='at_enrollment', ) self.ecu = EnterpriseCustomerUserFactory( user_id=self.user.id, enterprise_customer=self.enterprise_customer) self.valid_get_params = { 'enterprise_customer_uuid': self.enterprise_customer.uuid, 'next': 'https://google.com/', 'failure_url': 'https://facebook.com/', 'program_uuid': 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb', } self.valid_post_params = { 'enterprise_customer_uuid': self.enterprise_customer.uuid, 'redirect_url': 'https://google.com/', 'failure_url': 'https://facebook.com/', 'program_uuid': 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'data_sharing_consent': 'true', } super(TestProgramDataSharingPermissions, self).setUp()
def test_clean_user_already_linked(self, form_entry, existing_username, existing_email): user = UserFactory(username=existing_username, email=existing_email) EnterpriseCustomerUserFactory(user_id=user.id) # pylint: disable=no-member form = self._make_bound_form(form_entry) assert form.is_valid() cleaned_data = form.clean() assert cleaned_data[ManageLearnersForm.Fields.EMAIL_OR_USERNAME] == existing_email
def test_get_course_specific_consent( self, enrollment_deferred, supply_customer_uuid, course_api_client_mock, render_mock, # pylint: disable=unused-argument mock_config, *args # pylint: disable=unused-argument ): course_id = 'course-v1:edX+DemoX+Demo_Course' mock_config.get_value.return_value = 'My Platform' 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) params = { 'course_id': 'course-v1:edX+DemoX+Demo_Course', 'next': 'https://google.com' } if enrollment_deferred: params['enrollment_deferred'] = True if supply_customer_uuid: params['enterprise_id'] = str(enterprise_customer.uuid) response = self.client.get(self.url, data=params) assert response.status_code == 200 for key, value in { "platform_name": "My Platform", "consent_request_prompt": ('To access this course and use your discount, you must first consent to share your ' 'learning achievements with Starfleet Academy.'), 'confirmation_alert_prompt': ('In order to start this course and use your discount, you must consent to share your ' 'course data with Starfleet Academy.'), "course_id": "course-v1:edX+DemoX+Demo_Course", "course_name": "edX Demo Course", "redirect_url": "https://google.com", "enterprise_customer_name": ecu.enterprise_customer.name, "course_specific": True, "enrollment_deferred": enrollment_deferred, }.items(): assert response.context[key] == value # pylint:disable=no-member
def setUp(self): self.username = '******' self.user = UserFactory(username=self.username) self.course_id = 'course-v1:edX+DemoX+DemoCourse' self.enterprise_customer_user = EnterpriseCustomerUserFactory(user_id=self.user.id) self.enrollment = EnterpriseCourseEnrollment.objects.create( enterprise_customer_user=self.enterprise_customer_user, course_id=self.course_id, ) super(TestEnterpriseCourseEnrollment, self).setUp()
def test_string_conversion(self, method): """ Test ``UserDataSharingConsentAudit`` conversion to string """ user = UserFactory(email='*****@*****.**') enterprise_customer = EnterpriseCustomerFactory(name='EvilCorp') ec_user = EnterpriseCustomerUserFactory(user_id=user.id, enterprise_customer=enterprise_customer) audit = UserDataSharingConsentAuditFactory(user=ec_user) expected_to_str = "<UserDataSharingConsentAudit for [email protected] and EvilCorp: not_set>" assert expected_to_str == method(audit)
def setUp(self): self.course_run_id = 'course-v1:edX+DemoX+Demo_Course' self.user = UserFactory.create(is_staff=True, is_active=True) self.enterprise_customer = EnterpriseCustomerFactory( name='Starfleet Academy', enable_data_sharing_consent=True, enforce_data_sharing_consent='at_enrollment', ) self.enterprise_customer_user = EnterpriseCustomerUserFactory( user_id=self.user.id, enterprise_customer=self.enterprise_customer) super(CreateEnterpriseCourseEnrollmentCommandTests, self).setUp()