def nexus_mock_client(mocker, faker): """A nexus_client with the request method mocked""" class ResponseMock: def __init__(self): self.status_code = 200 self.content = faker.sentence() self.reason = faker.sentence() # prepare content for repositories.refresh() self._json = [ nexus_repository(name=faker.pystr(), format_=faker.random.choice([ 'pypi', 'nuget', 'raw', 'yum', 'rubygems' ])) for _ in range(faker.random_int(1, 10)) ] def json(self): return self._json mocker.patch('nexuscli.nexus_client.NexusClient.http_request', return_value=ResponseMock()) client = NexusClient() client._server_version = semver.VersionInfo(3, 19, 0) client.repositories.refresh() return client
def get_client(): client = NexusClient() try: client.read_config() return client except NexusClientConfigurationNotFound: sys.stderr.write( 'Configuration not found; please run nexus-cli.py login\n') sys.exit(1)
def do_login(): nexus_url = _input('Nexus OSS URL', NexusClient.DEFAULT_URL) nexus_user = _input('Nexus admin username', NexusClient.DEFAULT_USER) nexus_pass = getpass.getpass( prompt='Nexus admin password ({}):'.format(NexusClient.DEFAULT_PASS)) if not nexus_pass: nexus_pass = NexusClient.DEFAULT_PASS client = NexusClient(url=nexus_url, user=nexus_user, password=nexus_pass) client.write_config() sys.stderr.write('\nConfiguration saved to {}\n'.format( NexusClient.CONFIG_PATH))
def init_dialer(self): try: self.logger.debug("connection string: %s \n" % self.dial_config) self._client = NexusClient(**self.dial_config) except Exception as error: self.logger.error("Couldn't instantiate nexus client %s" % error) raise error
def cmd_login(_, __): """Performs ``nexus3 login``""" nexus_url = util.input_with_default('Nexus OSS URL', nexus_config.DEFAULTS['url']) nexus_user = util.input_with_default('Nexus admin username', nexus_config.DEFAULTS['username']) nexus_pass = getpass.getpass( prompt=f'Nexus admin password ({nexus_config.DEFAULTS["password"]}):') if not nexus_pass: nexus_pass = nexus_config.DEFAULTS['password'] nexus_verify = _input_yesno('Verify server certificate', nexus_config.DEFAULTS['x509_verify']) config = nexus_config.NexusConfig(username=nexus_user, password=nexus_pass, url=nexus_url, x509_verify=nexus_verify) # make sure configuration works before saving NexusClient(config=config) config.dump() sys.stderr.write(f'\nConfiguration saved to {config.config_file}\n')
def test_repositories(mocker): """ Ensure that the class fetches repositories on instantiation """ mocker.patch('nexuscli.nexus_client.RepositoryCollection') client = NexusClient() nexuscli.nexus_client.RepositoryCollection.assert_called() client.repositories.refresh.assert_called_once()
def get_client(): """ Returns a Nexus Client instance. Prints a warning if the configuration file doesn't exist. :rtype: nexuscli.nexus_client.NexusClient """ maybe_config = _get_client_kwargs() if maybe_config: config = NexusConfig(**_get_client_kwargs()) return NexusClient(config=config) config = NexusConfig() try: config.load() except FileNotFoundError: sys.stderr.write( 'Warning: configuration not found; proceeding with defaults.\n' 'To remove this warning, please run `nexus3 login`\n') return NexusClient(config=config)
def get_client(): """ Returns a Nexus Client instance. Prints a warning if a configuration file isn't file. :rtype: nexuscli.nexus_client.NexusClient """ config = NexusConfig() try: config.load() except FileNotFoundError: sys.stderr.write( 'Warning: configuration not found; proceeding with defaults.\n' 'To remove this warning, please run `nexus3 login`\n') return NexusClient(config=config)
def main(argv=None): arguments = docopt(__doc__, argv=argv) if arguments.get('login'): do_login() NexusClient() elif arguments.get('script'): cmd_script(arguments) elif arguments.get('repo'): cmd_repo(arguments) elif arguments.get('list') or arguments.get('ls'): cmd_list(arguments) elif arguments.get('upload') or arguments.get('up'): cmd_upload(arguments) else: raise NotImplementedError
def cmd_login(**kwargs): """Performs ``nexus3 login``""" config = nexus_config.NexusConfig(**kwargs) # make sure configuration works before saving try: NexusClient(config=config) except exception.NexusClientInvalidCredentials: # the regular message tells the user to try to login, which is what # they just did now, so override the msg raise exception.NexusClientInvalidCredentials('Invalid credentials') sys.stderr.write('\nLogin successful.\n') sys.stderr.write( f'Configuration saved to {config.dump()}, {config.dump_env()}\n') return exception.CliReturnCode.SUCCESS.value
def test_nexus_context_path(url, expected_base, mocker): """ Check that the nexus context (URL prefix) is taken into account """ class MockResponse: def __init__(self): self.status_code = 200 def json(self): return '{}' mocker.patch('requests.request', return_value=MockResponse()) NexusClient(NexusConfig(url=url)) requests.request.assert_called_once_with( auth=(DEFAULTS['username'], DEFAULTS['password']), method='get', stream=True, url=(expected_base + 'service/rest/v1/repositories'), verify=True)
def nexus_client(): config = NexusConfig() config.load() client = NexusClient(config=config) return client
def nexus_client(nexus_config): client = NexusClient(config=nexus_config) return client
def get_client(): if not os.path.isfile(NexusClient.CONFIG_PATH): sys.stderr.write( 'Warning: configuration not found; proceeding with defaults.\n' 'To remove this warning, please run nexus-cli.py login\n') return NexusClient()