def test_prepare_api_objects(self, mocker, profile_name, session, func): prof = mocker.Mock() prof.NAME = profile_name or DEFAULT_PROFILE_NAME prof.APP_ID = 'APP_ID' prof.DEVICE_ID = profile_name mgp = mocker.patch('picovico.cli.utils.profile_utils.get_profile', return_value=prof) mapi = mocker.MagicMock(spec=PicovicoAPI) mocker.patch('picovico.cli.utils.PicovicoAPI', return_value=mapi) #mapi.set_access_tokens = mocker.Mock() if not session: pv_utils.prepare_api_object(profile_name, session) mgp.assert_called_with(prof.NAME, info=True) else: sess = mocker.Mock() mgsi = mocker.patch('picovico.cli.utils.profile_utils.get_session_info', return_value=sess) sess.PROFILE = prof.NAME sess.ACCESS_KEY = 'access_key' sess.ACCESS_TOKEN = 'access_token' pv_utils.prepare_api_object(profile_name, session) #print sess.called mapi.set_access_tokens.assert_called_with('access_key', 'access_token') mgsi.return_value = None mauth = mocker.patch('picovico.cli.utils.profile_utils.get_auth_names') mauth.return_value = None mns = mocker.patch('picovico.cli.utils.prompt.show_no_session') def side_effect(*args, **kwargs): if prof.NAME in args: sys.exit(0) mns.side_effect = side_effect with pytest.raises(SystemExit): pv_utils.prepare_api_object(profile_name, session) mns.assert_called_with(prof.NAME) auth_name = getattr(profile_utils, '{}_INFO'.format(func.upper())) mauth.return_value = auth_name for n in auth_name: setattr(prof, n, None) with pytest.raises(SystemExit): pv_utils.prepare_api_object(profile_name, session) mns.assert_called_with(prof.NAME) for n in auth_name: setattr(prof, n, n.lower()) mact = mocker.patch('picovico.cli.utils.auth_action') pv_utils.prepare_api_object(profile_name, session) argument = {k.lower(): getattr(prof, k) for k in auth_name} mact.assert_called_with(func, prof.NAME, **argument)
def get_action_from_command(action, profile_name): action_maps = cli_map_command_to_actions() action_map = action_maps.get(action) current_action = action_map.get('action') component = action_map.get('component', None) if component: api = pv_cli_utils.prepare_api_object(profile_name, session=True) api._ready_component_property() api_component = getattr(api, component) current_action = getattr(api_component, current_action) return current_action
def get_project_api(profile, **kwargs): api = pv_cli_utils.prepare_api_object(profile_name=profile, session=True) video_id = kwargs.get('video', None) project_data = file_utils.read_from_project_file() or {} if not video_id: video_id = ''.join(project_data.keys()) if len(project_data) == 1 else None data = project_data.get(video_id, None) api.project.video = video_id if data: populate_vdd_to_project(api.project, data) return api
def logout(profile_name=None): api = pv_cli_utils.prepare_api_object(profile_name, session=True) api.logout() file_utils.delete_session_file()
def my_profile(profile_name=None): api = pv_cli_utils.prepare_api_object(profile_name, session=True) return api.me()