from unittest.mock import Mock

import requests
from django.test import RequestFactory
from django_cas_ng.utils import get_redirect_url, get_service_url, get_cas_client


#
# get_service_url tests
#
def test_service_url_helper():
    factory = RequestFactory()
    request = factory.get('/login/')

    actual = get_service_url(request)
    expected = 'http://testserver/login/?next=%2F'

    assert actual == expected


def test_service_url_helper_as_https():
    factory = RequestFactory()
    kwargs = {'secure': True, 'wsgi.url_scheme': 'https', 'SERVER_PORT': '443'}
    request = factory.get('/login/', **kwargs)

    actual = get_service_url(request)
    expected = 'https://testserver/login/?next=%2F'

    assert actual == expected


def test_service_url_helper_with_redirect():
    factory = RequestFactory()
    request = factory.get('/login/')

    actual = get_service_url(request, redirect_to='http://testserver/landing-page/')
    expected = 'http://testserver/login/?next=http%3A%2F%2Ftestserver%2Flanding-page%2F'

    assert actual == expected


def test_service_url_preserves_query_parameters():
    factory = RequestFactory()
    request = factory.get('/login/?foo=bar', secure=True)

    actual = get_service_url(request, redirect_to='https://testserver/landing-page/')
    assert 'next=https%3A%2F%2Ftestserver%2Flanding-page%2F' in actual


def test_service_url_avoids_next(settings):
    settings.CAS_STORE_NEXT = True

    factory = RequestFactory()
    request = factory.get('/login/')

    actual = get_service_url(request, redirect_to='/admin/')
    expected = 'http://testserver/login/'
    assert actual == expected


def test_service_url_root_proxied_as(settings):
    settings.CAS_ROOT_PROXIED_AS = 'https://foo.bar:8443'

    factory = RequestFactory()
    request = factory.get('/login/')

    actual = get_service_url(request)
    expected = 'https://foo.bar:8443/login/?next=%2F'
    assert actual == expected


def test_service_url_root_proxied_as_empty_string(settings):
    """
    If the settings module has the attribute CAS_ROOT_PROXIED_AS but its value
    is an empty string (or another falsy value), we must make sure the setting
    is not considered while constructing the redirect url.
    """
    settings.CAS_ROOT_PROXIED_AS = ''

    factory = RequestFactory()
    request = factory.get('/login/')

    actual = get_service_url(request)
    expected = 'http://testserver/login/?next=%2F'
    assert actual == expected


def test_force_ssl_service_url(settings):
    settings.CAS_FORCE_SSL_SERVICE_URL = True

    factory = RequestFactory()
    request = factory.get('/login/')

    actual = get_service_url(request)
    expected = 'https://testserver/login/?next=%2F'

    assert actual == expected


#
# get_redirect_url tests
#
def test_redirect_url_with_url_as_get_parameter():
    factory = RequestFactory()
    request = factory.get('/login/', data={'next': '/landing-page/'})

    actual = get_redirect_url(request)
    expected = '/landing-page/'

    assert actual == expected


def test_redirect_url_falls_back_to_cas_redirect_url_setting(settings):
    settings.CAS_IGNORE_REFERER = True
    settings.CAS_REDIRECT_URL = '/landing-page/'

    factory = RequestFactory()