# Path: eulfedora/util.py
# class RequestFailed(IOError):
#     '''An exception representing an arbitrary error while trying to access a
#     Fedora object or datastream.
#     '''
#     error_regex = re.compile('<pre>(.*\n.*)\n', re.MULTILINE)
# 
#     def __init__(self, response, content=None):
#         # init params:
#         #  response = HttpResponse with the error information
#         #  content = optional content of the response body, if it needed to be read
#         #            to determine what kind of exception to raise
#         super(RequestFailed, self).__init__('%d %s' % (response.status_code, response.text))
#         self.code = response.status_code
#         self.reason = response.text
#         if response.status_code == requests.codes.server_error:
#             # grab the response content if not passed in
#             if content is None:
#                 content = response.text
#             content = force_text(content)
#             # when Fedora gives a 500 error, it includes a stack-trace - pulling first line as detail
#             # NOTE: this is likely to break if and when Fedora error responses change
#             if 'content-type' in response.headers and response.headers['content-type'] == 'text/plain':
#                 # for plain text, first line of stack-trace is first line of text
#                 self.detail = content.split('\n')[0]
#             else:
#                 # for html, stack trace is wrapped with a <pre> tag; using regex to grab first line
#                 match = self.error_regex.findall(content)
#                 if len(match):
#                     self.detail = match[0]
# 
# class PermissionDenied(RequestFailed):
#     '''An exception representing a permission error while trying to access a
#     Fedora object or datastream.
#     '''


import unittest
    import django
    from unittest import skipIf
    from unittest2 import skipIf
from mock import Mock
    from django.template import Context, Template
from eulfedora.util import RequestFailed, PermissionDenied
    def value(self):
        if isinstance(self._value, Exception):
            raise self._value
        return self._value


@skipIf(django is None, 'Requires Django')
class TemplateTagTest(unittest.TestCase):
    def test_parse_fedora_access(self):
        TEMPLATE_TEXT = """
            {% load fedora %}
            {% fedora_access %}
                {{ test_obj.value }}
            {% permission_denied %}
                permission fallback
            {% fedora_failed %}
                connection fallback
            {% end_fedora_access %}
        """
        t = Template(TEMPLATE_TEXT)
        test_obj = MockFedoraObject()
        ctx = Context({'test_obj': test_obj})

        val = t.render(ctx)
        self.assertEqual(val.strip(), 'sample text')

        response = Mock()
        response.status_code = 401
        response.headers = {'content-type': 'text/plain'}
        response.content = ''

# Path: eulfedora/util.py
# class RequestFailed(IOError):
#     '''An exception representing an arbitrary error while trying to access a
#     Fedora object or datastream.
#     '''
#     error_regex = re.compile('<pre>(.*\n.*)\n', re.MULTILINE)
# 
#     def __init__(self, response, content=None):
#         # init params:
#         #  response = HttpResponse with the error information
#         #  content = optional content of the response body, if it needed to be read
#         #            to determine what kind of exception to raise
#         super(RequestFailed, self).__init__('%d %s' % (response.status_code, response.text))
#         self.code = response.status_code
#         self.reason = response.text
#         if response.status_code == requests.codes.server_error:
#             # grab the response content if not passed in
#             if content is None:
#                 content = response.text
#             content = force_text(content)
#             # when Fedora gives a 500 error, it includes a stack-trace - pulling first line as detail
#             # NOTE: this is likely to break if and when Fedora error responses change
#             if 'content-type' in response.headers and response.headers['content-type'] == 'text/plain':
#                 # for plain text, first line of stack-trace is first line of text
#                 self.detail = content.split('\n')[0]
#             else:
#                 # for html, stack trace is wrapped with a <pre> tag; using regex to grab first line
#                 match = self.error_regex.findall(content)
#                 if len(match):
#                     self.detail = match[0]
# 
# class PermissionDenied(RequestFailed):
#     '''An exception representing a permission error while trying to access a
#     Fedora object or datastream.
#     '''


# Path: test/test_fedora/test_templatetags.py
import unittest
    import django
    from unittest import skipIf
    from unittest2 import skipIf
from mock import Mock
    from django.template import Context, Template
from eulfedora.util import RequestFailed, PermissionDenied

    def value(self):
        if isinstance(self._value, Exception):
            raise self._value
        return self._value


@skipIf(django is None, 'Requires Django')
class TemplateTagTest(unittest.TestCase):
    def test_parse_fedora_access(self):
        TEMPLATE_TEXT = """
            {% load fedora %}
            {% fedora_access %}
                {{ test_obj.value }}
            {% permission_denied %}
                permission fallback
            {% fedora_failed %}
                connection fallback
            {% end_fedora_access %}
        """
        t = Template(TEMPLATE_TEXT)
        test_obj = MockFedoraObject()
        ctx = Context({'test_obj': test_obj})

        val = t.render(ctx)
        self.assertEqual(val.strip(), 'sample text')

        response = Mock()
        response.status_code = 401
        response.headers = {'content-type': 'text/plain'}
        response.content = ''

        test_obj._value = PermissionDenied(response)  # force test_obj.value to fail