def registration(): '''Функция регистрации пользователя Для авторизованных пользователей, функция осуществяляет перенаправление пользователя на страницу профиля. Проверка производится с помощью выражения "current_user.is_authenticated". Переменная current_user поступает из Flask-Login. В другом случае происход обработка формы RegistrationForm(). Когда браузер получает запрос POST в результате нажатия пользователем кнопки submit функция создает нового пользователя с именем, электронной почтой и паролем, записывает их в базу данных и затем перенаправляет запрос на вход, чтобы пользователь мог войти в систему. ''' if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): user = User(login=form.login.data.lower(), email=form.email.data) user.set_pass(form.password.data) db.session.add(user) db.session.commit() user_settings = Settings(id_user=user.id) db.session.add(user_settings) db.session.commit() flash('Congratulations, you are now a registered user!') return redirect(url_for('login')) return render_template('registration.html', title='Register', form=form)
def get(self, email: Email) -> Optional[User]: row = ( self.connection.cursor() .execute("SELECT * FROM users WHERE email = ?", (email,)) .fetchone() ) return User(email=Email(row[0]), cat=Uri(row[1])) if row else None
def __init__(self, handler: IO) -> None: self.handler = handler self.users = { Email(item["email"]): User(email=Email(item["email"]), cat=Uri(item["cat"])) for item in load(self.handler) }
def execute(self, email: str) -> User: email = convert(email) if self.user.get(email): raise UserAlreadyExists() user = User(email=email, cat=self.cat.get()) self.user.add(user) return user
def put(self, user_id, **attrs): self._data.append( User( user_id=user_id, analysis_id=attrs.get('analysis_id'), context=attrs.get('context'), user_name=attrs.get('user_name'), current_analysis=attrs.get('current_analysis'), current_question=attrs.get('current_question'), ))
def user_factory(**kwargs): return User(user_id=kwargs.setdefault('user_id', uuid4().hex), analysis_id=kwargs.setdefault('analysis_id', uuid4().hex), context=kwargs.setdefault('context', 'context'), user_name=kwargs.setdefault('user_name', 'test user'), current_analysis=kwargs.setdefault('current_analysis', uuid4().hex), current_question=kwargs.setdefault('current_question', 0))
def test_existing_user(mocker): # Given db = mocker.MagicMock() cat = mocker.MagicMock() existing_user = User(email="*****@*****.**", cat="http://my-uri.url") db.get.return_value = existing_user # When with pytest.raises(UserAlreadyExists): CreateUser(db, cat).execute(existing_user.email) # Then cat.get.assert_not_called() db.add.assert_not_called()
def test_create_user(mocker): # Given db = mocker.MagicMock() cat = mocker.MagicMock() user = User(email="*****@*****.**", cat="http://my-uri.url") db.get.return_value = None cat.get.return_value = user.cat # When out = CreateUser(db, cat).execute(user.email) # Then assert out == user db.get.assert_called_once_with(user.email) db.add.assert_called_once_with(user)
def load_initial_data(): # create default action types create_action = ActionType("CREATE") create_action.save() delete_action = ActionType("DELETE") delete_action.save() update_action = ActionType("UPDATE") update_action.save() read_action = ActionType("READ") read_action.save() # create default resources people_resource = Resource("People") people_resource.save() product_resource = Resource("Product") product_resource.save() # create users user1 = User("User1") user1.save() admin = User("Admin") admin.save() # create roles admin_role = Role("admin-role") admin_operations = set() for resource in Resource.get_all(): for action_type in ActionType.get_all(): admin_operations.add( get_operation_identifier(resource, action_type)) admin_role.bulk_add_allowed_operations(admin_operations) admin_role.save() read_role = Role("read-role") read_operations = set() for resource in Resource.get_all(): read_operations.add(get_operation_identifier(resource, read_action)) read_role.bulk_add_allowed_operations(read_operations) read_role.save() delete_role = Role("delete-role") delete_operations = set() for resource in Resource.get_all(): delete_operations.add(get_operation_identifier(resource, delete_action)) delete_role.bulk_add_allowed_operations(delete_operations) delete_role.save() create_role = Role("create-role") create_operations = set() for resource in Resource.get_all(): create_operations.add(get_operation_identifier(resource, create_action)) create_role.bulk_add_allowed_operations(create_operations) create_role.save() # assign roles to users admin.add_role(admin_role) user1.add_role(read_role) return admin