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 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()
示例#3
0
    def test_view_post(
        self,
        create_enterprise_customer,
        create_enterprise_customer_idp,
        no_of_enterprise_customer_idp,
        enable_slug_login,
        enterprise_slug,
        expected_response,
        status_code,
        expected_logger_message,
    ):
        """
        Test that view HTTP POST works as expected.
        """
        enterprise_customer = None
        if create_enterprise_customer:
            enterprise_customer = EnterpriseCustomerFactory(
                slug=enterprise_slug, enable_slug_login=enable_slug_login)

        if enterprise_customer and create_enterprise_customer_idp:
            for _ in range(no_of_enterprise_customer_idp):
                EnterpriseCustomerIdentityProviderFactory(
                    enterprise_customer=enterprise_customer)

        with LogCapture(LOGGER_NAME) as log:
            self._assert_post_request(enterprise_slug, status_code,
                                      expected_response)
            if expected_logger_message:
                log.check_present((
                    LOGGER_NAME,
                    'ERROR',
                    expected_logger_message.format(enterprise_slug),
                ))
 def test_handle_consent_enrollment_without_course_mode(
         self,
         registry_mock,
         *args
 ):  # pylint: disable=unused-argument
     """
     Verify that user is redirected to LMS dashboard in case there is
     no parameter `course_mode` in the request querystring.
     """
     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,
     )
     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)
     self._login()
     handle_consent_enrollment_url = self._append_fresh_login_param(
         reverse(
             'enterprise_handle_consent_enrollment',
             args=[enterprise_customer.uuid, course_id],
         )
     )
     response = self.client.get(handle_consent_enrollment_url)
     redirect_url = LMS_DASHBOARD_URL
     self.assertRedirects(response, redirect_url, fetch_redirect_response=False)
    def test_get_program_enrollment_page_for_inactive_user(
            self, registry_mock, *args):  # pylint: disable=unused-argument
        """
        The user is redirected to the login screen to sign in with an enterprise-linked SSO when inactive.
        """
        enterprise_customer = EnterpriseCustomerFactory()
        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)
        program_enrollment_page_url = reverse(
            'enterprise_program_enrollment_page',
            args=[enterprise_customer.uuid, self.dummy_program_uuid],
        )

        response = self.client.get(program_enrollment_page_url)
        expected_base_url = (
            '/login?next=%2Fenterprise%2F{enterprise_customer_uuid}%2F'
            'program%2F{program_uuid}%2Fenroll%2F').format(
                enterprise_customer_uuid=enterprise_customer.uuid,
                program_uuid=self.dummy_program_uuid)
        expected_fragments = ('tpa_hint%3D{provider_id}'.format(
            provider_id=provider_id, ), 'new_enterprise_login%3Dyes')
        assert response.status_code == 302
        assert expected_base_url in response.url
        for fragment in expected_fragments:
            assert fragment in response.url
    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,
        )
示例#7
0
 def test_handle_consent_enrollment_404(self, registry_mock,
                                        enrollment_api_client_mock, *args):  # pylint: disable=unused-argument
     """
     Verify that user gets HTTP 404 response if there is no enterprise in
     database against the provided enterprise UUID or if enrollment API
     client is unable to get course modes for the provided course id.
     """
     course_id = self.demo_course_id
     enrollment_client = enrollment_api_client_mock.return_value
     enrollment_client.get_course_modes.return_value = {}
     enterprise_customer = EnterpriseCustomerFactory(
         name='Starfleet Academy',
         enable_data_sharing_consent=True,
         enforce_data_sharing_consent='at_enrollment',
         enable_audit_enrollment=True,
     )
     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)
     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'})))
     with mock.patch('enterprise.views.render') as mock_render:
         mock_render.return_value = HttpResponse(
         )  # Must return response to keep view happy
         self.client.get(handle_consent_enrollment_url)
         assert mock_render.call_args_list[0][1]['status'] == 404
 def setUp(self):
     ecidp = EnterpriseCustomerIdentityProviderFactory(provider_id='provider_slug')
     self.customer = ecidp.enterprise_customer
     self.user = UserFactory(is_active=True)
     self.request_factory = RequestFactory()
     self.request = self.request_factory.get('/')
     self.request.session = cache.SessionStore()
     super().setUp()
