def test_social_auth_user_login_associated_with_one_enterprise(self, new_association, backend_name, multiple_enterprises_feature): """ Test that if socialAuth user has edx attached account and is part of one enterprises then redirection url is not changed """ kwargs = {'new_association': new_association} backend = self.get_mocked_sso_backend() backend.name = backend_name backend.strategy.session_get.return_value = 'not-an-enrollment-url' self.user = UserFactory(is_active=True) multiple_enterprises_feature.return_value = True enterprise_customer = EnterpriseCustomerFactory( enable_data_sharing_consent=False ) EnterpriseCustomerUser.objects.create( enterprise_customer=enterprise_customer, user_id=self.user.id, active=False ) with mock.patch('enterprise.tpa_pipeline.get_enterprise_customer_for_running_pipeline') as fake_get_ec: with mock.patch('enterprise.tpa_pipeline.select_enterprise_page_as_redirect_url') as ent_page_redirect: # pylint: disable=invalid-name fake_get_ec.return_value = None handle_enterprise_logistration(backend, self.user, **kwargs) ent_page_redirect.assert_not_called()
def test_handle_enterprise_logistration_user_multiple_enterprises_linking(self): """ Test that if user has multiple enterprise_customers then active status of latest enterprise_customer with which user is logged in will be marked as True and active status of other enterprise_customers will be marked as False. """ backend = self.get_mocked_sso_backend() self.user = UserFactory(is_active=True) with mock.patch('enterprise.tpa_pipeline.get_enterprise_customer_for_running_pipeline') as fake_get_ec: enterprise_customer = EnterpriseCustomerFactory( enable_data_sharing_consent=False ) enterprise_customer_old = EnterpriseCustomerFactory( enable_data_sharing_consent=False ) EnterpriseCustomerUser.objects.create( enterprise_customer=enterprise_customer_old, user_id=self.user.id, active=True ) fake_get_ec.return_value = enterprise_customer assert handle_enterprise_logistration(backend, self.user) is None assert EnterpriseCustomerUser.objects.filter( enterprise_customer=enterprise_customer, user_id=self.user.id, active=True ).count() == 1 assert EnterpriseCustomerUser.objects.filter( enterprise_customer=enterprise_customer_old, user_id=self.user.id, active=False ).count() == 1
def test_handle_enterprise_logistration_consent_not_required_for_existing_enterprise_user( self, configuration_helpers_mock, ): """ Test that when consent isn't required and learner is already linked, we simply return the existing EnterpriseCustomerUser. """ configuration_helpers_mock.get_value.return_value = 'edX' backend = self.get_mocked_sso_backend() with mock.patch( 'enterprise.tpa_pipeline.get_enterprise_customer_for_running_pipeline' ) as fake_get_ec: enterprise_customer = EnterpriseCustomerFactory( enable_data_sharing_consent=False) fake_get_ec.return_value = enterprise_customer # manually link the user with the enterprise EnterpriseCustomerUser.objects.create( enterprise_customer=enterprise_customer, user_id=self.user.id) assert handle_enterprise_logistration(backend, self.user) is None assert EnterpriseCustomerUser.objects.filter( enterprise_customer=enterprise_customer, user_id=self.user.id).count() == 1 assert UserDataSharingConsentAudit.objects.filter( user__user_id=self.user.id, user__enterprise_customer=enterprise_customer, ).count() == 0
def test_handle_enterprise_logistration_consent_not_required( self, user_is_active, configuration_helpers_mock, ): """ Test that when consent isn't required, we create an EnterpriseCustomerUser, then return. """ configuration_helpers_mock.get_value.return_value = 'edX' backend = self.get_mocked_sso_backend() self.user = UserFactory(is_active=user_is_active) with mock.patch( 'enterprise.tpa_pipeline.get_enterprise_customer_for_running_pipeline' ) as fake_get_ec: enterprise_customer = EnterpriseCustomerFactory( enable_data_sharing_consent=False) fake_get_ec.return_value = enterprise_customer assert handle_enterprise_logistration(backend, self.user) is None assert EnterpriseCustomerUser.objects.filter( enterprise_customer=enterprise_customer, user_id=self.user.id).count() == 1 assert UserDataSharingConsentAudit.objects.filter( user__user_id=self.user.id, user__enterprise_customer=enterprise_customer, ).count() == 0 # Now verify that request contains the expected messages when a # learner is linked with an enterprise self._assert_enterprise_linking_messages(backend.strategy.request, user_is_active)
def test_handle_enterprise_logistration_consent_required(self): """ Test that when consent is required, we redirect to the consent page. """ backend = mock.MagicMock(name=None) with mock.patch('enterprise.tpa_pipeline.get_ec_for_running_pipeline') as fake_get_ec: fake_get_ec.return_value = self.customer assert isinstance(handle_enterprise_logistration(backend, self.user), HttpResponseRedirect)
def test_handle_enterprise_logistration_consent_required_at_enrollment(self): """ Test that when consent is required at enrollment, but optional at logistration, we redirect to the consent page. """ backend = mock.MagicMock(name=None) with mock.patch('enterprise.tpa_pipeline.get_ec_for_running_pipeline') as fake_get_ec: self.customer.enforce_data_sharing_consent = EnterpriseCustomer.AT_ENROLLMENT fake_get_ec.return_value = self.customer assert isinstance(handle_enterprise_logistration(backend, self.user), HttpResponseRedirect)
def test_handle_enterprise_logistration_consent_optional(self): """ Test that when consent is optional, but requested, we redirect to the consent page. """ backend = mock.MagicMock(name=None) with mock.patch('enterprise.tpa_pipeline.get_ec_for_running_pipeline') as fake_get_ec: self.customer.enforce_data_sharing_consent = EnterpriseCustomer.DATA_CONSENT_OPTIONAL fake_get_ec.return_value = self.customer assert isinstance(handle_enterprise_logistration(backend, self.user), HttpResponseRedirect)
def test_handle_enterprise_logistration_no_pipeline(self): """ Test that when there's no pipeline, we do nothing, then return. """ backend = mock.MagicMock(name=None) with mock.patch('enterprise.tpa_pipeline.get_ec_for_running_pipeline') as fake_get_ec: fake_get_ec.return_value = None assert handle_enterprise_logistration(backend, self.user) is None assert EnterpriseCustomerUser.objects.count() == 0
def test_bypass_enterprise_selection_page_for_enrollment_url_login(self, using_enrollment_url, new_association, multiple_enterprise_switch, backend_name, multiple_enterprises_feature): """ Test that enterprise selection page is bypassed if socialAuth user is part of multiple enterprises and uses an enrollment url for login """ kwargs = {'new_association': new_association} backend = self.get_mocked_sso_backend() backend.name = backend_name if using_enrollment_url: backend.strategy.session_get.return_value = '/enterprise/12e87171-fb0a/course/course-v1:Test/enroll' else: backend.strategy.session_get.return_value = 'not-an-enrollment-url' self.user = UserFactory(is_active=True) multiple_enterprises_feature.return_value = multiple_enterprise_switch enterprise_customer = EnterpriseCustomerFactory( enable_data_sharing_consent=False ) enterprise_customer_old = EnterpriseCustomerFactory( enable_data_sharing_consent=False ) EnterpriseCustomerUser.objects.create( enterprise_customer=enterprise_customer_old, user_id=self.user.id, active=False ) EnterpriseCustomerUser.objects.create( enterprise_customer=enterprise_customer, user_id=self.user.id, active=True ) with mock.patch('enterprise.tpa_pipeline.get_enterprise_customer_for_running_pipeline') as fake_get_ec: with mock.patch( 'enterprise.tpa_pipeline.select_enterprise_page_as_redirect_url') as ent_page_redirect: # pylint: disable=invalid-name fake_get_ec.return_value = None handle_enterprise_logistration(backend, self.user, **kwargs) if new_association or not multiple_enterprise_switch or using_enrollment_url: ent_page_redirect.assert_not_called() else: ent_page_redirect.called_once()
def test_handle_enterprise_logistration_consent_not_required(self): """ Test that when consent isn't required, we create an EnterpriseCustomerUser, then return. """ backend = mock.MagicMock(name=None) with mock.patch('enterprise.tpa_pipeline.get_ec_for_running_pipeline' ) as fake_get_ec: enterprise_customer = EnterpriseCustomerFactory( enable_data_sharing_consent=False) fake_get_ec.return_value = enterprise_customer assert handle_enterprise_logistration(backend, self.user) is None assert EnterpriseCustomerUser.objects.filter( enterprise_customer=enterprise_customer, user_id=self.user.id).count() == 1
def test_handle_enterprise_logistration_consent_previously_declined(self): """ Test that when consent has been previously been declined, we redirect to the consent page. """ backend = mock.MagicMock(name=None) with mock.patch('enterprise.tpa_pipeline.get_ec_for_running_pipeline') as fake_get_ec: fake_get_ec.return_value = self.customer ec_user = EnterpriseCustomerUser.objects.create( user_id=self.user.id, # pylint: disable=no-member enterprise_customer=self.customer ) UserDataSharingConsentAudit.objects.create( # pylint: disable=no-member user=ec_user ) assert isinstance(handle_enterprise_logistration(backend, self.user), HttpResponseRedirect)
def test_handle_enterprise_logistration_not_user_linking(self): """ Test if there is not any enterprise customer then EnterpriseCustomerUser would not be associated with it. """ backend = self.get_mocked_sso_backend() self.user = UserFactory() with mock.patch( 'enterprise.tpa_pipeline.get_enterprise_customer_for_running_pipeline' ) as fake_get_ec: enterprise_customer = EnterpriseCustomerFactory( enable_data_sharing_consent=False) fake_get_ec.return_value = None assert handle_enterprise_logistration(backend, self.user) is None assert EnterpriseCustomerUser.objects.filter( enterprise_customer=enterprise_customer, user_id=self.user.id).count() == 0
def test_handle_enterprise_logistration_consent_provided(self): """ Test that when consent has been provided, we return and allow the pipeline to proceed. """ backend = mock.MagicMock(name=None) with mock.patch('enterprise.tpa_pipeline.get_ec_for_running_pipeline' ) as fake_get_ec: fake_get_ec.return_value = self.customer ec_user = EnterpriseCustomerUser.objects.create( user_id=self.user.id, # pylint: disable=no-member enterprise_customer=self.customer) UserDataSharingConsentAudit.objects.create( # pylint: disable=no-member user=ec_user, state='enabled', ) assert handle_enterprise_logistration(backend, self.user) is None
def test_handle_enterprise_logistration_user_linking( self, user_is_active, ): """ Test that we create an EnterpriseCustomerUser, then return. """ backend = self.get_mocked_sso_backend() self.user = UserFactory(is_active=user_is_active) with mock.patch( 'enterprise.tpa_pipeline.get_enterprise_customer_for_running_pipeline' ) as fake_get_ec: enterprise_customer = EnterpriseCustomerFactory( enable_data_sharing_consent=False) fake_get_ec.return_value = enterprise_customer assert handle_enterprise_logistration(backend, self.user) is None assert EnterpriseCustomerUser.objects.filter( enterprise_customer=enterprise_customer, user_id=self.user.id).count() == 1
def test_handle_enterprise_logistration_consent_not_required_for_existing_enterprise_user( self): """ Test that when consent isn't required and learner is already linked, we simply return the exi sting EnterpriseCustomerUser. """ backend = mock.MagicMock(name=None) with mock.patch('enterprise.tpa_pipeline.get_ec_for_running_pipeline' ) as fake_get_ec: enterprise_customer = EnterpriseCustomerFactory( enable_data_sharing_consent=False) fake_get_ec.return_value = enterprise_customer # manually link the user with the enterprise EnterpriseCustomerUser.objects.create( enterprise_customer=enterprise_customer, user_id=self.user.id) assert handle_enterprise_logistration(backend, self.user) is None assert EnterpriseCustomerUser.objects.filter( enterprise_customer=enterprise_customer, user_id=self.user.id).count() == 1
def test_handle_enterprise_logistration_consent_externally_managed( self, fake_get_ec, configuration_helpers_mock, ): """ Test that when consent is externally managed, we create an EnterpriseCustomerUser and UserDataSharingConsentAudit object, then return. """ configuration_helpers_mock.get_value.return_value = 'edX' backend = self.get_mocked_sso_backend() enterprise_customer = EnterpriseCustomerFactory( enable_data_sharing_consent=True, enforce_data_sharing_consent=EnterpriseCustomer.EXTERNALLY_MANAGED) fake_get_ec.return_value = enterprise_customer assert handle_enterprise_logistration(backend, self.user) is None assert EnterpriseCustomerUser.objects.filter( enterprise_customer=enterprise_customer, user_id=self.user.id).count() == 1 assert UserDataSharingConsentAudit.objects.filter( user__user_id=self.user.id, user__enterprise_customer=enterprise_customer, state=UserDataSharingConsentAudit.EXTERNALLY_MANAGED).count() == 1