def test_get_auth_names(self, mocker, func, no_func):
     val = False
     def side_effect(*args, **kwargs):
         if 'both' in kwargs:
             return val
         else:
             return True
     cfg = mocker.MagicMock(spec=ConfigParser)
     mocker.patch('picovico.cli.profile_utils.get_raw_profile', return_value=cfg)
     mock_one = mocker.patch('picovico.cli.profile_utils.check_'+func+'_info_value')
     mock_two = mocker.patch('picovico.cli.profile_utils.check_'+no_func+'_info_value')
     mock_one.return_value = True
     assert profile_utils.get_auth_names(default_section_name) == getattr(profile_utils, '{}_INFO'.format(func.upper()))
     if func == 'authenticate':
         mock_two.assert_not_called()
     if func == 'login':
         mock_one.side_effect = side_effect
         for v in range(2):
             val = bool(v)
             check_val = profile_utils.LOGIN_INFO[:1] if not val else profile_utils.LOGIN_INFO
             assert profile_utils.get_auth_names(default_section_name) == check_val
Exemplo n.º 2
0
def prepare_api_object(profile_name, session=False):
    profile_name = profile_name or profile_utils.DEFAULT_PROFILE_NAME
    profile = profile_utils.get_profile(profile_name, info=True)
    api = PicovicoAPI(profile.APP_ID, profile.DEVICE_ID)
    if session:
        sess = profile_utils.get_session_info()
        if sess and sess.PROFILE == profile.NAME:
            api.set_access_tokens(sess.ACCESS_KEY, sess.ACCESS_TOKEN)
            api._ready_component_property()
        else:
            auth_names = profile_utils.get_auth_names(profile.NAME)
            if not auth_names:
                prompt.show_no_session(profile.NAME)
            action = {
                profile_utils.LOGIN_INFO: 'login',
                profile_utils.AUTHENTICATE_INFO: 'authenticate',
            }.get(auth_names, None)
            if not all(getattr(profile, k, None) for k in auth_names):
                prompt.show_no_session(profile.NAME)
            else:
                arguments = {k.lower(): getattr(profile, k) for k in auth_names}
                api = auth_action(action, profile.NAME, **arguments)
    return api