示例#9
0
    def test_identity_provider(self):
        """
        Test identity_provider property returns correct value without errors.
        """
        faker = FakerFactory.create()
        provider_id = faker.slug()
        customer = EnterpriseCustomerFactory()
        EnterpriseCustomerIdentityProviderFactory(provider_id=provider_id, enterprise_customer=customer)

        assert customer.identity_provider == provider_id  # pylint: disable=no-member
示例#10
0
    def test_provider_name(self, mock_method):
        """
        Test provider_name property returns correct value without errors..
        """
        faker = FakerFactory.create()
        provider_name = faker.name()
        mock_method.return_value.configure_mock(name=provider_name)
        ec_idp = EnterpriseCustomerIdentityProviderFactory()

        assert ec_idp.provider_name == provider_name  # pylint: disable=no-member
 def setUp(self):
     """
     Set up test environment.
     """
     super(TestEnterpriseDecorators, self).setUp()
     faker = FakerFactory.create()
     self.provider_id = faker.slug()  # pylint: disable=no-member
     self.uuid = faker.uuid4()  # pylint: disable=no-member
     self.customer = EnterpriseCustomerFactory(uuid=self.uuid)
     EnterpriseCustomerIdentityProviderFactory(provider_id=self.provider_id, enterprise_customer=self.customer)
     self.session_engine = import_module(settings.SESSION_ENGINE)
示例#12
0
 def setUp(self):
     """
     Set up test environment.
     """
     super(TestUtils, self).setUp()
     faker = FakerFactory.create()
     self.provider_id = faker.slug()
     self.uuid = faker.uuid4()
     self.customer = EnterpriseCustomerFactory(uuid=self.uuid)
     EnterpriseCustomerIdentityProviderFactory(
         provider_id=self.provider_id, enterprise_customer=self.customer)
示例#13
0
 def test_get_remote_id(self, provider_id, expected_value, called, mock_third_party_api):
     user = UserFactory(username="******")
     enterprise_customer_user = EnterpriseCustomerUserFactory(user_id=user.id)
     if provider_id:
         EnterpriseCustomerIdentityProviderFactory(provider_id=provider_id,
                                                   enterprise_customer=enterprise_customer_user.enterprise_customer)
     mock_third_party_api.return_value.get_remote_id.return_value = 'saml-user-id'
     actual_value = enterprise_customer_user.get_remote_id()
     assert actual_value == expected_value
     if called:
         mock_third_party_api.return_value.get_remote_id.assert_called_once_with(provider_id, "hi")
     else:
         assert mock_third_party_api.return_value.get_remote_id.call_count == 0
示例#14
0
    def test_string_conversion(self, method):
        """
        Test ``EnterpriseCustomerIdentityProvider`` conversion to string.
        """
        provider_id, enterprise_customer_name = "saml-test", "TestShib"
        enterprise_customer = EnterpriseCustomerFactory(name=enterprise_customer_name)
        ec_idp = EnterpriseCustomerIdentityProviderFactory(
            enterprise_customer=enterprise_customer,
            provider_id=provider_id,
        )

        expected_to_str = "<EnterpriseCustomerIdentityProvider {provider_id}>: {enterprise_name}".format(
            provider_id=provider_id,
            enterprise_name=enterprise_customer_name,
        )
        self.assertEqual(method(ec_idp), expected_to_str)
示例#15
0
    def test_view_post(self, create_enterprise_customer,
                       create_enterprise_customer_idp, enable_slug_login,
                       enterprise_slug, expected_response, status_code):
        """
        Test that view HTTP POST works as expected.
        """
        enterprise_customer = None
        if create_enterprise_customer:
            enterprise_customer = EnterpriseCustomerFactory(
                slug=enterprise_slug, enable_slug_login=enable_slug_login)

        if enterprise_customer and create_enterprise_customer_idp:
            EnterpriseCustomerIdentityProviderFactory(
                enterprise_customer=enterprise_customer)

        self._assert_post_request(enterprise_slug, status_code,
                                  expected_response)
