Beispiel #1
0
def create_user(dbsession:Session, registry:Registry, email:str=EMAIL, password:str=PASSWORD, admin:bool=False) -> User:
    """A helper function to create normal and admin users for tests.

    :param admin: If True run :py:class:`websauna.system.user.usermixin.SiteCreator` login and set the user to admin group.
    """

    user = User(email=email)

    if password:
        hasher = registry.getUtility(IPasswordHasher)
        user.hashed_password = hasher.hash_password(password)

    user.user_registration_source = "dummy"
    dbsession.add(user)
    dbsession.flush()
    user.username = user.generate_username()
    user.activated_at = now()

    assert user.can_login()

    # First user, make it admin
    if admin:
        site_creator = get_site_creator(registry)
        site_creator.init_empty_site(dbsession, user)

    return user
Beispiel #2
0
def create_user(dbsession: Session,
                registry: Registry,
                email: str = EMAIL,
                password: str = PASSWORD,
                admin: bool = False) -> User:
    """A helper function to create normal and admin users for tests.

    :param admin: If True run :py:class:`websauna.system.user.usermixin.SiteCreator` login and set the user to admin group.
    """

    user = User(email=email)

    if password:
        hasher = registry.getUtility(IPasswordHasher)
        user.hashed_password = hasher.hash_password(password)

    user.user_registration_source = "dummy"
    dbsession.add(user)
    dbsession.flush()
    user.username = user.generate_username()
    user.activated_at = now()

    assert user.can_login()

    # First user, make it admin
    if admin:
        site_creator = get_site_creator(registry)
        site_creator.init_empty_site(dbsession, user)

    return user
Beispiel #3
0
    def initialize_object(self, form, appstruct, obj: User):
        password = appstruct.pop("password")
        form.schema.objectify(appstruct, obj)
        hasher = self.request.registry.getUtility(IPasswordHasher)
        obj.hashed_password = hasher.hash_password(password)

        # Users created through admin are useable right away, so activate the user
        obj.activated_at = now()
Beispiel #4
0
    def initialize_object(self, form, appstruct, obj: User):
        password = appstruct.pop("password")
        form.schema.objectify(appstruct, obj)
        hasher = self.request.registry.getUtility(IPasswordHasher)
        obj.hashed_password = hasher.hash_password(password)

        # Users created through admin are useable right away, so activate the user
        obj.activated_at = now()
Beispiel #5
0
def create_user(dbsession: Session,
                registry: Registry,
                email: str = EMAIL,
                password: str = PASSWORD,
                admin: bool = False) -> User:
    """A helper function to create normal and admin users for tests.

    Example:

    .. code-block:: python

        import transaction
        from websauna.tests.utils import create_user


        def test_some_stuff(dbsession, registry):

            with transaction.manager:
                u = create_user(registry)
                # Do stuff with new user



    :param email: User's email address. If inot given use unit testing default.

    :param password: Password as plain text. If not given use unit testing default.

    :param admin: If True run :py:class:`websauna.system.user.usermixin.SiteCreator` login and set the user to admin group.
    """

    user = User(email=email)

    if password:
        hasher = registry.getUtility(IPasswordHasher)
        user.hashed_password = hasher.hash_password(password)

    user.user_registration_source = "dummy"
    dbsession.add(user)
    dbsession.flush()
    user.username = user.generate_username()
    user.activated_at = now()

    assert user.can_login()

    # First user, make it admin
    if admin:
        site_creator = get_site_creator(registry)
        site_creator.init_empty_site(dbsession, user)

    return user
Beispiel #6
0
def create_user(dbsession: Session, registry: Registry, email: str=EMAIL, password: str=PASSWORD, admin: bool=False) -> User:
    """A helper function to create normal and admin users for tests.

    Example:

    .. code-block:: python

        import transaction
        from websauna.tests.utils import create_user


        def test_some_stuff(dbsession, registry):

            with transaction.manager:
                u = create_user(registry)
                # Do stuff with new user



    :param email: User's email address. If inot given use unit testing default.

    :param password: Password as plain text. If not given use unit testing default.

    :param admin: If True run :py:class:`websauna.system.user.usermixin.SiteCreator` login and set the user to admin group.
    """

    user = User(email=email)

    if password:
        hasher = registry.getUtility(IPasswordHasher)
        user.hashed_password = hasher.hash_password(password)

    user.user_registration_source = "dummy"
    dbsession.add(user)
    dbsession.flush()
    user.username = user.generate_username()
    user.activated_at = now()

    assert user.can_login()

    # First user, make it admin
    if admin:
        site_creator = get_site_creator(registry)
        site_creator.init_empty_site(dbsession, user)

    return user