Esempio n. 1
0
def test_create_user_fail_none_entity_provided(app):
    with app.app_context():
        user_service = UserService(app)
        with pytest.raises(
                ValueError,
                match=r"None object was provided to create new entity.*"):
            user = user_service.create(None)
Esempio n. 2
0
def render_register_page():
    register_form = RegisterForm()
    user_service = UserService(current_app)
    user = User()
    if request.method == 'POST' and register_form.validate_on_submit():
        user.login = register_form.email.data
        user.title = register_form.title.data
        user.password = register_form.password.data

        user_service.create(user)
        return redirect('/')

    # return render_template("user/register.html", form=register_form)
    return (WebSiteHtmlView("main_blocks/main_template.html").set_title(
        'Регистрация на сайте').set_content('user/register.html').render(
            form=register_form))
class RegistrationService():
    def __init__(self):
        self._notification_service = NotificationService()
        self._appuser_service = AppUserService()
        self._user_service = UserService()

    def create(self, **kwargs):
        with transaction.atomic():
            cellphone = kwargs.get('cellphone')
            user = self._user_service.create(**kwargs.get('user'))
            app_user = self._appuser_service.create(**{ 
                'user_id': user.id, 
                'cellphone':  cellphone,
                'accepted_terms': True
            })

            grp = Group.objects.get(name = 'Customer')
            grp.user_set.add(user)

            verification_code = 1234

            self._notification_service.send_sms(
                to = cellphone,
                msg = f'Your FoodACup verification code: {verification_code}'
            )

            return app_user
Esempio n. 4
0
def test_create_user(app):
    with app.app_context():
        user_service = UserService(app)
        user = User()
        user.login = '******'
        user.password = '******'
        user = user_service.create(user)
        assert user.id is not None
Esempio n. 5
0
def test_delete_by_id(app):
    with app.app_context():
        user_service = UserService(app)
        user = User()
        user.login = '******'
        user.password = '******'
        user = user_service.create(user)
        user_service.delete_by_id(user.id)
        assert user_service.get_by_id(user.id) is None
Esempio n. 6
0
def test_get_by_id(app):
    with app.app_context():
        user_service = UserService(app)
        user = User()
        user.login = '******'
        user.password = '******'
        user = user_service.create(user)
        assert user_service.get_by_id(
            user.id).id == user.id and user_service.get_by_id(
                user.id).login == user.login
Esempio n. 7
0
def test_update_user_cannot_assign_bad_column(app):
    with app.app_context():
        user_service = UserService(app)
        user = User()
        user.login = '******'
        user.password = '******'
        user = user_service.create(user)
        with pytest.raises(ValueError,
                           match=r"Model definition does not have such key*"):
            updated_user = user_service.update(user.id, {'metadata': 'test'})
Esempio n. 8
0
def test_update_user_change_title_and_password(app):
    with app.app_context():
        user_service = UserService(app)
        user = User()
        user.login = '******'
        user.password = '******'
        user.title = 'First title'
        user = user_service.create(user)
        new_title = 'Test title!'
        new_password = '******'
        updated_user = user_service.update(user.id, {
            'title': new_title,
            'password': new_password
        })
        assert updated_user.title == new_title
        assert user_service.check_password_hash(user.password, new_password)
Esempio n. 9
0
def create_user():
    """
    Params a username:password (required) and attempts to create a user.

    Username is a unique field.
    """
    auth = request.authorization

    user_service = UserService()
    created_user = user_service.create(auth.username, auth.password)

    if created_user:
        user_presenter = UserPresenter()
        user_result = user_presenter.dump(created_user)

        return jsonify(user_result.data)

    return Response(response='400 Unable to create user. User already exists.',
                    status=400,
                    mimetype="application/json")
Esempio n. 10
0
def test_create_user_and_login(app):
    with app.test_request_context():
        # Create the user first with a given login and password
        user_service = UserService(app)
        user = User()
        user.login = '******'
        user.password = '******'
        user.title = 'Test user'
        created_user = user_service.create(entity=user)
        assert created_user.id is not None

        # Now get this user by its ID
        user_from_db = user_service.get_by_login(login='******')
        assert user_from_db is not None
        assert user_from_db.title == 'Test user'

        # Compare passwords and login user in case everything is ok
        assert user_service.check_password_hash(
            hash_to_check=user_from_db.password, password_to_check='pwd111')
        login_user(user=user_from_db, remember=False)
        assert current_user.id == user_from_db.id