268 lines
16 KiB
Python
268 lines
16 KiB
Python
"""Модуль контейнера для отображения сертификата во вкладке 'Сертификаты'.
|
||
|
||
Содержит класс для работы с формой для отображения данных
|
||
сертификата во вкладке 'Сертификаты' через Playwright.
|
||
"""
|
||
|
||
from pathlib import Path
|
||
import os
|
||
from playwright.sync_api import Page
|
||
from tools.logger import get_logger
|
||
from locators.certificate_locators import CertificateLocators
|
||
from elements.text_input_element import TextInput
|
||
from elements.text_element import Text
|
||
from elements.tooltip_button_element import TooltipButton
|
||
from components.base_component import BaseComponent
|
||
|
||
logger = get_logger("VIEW_CRTIFICATE_FORM")
|
||
|
||
|
||
class ViewCertificateForm(BaseComponent):
|
||
"""Компонент формы для отображения данных сертификата во вкладке 'Сертификаты'.
|
||
|
||
Предоставляет методы для взаимодействия с элементами
|
||
формы для отображения данных сертификата во вкладке 'Сертификаты'.
|
||
"""
|
||
|
||
def __init__(self, page: Page):
|
||
"""Инициализирует компонент формы для отображения данных сертификата во вкладке 'Сертификаты'.
|
||
|
||
Args:
|
||
page: Экземпляр страницы Playwright.
|
||
"""
|
||
|
||
super().__init__(page)
|
||
|
||
button_locator = page.locator(CertificateLocators.FORM_CONTAINER).get_by_role("button")
|
||
self.button_export = TooltipButton(page, button_locator, "button_export")
|
||
|
||
# поля блока 'Основная информация'
|
||
base_info_title_locator = page.locator(CertificateLocators.BLOCK_HEADER_TEXT). \
|
||
filter(has_text='Основная информация')
|
||
self.base_info_title = Text(page, base_info_title_locator, "base_info_title")
|
||
|
||
self.base_info_version = TextInput(page, CertificateLocators.FIELD_VERSION, "base_info_version_field")
|
||
self.base_info_serial_number = TextInput(page, CertificateLocators.FIELD_SERIAL_NUMBER,
|
||
"base_info_serial_number_field")
|
||
self.base_info_signature_algorithm = TextInput(page, CertificateLocators.FIELD_SIGNATURE_ALGORITHM,
|
||
"base_info_signature_algorithm_field")
|
||
|
||
# поля блока 'Срок действия'
|
||
validity_title_locator = page.locator(CertificateLocators.BLOCK_HEADER_TEXT). \
|
||
filter(has_text='Срок действия')
|
||
self.validity_title = Text(page, validity_title_locator, "validity_title")
|
||
self.validity = TextInput(page, CertificateLocators.FIELD_VALIDITY, "validity_validity_field")
|
||
self.validity_not_before = TextInput(page, CertificateLocators.FIELD_NOT_BEFORE, "validity_not_before_field")
|
||
self.validity_not_after = TextInput(page, CertificateLocators.FIELD_NOT_AFTER, "validity_not_after_field")
|
||
|
||
# поля блока 'Издатель / Субъект'
|
||
subject_title_locator = page.locator(CertificateLocators.BLOCK_HEADER_TEXT). \
|
||
filter(has_text='Издатель / Субъект')
|
||
self.subject_title = Text(page, subject_title_locator, "subject_title")
|
||
self.subject_cert_name = TextInput(page, CertificateLocators.FIELD_CERT_NAME, "subject_cert_name_field")
|
||
self.subject_organization = TextInput(page, CertificateLocators.FIELD_ORGANIZATION,
|
||
"subject_organization_field")
|
||
self.subject_org_unit = TextInput(page, CertificateLocators.FIELD_ORG_UNIT, "subject_org_unit_field")
|
||
self.subject_country = TextInput(page, CertificateLocators.FIELD_COUNTRY, "subject_country_field")
|
||
self.subject_state = TextInput(page, CertificateLocators.FIELD_STATE, "subject_state_field")
|
||
self.subject_location = TextInput(page, CertificateLocators.FIELD_LOC, "subject_location_field")
|
||
|
||
# поля блока 'Ключ и отпечаток'
|
||
fingerprint_title_locator = page.locator(CertificateLocators.BLOCK_HEADER_TEXT). \
|
||
filter(has_text='Ключ и отпечаток')
|
||
self.fingerprint_title = Text(page, fingerprint_title_locator, "fingerprint_title")
|
||
self.fingerprint_public_key = TextInput(page, CertificateLocators.FIELD_PUBLIC_KEY_FINGERPRINT,
|
||
"fingerprint_public_key_field")
|
||
self.fingerprint_algorithm = TextInput(page, CertificateLocators.FIELD_ALGORITHM,
|
||
"fingerprint_algorithm_field")
|
||
self.fingerprint_key_size = TextInput(page, CertificateLocators.FIELD_KEY_SIZE,
|
||
"fingerprint_key_size_field")
|
||
|
||
# Действия:
|
||
def get_certificate(self) -> dict:
|
||
""" Возвращает значания полей отображаемого сертификата"""
|
||
|
||
certificate = {}
|
||
|
||
base_info_dict = {}
|
||
val = self.base_info_version.get_input_value().strip()
|
||
base_info_dict.update({"version": val})
|
||
val = self.base_info_serial_number.get_input_value().strip()
|
||
base_info_dict.update({"serialNumber": val})
|
||
val = self.base_info_signature_algorithm.get_input_value().strip()
|
||
base_info_dict.update({"signatureAlgorithm": val})
|
||
|
||
validity_dict = {}
|
||
val = self.validity.get_input_value().strip()
|
||
validity_dict.update({"status": val})
|
||
val = self.validity_not_before.get_input_value().strip()
|
||
validity_dict.update({"notBefore": val})
|
||
val = self.validity_not_after.get_input_value().strip()
|
||
validity_dict.update({"notAfter": val})
|
||
|
||
fingerprint_dict = {}
|
||
val = self.fingerprint_public_key.get_input_value().strip()
|
||
fingerprint_dict.update({"publicKeyFingerprint": val})
|
||
val = self.fingerprint_algorithm.get_input_value().strip()
|
||
fingerprint_dict.update({"algorithm": val})
|
||
val = self.fingerprint_key_size.get_input_value().strip()
|
||
fingerprint_dict.update({"keySize": int(val)})
|
||
|
||
subject_dict = {}
|
||
if self.subject_country.get_locator().count() != 0:
|
||
val = self.subject_country.get_input_value().strip()
|
||
subject_dict.update({"C": val})
|
||
if self.subject_state.get_locator().count() != 0:
|
||
val = self.subject_state.get_input_value().strip()
|
||
subject_dict.update({"ST": val})
|
||
if self.subject_location.get_locator().count() != 0:
|
||
val = self.subject_location.get_input_value().strip()
|
||
subject_dict.update({"L": val})
|
||
if self.subject_organization.get_locator().count() != 0:
|
||
val = self.subject_organization.get_input_value().strip()
|
||
subject_dict.update({"O": val})
|
||
if self.subject_org_unit.get_locator().count() != 0:
|
||
val = self.subject_org_unit.get_input_value().strip()
|
||
subject_dict.update({"OU": val})
|
||
if self.subject_cert_name.get_locator().count() != 0:
|
||
val = self.subject_cert_name.get_input_value().strip()
|
||
subject_dict.update({"CN": val})
|
||
|
||
certificate["baseInfo"] = base_info_dict
|
||
certificate["validity"] = validity_dict
|
||
certificate["fingerprint"] = fingerprint_dict
|
||
certificate["subject"] = subject_dict
|
||
|
||
return certificate
|
||
|
||
|
||
def export_certificate(self) -> str:
|
||
"""Нажатие кнопки 'Экспорт сертификата (CA)' в форме отображения сертификата и
|
||
скачивание текущего корневого сертификата.
|
||
|
||
Returns:
|
||
str : Полный путь к скачанному файлу.
|
||
"""
|
||
|
||
path_to_download = Path.home() / "Downloads"
|
||
|
||
self.button_export.check_visibility("Export certificate button is missing")
|
||
|
||
with self.page.expect_download() as download_info:
|
||
self.button_export.click()
|
||
download = download_info.value
|
||
|
||
download_error = download.failure()
|
||
assert not download_error, f"Download certificate error: {download_error}"
|
||
|
||
file_to_download = os.path.join(path_to_download, download.suggested_filename)
|
||
download.save_as(file_to_download)
|
||
|
||
assert os.path.exists(file_to_download), f"The certificate file '{file_to_download}' not found"
|
||
assert os.path.getsize(file_to_download) > 0, f"The certificate file '{file_to_download}' is empty"
|
||
|
||
return file_to_download
|
||
|
||
|
||
def _get_label_for_input_field(self, field_locator: str) -> str:
|
||
div_loc = f"//div[contains(@class, 'flex')][.{field_locator}]"
|
||
label = self.page.locator(div_loc).locator("//preceding-sibling::div[1]").locator("//input")
|
||
return label.input_value()
|
||
|
||
# Проверки:
|
||
def check_content(self):
|
||
"""Проверяет наличие и корректность всех элементов формы."""
|
||
|
||
self.button_export.check_visibility("Export certificate button is missing")
|
||
self.button_export.check_tooltip_with_text("Экспорт сертификата CA")
|
||
|
||
# проверка наличия всех полей формы
|
||
self.base_info_title.check_visibility("Title 'Основная информация' is missing")
|
||
|
||
version_label = self._get_label_for_input_field(CertificateLocators.FIELD_VERSION).strip()
|
||
assert version_label == 'ВЕРСИЯ (Version)', f"Unexpected field name {version_label} has got"
|
||
self.base_info_version.check_visibility("Field version value is missing")
|
||
|
||
serial_number_label = self._get_label_for_input_field(CertificateLocators.FIELD_SERIAL_NUMBER).strip()
|
||
assert serial_number_label == 'СЕРИЙНЫЙ НОМЕР (Serial Number)',\
|
||
f"Unexpected field name {serial_number_label} has got"
|
||
self.base_info_serial_number.check_visibility("Field serial number value is missing")
|
||
|
||
signature_algorithm_label = self._get_label_for_input_field(CertificateLocators.FIELD_SIGNATURE_ALGORITHM). \
|
||
strip()
|
||
assert signature_algorithm_label == 'АЛГОРИТМ ПОДПИСИ (Signature Algorithm)',\
|
||
f"Unexpected field name {signature_algorithm_label} has got"
|
||
self.base_info_signature_algorithm.check_visibility("Field signature algorithm value is missing")
|
||
|
||
self.validity_title.check_visibility("Title 'Срок действия' is missing")
|
||
|
||
validity_label = self._get_label_for_input_field(CertificateLocators.FIELD_VALIDITY).strip()
|
||
assert validity_label == 'СТАТУС (Validity)',\
|
||
f"Unexpected field name {validity_label} has got"
|
||
self.validity.check_visibility("Field validity value is missing")
|
||
|
||
validity_not_before_label = self._get_label_for_input_field(CertificateLocators.FIELD_NOT_BEFORE).strip()
|
||
assert validity_not_before_label == 'ДЕЙСТВИТЕЛЕН С (Not Before)',\
|
||
f"Unexpected field name {validity_not_before_label} has got"
|
||
self.validity_not_before.check_visibility("Field validity not before value is missing")
|
||
|
||
validity_not_after_label = self._get_label_for_input_field(CertificateLocators.FIELD_NOT_AFTER).strip()
|
||
assert validity_not_after_label == 'ДЕЙСТВИТЕЛЕН ДО (Not After)',\
|
||
f"Unexpected field name {validity_not_after_label} has got"
|
||
self.validity_not_after.check_visibility("Field validity not after value is missing")
|
||
|
||
self.subject_title.check_visibility("Title 'Издатель / Субъект' is missing")
|
||
|
||
if self.page.locator(CertificateLocators.FIELD_CERT_NAME).count() != 0:
|
||
cert_name_label = self._get_label_for_input_field(CertificateLocators.FIELD_CERT_NAME).strip()
|
||
assert cert_name_label == 'ИМЯ СЕРТИФИКАТА (CN)',\
|
||
f"Unexpected field name {cert_name_label} has got"
|
||
self.subject_cert_name.check_visibility("Field certificate name value is missing")
|
||
|
||
if self.page.locator(CertificateLocators.FIELD_ORGANIZATION).count() != 0:
|
||
organization_label = self._get_label_for_input_field(CertificateLocators.FIELD_ORGANIZATION).strip()
|
||
assert organization_label == 'ОРГАНИЗАЦИЯ (О)',\
|
||
f"Unexpected field name {organization_label} has got"
|
||
self.subject_organization.check_visibility("Field organization value is missing")
|
||
|
||
if self.page.locator(CertificateLocators.FIELD_ORG_UNIT).count() != 0:
|
||
org_unit_label = self._get_label_for_input_field(CertificateLocators.FIELD_ORG_UNIT).strip()
|
||
assert org_unit_label == 'ПОДРАЗДЕЛЕНИЕ (OU)',\
|
||
f"Unexpected field name {org_unit_label} has got"
|
||
self.subject_org_unit.check_visibility("Field organization unit value is missing")
|
||
|
||
if self.page.locator(CertificateLocators.FIELD_COUNTRY).count() != 0:
|
||
country_label = self._get_label_for_input_field(CertificateLocators.FIELD_COUNTRY).strip()
|
||
assert country_label == 'СТРАНА (С)',\
|
||
f"Unexpected field name {country_label} has got"
|
||
self.subject_country.check_visibility("Field country value is missing")
|
||
|
||
if self.page.locator(CertificateLocators.FIELD_STATE).count() != 0:
|
||
state_label = self._get_label_for_input_field(CertificateLocators.FIELD_STATE).strip()
|
||
assert state_label == 'РЕГИОН / ОБЛАСТЬ (ST)',\
|
||
f"Unexpected field name {state_label} has got"
|
||
self.subject_state.check_visibility("Field state value is missing")
|
||
|
||
if self.page.locator(CertificateLocators.FIELD_LOC).count() != 0:
|
||
location_label = self._get_label_for_input_field(CertificateLocators.FIELD_LOC).strip()
|
||
assert location_label == 'ГОРОД (l)',\
|
||
f"Unexpected field name {location_label} has got"
|
||
self.subject_location.check_visibility("Field location value is missing")
|
||
|
||
self.fingerprint_title.check_visibility("Title 'Ключ и отпечаток' is missing")
|
||
|
||
public_key_label = self._get_label_for_input_field(CertificateLocators.FIELD_PUBLIC_KEY_FINGERPRINT).strip()
|
||
assert public_key_label == 'ПУБЛИЧНЫЙ ОТПЕЧАТОК (PublicKeyFingerprint)',\
|
||
f"Unexpected field name {public_key_label} has got"
|
||
self.fingerprint_public_key.check_visibility("Field public key value is missing")
|
||
|
||
algorithm_label = self._get_label_for_input_field(CertificateLocators.FIELD_ALGORITHM).strip()
|
||
assert algorithm_label == 'АЛГОРИТМ (Algorithm)',\
|
||
f"Unexpected field name {algorithm_label} has got"
|
||
self.fingerprint_algorithm.check_visibility("Field algorithm value is missing")
|
||
|
||
key_size_label = self._get_label_for_input_field(CertificateLocators.FIELD_KEY_SIZE).strip()
|
||
assert key_size_label == 'ДЛИНА КЛЮЧА (Key Size)',\
|
||
f"Unexpected field name {key_size_label} has got"
|
||
self.fingerprint_key_size.check_visibility("Field key size value is missing")
|