This repository has been archived on 2023-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
python-blog/articles/tests/test_html_views.py

138 lines
4.3 KiB
Python
Raw Normal View History

2020-08-18 07:23:59 +02:00
import pytest
2021-01-03 22:01:46 +01:00
from django.template import Context, Template
2020-08-18 07:23:59 +02:00
from django.test import Client
from django.urls import reverse
2020-08-18 08:24:40 +02:00
from model_bakery import baker
2020-08-18 07:23:59 +02:00
from articles.models import Article, User
2020-08-18 07:23:59 +02:00
@pytest.mark.django_db
def test_can_access_list(client: Client, published_article: Article):
res = client.get(reverse("articles-list"))
2020-08-18 07:23:59 +02:00
assert res.status_code == 200
content = res.content.decode("utf-8")
assert published_article.title in content
2020-11-26 11:31:33 +01:00
assert published_article.get_abstract() not in content
2020-08-18 07:23:59 +02:00
@pytest.mark.django_db
2020-11-26 11:31:33 +01:00
def test_only_title_shown_on_list(client: Client, author: User):
title = "This is a very long title mouahahaha"
2020-08-18 07:23:59 +02:00
abstract = "Some abstract"
after = "Some content after abstract"
2020-08-18 08:24:40 +02:00
baker.make(
Article,
2020-11-26 11:31:33 +01:00
title=title,
2020-08-18 07:23:59 +02:00
status=Article.PUBLISHED,
2020-08-18 08:24:40 +02:00
author=author,
2020-08-18 07:23:59 +02:00
content=f"{abstract}\n<!--more-->\n{after}",
) # type: Article
res = client.get(reverse("articles-list"))
2020-08-18 07:23:59 +02:00
content = res.content.decode("utf-8")
2020-11-26 11:31:33 +01:00
assert title in content
assert abstract not in content
2020-08-18 07:23:59 +02:00
assert after not in content
2020-08-18 08:36:55 +02:00
@pytest.mark.django_db
def test_access_article_by_slug(client: Client, published_article: Article):
2020-12-03 21:15:48 +01:00
_test_access_article_by_slug(client, published_article)
def _test_access_article_by_slug(client: Client, item: Article):
res = client.get(reverse("article-detail", kwargs={"slug": item.slug}))
_assert_article_is_rendered(item, res)
def _assert_article_is_rendered(item: Article, res):
2020-12-03 21:15:48 +01:00
assert res.status_code == 200
content = res.content.decode("utf-8")
assert item.title in content
2021-01-03 22:01:46 +01:00
html = item.get_formatted_content
assert html in content
2020-12-03 21:15:48 +01:00
@pytest.mark.django_db
def test_anonymous_cant_access_draft_detail(
client: Client, unpublished_article: Article
):
res = client.get(
reverse("article-detail", kwargs={"slug": unpublished_article.slug})
)
assert res.status_code == 404
@pytest.mark.django_db
def test_anonymous_can_access_draft_detail_with_key(
client: Client, unpublished_article: Article
):
res = client.get(
reverse("article-detail", kwargs={"slug": unpublished_article.slug})
+ f"?draft_key={unpublished_article.draft_key}"
)
_assert_article_is_rendered(unpublished_article, res)
2020-12-03 21:15:48 +01:00
@pytest.mark.django_db
def test_user_can_access_draft_detail(
client: Client, author: User, unpublished_article: Article
):
client.force_login(author)
_test_access_article_by_slug(client, unpublished_article)
@pytest.mark.django_db
def test_anonymous_cant_access_drafts_list(
client: Client, unpublished_article: Article
):
res = client.get(reverse("drafts-list"))
assert res.status_code == 302
@pytest.mark.django_db
def test_user_can_access_drafts_list(
client: Client, author: User, unpublished_article: Article
):
client.force_login(author)
res = client.get(reverse("drafts-list"))
2020-08-18 08:36:55 +02:00
assert res.status_code == 200
content = res.content.decode("utf-8")
2020-12-03 21:15:48 +01:00
assert unpublished_article.title in content
@pytest.mark.django_db
def test_has_goatcounter_if_set(client: Client, settings):
settings.GOATCOUNTER_DOMAIN = "gc.gabnotes.org"
res = client.get(reverse("articles-list"))
content = res.content.decode("utf-8")
assert "window.goatcounter" in content
assert f"{settings.GOATCOUNTER_DOMAIN}/count" in content
@pytest.mark.django_db
def test_doesnt_have_goatcounter_if_unset(client: Client, settings):
settings.GOATCOUNTER_DOMAIN = None
res = client.get(reverse("articles-list"))
content = res.content.decode("utf-8")
assert "window.goatcounter" not in content
assert f"{settings.GOATCOUNTER_DOMAIN}/count" not in content
@pytest.mark.django_db
def test_logged_in_user_doesnt_have_goatcounter(client: Client, author: User, settings):
client.force_login(author)
settings.GOATCOUNTER_DOMAIN = "gc.gabnotes.org"
res = client.get(reverse("articles-list"))
content = res.content.decode("utf-8")
assert "window.goatcounter" not in content
assert f"{settings.GOATCOUNTER_DOMAIN}/count" not in content
@pytest.mark.django_db
def test_image_is_lazy(client: Client, published_article: Article):
res = client.get(reverse("article-detail", kwargs={"slug": published_article.slug}))
assert res.status_code == 200
content = res.content.decode("utf-8")
assert content.count('loading="lazy"') == 2