Tests/tests/api/test_posts.py
2026-01-19 23:32:11 +04:00

206 lines
8.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import Any, Dict
import pytest
import allure
import logging
from tests.api.utils.api_client import APIClient
logger = logging.getLogger(__name__)
# Фикстура для получения списка постов из API
@pytest.fixture(scope="function")
def list_of_posts(api_client):
with allure.step("Get all posts"):
resp = api_client.get_all_posts()
assert resp.status_code == 200
return resp.json()
# Отдельные классы вариант
@allure.feature("Posts")
@allure.story("Guest permissions")
class TestGuestPosts:
"""Тестирование операций с постами под гостем (неавторизованный доступ)"""
@allure.title("Guest: Creating new posts - should fail")
def test_guest_posts_creating(self, api_client, api_post_data):
with allure.step("Logged as guest"):
logger.info("Guest trying to create posts")
for post in api_post_data:
resp = api_client.create_post(post)
assert resp.status_code == 401
@allure.title("Guest: Update posts - should fail")
def test_guest_posts_update(self, api_client, list_of_posts):
posts = list_of_posts
with allure.step("Guest trying to change posts data"):
for post in posts:
logger.info(f"Guest changing post: {post['title']}")
post["title"] = "Changed by guest"
post["description"] = "Changed by guest"
post["content"] = "Changed by guest"
resp = api_client.update_post(post["id"], post)
assert resp.status_code == 401
@allure.title("Guest: Get all posts - should succeed")
def test_guest_get_all_posts(self, api_client):
with allure.step("Guest getting all posts"):
logger.info("Guest getting all posts")
resp = api_client.get_all_posts()
assert resp.status_code == 200
@allure.title("Guest: Get post by ID - should succeed")
def test_guest_get_post_by_id(self, api_client, list_of_posts):
posts = list_of_posts
if posts:
post_id = posts[0]["id"]
with allure.step(f"Guest getting post with ID {post_id}"):
logger.info(f"Guest getting post: {post_id}")
resp = api_client.get_post(post_id)
assert resp.status_code == 200
@allure.title("Guest: Delete posts - should fail")
def test_guest_posts_deleting(self, api_client, list_of_posts):
posts = list_of_posts
with allure.step("Guest trying to delete posts"):
for post in posts:
logger.info(f"Guest deleting post: {post['title']}")
resp = api_client.delete_post(post["id"])
assert resp.status_code == 401
@allure.feature("Posts")
@allure.story("Admin permissions")
class TestAdminPosts:
"""Тестирование операций с постами под администратором"""
@allure.title("Admin: Creating new posts - should succeed")
def test_admin_posts_creating(self, auth_admin, api_post_data, api_user_data):
id = ''
with allure.step("Logged as admin"):
pass
with allure.step("Create new user"):
user = api_user_data[0]
resp = auth_admin.create_user(user)
assert resp.status_code == 201
id = resp.json()['id']
with allure.step("Create posts"):
logger.info("Admin creating posts")
for post in api_post_data:
post['userId'] = id
resp = auth_admin.create_post(post)
assert resp.status_code == 201
@allure.title("Admin: Update posts - should succeed")
def test_admin_posts_update(self, auth_admin, list_of_posts):
posts = list_of_posts
with allure.step("Admin changing posts data"):
for post in posts:
logger.info(f"Admin changing post: {post['title']}")
new_post: Dict[str, Any] = {}
new_post["title"] = "Changed by admin."
new_post["description"] = "Changed by admin. Test data."
new_post["content"] = "Changed by admin."
resp = auth_admin.update_post(post["id"], new_post)
assert resp.status_code == 200
@allure.title("Admin: Get all posts - should succeed")
def test_admin_get_all_posts(self, auth_admin):
with allure.step("Admin getting all posts"):
logger.info("Admin getting all posts")
resp = auth_admin.get_all_posts()
assert resp.status_code == 200
@allure.title("Admin: Get post by ID - should succeed")
def test_admin_get_post_by_id(self, auth_admin, list_of_posts):
posts = list_of_posts
if posts:
post_id = posts[0]["id"]
with allure.step(f"Admin getting post with ID {post_id}"):
logger.info(f"Admin getting post: {post_id}")
resp = auth_admin.get_post(post_id)
assert resp.status_code == 200
@allure.title("Admin: Delete posts - should succeed")
def test_admin_posts_deleting(self, auth_admin, list_of_posts):
posts = list_of_posts
id = posts[0]['userId']
with allure.step("Admin deleting posts"):
for post in posts:
logger.info(f"Admin deleting post: {post['title']}")
resp = auth_admin.delete_post(post["id"])
assert resp.status_code == 200
with allure.step("Delete user"):
resp = auth_admin.delete_user(id)
assert resp.status_code == 200
@allure.feature("Posts")
@allure.story("Regular user permissions")
class TestRegularUserPosts:
"""Тестирование операций с постами под обычным пользователем"""
@allure.title("User: Creating new posts - should succeed")
def test_user_posts_creating(self, auth_user: APIClient, api_post_data):
id = auth_user.get_all_users().json()[0]['id']
with allure.step("Logged as user"):
pass
with allure.step("Create new posts"):
logger.info("User creating posts")
for post in api_post_data:
post['userId'] = id
resp = auth_user.create_post(post)
assert resp.status_code == 201
@allure.title("User: Update posts - should succeed")
def test_user_posts_update(self, auth_user, list_of_posts):
posts = list_of_posts
with allure.step("User changing posts data"):
for post in posts:
logger.info(f"User changing post: {post['title']}")
post["title"] = "Changed by user"
post["description"] = "Changed by user"
post["content"] = "Changed by user"
resp = auth_user.update_post(post["id"], post)
assert resp.status_code == 200
@allure.title("User: Get all posts - should succeed")
def test_user_get_all_posts(self, auth_user):
with allure.step("User getting all posts"):
logger.info("User getting all posts")
resp = auth_user.get_all_posts()
assert resp.status_code == 200
@allure.title("User: Get post by ID - should succeed")
def test_user_get_post_by_id(self, auth_user, list_of_posts):
posts = list_of_posts
if posts:
post_id = posts[0]["id"]
with allure.step(f"User getting post with ID {post_id}"):
logger.info(f"User getting post: {post_id}")
resp = auth_user.get_post(post_id)
assert resp.status_code == 200
@allure.title("User: Delete posts - should succeed")
def test_user_posts_deleting(self, auth_user, list_of_posts):
posts = list_of_posts
with allure.step("User deleting posts"):
for post in posts:
logger.info(f"User deleting post: {post['title']}")
resp = auth_user.delete_post(post["id"])
assert resp.status_code == 200