示例#16
0
    def test_create_new_identity_provider_link(self, mock_idp_choices,
                                               mock_url, mock_saml_config,
                                               mock_method):
        """
        Test create new identity provider link in help text.
        """
        provider_id = FAKER.slug()  # pylint: disable=no-member
        name = FAKER.name()

        # pylint: disable=invalid-name
        enterprise_customer_identity_provider = EnterpriseCustomerIdentityProviderFactory(
            enterprise_customer=EnterpriseCustomerFactory(site=SiteFactory(
                domain="site.localhost.com")))
        mock_method.return_value = mock.Mock(pk=1,
                                             name=name,
                                             provider_id=provider_id)
        mock_saml_config._meta.app_label = 'test_app'
        mock_saml_config._meta.model_name = 'test_model'
        mock_url.return_value = '/test_saml_app/test_saml_model/add/'
        mock_idp_choices.return_value = self.idp_choices
        form = EnterpriseCustomerIdentityProviderAdminForm(
            {
                'provider_id': provider_id,
                'enterprise_customer': self.enterprise_customer
            },
            instance=enterprise_customer_identity_provider)
        assert '/test_saml_app/test_saml_model/add/?source=1' in form.fields[
            'provider_id'].help_text
        assert form.fields['provider_id'].choices == list(self.idp_choices)

        # Without provider id information.
        form = EnterpriseCustomerIdentityProviderAdminForm(
            {'enterprise_customer': self.enterprise_customer}, instance=None)
        assert 'Create a new identity provider' in form.fields[
            'provider_id'].help_text
        assert '/test_saml_app/test_saml_model/add/?source=1' not in form.fields[
            'provider_id'].help_text
        assert form.fields['provider_id'].choices == list(self.idp_choices)

        mock_method.return_value = None
        # Invalid provider id.
        form = EnterpriseCustomerIdentityProviderAdminForm(
            {'enterprise_customer': self.enterprise_customer},
            instance=enterprise_customer_identity_provider)
        assert 'Make sure you have added a valid provider_id' in form.fields[
            'provider_id'].help_text
示例#17
0
    def test_export_as_csv_actual_model(self):
        """
        Tests export_as_csv as it is used in EnterpriseCustomer admin.
        """
        fields = EnterpriseCustomerAdmin.EXPORT_AS_CSV_FIELDS
        collection = [
            EnterpriseCustomerFactory(),
            EnterpriseCustomerFactory(),
            EnterpriseCustomerFactory(),
        ]

        for item in collection:
            EnterpriseCustomerIdentityProviderFactory(enterprise_customer=item)

        expected_rows = [fields] + [[getattr(customer, field) for field in fields] for customer in collection]

        export_as_csv = export_as_csv_action("CSV Export", fields=fields)
        export_as_csv(self.model_admin_mock, mock.Mock(), collection)
        self._assert_correct_csv(self.output_stream, expected_rows)
 def test_handle_consent_enrollment_with_invalid_course_mode(
         self,
         registry_mock,
         enrollment_api_client_mock,
         get_ec_user_mock,
         *args
 ):  # pylint: disable=unused-argument
     """
     Verify that user is redirected to LMS dashboard in case the provided
     course mode does not exist.
     """
     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,
     )
     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
     mocked_enterprise_customer_user = get_ec_user_mock.return_value
     mocked_enterprise_customer_user.return_value = enterprise_customer_user
     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': 'some-invalid-course-mode'})
         )
     )
     response = self.client.get(handle_consent_enrollment_url)
     redirect_url = LMS_DASHBOARD_URL
     self.assertRedirects(response, redirect_url, fetch_redirect_response=False)
 def setUp(self):
     ecidp = EnterpriseCustomerIdentityProviderFactory(
         provider_id='provider_slug')
     self.customer = ecidp.enterprise_customer
     self.user = UserFactory(is_active=True)
     super(TestTpaPipeline, self).setUp()
 def setUpTestData(cls):
     super().setUpTestData()
     cls.client = Client()
     cls.enterprise_customer = EnterpriseCustomerFactory()
     cls.identity_provider = EnterpriseCustomerIdentityProviderFactory(enterprise_customer=cls.enterprise_customer)