Пример #1
0
def test_check_if_user_is_admin_normal_user(db: Session) -> None:
    username = random_email()
    password = random_lower_string()
    user_in = UserCreateDto(email=username, password=password)
    user = services.user.create(db, obj_in=user_in)
    is_admin = services.user.is_admin(user)
    assert is_admin is False
Пример #2
0
def test_create_user(db: Session) -> None:
    email = random_email()
    password = random_lower_string()
    user_in = UserCreateDto(email=email, password=password)
    user = services.user.create(db, obj_in=user_in)
    assert user.email == email
    assert hasattr(user, "hashed_password")
Пример #3
0
def test_check_if_user_is_admin(db: Session) -> None:
    email = random_email()
    password = random_lower_string()
    user_in = UserCreateDto(email=email, password=password, is_admin=True)
    user = services.user.create(db, obj_in=user_in)
    is_admin = services.user.is_admin(user)
    assert is_admin is True
Пример #4
0
def test_get_user(db: Session) -> None:
    password = random_lower_string()
    username = random_email()
    user_in = UserCreateDto(email=username, password=password, is_admin=True)
    user = services.user.create(db, obj_in=user_in)
    user_2 = services.user.get(db, _id=user.id)
    assert user_2
    assert user.email == user_2.email
    assert jsonable_encoder(user) == jsonable_encoder(user_2)
Пример #5
0
def test_authenticate_user(db: Session) -> None:
    email = random_email()
    password = random_lower_string()
    user_in = UserCreateDto(email=email, password=password)
    user = services.user.create(db, obj_in=user_in)
    authenticated_user = services.user.authenticate(db,
                                                    email=email,
                                                    password=password)
    assert authenticated_user
    assert user.email == authenticated_user.email
Пример #6
0
def test_retrieve_users(client: TestClient, superuser_token_headers: dict,
                        db: Session) -> None:
    username = random_email()
    password = random_lower_string()
    user_in = UserCreateDto(email=username, password=password)
    services.user.create(db, obj_in=user_in)

    username2 = random_email()
    password2 = random_lower_string()
    user_in2 = UserCreateDto(email=username2, password=password2)
    services.user.create(db, obj_in=user_in2)

    r = client.get(f"{settings.API_V1_STR}/users/",
                   headers=superuser_token_headers)
    all_users = r.json()

    assert len(all_users) > 1
    for item in all_users:
        assert "email" in item
Пример #7
0
def test_update_user(db: Session) -> None:
    password = random_lower_string()
    email = random_email()
    user_in = UserCreateDto(email=email, password=password, is_admin=True)
    user = services.user.create(db, obj_in=user_in)
    new_password = random_lower_string()
    user_in_update = UserUpdateDto(password=new_password, is_admin=True)
    services.user.update(db, db_obj=user, obj_in=user_in_update)
    user_2 = services.user.get(db, _id=user.id)
    assert user_2
    assert user.email == user_2.email
    assert verify_password(new_password, user_2.hashed_password)
Пример #8
0
def test_get_existing_user(client: TestClient, superuser_token_headers: dict,
                           db: Session) -> None:
    username = random_email()
    password = random_lower_string()
    user_in = UserCreateDto(email=username, password=password)
    user = services.user.create(db, obj_in=user_in)
    user_id = user.id
    r = client.get(
        f"{settings.API_V1_STR}/users/{user_id}",
        headers=superuser_token_headers,
    )
    assert 200 <= r.status_code < 300
    api_user = r.json()
    existing_user = services.user.get_by_email(db, email=username)
    assert existing_user
    assert existing_user.email == api_user["email"]
Пример #9
0
def test_create_user_existing_username(client: TestClient,
                                       superuser_token_headers: dict,
                                       db: Session) -> None:
    username = random_email()
    # username = email
    password = random_lower_string()
    user_in = UserCreateDto(email=username, password=password)
    services.user.create(db, obj_in=user_in)
    data = {"email": username, "password": password}
    r = client.post(
        f"{settings.API_V1_STR}/users/",
        headers=superuser_token_headers,
        json=data,
    )
    created_user = r.json()
    assert r.status_code == 400
    assert "_id" not in created_user
Пример #10
0
def authentication_token_from_email(client: TestClient, email: str,
                                    db: Session) -> Dict[str, str]:
    """
    Return a valid token for the user with given email.

    If the user doesn't exist it is created first.
    """
    password = random_lower_string()
    user = services.user.get_by_email(db, email=email)
    if not user:
        user_in_create = UserCreateDto(username=email,
                                       email=email,
                                       password=password)
        services.user.create(db, obj_in=user_in_create)
    else:
        user_in_update = UserUpdateDto(password=password)
        services.user.update(db, db_obj=user, obj_in=user_in_update)

    return _user_authentication_headers(client=client,
                                        email=email,
                                        password=password)