Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions lesson_14/homework_14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Ваша команда та ви розробляєте систему входу для веб-додатка,
і вам потрібно реалізувати тести на функцію для логування подій в системі входу.
Дано функцію, напишіть набір тестів для неї.
"""

import logging
import pytest


LOG_FILE = 'login_system.log'

logging.basicConfig(
filename=LOG_FILE,
level=logging.INFO,
format = '%(asctime)s - %(levelname)s - %(message)s',
force=True
)

def log_event(username: str, status: str):
"""
Логує подію входу в систему.

username: Ім'я користувача, яке входить в систему.

status: Статус події входу:

* success - успішний, логується на рівні інфо
* expired - пароль застаріває і його слід замінити, логується на рівні warning
* failed - пароль невірний, логується на рівні error
"""
log_message = f"Login event - Username: {username}, Status: {status}"

# Створення та налаштування логера

logger = logging.getLogger("log_event")

# Логування події
if status == "success":
logger.info(log_message)
elif status == "expired":
logger.warning(log_message)
else:
logger.error(log_message)


@pytest.mark.parametrize("username, status, expected_level", [

("admin", "expired", "WARNING"),
("president", "success", "INFO"),
("Mario", "failed", "ERROR")
])

def test_log_event(username: str, status: str, expected_level: str):
log_event(username, status)
with open("login_system.log", "r") as log_file:
log = log_file.readlines()
assert expected_level in log[-1]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

це терба виносити з під with