def test_run_password(self):
        flexmock(DialogHelper)
        DialogHelper.should_receive('ask_for_password').and_return('foobar')
        comm = Command('ask_password', {}, {'__ui__': 'cli'})
        res = self.acr(comm).run()

        assert res[0] is True
        assert res[1] == 'foobar'
Ejemplo n.º 2
0
    def test_run_password(self):
        flexmock(DialogHelper)
        DialogHelper.should_receive('ask_for_password').and_return('foobar')
        comm = Command('ask_password', {}, {'__ui__': 'cli'})
        res = self.acr(comm).run()

        assert res[0] is True
        assert res[1] == 'foobar'
    def test_run_input(self, inp):
        flexmock(DialogHelper)
        DialogHelper.should_receive('ask_for_input_with_prompt').and_return(inp)
        comm = Command('ask_input', {}, {'__ui__': 'cli'})
        res = self.acr(comm).run()

        assert res[0] is bool(inp)
        assert res[1] == inp
    def test_run_confirm(self, decision):
        flexmock(DialogHelper)
        DialogHelper.should_receive('ask_for_confirm_with_message').and_return(decision)
        comm = Command('ask_confirm', {}, {'__ui__': 'cli'})
        res = self.acr(comm).run()

        assert res[0] is decision
        assert res[1] == decision
Ejemplo n.º 5
0
    def test_run_confirm(self, decision):
        flexmock(DialogHelper)
        DialogHelper.should_receive('ask_for_confirm_with_message').and_return(
            decision)
        comm = Command('ask_confirm', {}, {'__ui__': 'cli'})
        res = self.acr(comm).run()

        assert res[0] is decision
        assert res[1] == decision
Ejemplo n.º 6
0
    def test_run_input(self, inp):
        flexmock(DialogHelper)
        DialogHelper.should_receive('ask_for_input_with_prompt').and_return(
            inp)
        comm = Command('ask_input', {}, {'__ui__': 'cli'})
        res = self.acr(comm).run()

        assert res[0] is bool(inp)
        assert res[1] == inp
Ejemplo n.º 7
0
 def run(cls, c):
     if c.input_res and not isinstance(c.input_res, dict):
         raise exceptions.CommandException('{0} needs a mapping as input!'.format(c.comm_type))
     if c.comm_type == 'ask_password':
         res = DialogHelper.ask_for_password(**c.input_res)
     elif c.comm_type == 'ask_confirm':
         res = DialogHelper.ask_for_confirm_with_message(**c.input_res)
     else:
         raise exceptions.CommandException('Unknown command type {ct}.'.format(ct=c.comm_type))
     return bool(res), res
Ejemplo n.º 8
0
 def run(cls, c):
     var, args = cls.format_args(c)
     if c.comm_type == 'ask_password':
         result = [True, DialogHelper.ask_for_password(**args)]
     elif c.comm_type == 'ask_confirm':
         result = [True, DialogHelper.ask_for_confirm_with_message(**args)]
     else:
         raise exceptions.CommandException('Unknown command type {ct}.'.format(ct=c.comm_type))
     c.kwargs[var] = result[1]
     return result
Ejemplo n.º 9
0
 def run(cls, c):
     if c.input_res and not isinstance(c.input_res, dict):
         raise exceptions.CommandException('{0} needs a mapping as input!'.format(c.comm_type))
     if c.comm_type == 'ask_password':
         res = DialogHelper.ask_for_password(**c.input_res)
     elif c.comm_type == 'ask_confirm':
         res = DialogHelper.ask_for_confirm_with_message(**c.input_res)
     else:
         raise exceptions.CommandException('Unknown command type {ct}.'.format(ct=c.comm_type))
     return bool(res), res
Ejemplo n.º 10
0
 def _ask_to_confirm(self, pac_man, *to_install):
     """ Return True if user wants to install packages, False otherwise """
     ret = DialogHelper.ask_for_package_list_confirm(
         prompt=pac_man.get_perm_prompt(to_install),
         package_list=to_install,
     )
     return bool(ret)
Ejemplo n.º 11
0
    def _try_login_with_password_ntimes(cls, login, ntimes):
        user = None

        for i in range(0, ntimes):
            password = DialogHelper.ask_for_password(
                prompt='Github Password for {username}:'.format(username=login))

            # user pressed Ctrl + D
            if password is None:
                break

            gh = cls._gh_module.Github(login_or_token=login, password=password)
            user = gh.get_user()
            try:
                user.login
                break  # if user.login doesn't raise, authentication has been successful
            except cls._gh_module.GithubException as e:
                user = None
                msg = 'Wrong Github username or password; message from Github: {0}\n'.\
                    format(e.data.get('message', 'Unknown authentication error'))
                msg += 'Try again or press {0} to abort.'
                if current_run.UI == 'cli':
                    msg = msg.format('Ctrl + D')
                else:
                    msg = msg.format('"Cancel"')
                logger.warning(msg)

        return user
