예제 #1
0
    def test_force_fresh_session_param_not_received(
            self, mock_get_identity_provider):
        """
        Test that the force_fresh_session decorator redirects authenticated
        users with the appropriate provider config depending on the IdPs configuration.
        """
        mock_get_identity_provider.return_value.configure_mock(
            provider_id=self.provider_id, )
        view_function = mock_view_function()
        course_id = 'course-v1:edX+DemoX+Demo_Course'
        url_path = reverse(
            'enterprise_course_run_enrollment_page',
            args=[self.customer.uuid, course_id],
        )
        query = 'foo=bar'
        # Adding query parameter here to verify
        # the redirect URL is getting escaped properly.
        url = '{path}?{query}'.format(path=url_path, query=query)
        request = self._prepare_request(url, UserFactory(is_active=True))

        response = force_fresh_session(view_function)(
            request, enterprise_uuid=self.customer.uuid, course_id=course_id)

        # Assert that redirect status code 302 is returned
        assert response.status_code == 302
        # Assert the redirect URL query string is intact.
        redirect_url_query = parse_qs(urlparse(response.url).query)
        assert urlparse(unquote(
            redirect_url_query['redirect_url'][0])).query == query
예제 #2
0
    def test_enterprise_login_required(self):
        """
        Test that the enterprise login decorator calls the view function.

        Test that the decorator `enterprise_login_required` calls the view
        function when:
            1. `enterprise_uuid` is provided and corresponding enterprise
                customer exists in database.
            2. User making the request is authenticated.

        """
        view_function = mock_view_function()
        course_id = 'course-v1:edX+DemoX+Demo_Course'
        enterprise_launch_url = reverse(
            'enterprise_course_run_enrollment_page',
            args=[self.customer.uuid, course_id],
        )
        request = self._prepare_request(enterprise_launch_url,
                                        UserFactory(is_active=True))

        enterprise_login_required(view_function)(
            request, enterprise_uuid=self.customer.uuid, course_id=course_id)

        # Assert that view function was called.
        assert view_function.called
예제 #3
0
    def test_enterprise_login_required_redirects_for_anonymous_users_with_querystring(
            self):
        """
        Test that the decorator `enterprise_login_required` returns Http
        Redirect for anonymous users while keeping the format of query
        parameters unchanged.
        """
        view_function = mock_view_function()
        course_id = 'course-v1:edX+DemoX+Demo_Course'
        course_enrollment_url = reverse(
            'enterprise_course_run_enrollment_page',
            args=[self.customer.uuid, course_id],
        )
        querystring = 'catalog=dummy-catalog-uuid'
        course_enrollment_url = '{course_enrollment_url}?{querystring}'.format(
            course_enrollment_url=course_enrollment_url,
            querystring=querystring)
        request = self._prepare_request(course_enrollment_url, AnonymousUser())

        response = enterprise_login_required(view_function)(
            request, enterprise_uuid=self.customer.uuid, course_id=course_id)

        # Assert that redirect status code 302 is returned when an anonymous
        # user tries to access enterprise course enrollment page.
        assert response.status_code == 302

        # Now verify that the query parameters in the querystring of next url
        # are unchanged
        next_url = parse_qs(urlparse(response.url).query)['next'][0]
        next_url_querystring = unquote(urlparse(next_url).query)
        assert 'new_enterprise_login=yes' in next_url_querystring
        assert 'tpa_hint' in next_url_querystring
        assert querystring in next_url_querystring
예제 #4
0
    def test_enterprise_customer_required_raises_403(self):
        """
        Test that the decorator `enterprise_customer_required` raises
        PermissionDenied if the current user is not associated with
        an EnterpriseCustomer.
        """
        view_function = mock_view_function()
        url = reverse('catalogs-courses', (1, ))
        request = self._prepare_request(url, self.user)

        with raises(PermissionDenied):
            enterprise_customer_required(view_function)(request)
