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
    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
    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
    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