import pytest

from aiohttp.test_utils import TestClient

from ahriman.models.build_status import BuildStatusEnum
from ahriman.models.changes import Changes
from ahriman.models.package import Package
from ahriman.models.user_access import UserAccess
from ahriman.web.views.v1.packages.changes import ChangesView


async def test_get_permission() -> None:
    """
    must return correct permission for the request
    """
    for method in ("GET",):
        request = pytest.helpers.request("", "", method)
        assert await ChangesView.get_permission(request) == UserAccess.Reporter
    for method in ("POST",):
        request = pytest.helpers.request("", "", method)
        assert await ChangesView.get_permission(request) == UserAccess.Full


def test_routes() -> None:
    """
    must return correct routes
    """
    assert ChangesView.ROUTES == ["/api/v1/packages/{package}/changes"]


async def test_get(client: TestClient, package_ahriman: Package) -> None:
    """
    must get changes for package
    """
    changes = Changes("sha", "change")
    await client.post(f"/api/v1/packages/{package_ahriman.base}",
                      json={"status": BuildStatusEnum.Success.value, "package": package_ahriman.view()})
    await client.post(f"/api/v1/packages/{package_ahriman.base}/changes", json=changes.view())
    response_schema = pytest.helpers.schema_response(ChangesView.get)

    response = await client.get(f"/api/v1/packages/{package_ahriman.base}/changes")
    assert response.status == 200

    assert await response.json() == changes.view()
    assert not response_schema.validate(changes.view())


async def test_get_not_found(client: TestClient, package_ahriman: Package) -> None:
    """
    must return not found for missing package
    """
    response_schema = pytest.helpers.schema_response(ChangesView.get, code=404)

    response = await client.get(f"/api/v1/packages/{package_ahriman.base}/changes")
    assert response.status == 404
    assert not response_schema.validate(await response.json())


async def test_post(client: TestClient, package_ahriman: Package) -> None:
    """
    must update package changes
    """
    await client.post(f"/api/v1/packages/{package_ahriman.base}",
                      json={"status": BuildStatusEnum.Success.value, "package": package_ahriman.view()})
    request_schema = pytest.helpers.schema_request(ChangesView.post)

    changes = Changes("sha", "change")
    assert not request_schema.validate(changes.view())
    response = await client.post(f"/api/v1/packages/{package_ahriman.base}/changes", json=changes.view())
    assert response.status == 204

    response = await client.get(f"/api/v1/packages/{package_ahriman.base}/changes")
    assert await response.json() == changes.view()


async def test_post_exception(client: TestClient, package_ahriman: Package) -> None:
    """
    must raise exception on invalid payload
    """
    response_schema = pytest.helpers.schema_response(ChangesView.post, code=400)

    response = await client.post(f"/api/v1/packages/{package_ahriman.base}/changes", json=[])
    assert response.status == 400
    assert not response_schema.validate(await response.json())
