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 5import pathlib 6from unittest import mock 7 8from crossbench.action_runner.basic_action_runner import BasicActionRunner 9from crossbench.benchmarks.loading.config.pages import PagesConfig 10from crossbench.benchmarks.loading.loading_benchmark import LoadingPageFilter 11from crossbench.browsers.settings import Settings 12from crossbench.cli.config.secrets import Secret 13from crossbench.cli.config.secret_type import SecretType 14from crossbench.flags.base import Flags 15from crossbench.runner.groups.session import BrowserSessionRunGroup 16from tests import test_helper 17from tests.crossbench.action_runner.action_runner_test_case import ActionRunnerTestCase 18from tests.crossbench.mock_browser import MockChromeStable 19from tests.crossbench.mock_helper import (ChromeOsSshMockPlatform, 20 LinuxMockPlatform) 21from tests.crossbench.runner.helper import MockRun, MockRunner 22 23 24class ChromeOSLoginTestCase(ActionRunnerTestCase): 25 _CONFIG_DATA = { 26 "secrets": { 27 "google": { 28 "username": "test", 29 "password": "s3cr3t" 30 } 31 }, 32 "pages": { 33 "Google Story": { 34 "login": "google", 35 "actions": [{ 36 "action": "get", 37 "url": "https://www.google.com" 38 },] 39 } 40 } 41 } 42 43 def setUp(self) -> None: 44 super().setUp() 45 self.host_platform = LinuxMockPlatform() 46 self.platform = ChromeOsSshMockPlatform( 47 host_platform=self.host_platform, 48 host="1.1.1.1", 49 port="1234", 50 ssh_port="22", 51 ssh_user="root") 52 53 self.platform.expect_sh("[", "-e", "/usr/bin/google-chrome", "]", result="") 54 self.platform.expect_sh("[", "-f", "/usr/bin/google-chrome", "]", result="") 55 56 self.browser = MockChromeStable( 57 "mock browser", settings=Settings(platform=self.platform)) 58 self.runner = MockRunner() 59 self.root_dir = pathlib.Path() 60 self.session = BrowserSessionRunGroup(self.runner.env, 61 self.runner.probes, self.browser, 62 Flags(), 1, self.root_dir, True, True) 63 self.run = MockRun(self.runner, self.session, "run 1") 64 65 self.action_runner = BasicActionRunner() 66 self.mock_args = mock.Mock() 67 68 def expect_google_login(self): 69 self.browser.expect_js(result=True) 70 self.browser.expect_js(result=True) 71 self.browser.expect_js(result=True) 72 self.browser.expect_js(result=True) 73 self.browser.expect_js(result=True) 74 self.browser.expect_js(result=True) 75 self.browser.expect_js(result=True) 76 77 def test_google_account(self): 78 config = PagesConfig.parse(self._CONFIG_DATA) 79 page = LoadingPageFilter.stories_from_config(self.mock_args, config) 80 81 self.expect_google_login() 82 83 config.pages[0].login.run_with(self.action_runner, self.run, page[0]) 84 85 def test_logged_in_google_account(self): 86 config = PagesConfig.parse(self._CONFIG_DATA) 87 page = LoadingPageFilter.stories_from_config(self.mock_args, config) 88 89 self.browser.expect_is_logged_in( 90 Secret(SecretType.GOOGLE, "test", "s3cr3t")) 91 92 config.pages[0].login.run_with(self.action_runner, self.run, page[0]) 93 94 def test_logged_in_non_google_account(self): 95 config = PagesConfig.parse(self._CONFIG_DATA) 96 page = LoadingPageFilter.stories_from_config(self.mock_args, config) 97 98 self.browser.expect_is_logged_in(Secret(None, "test", "s3cr3t")) 99 100 self.expect_google_login() 101 102 config.pages[0].login.run_with(self.action_runner, self.run, page[0]) 103 104 105if __name__ == "__main__": 106 test_helper.run_pytest(__file__) 107