urlpatterns = [path('author/', views.AboutAuthorView.as_view(), name='author'),
path('contact/', views.AboutContactView.as_view(), name='contact'),
]
from http import HTTPStatus
from django.test import Client, TestCase
class StaticURLTests(TestCase):
def setUp(self) -> None:
self.guest_client = Client()
def test_static_page(self) -> None:
"""Страница доступка по URL."""
pages: tuple = ('/about/author/', '/about/tech/')
for page in pages:
response = self.guest_client.get(page)
error_name: str = f'Ошибка: нет доступа до страницы {page}'
self.assertEqual(response.status_code, HTTPStatus.OK, error_name)
def test_urls_uses_correct_template(self) -> None:
"""URL-адрес использует соответствующий шаблон."""
templates_url_names: dict = {
'/about/author/': 'about/author.html',
'/about/tech/': 'about/tech.html',
}
for adress, template in templates_url_names.items():
with self.subTest(adress=adress):
response = self.guest_client.get(adress)
error_name: str = f'Ошибка: {adress} ожидал шаблон {template}'
self.assertTemplateUsed(response, template, error_name)
from django.urls import path
from django.conf.urls import handler404, handler500
from . import views
handler404 = 'posts.views.error_404_view'
handler500 = 'posts.views.error_500_view'
app_name = 'posts'
urlpatterns = [path('', views.index, name='index'),
path('group/<slug:slug>/', views.group_posts,
name='group_list'),
path('create/', views.post_create, name='post_create'),
path('posts/<int:post_id>/', views.post_detail,
name='post_detail'),
path('profile/<str:username>/', views.profile, name='profile'),
path('posts/<int:post_id>/edit/', views.post_edit,
name='post_edit'),
]
from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from http import HTTPStatus
from ..models import Group, Post
User = get_user_model()
class PostURLTests(TestCase):
def setUp(self):
self.guest_client = Client()
self.user = User.objects.create_user(username='auth')
self.authorized_client = Client()
self.authorized_client.force_login(self.user)
self.post = Post.objects.create(
author=self.user,
text='Тестовое описание поста')
self.group = Group.objects.create(
title='Тестовая группа',
slug='test_group',
description='Тестовое описание')
def test_urls_guest_client(self):
"""Доступ неавторизованного пользователя"""
pages: tuple = ('/',
f'/group/{self.group.slug}/',
f'/profile/{self.user.username}/',
f'/posts/{self.post.id}/')
for page in pages:
response = self.guest_client.get(page)
error_name = f'Ошибка: нет доступа до страницы {page}'
self.assertEqual(response.status_code, HTTPStatus.OK, error_name)
def test_urls_redirect_guest_client(self):
"""Редирект неавторизованного пользователя"""
url1 = '/auth/login/?next=/create/'
url2 = f'/auth/login/?next=/posts/{self.post.id}/edit/'
pages = {'/create/': url1,
f'/posts/{self.post.id}/edit/': url2}
for page, value in pages.items():
response = self.guest_client.get(page)
self.assertRedirects(response, value)
@login_required
def post_create(request):
template = 'posts/create_post.html'
form = PostForm(request.POST or None)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
return redirect('posts:profile', request.user)
return render(request, template, {'form': form})
def test_urls_authorized_client(self):
"""Доступ авторизованного пользователя"""
pages: tuple = ('/create/',
f'/posts/{self.post.id}/edit/')
for page in pages:
response = self.authorized_client.get(page)
error_name = f'Ошибка: нет доступа до страницы {page}'
self.assertEqual(response.status_code, HTTPStatus.OK, error_name)
def test_urls_uses_correct_template(self):
"""URL-адрес использует соответствующий шаблон."""
templates_url_names: dict = {
'/': 'posts/index.html',
f'/group/{self.group.slug}/': 'posts/group_list.html',
f'/profile/{self.user.username}/': 'posts/profile.html',
f'/posts/{self.post.id}/': 'posts/post_detail.html',
'/create/': 'posts/create_post.html',
f'/posts/{self.post.id}/edit/': 'posts/create_post.html'}
for adress, template in templates_url_names.items():
with self.subTest(adress=adress):
response = self.authorized_client.get(adress)
error_name = f'Ошибка: {adress} ожидал шаблон {template}'
self.assertTemplateUsed(response, template, error_name)