Beispiel #1
0
    def test_long_password_error(self):
        with self.assertRaises(ValueError) as manager:
            BaseUser.create_user_sync(
                username="******",
                password="******" * (BaseUser._max_password_length + 1),
            )

        self.assertEqual(manager.exception.__str__(),
                         "The password is too long.")
Beispiel #2
0
    def test_hashed_password_error(self, logger: MagicMock):
        with self.assertRaises(ValueError) as manager:
            BaseUser.create_user_sync(username="******",
                                      password="******")

        self.assertEqual(manager.exception.__str__(),
                         "Do not pass a hashed password.")
        self.assertEqual(
            logger.method_calls,
            [
                call.warning(
                    "Tried to create a user with an already hashed password.")
            ],
        )
Beispiel #3
0
def create(
    username: t.Optional[str] = None,
    email: t.Optional[str] = None,
    password: t.Optional[str] = None,
    is_admin: t.Optional[bool] = None,
    is_superuser: t.Optional[bool] = None,
    is_active: t.Optional[bool] = None,
):
    """
    Create a new user.
    """
    username = get_username() if username is None else username
    email = get_email() if email is None else email
    if password is None:
        password = get_password()
        confirmed_password = get_confirmed_password()

        if password != confirmed_password:
            sys.exit("Passwords don't match!")

    is_admin = get_is_admin() if is_admin is None else is_admin
    is_superuser = get_is_superuser() if is_superuser is None else is_superuser
    is_active = get_is_active() if is_active is None else is_active

    user = BaseUser.create_user_sync(
        username=username,
        password=password,
        admin=is_admin,
        email=email,
        active=is_active,
        superuser=is_superuser,
    )

    print(f"Created User {user.id}")
Beispiel #4
0
    def test_no_password_error(self):
        with self.assertRaises(ValueError) as manager:
            BaseUser.create_user_sync(username="******", password=None)

        self.assertEqual(manager.exception.__str__(),
                         "A password must be provided.")
Beispiel #5
0
    def test_no_username_error(self):
        with self.assertRaises(ValueError) as manager:
            BaseUser.create_user_sync(username=None, password="******")

        self.assertEqual(manager.exception.__str__(),
                         "A username must be provided.")
Beispiel #6
0
    def test_short_password_error(self):
        with self.assertRaises(ValueError) as manager:
            BaseUser.create_user_sync(username="******", password="******")

        self.assertEqual(manager.exception.__str__(),
                         "The password is too short.")
Beispiel #7
0
 def test_success(self):
     user = BaseUser.create_user_sync(username="******", password="******")
     self.assertTrue(isinstance(user, BaseUser))
     self.assertEqual(
         BaseUser.login_sync(username="******", password="******"), user.id)