parameters: (v, username, password=None)
docstring: Context manager to allow running tests as an alternative login user.
docstring_summary: Context manager to allow running tests as an alternative login user.
docstring_tokens: Context manager to allow running tests as an alternative login user .
function: def as_user(v, username, password=None):
      """Context manager to allow running tests as an alternative login user."""
      access_token = False
      if not isinstance(v, api.client.Connection):
          connection = v.connection
      else:
          connection = v
  
      if isinstance(username, api.User):
          password = username.password
          username = username.username
  
      if isinstance(username, api.OAuth2AccessToken):
          access_token = username.token
          username = None
          password = None
  
      try:
          if config.use_sessions:
              session_id = None
              domain = None
              # requests doesn't provide interface for retrieving
              # domain segregated cookies other than iterating.
              for cookie in connection.session.cookies:
                  if cookie.name == 'sessionid':
                      session_id = cookie.value
                      domain = cookie.domain
                      break
              if session_id:
                  del connection.session.cookies['sessionid']
              if access_token:
                  kwargs = dict(token=access_token)
              else:
                  kwargs = connection.get_session_requirements()
          else:
              previous_auth = connection.session.auth
              kwargs = dict()
          connection.login(username, password, **kwargs)
          yield
      finally:
          if config.use_sessions:
              if access_token:
                  connection.session.auth = None
              del connection.session.cookies['sessionid']
              if session_id:
                  connection.session.cookies.set('sessionid', session_id, domain=domain)
          else:
              connection.session.auth = previous_auth
function_tokens: def as_user ( v , username , password = None ) : access_token = False if not isinstance ( v , api . client . Connection ) : connection = v . connection else : connection = v if isinstance ( username , api . User ) : password = username . password username = username . username if isinstance ( username , api . OAuth2AccessToken ) : access_token = username . token username = None password = None try : if config . use_sessions : session_id = None domain = None # requests doesn't provide interface for retrieving # domain segregated cookies other than iterating. for cookie in connection . session . cookies : if cookie . name == 'sessionid' : session_id = cookie . value domain = cookie . domain break if session_id : del connection . session . cookies [ 'sessionid' ] if access_token : kwargs = dict ( token = access_token ) else : kwargs = connection . get_session_requirements ( ) else : previous_auth = connection . session . auth kwargs = dict ( ) connection . login ( username , password , * * kwargs ) yield finally : if config . use_sessions : if access_token : connection . session . auth = None del connection . session . cookies [ 'sessionid' ] if session_id : connection . session . cookies . set ( 'sessionid' , session_id , domain = domain ) else : connection . session . auth = previous_auth