def do_create(user: Dict[str, Any]) -> Tuple[Content, HttpStatusCode]: try: new_user = User(username=user['username'], email=user['email'], password=user['password'], roles=[Role(name='user')]) new_user.save() try: default_groups = Group.get_default_groups() for group in default_groups: group.add_user(new_user) except Exception: log.warning( "User has been created, but not added to default group.") except AssertionError as e: content = { 'msg': USER['create']['failure']['invalid'].format(reason=e) } status = 422 except IntegrityError: content = {'msg': USER['create']['failure']['duplicate']} status = 409 except Exception as e: content = {'msg': GENERAL['internal_error'] + str(e)} status = 500 else: content = { 'msg': USER['create']['success'], 'user': new_user.as_dict(include_private=True) } status = 201 finally: return content, status
def test_exception_on_creating_user_with_not_unique_username(tables): with pytest.raises(IntegrityError): duplicated_username = '******' password = '******' existing_user = User(username=duplicated_username, password=password) duplicated_user = User(username=duplicated_username, password=password) existing_user.save() duplicated_user.save()
def do_create(user): try: new_user = User(username=user['username'], email=user['email'], password=user['password'], roles=[Role(name='user')]) new_user.save() except AssertionError as e: content = {'msg': R['create']['failure']['invalid'].format(reason=e)} status = 422 except IntegrityError: content = {'msg': R['create']['failure']['duplicate']} status = 409 except Exception as e: content = {'msg': G['internal_error'] + str(e)} status = 500 else: content = {'msg': R['create']['success'], 'user': new_user.as_dict} status = 201 finally: return content, status
print(content, status) elif action == '7': task_id = input('ID > ') content, status = business_destroy(int(task_id)) print(content, status) elif action == '8': import string import random rand_str = lambda: ''.join( random.choice(string.ascii_uppercase) for x in range(8)) random_username, random_email = rand_str( ), rand_str() + '@test.com' user = User(password='******', email=random_email, username=random_username) user.save() for _ in range(3): # TODO add spawnAt, terminateAt content, status = business_create( dict(userId=user.id, hostname=host, command=cmd)) print(content, status) print(user) elif action == '9': user_id = input('User ID > ') user = User.get(user_id) print('[BEFORE] User has {} tasks.'.format(len(user.tasks))) user.destroy() tasks_after = Task.query.filter(Task.user_id == user_id).all() print('[AFTER] User has now {} tasks.'.format(len(tasks_after))) elif action == '10': task_id = input('ID > ')
class AccountCreator: ''' It asks all the necessary questions in order to set up a new account. Takes user's input from CLI. Exits on Ctrl+C ''' def __init__(self): init_db() # Prepare empty ORM object self.new_user = User() def run_prompt(self): self._ask_for_username() self._ask_for_email() self._ask_for_password() self._ask_for_role() self._create_user() def _create_user(self): try: self.new_user.save() except Exception as e: click.echo( 'Account creation failed due to an error: {}.'.format(e)) else: click.echo('Account created successfully.') def _ask_for_username(self): ''' Process is repeated until username becomes valid. If so, it assignes the vaule to the ORM object. ''' valid_username_provided = False while not valid_username_provided: try: username = click.prompt('[1/4] UNIX username', type=str) self.new_user.username = username except click.Abort: raise except Exception as e: click.echo('Invalid username: {reason}.'.format(reason=e)) else: valid_username_provided = True def _ask_for_email(self): valid_email_provided = False while not valid_email_provided: try: email = click.prompt( '[2/4] email (for TensorHive warnings only)', type=str) self.new_user.email = email except click.Abort: raise except Exception as e: click.echo('Invalid email: {reason}.'.format(reason=e)) else: valid_email_provided = True def _ask_for_password(self): # Useful aliases prompt_for_password = lambda message: click.prompt( message, type=str, hide_input=True) password_length_requirement = 'at least {} characters'.format( self.new_user.min_password_length) first_password_message = '[3/4] password ({})'.format( password_length_requirement) repeated_password_message = '[3/4] repeat password' valid_password_provided = False while not valid_password_provided: try: password1 = prompt_for_password(message=first_password_message) self.new_user.password = password1 password2 = prompt_for_password( message=repeated_password_message) assert password1 == password2, 'Passwords don\'t match, please try again.' except click.Abort: raise except Exception as error_msg: click.echo(str(error_msg)) else: valid_password_provided = True def _ask_for_role(self): try: make_admin = click.confirm('[4/4] admin account?', default=False) # TODO Refactor roles: admin or not instead of two mutually exclusive 'admin' and 'user self.new_user.roles.append(Role(name='user')) if make_admin: self.new_user.roles.append(Role(name='admin')) except click.Abort: raise except Exception: click.echo('Unknown error - could not assign role.')
class AccountCreator: ''' It asks all the necessary questions in order to set up a new account. Takes user's input from CLI. Exits on Ctrl+C ''' def __init__(self): ensure_db_with_current_schema() self._check_restrictions() # Prepare empty ORM object self.new_user = User() def run_prompt(self): self._ask_for_username() self._ask_for_email() self._ask_for_password() self._ask_for_role() if self._create_user(): self._add_to_default_groups() def _create_user(self) -> bool: try: self.new_user.save() except Exception as e: click.echo( red('Account creation failed due to an error: {}.'.format(e))) return False else: click.echo(green('Account created successfully.')) return True def _ask_for_username(self): ''' Process is repeated until username becomes valid. If so, it assignes the vaule to the ORM object. ''' valid_username_provided = False while not valid_username_provided: try: username = click.prompt('[1/4] UNIX username', type=str) self.new_user.username = username except click.Abort: raise except Exception as e: click.echo(red('Invalid username: {reason}.'.format(reason=e))) else: valid_username_provided = True def _ask_for_email(self): valid_email_provided = False while not valid_email_provided: try: email = click.prompt( '[2/4] email (for TensorHive warnings only)', type=str) self.new_user.email = email except click.Abort: raise except Exception as e: click.echo(red('Invalid email: {reason}.'.format(reason=e))) else: valid_email_provided = True def _ask_for_password(self): # Useful aliases prompt_for_password = lambda message: click.prompt( message, type=str, hide_input=True) password_length_requirement = 'at least {} characters'.format( self.new_user.min_password_length) first_password_message = '[3/4] password ({})'.format( password_length_requirement) repeated_password_message = '[3/4] repeat password' valid_password_provided = False while not valid_password_provided: try: password1 = prompt_for_password(message=first_password_message) self.new_user.password = password1 password2 = prompt_for_password( message=repeated_password_message) assert password1 == password2, orange( 'Passwords don\'t match, please try again.') except click.Abort: raise except Exception as error_msg: click.echo(str(error_msg)) else: valid_password_provided = True def _ask_for_role(self): try: make_admin = click.confirm('[4/4] admin account?', default=False) # TODO Refactor roles: admin or not instead of two mutually exclusive 'admin' and 'user self.new_user.roles.append(Role(name='user')) if make_admin: self.new_user.roles.append(Role(name='admin')) except click.Abort: raise except Exception: click.echo(red('Unknown error - could not assign role.')) def _add_to_default_groups(self): groups = Group.get_default_groups() for group in groups: group.add_user(self.new_user) if len(groups) > 0: click.echo(green('Account added to the existing default groups')) @classmethod def _check_restrictions(cls): # If there are already users in the DB, don't bother if User.query.count() > 0: return if Restriction.query.count() == 0: if click.confirm( orange('There are no permissions specified') + ' - that means, that by default ' 'users will not have access to any resources. Would you like to create ' 'a default permission together with a default group now? (All users ' 'would have access to every resource)', default=True): default_group = Group(name='users') default_group._is_default = True default_group.save() default_restriction = Restriction( name='can always use everything', starts_at=datetime.utcnow(), is_global=True) default_restriction.apply_to_group(default_group) click.echo( green( 'Created a default group: {} and a permission "{}" '. format(default_group.name, default_restriction.name) + 'allowing access to every resource ' 'at any time.')) else: click.echo( orange( '[•] OK - not creating any permissions. Remember that you need to define permissions' ' in order for users to be able to access the resources.' ))