test_content.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from django.contrib.auth import get_user_model
  2. from news.forms import CommentForm
  3. from .test_constants import NEWS_COUNT_ON_HOME_PAGE
  4. User = get_user_model()
  5. def test_news_count_and_order(client, home_url, initial_news):
  6. response = client.get(home_url)
  7. object_list = response.context['object_list']
  8. news_count = len(object_list)
  9. assert news_count == NEWS_COUNT_ON_HOME_PAGE
  10. all_dates = [news.date for news in object_list]
  11. sorted_dates = sorted(all_dates, key=lambda x: x, reverse=True)
  12. assert all_dates == sorted_dates
  13. def test_comments_order(client, detail_url, initial_data):
  14. news_obj, _ = initial_data
  15. response = client.get(detail_url)
  16. assert 'news' in response.context
  17. news = response.context['news']
  18. all_comments = news.comment_set.all()
  19. all_dates = [comment.created for comment in all_comments]
  20. sorted_dates = sorted(all_dates, key=lambda x: x)
  21. assert all_dates == sorted_dates
  22. def test_anonymous_client_has_no_form(client, detail_url, initial_data):
  23. news_obj, _ = initial_data
  24. response = client.get(detail_url)
  25. assert 'form' not in response.context
  26. def test_authorized_client_has_form(client, detail_url, initial_data):
  27. news_obj, author = initial_data
  28. client.force_login(author)
  29. response = client.get(detail_url)
  30. assert 'form' in response.context
  31. assert isinstance(response.context['form'], CommentForm)