from apps.blog.models import Post
from apps.services.models import Service


def test_robots_blocks_back_office_and_points_to_sitemap(client, db):
    response = client.get("/robots.txt")
    assert response["Content-Type"].startswith("text/plain")
    body = response.content.decode()
    assert "Disallow: /dashboard/" in body
    assert "Disallow: /admin/" in body
    assert "Disallow: /api/" not in body
    assert "Sitemap: http://testserver/sitemap.xml" in body


def test_sitemap_lists_pages_and_published_content(client, content):
    body = client.get("/sitemap.xml").content.decode()
    assert "<loc>http://testserver/</loc>" in body
    assert "<loc>http://testserver/contact</loc>" in body
    service = Service.objects.filter(is_published=True).first()
    assert f"<loc>http://testserver/nos-services/{service.slug}</loc>" in body
    post = Post.visible.first()
    assert f"<loc>http://testserver/blog/{post.slug}</loc>" in body
    assert "<lastmod>" in body
    assert "/dashboard" not in body


def test_sitemap_hides_unpublished_content(client, content):
    service = Service.objects.filter(is_published=True).first()
    service.is_published = False
    service.save()
    post = Post.visible.first()
    post.is_published = False
    post.save()
    body = client.get("/sitemap.xml").content.decode()
    assert f"/nos-services/{service.slug}<" not in body
    assert f"/blog/{post.slug}<" not in body
