def test_get_project__force_env_use__only_url(self): self.env.update( dict(KECHAIN_URL=TEST_URL, KECHAIN_FORCE_ENV_USE='True')) with self.env: self.assertTrue(env.bool(KechainEnv.KECHAIN_FORCE_ENV_USE)) with self.assertRaisesRegex( ClientError, "should be provided as environment variable"): # Error: KECHAIN_SCOPE or KECHAIN_SCOPE_ID should be provided as environment variable get_project()
def test_get_project__force_env_use_no_vars(self): """Test the get_project by using KECHAIN_FORCE_ENV_USE=True""" self.env.set('KECHAIN_FORCE_ENV_USE', 'True') with self.env: self.assertTrue(env.bool(KechainEnv.KECHAIN_FORCE_ENV_USE)) with self.assertRaisesRegex( ClientError, "should be provided as environment variable"): # KECHAIN_SCOPE or KECHAIN_SCOPE_ID should be provided as environment variable get_project()
def test_get_project__not_for_travis(self): self.env.set(KechainEnv.KECHAIN_FORCE_ENV_USE, "false") with self.env: project = get_project(TEST_URL, token=TEST_TOKEN, scope=TEST_SCOPE_NAME) self.assertEqual(project.name, TEST_SCOPE_NAME)
def test_get_project__force_env_use__url_token_and_name(self): self.env.update( dict(KECHAIN_URL=TEST_URL, KECHAIN_TOKEN=TEST_TOKEN, KECHAIN_SCOPE_ID=TEST_SCOPE_ID, KECHAIN_FORCE_ENV_USE='True')) with self.env: self.assertTrue(env.bool(KechainEnv.KECHAIN_FORCE_ENV_USE)) self.assertEqual(get_project().name, TEST_SCOPE_NAME)
def test_get_project_from_env__not_for_travis(self): # setup self.env.update( dict(KECHAIN_URL=TEST_URL, KECHAIN_TOKEN=TEST_TOKEN, KECHAIN_SCOPE=TEST_SCOPE_NAME)) with self.env: project = get_project() self.assertEqual(project.name, self.env['KECHAIN_SCOPE'])
def test_get_project__force_env_use__other_things_provided(self): self.env.update( dict(KECHAIN_URL=TEST_URL, KECHAIN_TOKEN=TEST_TOKEN, KECHAIN_SCOPE_ID=TEST_SCOPE_ID, KECHAIN_FORCE_ENV_USE='True')) with self.env: self.assertTrue(env.bool(KechainEnv.KECHAIN_FORCE_ENV_USE)) self.assertEqual( get_project(url='http://whatever', token=PSEUDO_TOKEN).name, TEST_SCOPE_NAME)
def test_upload_non_interactive(self): pkgname = 'new_pkg' env = Env.read_envfile() self.assertTrue( os.environ.get('KECHAIN_URL'), "KECHAIN_URL is not set in environment, cannot perform this test") with temp_chdir() as d: runner = CliRunner() result = runner.invoke(kecpkg, ['new', pkgname, '--no-venv']) package_dir = get_package_dir(pkgname) self.assertTrue(os.path.exists(package_dir)) os.chdir(package_dir) result = runner.invoke(kecpkg, ['build', pkgname]) self.assertEqual(result.exit_code, 0) self.assertExists(os.path.join(package_dir, 'dist')) pkgpath = os.path.join(package_dir, 'dist', '{}-0.0.1-py3.5.kecpkg'.format(pkgname)) self.assertExists(pkgpath) result = runner.invoke( kecpkg, [ 'upload', pkgname, '--url', os.environ.get('KECHAIN_URL'), '--token', os.environ.get('KECHAIN_TOKEN'), '--scope-id', os.environ.get('KECHAIN_SCOPE_ID'), '--kecpkg', pkgpath, '--store' # store the service_id in the settings (for teardown) ]) self.assertEqual(result.exit_code, 0) # teardown the just uploaded service from kecpkg.settings import load_settings settings = load_settings(package_dir=get_package_dir(pkgname)) from pykechain import get_project scope = get_project(url=os.environ.get('KECHAIN_URL'), token=os.environ.get('KECHAIN_TOKEN'), scope_id=os.environ.get('KECHAIN_SCOPE_ID')) service = scope.service(pk=settings.get('service_id')) service.delete()
def test_test_get_project_with_scope_id__not_for_travis(self): project = get_project(TEST_URL, token=TEST_TOKEN, scope_id=TEST_SCOPE_ID) self.assertEqual(project.name, TEST_SCOPE_NAME)
def test_project_raises_error__token_and_no_url(self): with self.assertRaisesRegex(ClientError, "Error: insufficient arguments"): get_project(token=PSEUDO_TOKEN, scope_id=PSEUDO_SCOPE_ID)
def test_project_raises_error__scope_id_and_no_pass(self): with self.assertRaisesRegex(ClientError, "Error: insufficient arguments"): get_project(url=TEST_URL, username='******', scope_id=PSEUDO_SCOPE_ID)
def test_project_raises_error__auth_and_no_url(self): with self.assertRaisesRegex(ClientError, "Error: insufficient arguments"): get_project(username='******', password=PSEUDO_PASSWORD, scope_id=PSEUDO_SCOPE_ID)
def test_project_raises_error__auth_and_no_scope(self): with self.assertRaisesRegex(ClientError, "Error: insufficient arguments"): get_project(url=TEST_URL, username='******', password=PSEUDO_PASSWORD)
def test_project_raises_error__no_pass(self): with self.assertRaisesRegex(ClientError, "Error: insufficient arguments"): get_project(url=TEST_URL, username='******', scope='Scope')
def test_project_raises_error__token_and_no_scope(self): with self.assertRaisesRegex(ClientError, "Error: insufficient arguments"): get_project(url=TEST_URL, token=TEST_TOKEN)
def test_project_raises_error__no_auth(self): with self.assertRaisesRegex(ClientError, "Error: insufficient arguments"): get_project(url=TEST_URL)
def upload(package=None, url=None, username=None, password=None, token=None, scope=None, scope_id=None, kecpkg=None, **options): """ Upload built kecpkg to KE-chain. If no options are provided, the interactive mode is triggered. """ package_name = package or get_package_name() or click.prompt( 'Package name') settings = load_settings( package_dir=get_package_dir(package_name), settings_filename=options.get('settings_filename')) if not url or not ((username and password) or token): url = click.prompt('Url (incl http(s)://)', default=settings.get('url') or url) username = click.prompt('Username', default=settings.get('username') or username) password = click.prompt('Password', hide_input=True) # set the interactive world to True for continuation sake options['interactive'] = True elif not options.get('interactive'): url = url or settings.get('url') username = username or settings.get('username') token = token or settings.get('token') scope_id = scope_id or settings.get('scope_id') client = Client(url) client.login(username=username, password=password, token=token) # scope finder if not scope_id and settings.get('scope_id') and \ click.confirm("Do you wish to use the stored `scope_id` in settings: `{}`".format( settings.get('scope_id')), default=True): scope_id = settings.get('scope_id') if not scope_id: scopes = client.scopes() scope_matcher = [ dict(number=i, scope_id=scope.id, scope=scope.name) for i, scope in zip(range(1, len(scopes)), scopes) ] # nice UI echo_info('Choose from following scopes:') for match_dict in scope_matcher: echo_info( "{number} | {scope_id:.8} | {scope}".format(**match_dict)) scope_match = None while not scope_match: scope_guess = click.prompt('Row number, part of Id or Scope') scope_match = validate_scopes(scope_guess, scope_matcher) echo_success( "Scope selected: '{scope}' ({scope_id})".format(**scope_match)) scope_id = scope_match['scope_id'] scope_to_upload = get_project(url, username, password, token, scope_id=scope_id) # service reupload service_id = options.get('service_id') or settings.get('service_id') if options.get('reupload') and not service_id: echo_failure('Please provide a service id to reupload to.') elif service_id and not options.get('reupload') and options.get( 'interactive'): if click.confirm( "Do you wish to *replace* the previously uploaded service: `{}`" .format(service_id), default=True): service_id = service_id else: service_id = None # store to settings if options.get('store'): settings.update( dict(url=url, username=username, scope_id=str(scope_id))) if service_id: settings['service_id'] = str(service_id) save_settings(settings, settings_filename=options.get('settings_filename')) # do upload build_path = os.path.join(get_package_dir(package_name), settings.get('build_dir')) if not os.path.exists(build_path): echo_failure('Cannot find build path, please do `kecpkg build` first') sys.exit(400) upload_package(scope_to_upload, build_path, kecpkg, service_id=service_id, settings=settings, store_settings=options.get('store'), settings_filename=options.get('settings_filename'))