async def test_create_token(driver: webdriver.Chrome, selenium_config: SeleniumConfig) -> None: cookie = State(token=selenium_config.token).as_cookie() driver.header_overrides = {"Cookie": f"{COOKIE_NAME}={cookie}"} tokens_url = urljoin(selenium_config.url, "/auth/tokens") driver.get(tokens_url) tokens_page = TokensPage(driver) assert tokens_page.get_tokens(TokenType.user) == [] session_tokens = tokens_page.get_tokens(TokenType.session) assert len(session_tokens) == 1 assert session_tokens[0].token == selenium_config.token.key # Drop our cookie in favor of the one the browser is now sending, since # the browser one contains a CSRF token that will be required for token # creation. del driver.header_overrides create_modal = await tokens_page.click_create_token() create_modal.set_token_name("test token") await create_modal.submit() new_token_modal = tokens_page.get_new_token_modal() assert new_token_modal.token.startswith("gt-") new_token_modal.dismiss() user_tokens = tokens_page.get_tokens(TokenType.user) assert len(user_tokens) == 1 assert user_tokens[0].name == "test token"
async def selenium_config( tmp_path: Path, driver: webdriver.Chrome, empty_database: None ) -> AsyncIterator[SeleniumConfig]: """Start a server for Selenium tests. The server will be automatically stopped at the end of the test. The Selenium web driver will be automatically configured with a valid authentication token in a cookie. Returns ------- config : `tests.support.selenium.SeleniumConfig` Configuration information for the server. """ settings_path = build_settings(tmp_path, "selenium") config_dependency.set_settings_path(str(settings_path)) async with run_app(tmp_path, settings_path) as config: cookie = await State(token=config.token).as_cookie() driver.header_overrides = {"Cookie": f"{COOKIE_NAME}={cookie}"} # The synthetic cookie doesn't have a CSRF token, so we want to # replace it with a real cookie. Do this by visiting the top-level # page of the UI and waiting for the token list to appear, which will # trigger fleshing out the state, and then dropping the header # override for subsequent calls so that the cookie set in the browser # will be used. driver.get(urljoin(config.url, "/auth/tokens/")) tokens_page = TokensPage(driver) tokens_page.get_tokens(TokenType.session) del driver.header_overrides yield config