def test_is_enterprise_learner(self): with mock.patch('django.core.cache.cache.set') as mock_cache_set: EnterpriseCustomerUserFactory.create(active=True, user_id=self.user.id) self.assertTrue(is_enterprise_learner(self.user)) self.assertTrue(mock_cache_set.called)
def _create_enterprise_course_enrollments(cls, count): """ Creates `count` test enrollments plus 1 invalid and 1 Audit enrollment """ for _ in range(count): user = UserFactory() course_enrollment = CourseEnrollmentFactory( mode=CourseMode.VERIFIED, user=user) course = course_enrollment.course enterprise_customer_user = EnterpriseCustomerUserFactory( user_id=user.id) EnterpriseCourseEnrollmentFactory( enterprise_customer_user=enterprise_customer_user, course_id=course.id) # creating audit enrollment user = UserFactory() course_enrollment = CourseEnrollmentFactory(mode=CourseMode.AUDIT, user=user) course = course_enrollment.course enterprise_customer_user = EnterpriseCustomerUserFactory( user_id=user.id) EnterpriseCourseEnrollmentFactory( enterprise_customer_user=enterprise_customer_user, course_id=course.id) # creating invalid enrollment (with no CourseEnrollment) user = UserFactory() enterprise_customer_user = EnterpriseCustomerUserFactory( user_id=user.id) EnterpriseCourseEnrollmentFactory( enterprise_customer_user=enterprise_customer_user, course_id=course.id)
def test_get_enterprise_learner_portal_no_branding_config(self): """ Test that only an enabled enterprise portal is returned, and that it matches the customer UUID provided in the request, even if no branding config is associated with the customer. """ enterprise_customer_user = EnterpriseCustomerUserFactory.create( active=True, user_id=self.user.id) enterprise_customer_user.enterprise_customer.enable_learner_portal = True enterprise_customer_user.enterprise_customer.save() request = mock.MagicMock(session={}, user=self.user) # Indicate the "preferred" customer in the request request.GET = { 'enterprise_customer': enterprise_customer_user.enterprise_customer.uuid } portal = get_enterprise_learner_portal(request) self.assertDictEqual( portal, { 'name': enterprise_customer_user.enterprise_customer.name, 'slug': enterprise_customer_user.enterprise_customer.slug, 'logo': enterprise_customer_user.enterprise_customer. safe_branding_configuration.safe_logo_url, })
def test_authenticated_account_activation_with_valid_next_url(self): """ Verify that an activation link with a valid next URL will redirect the activated enterprise user to that next URL, even if the AuthN MFE is active and redirects to it are enabled. """ self._assert_user_active_state(expected_active_state=False) EnterpriseCustomerUserFactory(user_id=self.user.id) # Make sure the user is authenticated before activation. self.login() redirect_url = 'http://localhost:1991/pied-piper/learn' base_activation_url = reverse('activate', args=[self.registration.activation_key]) activation_url = '{base}?{params}'.format( base=base_activation_url, params=urlencode({'next': redirect_url}), ) # HTTP_ACCEPT is needed so the safe redirect checks pass. response = self.client.get(activation_url, follow=True, HTTP_ACCEPT='*/*') # There's not actually a server running at localhost:1991 for testing, # so we should expect to land on `redirect_url` but with a status code of 404. self.assertRedirects(response, redirect_url, target_status_code=404) self._assert_user_active_state(expected_active_state=True)
def setUpClass(cls): super().setUpClass() cls.user = UserFactory() modulestore_course = ModuleStoreCourseFactory() course_run = CourseRunFactory(key=str(modulestore_course.id)) course = CourseFactory(course_runs=[course_run]) enterprise_customer = EnterpriseCustomerFactory(uuid=cls.enterprise_uuid) enterprise_customer_user = EnterpriseCustomerUserFactory( user_id=cls.user.id, enterprise_customer=enterprise_customer ) CourseEnrollmentFactory( is_active=True, course_id=modulestore_course.id, user=cls.user ) EnterpriseCourseEnrollmentFactory( course_id=modulestore_course.id, enterprise_customer_user=enterprise_customer_user ) cls.program = ProgramFactory( uuid=cls.program_uuid, courses=[course], title='Journey to cooking', type='MicroMasters', authoring_organizations=[{ 'key': 'MAX', 'logo_image_url': 'http://test.org/media/organization/logos/test-logo.png' }], ) cls.site = SiteFactory(domain='test.localhost')
def test_register_user(self, mock_update_user): """ make sure marketing enterprise user call invokes update_user """ enterprise_customer = EnterpriseCustomerFactory() EnterpriseCustomerUserFactory(user_id=self.user.id, enterprise_customer=enterprise_customer) self.assertTrue(mock_update_user.called)
def test_update_validation_error_for_enterprise(self, field_name, field_value): EnterpriseCustomerUserFactory(user_id=self.user.id) update_data = {field_name: field_value} with self.assertRaises(AccountValidationError) as validation_error: update_account_settings(self.user, update_data) field_errors = validation_error.exception.field_errors self.assertEqual("This field is not editable via this API", field_errors[field_name]["developer_message"])
def test_enterprise_in_url( self, expected_redirect, is_activated, reverse_mock, mock_activate_learner_enterprise, mock_api_client_class ): """ If user has multiple enterprises and the enterprise is present in url, activate that url """ api_response = {} enterprise_1 = EnterpriseCustomerUserFactory(user_id=self.user.id).enterprise_customer enterprise_2 = EnterpriseCustomerUserFactory(user_id=self.user.id).enterprise_customer api_response['results'] = [ { "enterprise_customer": { "uuid": enterprise_1.uuid, "name": enterprise_1.name, "active": enterprise_1.active, } }, { "enterprise_customer": { "uuid": enterprise_2.uuid, "name": enterprise_2.name, "active": enterprise_2.active, } } ] next_url = '/enterprise/{}/course/{}/enroll/?catalog=catalog_uuid&utm_medium=enterprise'.format( enterprise_1.uuid, 'course-v1:testX+test101+2T2020' ) mock_client = mock_api_client_class.return_value mock_client.fetch_enterprise_learner_data.return_value = api_response mock_activate_learner_enterprise.return_value = is_activated reverse_mock.return_value = '/enterprise/select/active' response, _ = self._login_response( self.user.email, self.password, extra_post_params={'next': next_url}, HTTP_ACCEPT='*/*', ) self._assert_response(response, success=True) self._assert_redirect_url(response, settings.LMS_ROOT_URL + expected_redirect + next_url)
def test_update_success_for_enterprise(self): EnterpriseCustomerUserFactory(user_id=self.user.id) level_of_education = "m" successful_update = { "level_of_education": level_of_education, } update_account_settings(self.user, successful_update) account_settings = get_account_settings(self.default_request)[0] self.assertEqual(level_of_education, account_settings["level_of_education"])
def test_activate_learner_enterprise(self): """ Test enterprise is activated successfully for user """ request_mock = mock.MagicMock(session={}, user=self.user) enterprise_customer_user = EnterpriseCustomerUserFactory(user_id=self.user.id) enterprise_customer_uuid = enterprise_customer_user.enterprise_customer.uuid activate_learner_enterprise(request_mock, self.user, enterprise_customer_uuid) assert request_mock.session['enterprise_customer']['uuid'] == str(enterprise_customer_uuid)
def _create_enterprise_enrollment(self, user_id, course_id): """ Create enterprise user and enrollment """ enterprise_customer_user = EnterpriseCustomerUserFactory( user_id=user_id, enterprise_customer=self.enterprise_customer) EnterpriseCourseEnrollmentFactory( course_id=course_id, enterprise_customer_user=enterprise_customer_user, )
def test_is_enterprise_customer_user(self): """ Verify that if user is an enterprise learner. """ # Create users from factory user = UserFactory(username='******', email='*****@*****.**') other_user = UserFactory(username='******', email='*****@*****.**') customer_idp = EnterpriseCustomerIdentityProviderFactory.create( provider_id='the-provider', ) customer = customer_idp.enterprise_customer EnterpriseCustomerUserFactory.create( enterprise_customer=customer, user_id=user.id, ) assert is_enterprise_customer_user('the-provider', user) assert not is_enterprise_customer_user('the-provider', other_user)
def test_get_enterprise_event_context(self): course_enrollment = CourseEnrollmentFactory(user=self.user) course = course_enrollment.course enterprise_customer_user = EnterpriseCustomerUserFactory( user_id=self.user.id) EnterpriseCourseEnrollmentFactory( enterprise_customer_user=enterprise_customer_user, course_id=course.id) assert get_enterprise_event_context(course_id=course.id, user_id=self.user.id) == \ {'enterprise_uuid': str(enterprise_customer_user.enterprise_customer_id)}
def test_login_success_for_multiple_enterprises( self, expected_redirect, user_has_multiple_enterprises, reverse_mock, mock_api_client_class ): """ Test that if multiple enterprise feature is enabled, user is redirected to correct page """ api_response = {'results': []} enterprise = EnterpriseCustomerUserFactory(user_id=self.user.id).enterprise_customer api_response['results'].append( { "enterprise_customer": { "uuid": enterprise.uuid, "name": enterprise.name, "active": enterprise.active, } } ) if user_has_multiple_enterprises: enterprise = EnterpriseCustomerUserFactory(user_id=self.user.id).enterprise_customer api_response['results'].append( { "enterprise_customer": { "uuid": enterprise.uuid, "name": enterprise.name, "active": enterprise.active, } } ) mock_client = mock_api_client_class.return_value mock_client.fetch_enterprise_learner_data.return_value = api_response reverse_mock.return_value = '/enterprise/select/active' response, _ = self._login_response( self.user.email, self.password, HTTP_ACCEPT='*/*', ) self._assert_response(response, success=True) self._assert_redirect_url(response, settings.LMS_ROOT_URL + expected_redirect)
def test_get_enterprise_learner_portal_no_customer_from_request(self): """ Test that only one enabled enterprise portal is returned, even if enterprise_customer_uuid_from_request() returns None. """ # Create another enterprise customer association for the same user. # There should be no data returned for this customer's portal, # because another customer is later created with a more recent active/modified time. other_enterprise_customer_user = EnterpriseCustomerUserFactory(active=True, user_id=self.user.id) other_enterprise_customer_user.enable_learner_portal = True other_enterprise_customer_user.save() enterprise_customer_user = EnterpriseCustomerUserFactory(active=True, user_id=self.user.id) EnterpriseCustomerBrandingConfigurationFactory( enterprise_customer=enterprise_customer_user.enterprise_customer, ) enterprise_customer_user.enterprise_customer.enable_learner_portal = True enterprise_customer_user.enterprise_customer.save() request = mock.MagicMock(session={}, user=self.user) with mock.patch( 'openedx.features.enterprise_support.api.enterprise_customer_uuid_for_request', return_value=None, ): portal = get_enterprise_learner_portal(request) self.assertDictEqual(portal, { 'name': enterprise_customer_user.enterprise_customer.name, 'slug': enterprise_customer_user.enterprise_customer.slug, 'logo': enterprise_customer_user.enterprise_customer.safe_branding_configuration.safe_logo_url, })
def test_get_enterprise_learner_portal_uncached(self): """ Test that only an enabled enterprise portal is returned, and that it matches the customer UUID provided in the request. """ enterprise_customer_user = EnterpriseCustomerUserFactory(active=True, user_id=self.user.id) EnterpriseCustomerBrandingConfigurationFactory( enterprise_customer=enterprise_customer_user.enterprise_customer, ) enterprise_customer_user.enterprise_customer.enable_learner_portal = True enterprise_customer_user.enterprise_customer.save() request = mock.MagicMock(session={}, user=self.user) # Indicate the "preferred" customer in the request request.GET = {'enterprise_customer': enterprise_customer_user.enterprise_customer.uuid} # Create another enterprise customer association for the same user. # There should be no data returned for this customer's portal, # because we filter for only the enterprise customer uuid found in the request. other_enterprise_customer_user = EnterpriseCustomerUserFactory(active=True, user_id=self.user.id) other_enterprise_customer_user.enable_learner_portal = True other_enterprise_customer_user.save() portal = get_enterprise_learner_portal(request) self.assertDictEqual(portal, { 'name': enterprise_customer_user.enterprise_customer.name, 'slug': enterprise_customer_user.enterprise_customer.slug, 'logo': enterprise_customer_user.enterprise_customer.safe_branding_configuration.safe_logo_url, })
def test_register_user(self, mock_update_user): """ make sure marketing enterprise user call invokes update_user """ enterprise_customer = EnterpriseCustomerFactory() EnterpriseCustomerUserFactory(user_id=self.user.id, enterprise_customer=enterprise_customer) mock_update_user.assert_called_with(sailthru_vars={ 'is_enterprise_learner': True, 'enterprise_name': enterprise_customer.name, }, email=self.user.email)
def test_signal_update_dsc_cache_on_course_enrollment(self): """ make sure update_dsc_cache_on_course_enrollment signal clears cache when Enterprise Course Enrollment takes place """ self._create_dsc_cache(self.user.id, self.course_id) self.assertTrue(self._is_dsc_cache_found(self.user.id, self.course_id)) # Enrolling user to Course enterprise_customer_user = EnterpriseCustomerUserFactory( user_id=self.user.id) EnterpriseCourseEnrollmentFactory( course_id=self.course_id, enterprise_customer_user=enterprise_customer_user, ) self.assertFalse(self._is_dsc_cache_found(self.user.id, self.course_id))
def test_get_enterprise_learner_portals_uncached(self): """ Test that only enabled enterprise portals are returned """ enterprise_customer_user = EnterpriseCustomerUserFactory(active=True, user_id=self.user.id) EnterpriseCustomerBrandingConfigurationFactory( enterprise_customer=enterprise_customer_user.enterprise_customer, ) enterprise_customer_user.enterprise_customer.enable_learner_portal = True enterprise_customer_user.enterprise_customer.save() request = mock.MagicMock(session={}, user=self.user) portals = get_enterprise_learner_portals(request) self.assertEqual(len(portals), 1) self.assertDictEqual(portals[0], { 'name': enterprise_customer_user.enterprise_customer.name, 'slug': enterprise_customer_user.enterprise_customer.slug, 'logo': enterprise_customer_user.enterprise_customer.branding_configuration.logo.url, })
def test_unlink_enterprise_user_from_idp(self, mock_customer_from_request, mock_registry): customer_idp = EnterpriseCustomerIdentityProviderFactory.create( provider_id='the-provider', ) customer = customer_idp.enterprise_customer customer_user = EnterpriseCustomerUserFactory.create( # lint-amnesty, pylint: disable=unused-variable enterprise_customer=customer, user_id=self.user.id, ) mock_customer_from_request.return_value = { 'uuid': customer.uuid, } mock_registry.get_enabled_by_backend_name.return_value = [ mock.Mock(provider_id='the-provider') ] request = mock.Mock() unlink_enterprise_user_from_idp(request, self.user, idp_backend_name='the-backend-name') assert 0 == EnterpriseCustomerUser.objects.filter(user_id=self.user.id).count()
def test_get_consent_required_courses(self, mock_catalog_contains_course): mock_catalog_contains_course.return_value = True user = UserFactory() enterprise_customer_user = EnterpriseCustomerUserFactory( user_id=user.id) course_id = 'fake-course' data_sharing_consent = DataSharingConsent( course_id=course_id, enterprise_customer=enterprise_customer_user.enterprise_customer, username=user.username, granted=False) data_sharing_consent.save() consent_required = get_consent_required_courses(user, [course_id]) self.assertTrue(course_id in consent_required) # now grant consent and call our method again data_sharing_consent.granted = True data_sharing_consent.save() consent_required = get_consent_required_courses(user, [course_id]) self.assertFalse(course_id in consent_required)
def test_signal_update_dsc_cache_on_enterprise_customer_update(self): """ make sure update_dsc_cache_on_enterprise_customer_update signal clears data_sharing_consent_needed cache after enable_data_sharing_consent flag is changed. """ # Enrolling user to Course enterprise_customer = EnterpriseCustomerFactory() enterprise_customer_user = EnterpriseCustomerUserFactory( user_id=self.user.id, enterprise_customer=enterprise_customer) EnterpriseCourseEnrollmentFactory( course_id=self.course_id, enterprise_customer_user=enterprise_customer_user, ) self._create_dsc_cache(self.user.id, self.course_id) self.assertTrue(self._is_dsc_cache_found(self.user.id, self.course_id)) # updating enable_data_sharing_consent flag enterprise_customer.enable_data_sharing_consent = False enterprise_customer.save() self.assertFalse(self._is_dsc_cache_found(self.user.id, self.course_id))
def test_user_enterprise(self, mock_get_programs_by_type): mock_get_programs_by_type.return_value = [self.program] EnterpriseCustomerUserFactory.create(user_id=self.user.id) assert not show_user_demographics(user=self.user)
def test_get_enterprise_learner_data_from_db(self): enterprise_customer_user = EnterpriseCustomerUserFactory(user_id=self.user.id) # lint-amnesty, pylint: disable=unused-variable user_data = get_enterprise_learner_data_from_db(self.user)[0]['user'] assert user_data['username'] == self.user.username
def test_get_enterprise_learner_data_from_db(self): enterprise_customer_user = EnterpriseCustomerUserFactory(user_id=self.user.id) user_data = get_enterprise_learner_data_from_db(self.user)[0]['user'] assert user_data['username'] == self.user.username
def setUpTestData(cls): # lint-amnesty, pylint: disable=super-method-not-called enterprise_customer_user = EnterpriseCustomerUserFactory() enterprise_course_enrollment = EnterpriseCourseEnrollmentFactory( enterprise_customer_user=enterprise_customer_user) cls.enterprise_customer_user = enterprise_customer_user cls.enterprise_course_enrollment = enterprise_course_enrollment