예제 #5
0
    def test_enterprise_login_required_raises_404(self, kwargs):
        """
        Test that the decorator `enterprise_login_required` raises `Http404`
        error when called with invalid or missing arguments.
        """
        view_function = mock_view_function()
        enterprise_launch_url = reverse(
            'enterprise_course_run_enrollment_page',
            args=[self.customer.uuid, 'course-v1:edX+DemoX+Demo_Course'],
        )
        request = self._prepare_request(enterprise_launch_url, UserFactory(is_active=True))

        with raises(Http404):
            enterprise_login_required(view_function)(request, **kwargs)
예제 #6
0
    def test_enterprise_customer_required_calls_view(self):
        """
        Test that the decorator `enterprise_customer_required` calls
        the decorated function if the current user is associated
        with an EnterpriseCustomer and passes the EnterpriseCustomer
        to the decorated function.
        """
        view_function = mock_view_function()
        url = reverse('catalogs-courses', (1,))
        request = self._prepare_request(url, self.user)
        EnterpriseCustomerUserFactory(
            user_id=self.user.id,
            enterprise_customer=self.customer,
        )

        enterprise_customer_required(view_function)(request)

        call_args, __ = view_function.call_args  # pylint: disable=unpacking-non-sequence
        assert str(call_args[1].uuid) == str(self.customer.uuid)
예제 #7
0
    def test_force_fresh_session_anonymous_user(self):
        """
        Test that the force_fresh_session decorator calls the
        decorated view the request is made by an unauthenticated
        user.
        """
        view_function = mock_view_function()
        course_id = 'course-v1:edX+DemoX+Demo_Course'
        enterprise_launch_url = reverse(
            'enterprise_course_run_enrollment_page',
            args=[self.customer.uuid, course_id],
        )
        request = self._prepare_request(enterprise_launch_url, AnonymousUser())

        force_fresh_session(view_function)(request,
                                           enterprise_uuid=self.customer.uuid,
                                           course_id=course_id)

        # Assert that view function was called and the session flag was set.
        assert view_function.called
예제 #8
0
    def test_force_fresh_session_param_received(self):
        """
        Test that the force_fresh_session decorator calls the view function
        if the session is fresh.
        """
        view_function = mock_view_function()
        course_id = 'course-v1:edX+DemoX+Demo_Course'
        enterprise_launch_url = reverse(
            'enterprise_course_run_enrollment_page',
            args=[self.customer.uuid, course_id],
        )
        enterprise_launch_url += '?new_enterprise_login=yes'
        request = self._prepare_request(enterprise_launch_url, UserFactory(is_active=True))

        force_fresh_session(view_function)(
            request, enterprise_uuid=self.customer.uuid, course_id=course_id
        )

        # Assert that view function was called.
        assert view_function.called
예제 #9
0
    def test_force_fresh_session_no_sso_provider(self, mock_get_idp):  # pylint: disable=unused-argument
        """
        Test that the force_fresh_session decorator calls the view function
        when no sso provider is configured.
        """
        mock_get_idp.return_value = None
        view_function = mock_view_function()
        course_id = 'course-v1:edX+DemoX+Demo_Course'
        enterprise_launch_url = reverse(
            'enterprise_course_run_enrollment_page',
            args=[self.customer.uuid, course_id],
        )
        request = self._prepare_request(enterprise_launch_url, UserFactory(is_active=True))

        force_fresh_session(view_function)(
            request, enterprise_uuid=self.customer.uuid, course_id=course_id
        )

        # Assert that view function was called.
        assert view_function.called
예제 #10
0
    def test_enterprise_login_required_redirects_for_anonymous_users(self):
        """
        Test that the decorator `enterprise_login_required` returns Http
        Redirect for anonymous users.
        """
        view_function = mock_view_function()
        course_id = 'course-v1:edX+DemoX+Demo_Course'
        enterprise_launch_url = reverse(
            'enterprise_course_run_enrollment_page',
            args=[self.customer.uuid, course_id],
        )
        request = self._prepare_request(enterprise_launch_url, AnonymousUser())

        response = enterprise_login_required(view_function)(
            request, enterprise_uuid=self.customer.uuid, course_id=course_id)

        # Assert that redirect status code 302 is returned when an anonymous
        # user tries to access enterprise course enrollment page.
        assert response.status_code == 302
        assert 'new_enterprise_login%3Dyes' in response.url
        assert 'tpa_hint' in response.url