"""Tests for the client page.""" from selenium.webdriver.common.by import By from selenium.webdriver.remote.webdriver import WebDriver import pytest from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from sshecret.crypto import generate_private_key, generate_public_key_string from tests.integration.types import AdminServer from .helpers.auth import login from .helpers import wait_helpers @pytest.fixture def client_page(ui_admin_server: AdminServer, driver: WebDriver) -> WebDriver: """Log in and show the client page.""" admin_url = ui_admin_server[0] driver = login(ui_admin_server, driver) driver.get(admin_url + "/clients") return driver class TestClientPage: """Test client page.""" def test_client_page_loaded(self, client_page: WebDriver) -> None: """Test that the client page loads.""" # Ensure that the create client button is present client_page.refresh() create_client_button = client_page.find_element(By.ID, "createClientButton") assert create_client_button is not None # Ensure that the table is loaded client_table = client_page.find_element(By.ID, "clientListTable") assert client_table is not None def test_create_client_button(self, client_page: WebDriver) -> None: """Test that the Create Client button works.""" client_page.refresh() create_client_button = client_page.find_element(By.ID, "createClientButton") assert create_client_button is not None create_client_button.click() wait_helpers.wait_for_element_to_be_visisble(client_page, "drawer-create-client-default") add_client_button = client_page.find_element(By.XPATH, "//button[@type='submit']") assert add_client_button.text.lower() == "add client" def test_create_client(self, client_page: WebDriver) -> None: """Test create clients.""" client_page.refresh() private_key = generate_private_key() public_key = generate_public_key_string(private_key.public_key()) create_client_button = client_page.find_element(By.ID, "createClientButton") assert create_client_button is not None create_client_button.click() wait_helpers.wait_for_element_to_be_visisble(client_page, "drawer-create-client-default") drawer = client_page.find_element(By.ID, "drawer-create-client-default") assert drawer is not None name_input = drawer.find_element(By.NAME, "name") assert name_input is not None description_input = drawer.find_element(By.NAME, "description") assert description_input is not None sources_input = drawer.find_element(By.NAME, "sources") assert sources_input is not None public_key_input = drawer.find_element(By.NAME, "public_key") assert public_key_input is not None name_input.send_keys("testuser") description_input.send_keys("Test") sources_input.clear() sources_input.send_keys("0.0.0.0/0, ::/0") public_key_input.send_keys(public_key) validation_field = drawer.find_element(By.ID, "clientPublicKeyValidation") assert validation_field is not None error_message = validation_field.find_elements(By.TAG_NAME, "p") assert len(error_message) == 0 # Submit the request add_client_button = drawer.find_element(By.XPATH, "//button[@type='submit']") assert add_client_button.text.lower() == "add client" add_client_button.click() client_appeared = wait_helpers.wait_for_element_with_text(client_page, "td", "testuser") assert client_appeared is not False def test_delete_client(self, client_page: WebDriver) -> None: """Test deletion of a test client.""" self.test_create_client(client_page) client_page.refresh() client_field = client_page.find_element(By.XPATH, "//tr/td[contains(text(), 'testuser')]") assert client_field is not None row = client_field.find_element(By.XPATH, "./..") assert row is not None assert row.tag_name == "tr" row_id = row.get_attribute("id") assert row_id is not None print(row_id) client_id = row_id[7:] wait_helpers.wait_for_element_to_be_visisble(client_page, row_id) delete_button = client_page.find_element(By.ID, f"deleteClientButton-{client_id}") assert delete_button is not None delete_button.click() drawer_name = f"drawer-delete-{row_id}" wait_helpers.wait_for_element_to_be_visisble(client_page, drawer_name) drawer = client_page.find_element(By.ID, drawer_name) assert drawer is not None confirm_button = drawer.find_element(By.XPATH, "//button[contains(text(), 'Yes, delete the client')]") assert confirm_button is not None confirm_button.click() WebDriverWait(client_page, 10).until( EC.none_of( EC.presence_of_element_located((By.ID, row_id)) ) ) def test_update_client(self, client_page: WebDriver) -> None: """Test updating a client.""" self.test_create_client(client_page) client_page.refresh() client_field = client_page.find_element(By.XPATH, "//tr/td[contains(text(), 'testuser')]") assert client_field is not None row = client_field.find_element(By.XPATH, "./..") assert row is not None assert row.tag_name == "tr" row_id = row.get_attribute("id") assert row_id is not None print(row_id) client_id = row_id[7:] wait_helpers.wait_for_element_to_be_visisble(client_page, row_id) update_button = client_page.find_element(By.ID, f"updateClientButton-{client_id}") assert update_button is not None update_button.click() drawer_name = f"drawer-update-{row_id}" wait_helpers.wait_for_element_to_be_visisble(client_page, drawer_name) drawer = client_page.find_element(By.ID, drawer_name) description_input = drawer.find_element(By.NAME, "description") description_input.clear() description_input.send_keys("New Description") confirm_button = drawer.find_element(By.XPATH, "//button[contains(text(), 'Update')]") assert confirm_button is not None confirm_button.click() WebDriverWait(client_page, 10).until( EC.text_to_be_present_in_element((By.XPATH, f"//tr[@id='{row_id}']/td[3]"), "New Description") ) def test_delete_from_update_client(self, client_page: WebDriver) -> None: """Test updating a client.""" self.test_create_client(client_page) client_page.refresh() client_field = client_page.find_element(By.XPATH, "//tr/td[contains(text(), 'testuser')]") assert client_field is not None row = client_field.find_element(By.XPATH, "./..") assert row is not None assert row.tag_name == "tr" row_id = row.get_attribute("id") assert row_id is not None print(row_id) client_id = row_id[7:] wait_helpers.wait_for_element_to_be_visisble(client_page, row_id) update_button = client_page.find_element(By.ID, f"updateClientButton-{client_id}") assert update_button is not None update_button.click() drawer_name = f"drawer-update-{row_id}" wait_helpers.wait_for_element_to_be_visisble(client_page, drawer_name) drawer = client_page.find_element(By.ID, drawer_name) description_input = drawer.find_element(By.NAME, "description") description_input.clear() description_input.send_keys("New Description") delete_button = drawer.find_element(By.ID, f"delete-button-{client_id}") assert delete_button is not None delete_button.click() alert = WebDriverWait(client_page, 10).until(lambda d: d.switch_to.alert) assert alert.text == "Are you sure?" alert.accept() WebDriverWait(client_page, 10).until( EC.none_of( EC.presence_of_element_located((By.ID, row_id)) ) )