import pytest
from django.contrib.auth.models import User, Group
from django.urls import reverse


@pytest.mark.django_db()
def test_no_admin(user, client):
    client.force_login(user)
    response = client.get("/admin/")
    assert response.status_code != 200


@pytest.mark.django_db()
def test_user_cannot_change_program_or_episode(user, client, program):
    assert not user.has_perm("aircox.change_program")
    assert not user.has_perm("aircox.change_episode")


@pytest.mark.django_db()
def test_group_can_change_program(user, client, program):
    assert program.editors in Group.objects.all()
    assert not user.has_perm("aircox.%s" % program.change_permission_codename)
    user.groups.add(program.editors)
    user = User.objects.get(pk=user.pk)  # reload user in order to have permissions set
    assert program.editors in user.groups.all()
    assert user.has_perm("aircox.%s" % program.change_permission_codename)


@pytest.mark.django_db()
def test_group_change_program(user, client, program):
    client.force_login(user)
    response = client.get(reverse("program-edit", kwargs={"pk": program.pk}))
    assert response.status_code == 403
    user.groups.add(program.editors)
    response = client.get(reverse("program-edit", kwargs={"pk": program.pk}))
    assert response.status_code == 200
