code: def test_login_scenarios(
      mock_getpass: mock.MagicMock,
      mock_do_login: mock.MagicMock,
      mock_is_token_valid: mock.MagicMock,
      scenario_set: Login,
  ) -> None:
      def getpass(*_: Any) -> str:
          return "prompt_pass"
  
      def _is_token_valid(
          master_url: str, token: str, cert: Any
      ) -> bool:
          return token in ["cache", "env_token"]
  
      def do_login(*_: Any) -> str:
          return "new"
  
      mock_getpass.side_effect = getpass
      mock_is_token_valid.side_effect = _is_token_valid
      mock_do_login.side_effect = do_login
  
      for scenario in scenario_set.scenarios():
          with contextlib.ExitStack() as es:
              # Configure environment.
              es.enter_context(
                  util.setenv_optional("DET_MASTER", MOCK_MASTER_URL)
              )
              es.enter_context(
                  util.setenv_optional("DET_USER", scenario.env_user)
              )
              es.enter_context(
                  util.setenv_optional("DET_PASS", scenario.env_pass)
              )
              es.enter_context(
                  util.setenv_optional(
                      "DET_USER_TOKEN", scenario.env_token
                  )
              )
  
              # Configure allowable TokenStore calls.
              mts = es.enter_context(util.MockTokenStore(strict=False))
              mts.get_token("user", retval=scenario.cache)
              mts.get_token("extra", retval=scenario.cache)
              mts.get_token("determined", retval=scenario.cache)
              mts.drop_user("user")
              mts.drop_user("extra")
              mts.set_token("user", "new")
              mts.set_token("determined", "new")
  
              # The cache active user is always "user".  This deserves some explanation.  Effectively,
              # since this is only called within the `default_load_user_password()` function, that
              # function will never show evidence that it checked the environment.  However, for the
              # remainder of the authentication flow, it never matters again that the user came from
              # the TokenStore.get_active_user(), so if we modeled that in the table it wouldn't
              # actually increase code coverage for the Authentication object, which is what we are
              # focusing on.
              mts.get_active_user(retval=scenario.cache and "user")
  
              try:
                  auth = authentication.Authentication(
                      "master_url",
                      scenario.req_user,
                      scenario.req_pass,
                      None,
                  )
  
                  # Make sure we got the results we expected.
                  for exp in scenario_set.expected:
                      if isinstance(exp, Check):
                          mock_is_token_valid.assert_has_calls(
                              [mock.call("master_url", exp.token, None)]
                          )
                      elif isinstance(exp, DoLogin):
                          mock_do_login.assert_has_calls(
                              [
                                  mock.call(
                                      "master_url",
                                      exp.username,
                                      exp.password,
                                      None,
                                  )
                              ]
                          )
                      elif isinstance(exp, Use):
                          assert auth.session.username == exp.user
                          assert auth.session.token == exp.token
                      else:
                          raise ValueError(f"unexpected result: {exp}")
  
                  # Make sure we didn't get any unexpected results.
                  if not any(
                      isinstance(exp, Check)
                      for exp in scenario_set.expected
                  ):
                      mock_is_token_valid.assert_not_called()
                  if not any(
                      isinstance(exp, DoLogin)
                      for exp in scenario_set.expected
                  ):
                      mock_do_login.assert_not_called()
  
              except Exception as e:
                  raise RuntimeError(
                      f"failed scenario_set: {scenario_set}, scenario={scenario}"
                  ) from e
  
imports: import collections
  import contextlib
  import itertools
  from typing import Any, Iterator, List, Tuple, no_type_check
  from unittest import mock
  import pytest
  import responses
  from responses import matchers, registries
  from determined.cli import cli
  from determined.common.api import authentication
  from tests.cli import util
label_desc: Use image node_id 1 for calling a global function with example usage: test_login_scenarios(mock_getpass, mock_do_login, mock_is_token_valid, scenario_set) and returns: str, unknown, str