mirror of
https://github.com/EDeev/deev.space.git
synced 2026-10-07 20:49:59 +03:00
populate_demo падала на импорте несуществующей ArticleCategory, строковых статусах проектов и удалённом поле is_featured; теперь создаёт статусы проектов и работает, это проверяет тест. load_initial_data была загрузчиком старой версии резюме под прежние модели и тоже не запускалась — актуальный контент живёт в админке.
77 lines
2.4 KiB
Python
77 lines
2.4 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
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_populate_demo_fills_site(client):
|
|
import io
|
|
|
|
from django.core.management import call_command
|
|
|
|
from main.models import Article, Project
|
|
|
|
call_command('populate_demo', stdout=io.StringIO())
|
|
assert Project.objects.exists() and Article.objects.exists()
|
|
for name in ('index', 'projects', 'blog', 'about'):
|
|
assert client.get(reverse(name), HTTP_USER_AGENT=UA).status_code == 200
|