1
0
Fork 0
mirror of https://github.com/EDeev/deev.space.git synced 2026-10-07 20:49:59 +03:00
deev.space/main/tests/test_pages.py
Egor Deev 268652505a «Обо мне»: исправлена ссылка на несуществующую картинку-заглушку
Без загруженного фото владельца страница падала с 500 (в манифесте статики нет
images/hero.png). Теперь используется static/img/hero.jpg; добавлен тест, который
проверяет, что все {% static %} в шаблонах указывают на существующие файлы.
2026-10-02 12:10:57 +00:00

63 lines
2 KiB
Python

import pytest
from django.urls import reverse
from main.models import ArticleView
UA = 'Mozilla/5.0 (X11; Linux x86_64) Firefox/131.0'
@pytest.mark.django_db
@pytest.mark.parametrize('name', ['index', 'about', 'projects', 'achievements', 'contacts', 'blog'])
def test_page_opens(client, name):
assert client.get(reverse(name), HTTP_USER_AGENT=UA).status_code == 200
@pytest.mark.django_db
def test_sitemap_and_robots(client, article):
assert client.get('/sitemap.xml').status_code == 200
@pytest.mark.django_db
def test_article_counts_unique_views(client, article):
url = article.get_absolute_url()
client.get(url, HTTP_USER_AGENT=UA)
client.get(url, HTTP_USER_AGENT=UA)
article.refresh_from_db()
assert article.views == 2
assert ArticleView.objects.filter(article=article).count() == 1
@pytest.mark.django_db
def test_bots_do_not_create_unique_views(client, article):
client.get(article.get_absolute_url(), HTTP_USER_AGENT='Googlebot/2.1')
assert not ArticleView.objects.exists()
@pytest.mark.django_db
def test_visitor_cookie_is_signed_and_httponly(client):
response = client.get(reverse('index'), HTTP_USER_AGENT=UA)
cookie = response.cookies['dspace_vid']
assert cookie['httponly']
assert ':' in cookie.value
@pytest.mark.django_db
def test_unknown_page_returns_404(client):
assert client.get('/no-such-page/', HTTP_USER_AGENT=UA).status_code == 404
def test_all_static_references_exist():
"""Каждый {% static %} в шаблонах указывает на существующий файл (иначе 500 в бою)."""
import re
from pathlib import Path
from django.conf import settings
pattern = re.compile(r"{% static '([^']+)' %}")
missing = [
f'{tpl.name}: {ref}'
for tpl in Path(settings.BASE_DIR, 'templates').rglob('*.html')
for ref in pattern.findall(tpl.read_text(encoding='utf-8'))
if not Path(settings.BASE_DIR, 'static', ref).exists()
]
assert not missing