<commit_before>import pytest
from django.conf import settings
from django.http import Http404
from django.test.utils import override_settings
from django.utils.timezone import now

from pretix.base.models import Event, Organizer
from pretix.multidomain.models import KnownDomain


@pytest.fixture
def env():
    o = Organizer.objects.create(name='MRMCD', slug='mrmcd')
    event = Event.objects.create(
        organizer=o, name='MRMCD2015', slug='2015',
        date_from=now()
    )
    settings.SITE_URL = 'http://example.com'
    return o, event


@pytest.mark.django_db
def test_control_only_on_main_domain(env, client):
    KnownDomain.objects.create(domainname='foobar', organizer=env[0])
    r = client.get('/control/login', HTTP_HOST='foobar')
    assert r.status_code == 302
    assert r['Location'] == 'http://example.com/control/login'


@pytest.mark.django_db
def test_unknown_domain(env, client):
    r = client.get('/control/login', HTTP_HOST='foobar')
    assert r.status_code == 400


@pytest.mark.django_db
def test_event_on_custom_domain(env, client):
    KnownDomain.objects.create(domainname='foobar', organizer=env[0])
    r = client.get('/2015/', HTTP_HOST='foobar')
    assert r.status_code == 200


@pytest.mark.django_db
def test_event_with_custom_domain_on_main_domain(env, client):
    KnownDomain.objects.create(domainname='foobar', organizer=env[0])
    r = client.get('/mrmcd/2015/', HTTP_HOST='example.com')
    assert r.status_code == 302
    assert r['Location'] == 'http://foobar/2015/'


@pytest.mark.django_db
def test_organizer_with_custom_domain_on_main_domain(env, client):
    KnownDomain.objects.create(domainname='foobar', organizer=env[0])
    r = client.get('/mrmcd/', HTTP_HOST='example.com')
    assert r.status_code == 302
    assert r['Location'] == 'http://foobar'


@pytest.mark.django_db
def test_event_on_custom_domain_only_with_wrong_organizer(env, client):
    organizer2 = Organizer.objects.create(name='Dummy', slug='dummy')
    Event.objects.create(
        organizer=organizer2, name='D1234', slug='1234',
        date_from=now()
    )
    KnownDomain.objects.create(domainname='foobar', organizer=env[0])
    with pytest.raises(Http404):
        client.get('/dummy/1234/', HTTP_HOST='foobar')


@pytest.mark.django_db
def test_unknown_event_on_custom_domain(env, client):
    organizer2 = Organizer.objects.create(name='Dummy', slug='dummy')
    Event.objects.create(
        organizer=organizer2, name='D1234', slug='1234',
        date_from=now()
    )
    KnownDomain.objects.create(domainname='foobar', organizer=env[0])
    r = client.get('/1234/', HTTP_HOST='foobar')
    assert r.status_code == 404


@pytest.mark.django_db<commit_msg>Fixed a broken test<commit_after>import pytest
from django.conf import settings
from django.http import Http404
from django.test.utils import override_settings
from django.utils.timezone import now

from pretix.base.models import Event, Organizer
from pretix.multidomain.models import KnownDomain


@pytest.fixture
def env():
    o = Organizer.objects.create(name='MRMCD', slug='mrmcd')
    event = Event.objects.create(
        organizer=o, name='MRMCD2015', slug='2015',
        date_from=now()
    )
    settings.SITE_URL = 'http://example.com'
    return o, event


@pytest.mark.django_db
def test_control_only_on_main_domain(env, client):
    KnownDomain.objects.create(domainname='foobar', organizer=env[0])
    r = client.get('/control/login', HTTP_HOST='foobar')
    assert r.status_code == 302
    assert r['Location'] == 'http://example.com/control/login'


@pytest.mark.django_db
def test_unknown_domain(env, client):
    r = client.get('/control/login', HTTP_HOST='foobar')
    assert r.status_code == 400


@pytest.mark.django_db
def test_event_on_custom_domain(env, client):
    KnownDomain.objects.create(domainname='foobar', organizer=env[0])
    r = client.get('/2015/', HTTP_HOST='foobar')
    assert r.status_code == 200


@pytest.mark.django_db
def test_event_with_custom_domain_on_main_domain(env, client):
    KnownDomain.objects.create(domainname='foobar', organizer=env[0])
    r = client.get('/mrmcd/2015/', HTTP_HOST='example.com')
    assert r.status_code == 302
    assert r['Location'] == 'http://foobar/2015/'


@pytest.mark.django_db
def test_organizer_with_custom_domain_on_main_domain(env, client):
    KnownDomain.objects.create(domainname='foobar', organizer=env[0])
    r = client.get('/mrmcd/', HTTP_HOST='example.com')
    assert r.status_code == 302
    assert r['Location'] == 'http://foobar'


@pytest.mark.django_db
def test_event_on_custom_domain_only_with_wrong_organizer(env, client):
    organizer2 = Organizer.objects.create(name='Dummy', slug='dummy')
    Event.objects.create(
        organizer=organizer2, name='D1234', slug='1234',
        date_from=now()
    )
    KnownDomain.objects.create(domainname='foobar', organizer=env[0])
    r = client.get('/dummy/1234/', HTTP_HOST='foobar')
    assert r.status_code == 404


@pytest.mark.django_db
def test_unknown_event_on_custom_domain(env, client):
    organizer2 = Organizer.objects.create(name='Dummy', slug='dummy')
    Event.objects.create(
        organizer=organizer2, name='D1234', slug='1234',
        date_from=now()
    )
    KnownDomain.objects.create(domainname='foobar', organizer=env[0])
    r = client.get('/1234/', HTTP_HOST='foobar')
    assert r.status_code == 404


@pytest.mark.django_db
{'fraction_non_alphanumeric': 0.1408763172490294, 'fraction_numerical': 0.028840820854132, 'mean_word_length': 4.6471816283924845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3858083', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1812, 'n_words': 304}