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_api_views.py

67 lines
2.4 KiB
Python
Raw Normal View History

2020-12-29 13:16:54 +01:00
import pytest
from django.test import Client
from django.urls import reverse
2021-01-03 21:51:46 +01:00
from articles.models import Article
from articles.utils import format_article_content
2020-12-29 13:16:54 +01:00
@pytest.mark.django_db
def test_unauthenticated_render_redirects(published_article: Article, client: Client):
api_res = client.post(
reverse("api-render-article", kwargs={"article_pk": published_article.pk}),
data={"content": published_article.content},
)
assert api_res.status_code == 302
@pytest.mark.django_db
def test_render_article_same_content(published_article: Article, client: Client):
client.force_login(published_article.author)
api_res = client.post(
reverse("api-render-article", kwargs={"article_pk": published_article.pk}),
data={"content": published_article.content},
)
2020-12-29 13:16:54 +01:00
standard_res = client.get(
reverse("article-detail", kwargs={"slug": published_article.slug})
)
assert api_res.status_code == 200
assert standard_res.status_code == 200
# ignore an expected difference
api_content = api_res.content.decode("utf-8") # type: str
standard_content = standard_res.content.decode("utf-8")
api_content = api_content.replace(
"?next=/api/render/1/",
"?next=/some-article-slug/",
)
assert api_content == standard_content
@pytest.mark.django_db
def test_render_article_change_content(published_article: Article, client: Client):
client.force_login(published_article.author)
preview_content = "This is a different content **with strong emphasis**"
api_res = client.post(
reverse("api-render-article", kwargs={"article_pk": published_article.pk}),
data={"content": preview_content},
)
2020-12-29 13:16:54 +01:00
assert api_res.status_code == 200
api_content = api_res.content.decode("utf-8") # type: str
html_preview_content = format_article_content(preview_content)
assert html_preview_content in api_content
@pytest.mark.django_db
def test_render_article_doesnt_save(published_article, client: Client):
client.force_login(published_article.author)
original_content = published_article.content
preview_content = "This is a different content **with strong emphasis**"
api_res = client.post(
reverse("api-render-article", kwargs={"article_pk": published_article.pk}),
data={"content": preview_content},
)
2020-12-29 13:16:54 +01:00
assert api_res.status_code == 200
published_article.refresh_from_db()
assert published_article.content == original_content