| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- from http import HTTPStatus
- from django.contrib.auth import get_user_model
- from django.urls import reverse
- from django.test import TestCase
- import pytest
- from news.forms import WARNING
- from news.models import Comment
- User = get_user_model()
- def get_news_detail_url(news_id: int):
- """Возвращает детали новости по урле."""
- return reverse('news:detail', args=(news_id,))
- def get_news_delete_url(comment_id):
- return reverse('news:delete', args=(comment_id,))
- def get_news_edit_url(comment_id):
- return reverse('news:edit', args=(comment_id,))
- @pytest.mark.django_db
- def test_anonymous_user_cant_create_comment(client, news, comment_text):
- url = get_news_detail_url(news.id)
- form_data = {'text': comment_text}
- initial_comment_count = Comment.objects.count()
- assert initial_comment_count == 0
- response = client.post(url, data=form_data)
- assert response.status_code == HTTPStatus.FOUND
- assert Comment.objects.count() == initial_comment_count
- @pytest.mark.django_db
- def test_user_can_create_comment(client, news, comment_text):
- user = User.objects.create(username='Мимо Крокодил')
- client.force_login(user)
- url = get_news_detail_url(news.id)
- form_data = {'text': comment_text}
- initial_comment_count = Comment.objects.count()
- assert initial_comment_count == 0
- response = client.post(url, data=form_data)
- assert response.status_code == HTTPStatus.FOUND
- assert Comment.objects.count() == initial_comment_count + 1
- comment = Comment.objects.first()
- assert comment.text == form_data['text']
- assert comment.news == news
- assert comment.author == user
- @pytest.mark.django_db
- def test_user_cant_use_bad_words(client, news, bad_words_data):
- user = User.objects.create(username='Мимо Крокодил')
- client.force_login(user)
- url = get_news_detail_url(news.id)
- form_data = {'text': bad_words_data}
- response = client.post(url, data=form_data)
- assert response.status_code == HTTPStatus.OK
- assert 'form' in response.context
- TestCase().assertFormError(response, 'form', 'text', WARNING)
- assert Comment.objects.count() == 0
- @pytest.mark.django_db
- def test_author_can_delete_comment(client, comment, comment_detail_url):
- initial_comment_count = Comment.objects.count()
- assert initial_comment_count == 1
- url = get_news_delete_url(comment.id)
- response = client.delete(url)
- assert response.status_code == HTTPStatus.FOUND
- assert response.url == comment_detail_url
- assert Comment.objects.count() == initial_comment_count - 1
- @pytest.mark.django_db
- def test_user_cant_delete_comment_of_another_user(
- client,
- comment,
- comment_detail_url
- ):
- user = User.objects.create(username='Читатель')
- client.force_login(user)
- url = get_news_delete_url(comment.id)
- initial_comment_count = Comment.objects.count()
- assert initial_comment_count == 1
- response = client.delete(url)
- assert response.status_code == HTTPStatus.NOT_FOUND
- assert Comment.objects.count() == initial_comment_count
- @pytest.mark.django_db
- def test_author_can_edit_comment(client, comment, comment_detail_url):
- client.force_login(comment.author)
- url = get_news_edit_url(comment.id)
- form_data = {'text': 'Обновлённый комментарий'}
- response = client.post(url, data=form_data)
- assert response.status_code == HTTPStatus.FOUND
- assert response.url == comment_detail_url
- comment.refresh_from_db()
- assert comment.text == form_data['text']
- @pytest.mark.django_db
- def test_user_cant_edit_comment_of_another_user(
- client,
- comment,
- comment_detail_url
- ):
- user = User.objects.create(username='Читатель')
- client.force_login(user)
- url = get_news_edit_url(comment.id)
- form_data = {'text': 'Обновлённый комментарий'}
- original_comment = Comment.objects.get(id=comment.id)
- response = client.post(url, data=form_data)
- assert response.status_code == HTTPStatus.NOT_FOUND
- comment.refresh_from_db()
- assert comment.text == original_comment.text
- assert comment.author == original_comment.author
- assert comment.news == original_comment.news
|