• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5from __future__ import annotations
6
7import logging
8from typing import TYPE_CHECKING, Final
9
10from crossbench.benchmarks.loading.config.blocks import ActionBlock
11
12if TYPE_CHECKING:
13  from crossbench.benchmarks.loading.page.interactive import InteractivePage
14  from crossbench.cli.config.secrets import Secret, SecretType
15  from crossbench.runner.run import Run
16
17
18class BaseLoginBlock(ActionBlock):
19  LABEL: Final[str] = "login"
20
21  def validate(self) -> None:
22    super().validate()
23    assert self.index == 0, (
24        f"Login block has to be the first, but got {self.index}")
25
26  @property
27  def is_login(self) -> bool:
28    return True
29
30  def get_secret(
31      self,
32      run: Run,
33      page: InteractivePage,
34      type: SecretType  # pylint: disable=redefined-builtin
35  ) -> Secret:
36    logging.debug("Looking up secrets {%s} for page %s", type, page)
37    if secret := page.secrets.get(type):
38      return secret
39    if secret := run.browser.secrets.get(type):
40      return secret
41    raise LookupError(f"Could not find any secret for {repr(str(type))} "
42                      f"on {page} or on {run.browser}")
43
44  def is_logged_in(self,
45                   run: Run,
46                   secret: Secret,
47                   strict: bool = False) -> bool:
48    return run.browser.is_logged_in(secret, strict)
49
50
51class PresetLoginBlock(BaseLoginBlock):
52
53  def validate_actions(self) -> None:
54    """Skip validation, since PresetLoginBlocks have an unknown number
55    of actions."""
56
57  def __len__(self) -> int:
58    """LoginBlocks will have at least one action. Given they're not known
59    upfront we set this to 1. This also ensures that bool(login_block) is
60    True."""
61    return 1
62