# Path: invenio_oauth2server/validators.py
# class URLValidator(URL):
#     """URL validator."""
# 
#     def __call__(self, form, field):
#         """Check URL."""
#         parsed = urlparse(field.data)
#         if current_app.debug and parsed.hostname == 'localhost':
#             return
#         super(URLValidator, self).__call__(form=form, field=field)
# 
# def validate_redirect_uri(value):
#     """Validate a redirect URI.
# 
#     Redirect URIs must be a valid URL and use https unless the host is
#     localhost for which http is accepted.
# 
#     :param value: The redirect URI.
#     """
#     sch, netloc, path, par, query, fra = urlparse(value)
#     if not (sch and netloc):
#         raise InvalidRedirectURIError()
#     if sch != 'https':
#         if ':' in netloc:
#             netloc, port = netloc.split(':', 1)
#         if not (netloc in ('localhost', '127.0.0.1') and sch == 'http'):
#             raise InsecureTransportError()


from collections import namedtuple
from oauthlib.oauth2.rfc6749.errors import InsecureTransportError, \
    InvalidRedirectURIError
from wtforms.validators import ValidationError
from invenio_oauth2server.validators import URLValidator, validate_redirect_uri
import pytest
    ('https://localhost/', None),
    ('https://127.0.0.1', None),
])
def test_validate_redirect_uri(input, expected):
    """Test redirect URI validator."""
    try:
        validate_redirect_uri(input)
    except Exception as e:
        assert type(e) is type(expected)


def test_url_validator(app):
    """Test url validator."""
    class Field(object):

        def __init__(self, data):
            self.data = data

        def gettext(self, *args, **kwargs):
            return 'text'

    with app.app_context():
        # usually if localhost, validation is failing
        with pytest.raises(ValidationError):
            URLValidator()(
                form=None, field=Field(data='http://localhost:5000'))
        URLValidator()(form=None, field=Field(data='http://mywebsite.it:5000'))

        # enable debug mode to accept also localhost
        app.config['DEBUG'] = True

# Path: invenio_oauth2server/validators.py
# class URLValidator(URL):
#     """URL validator."""
# 
#     def __call__(self, form, field):
#         """Check URL."""
#         parsed = urlparse(field.data)
#         if current_app.debug and parsed.hostname == 'localhost':
#             return
#         super(URLValidator, self).__call__(form=form, field=field)
# 
# def validate_redirect_uri(value):
#     """Validate a redirect URI.
# 
#     Redirect URIs must be a valid URL and use https unless the host is
#     localhost for which http is accepted.
# 
#     :param value: The redirect URI.
#     """
#     sch, netloc, path, par, query, fra = urlparse(value)
#     if not (sch and netloc):
#         raise InvalidRedirectURIError()
#     if sch != 'https':
#         if ':' in netloc:
#             netloc, port = netloc.split(':', 1)
#         if not (netloc in ('localhost', '127.0.0.1') and sch == 'http'):
#             raise InsecureTransportError()


# Path: tests/test_validators.py
from collections import namedtuple
from oauthlib.oauth2.rfc6749.errors import InsecureTransportError, \
    InvalidRedirectURIError
from wtforms.validators import ValidationError
from invenio_oauth2server.validators import URLValidator, validate_redirect_uri
import pytest

    ('https://localhost/', None),
    ('https://127.0.0.1', None),
])
def test_validate_redirect_uri(input, expected):
    """Test redirect URI validator."""
    try:
        validate_redirect_uri(input)
    except Exception as e:
        assert type(e) is type(expected)


def test_url_validator(app):
    """Test url validator."""
    class Field(object):

        def __init__(self, data):
            self.data = data

        def gettext(self, *args, **kwargs):
            return 'text'

    with app.app_context():
        # usually if localhost, validation is failing
        with pytest.raises(ValidationError):
            URLValidator()(
                form=None, field=Field(data='http://localhost:5000'))
        URLValidator()(form=None, field=Field(data='http://mywebsite.it:5000'))

        # enable debug mode to accept also localhost
        app.config['DEBUG'] = True

        URLValidator()(form=None, field=Field(data='http://localhost:5000'))