test_logic.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from http import HTTPStatus
  2. from django.contrib.auth import get_user_model
  3. from django.urls import reverse
  4. from django.test import TestCase
  5. import pytest
  6. from news.forms import WARNING
  7. from news.models import Comment
  8. User = get_user_model()
  9. def get_news_detail_url(news_id: int):
  10. """Возвращает детали новости по урле."""
  11. return reverse('news:detail', args=(news_id,))
  12. def get_news_delete_url(comment_id):
  13. return reverse('news:delete', args=(comment_id,))
  14. def get_news_edit_url(comment_id):
  15. return reverse('news:edit', args=(comment_id,))
  16. @pytest.mark.django_db
  17. def test_anonymous_user_cant_create_comment(client, news, comment_text):
  18. url = get_news_detail_url(news.id)
  19. form_data = {'text': comment_text}
  20. initial_comment_count = Comment.objects.count()
  21. assert initial_comment_count == 0
  22. response = client.post(url, data=form_data)
  23. assert response.status_code == HTTPStatus.FOUND
  24. assert Comment.objects.count() == initial_comment_count
  25. @pytest.mark.django_db
  26. def test_user_can_create_comment(client, news, comment_text):
  27. user = User.objects.create(username='Мимо Крокодил')
  28. client.force_login(user)
  29. url = get_news_detail_url(news.id)
  30. form_data = {'text': comment_text}
  31. initial_comment_count = Comment.objects.count()
  32. assert initial_comment_count == 0
  33. response = client.post(url, data=form_data)
  34. assert response.status_code == HTTPStatus.FOUND
  35. assert Comment.objects.count() == initial_comment_count + 1
  36. comment = Comment.objects.first()
  37. assert comment.text == form_data['text']
  38. assert comment.news == news
  39. assert comment.author == user
  40. @pytest.mark.django_db
  41. def test_user_cant_use_bad_words(client, news, bad_words_data):
  42. user = User.objects.create(username='Мимо Крокодил')
  43. client.force_login(user)
  44. url = get_news_detail_url(news.id)
  45. form_data = {'text': bad_words_data}
  46. response = client.post(url, data=form_data)
  47. assert response.status_code == HTTPStatus.OK
  48. assert 'form' in response.context
  49. TestCase().assertFormError(response, 'form', 'text', WARNING)
  50. assert Comment.objects.count() == 0
  51. @pytest.mark.django_db
  52. def test_author_can_delete_comment(client, comment, comment_detail_url):
  53. initial_comment_count = Comment.objects.count()
  54. assert initial_comment_count == 1
  55. url = get_news_delete_url(comment.id)
  56. response = client.delete(url)
  57. assert response.status_code == HTTPStatus.FOUND
  58. assert response.url == comment_detail_url
  59. assert Comment.objects.count() == initial_comment_count - 1
  60. @pytest.mark.django_db
  61. def test_user_cant_delete_comment_of_another_user(
  62. client,
  63. comment,
  64. comment_detail_url
  65. ):
  66. user = User.objects.create(username='Читатель')
  67. client.force_login(user)
  68. url = get_news_delete_url(comment.id)
  69. initial_comment_count = Comment.objects.count()
  70. assert initial_comment_count == 1
  71. response = client.delete(url)
  72. assert response.status_code == HTTPStatus.NOT_FOUND
  73. assert Comment.objects.count() == initial_comment_count
  74. @pytest.mark.django_db
  75. def test_author_can_edit_comment(client, comment, comment_detail_url):
  76. client.force_login(comment.author)
  77. url = get_news_edit_url(comment.id)
  78. form_data = {'text': 'Обновлённый комментарий'}
  79. response = client.post(url, data=form_data)
  80. assert response.status_code == HTTPStatus.FOUND
  81. assert response.url == comment_detail_url
  82. comment.refresh_from_db()
  83. assert comment.text == form_data['text']
  84. @pytest.mark.django_db
  85. def test_user_cant_edit_comment_of_another_user(
  86. client,
  87. comment,
  88. comment_detail_url
  89. ):
  90. user = User.objects.create(username='Читатель')
  91. client.force_login(user)
  92. url = get_news_edit_url(comment.id)
  93. form_data = {'text': 'Обновлённый комментарий'}
  94. original_comment = Comment.objects.get(id=comment.id)
  95. response = client.post(url, data=form_data)
  96. assert response.status_code == HTTPStatus.NOT_FOUND
  97. comment.refresh_from_db()
  98. assert comment.text == original_comment.text
  99. assert comment.author == original_comment.author
  100. assert comment.news == original_comment.news