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

133 lines
6 KiB
Python
Raw 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.

import pytest
import allure
import logging
from tests.api.utils.api_client import APIClient
logger = logging.getLogger(__name__)
# Фикстура для получения списка пользователей
@pytest.fixture(scope="class")
def list_of_users(api_client):
with allure.step("Get all users"):
resp = api_client.get_all_users()
assert resp.status_code == 200
return resp.json()
# Тест для гостя (неавторизованный пользователь)
@allure.story("Guest permissions")
class TestGuestUsers:
"""Тестирование операций с пользователями под гостем (неавторизованный доступ)"""
@allure.title("Guest: Creating new user - should fail")
def test_guest_users_creating(self, api_client: APIClient, api_user_data):
assert api_client.logged_in == False
with allure.step("Logged as guest - trying to create users"):
logger.info("Guest trying to create users")
for user in api_user_data:
resp = api_client.create_user(user)
assert resp.status_code == 401, \
f"Guest should not be able to create users. Got {resp.status_code}"
@allure.title("Guest: Update user - should fail")
def test_guest_users_update(self, api_client: APIClient, list_of_users):
users = list_of_users
with allure.step("Guest trying to change users data"):
for user in users:
logger.info(f"Guest changing user: {user['username']}")
user["motto"] = "Changed by guest"
resp = api_client.update_user(user["id"], user)
assert resp.status_code == 401, \
f"Guest should not be able to update users. Got {resp.status_code}"
@allure.title("Guest: Deleting users - should fail")
def test_guest_users_deleting(self, api_client: APIClient, list_of_users):
users = list_of_users
with allure.step("Guest trying to delete users"):
for user in users:
logger.info(f"Guest deleting user: {user['username']}")
resp = api_client.delete_user(user["id"])
assert resp.status_code == 401, \
f"Guest should not be able to delete users. Got {resp.status_code}"
# Тест для администратора
@allure.story("Admin permissions")
class TestAdminUsers:
"""Тестирование операций с пользователями под администратором"""
@allure.title("Admin: Creating new user - should succeed")
def test_admin_users_creating(self, auth_admin, api_user_data):
with allure.step("Logged as admin - creating users"):
logger.info("Admin creating users")
for user in api_user_data:
resp = auth_admin.create_user(user)
assert resp.status_code == 201, \
f"Admin should be able to create users. Got {resp.status_code}"
@allure.title("Admin: Update user - should succeed")
def test_admin_users_update(self, auth_admin, list_of_users):
users = list_of_users
with allure.step("Admin changing users data"):
for user in users:
logger.info(f"Admin changing user: {user['username']}")
user["motto"] = "Changed by admin"
user["password"] = "SomeRandomPass1205"
resp = auth_admin.update_user(user["id"], user)
assert resp.status_code == 200, \
f"Admin should be able to update users. Got {resp.status_code}"
@allure.title("Admin: Deleting users - should succeed")
def test_admin_users_deleting(self, auth_admin, list_of_users):
users = list_of_users
with allure.step("Admin deleting users"):
for user in users:
logger.info(f"Admin deleting user: {user['username']}")
resp = auth_admin.delete_user(user["id"])
assert resp.status_code == 200, \
f"Admin should be able to delete users. Got {resp.status_code}"
# Тест для обычного пользователя
@allure.story("Regular user permissions")
class TestRegularUserUsers:
"""Тестирование операций с пользователями под обычным пользователем"""
@allure.title("User: Creating new user - should succeed")
def test_user_users_creating(self, auth_user, api_user_data):
with allure.step("Logged as regular user - creating users"):
logger.info("Regular user creating users")
for user in api_user_data:
resp = auth_user.create_user(user)
assert resp.status_code == 201, \
f"Regular user should be able to create users. Got {resp.status_code}"
@allure.title("User: Update user - should succeed")
def test_user_users_update(self, auth_user, list_of_users):
users = list_of_users
with allure.step("Regular user changing users data"):
for user in users:
logger.info(f"Regular user changing: {user['username']}")
user["motto"] = "Changed by regular user"
user["password"] = "SomeRandomPass1205"
resp = auth_user.update_user(user["id"], user)
assert resp.status_code == 200, \
f"Regular user should be able to update users. Got {resp.status_code}"
@allure.title("User: Deleting users - should succeed")
def test_user_users_deleting(self, auth_user, list_of_users):
users = list_of_users
with allure.step("Regular user deleting users"):
for user in users:
logger.info(f"Regular user deleting: {user['username']}")
resp = auth_user.delete_user(user["id"])
assert resp.status_code == 200, \
f"Regular user should be able to delete users. Got {resp.status_code}"