Ejemplo n.º 12
0
    def _github_create_twofactor_authorization(cls, ui):
        """Create an authorization for a GitHub user using two-factor
           authentication. Unlike its non-two-factor counterpart, this method
           does not traverse the available authentications as they are not
           visible until the user logs in.

           Please note: cls._user's attributes are not accessible until the
           authorization is created due to the way (py)github works.
        """
        try:
            try:  # This is necessary to trigger sending a 2FA key to the user
                auth = cls._user.create_authorization()
            except cls._gh_exceptions.GithubException:
                onetime_pw = DialogHelper.ask_for_password(
                    ui, prompt='Your one time password:'******'repo', 'user', 'admin:public_key'],
                    note="DevAssistant",
                    onetime_password=onetime_pw)
                cls._user = cls._gh_module.Github(
                    login_or_token=auth.token).get_user()
                logger.debug(
                    'Two-factor authorization for user "{0}" created'.format(
                        cls._user.login))
                cls._github_store_authorization(cls._user, auth)
                logger.debug('Two-factor authorization token stored')
        except cls._gh_exceptions.GithubException as e:
            logger.warning(
                'Creating two-factor authorization failed: {0}'.format(e))
Ejemplo n.º 13
0
 def _ask_to_confirm(self, pac_man, *to_install):
     """ Return True if user wants to install packages, False otherwise """
     ret = DialogHelper.ask_for_package_list_confirm(
         prompt=pac_man.get_perm_prompt(to_install),
         package_list=to_install,
     )
     return bool(ret)
Ejemplo n.º 14
0
 def _get_github_user(cls, login, token, **kwargs):
     if not cls._user:
         try:
             # try logging with token
             gh = cls._gh_module.Github(login_or_token=token)
             cls._user = gh.get_user()
             # try if the authentication was successful
             cls._user.login
         except cls._gh_module.GithubException:
             # if the token was set, it was wrong, so make sure it's reset
             cls._token = None
             # login with username/password
             password = DialogHelper.ask_for_password(
                     prompt='Github Password for {username}:'.format(username=login))
             gh = cls._gh_module.Github(login_or_token=login, password=password)
             cls._user = gh.get_user()
             try:
                 cls._user.login
                 cls._github_create_auth(**kwargs) # create auth for future use
             except cls._gh_module.GithubException as e:
                 msg = 'Wrong username or password\nGitHub exception: {0}'.format(e)
                 logger.error(msg)
                 # reset cls._user to None, so that we don't use it if calling this multiple times
                 cls._user = None
                 raise exceptions.RunException(msg)
     return cls._user
Ejemplo n.º 15
0
 def _get_github_user(cls, login):
     if not cls._user:
         try:
             # try logging with token
             token = cls._github_token(login)
             gh = cls._gh_module.Github(login_or_token=token)
             cls._user = gh.get_user()
             # try if the authentication was successful
             cls._user.login
         except cls._gh_module.GithubException:
             # if the token was set, it was wrong, so make sure it's reset
             cls._token = None
             # login with username/password
             password = DialogHelper.ask_for_password(
                     prompt='Github Password for {username}:'.format(username=login))
             gh = cls._gh_module.Github(login_or_token=login, password=password)
             cls._user = gh.get_user()
             try:
                 cls._user.login
                 cls._github_create_auth() # create auth for future use
             except cls._gh_module.GithubException as e:
                 msg = 'Wrong username or password\nGitHub exception: {0}'.format(e)
                 # reset cls._user to None, so that we don't use it if calling this multiple times
                 cls._user = None
                 raise exceptions.CommandException(msg)
     return cls._user
Ejemplo n.º 16
0
 def _ask_to_confirm(self, pac_man, *to_install):
     """ Return True if user wants to install packages, False otherwise """
     message = '\n'.join(sorted(to_install))
     ret = DialogHelper.ask_for_package_list_confirm(
         prompt=pac_man.get_perm_prompt(to_install),
         package_list=to_install,
     )
     return False if ret is False else True
Ejemplo n.º 17
0
 def _ask_to_confirm(self, pac_man, *to_install):
     """ Return True if user wants to install packages, False otherwise """
     message = '\n'.join(sorted(to_install))
     ret = DialogHelper.ask_for_package_list_confirm(
         prompt=pac_man.get_perm_prompt(to_install),
         package_list=to_install,
     )
     return False if ret is False else True
Ejemplo n.º 18
0
    def _github_create_twofactor_authorization(cls, ui):
        """Create an authorization for a GitHub user using two-factor
           authentication. Unlike its non-two-factor counterpart, this method
           does not traverse the available authentications as they are not
           visible until the user logs in.

           Please note: cls._user's attributes are not accessible until the
           authorization is created due to the way (py)github works.
        """
        try:
            try: # This is necessary to trigger sending a 2FA key to the user
                auth = cls._user.create_authorization()
            except cls._gh_exceptions.GithubException:
                onetime_pw = DialogHelper.ask_for_password(ui, prompt='Your one time password:'******'repo', 'user', 'admin:public_key'],
                                            note="DevAssistant",
                                            onetime_password=onetime_pw)
                cls._user = cls._gh_module.Github(login_or_token=auth.token).get_user()
                logger.debug('Two-factor authorization for user "{0}" created'.format(cls._user.login))
                cls._github_store_authorization(cls._user, auth)
                logger.debug('Two-factor authorization token stored')
        except cls._gh_exceptions.GithubException as e:
            logger.warning('Creating two-factor authorization failed: {0}'.format(e))