import pytest


@pytest.mark.parametrize(
    "url,redirect",
    (
        ("/", "/accounts/login/?next=/"),
        ("/accounts/profile/", "/accounts/login/?next=/accounts/profile/"),
    ),
)
def test_root_view_redirects_valid_urls(client, url, redirect):
    response = client.get(url)
    assert response.status_code == 302
    assert response.url == redirect


def test_root_view_invalid_urls_404(client):
    response = client.get("/foobarbaz/")
    assert response.status_code == 404


@pytest.mark.django_db
def test_owner_can_access_dashboard(client, org_owner, organization):
    client.force_login(org_owner)
    response = client.get(organization.urls.base)
    assert organization.namespace == f"org-{organization.pk}"
    assert response.status_code == 200
    assert organization.name in response.content.decode()


@pytest.mark.django_db
def test_user_cannot_see_other_organization(client, org_owner, other_organization):
    client.force_login(org_owner)
    response = client.get(other_organization.urls.base)
    assert response.status_code == 403
    assert other_organization.name not in response.content.decode()


@pytest.mark.django_db
def test_organization_linked_in_sidebar(
    client, org_owner, organization, other_organization
):
    client.force_login(org_owner)
    response = client.get("/", follow=True)
    assert response.status_code == 200
    assert organization.name in response.content.decode()
    assert other_organization.name not in response.content.decode()
