"""Testing the new async search."""
import binascii


def test_feature_flag_not_true_uses_old_search(webapp):
    """By default the old search is used."""
    domain = 'a.com'
    response = webapp.post('/search', {'domains': domain})

    assert response.headers['location'] == 'http://localhost:80/search/612e636f6d'


def test_feature_flag_set_to_true_uses_new_search(webapp, monkeypatch):
    """A value of 'true' enables the new search."""
    monkeypatch.setenv('feature.async_search', 'true')

    domain = 'a.com'
    response = webapp.post('/search', {'domains': domain})

    assert response.headers['location'] == 'http://localhost:80/search?ed=612e636f6d'


def test_feature_flag_set_to_true_redirects_get_urls(webapp, monkeypatch):
    """A value of 'true' enables the new search."""
    monkeypatch.setenv('feature.async_search', 'true')

    domain = 'a.com'
    response = webapp.get('/search/{}'.format(binascii.hexlify(domain)))

    assert response.headers['location'] == 'http://localhost:80/search?ed=612e636f6d'


def test_feature_flag_not_true_does_not_redirect_get_urls(webapp, monkeypatch):
    """A value of 'true' enables the new search."""
    domain = 'a.com'
    response = webapp.get('/search/{}'.format(binascii.hexlify(domain)))

    # Not a redirect.
    assert response.status_code == 200

"""Testing the new async search."""
import binascii
import datetime


def test_feature_flag_not_true_uses_old_search(webapp):
    """By default the old search is used."""
    domain = 'a.com'
    response = webapp.post('/search', {'domains': domain})

    assert response.headers['location'] == 'http://localhost:80/search/612e636f6d'


def test_feature_flag_set_to_true_uses_new_search(webapp, monkeypatch):
    """A value of 'true' enables the new search."""
    monkeypatch.setenv('feature.async_search', 'true')

    domain = 'a.com'
    response = webapp.post('/search', {'domains': domain})

    assert response.headers['location'] == 'http://localhost:80/search?ed=612e636f6d'


def test_feature_flag_set_to_true_redirects_get_urls(webapp, monkeypatch):
    """A value of 'true' enables the new search."""
    monkeypatch.setenv('feature.async_search', 'true')

    domain = 'a.com'
    response = webapp.get('/search/{}'.format(binascii.hexlify(domain)))

    assert response.headers['location'] == 'http://localhost:80/search?ed=612e636f6d'


def test_feature_flag_not_true_does_not_redirect_get_urls(webapp, monkeypatch):
    """A value of 'true' enables the new search."""
    domain = 'a.com'
    response = webapp.get('/search/{}'.format(binascii.hexlify(domain)))

    # Not a redirect.
    assert response.status_code == 200


def test_async_search_scales_with_long_domains(webapp, monkeypatch):
    """The old search breaks with long-enough domains due to the massive
    number of results generated by homoglyph that need validation.

    The new async search has no significant page load time difference between
    short and long domains.
    """
    monkeypatch.setenv('feature.async_search', 'true')

    start1 = datetime.datetime.now()
    webapp.get('/search/{}'.format(binascii.hexlify('a.com'))).body
    duration1 = (datetime.datetime.now() - start1).total_seconds()

    start2 = datetime.datetime.now()
    webapp.get('/search/{}'.format(binascii.hexlify('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.zzzzzzzzzzzzzzzzzzzzzzzzzppieo.com'))).body
    duration2 = (datetime.datetime.now() - start2).total_seconds()

    assert abs(duration1 - duration2) < 0.01

Test the new search is actually fast to load the basic search page