예제 #1
0
def test_get_user_with_email(db: Session) -> None:
    password = random_lower_string()
    email = random_email()
    user_in = UserCreate(email=email, password=password, is_superuser=True)
    user = crud.user.create(db, obj_in=user_in)
    user_2 = crud.user.get_by_email(db, email=user.email)
    assert user_2
    assert user.email == user_2.email
    assert jsonable_encoder(user) == jsonable_encoder(user_2)
예제 #2
0
def test_get_multi_in_group(db: Session):
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(email=email, password=password)
    user = crud.user.create(db, obj_in=user_in)
    user_group = create_random_user_group(db)
    crud.user_group.add_user(db, user_group=user_group, user_id=user.id)
    users_in_group = crud.user.get_multi_in_group(db,
                                                  user_group_id=user_group.id)
    assert user in users_in_group.records
def test_delete_user(db: Session) -> None:
    password = random_lower_string()
    email = random_email()
    user_in = UserCreate(email=email, password=password, is_superuser=True)
    user = crud.user.create(db, obj_in=user_in)
    user2 = crud.user.remove(db=db, id=user.id)
    user3 = crud.user.get(db=db, id=user.id)
    assert user3 is None
    assert user2.id == user.id
    assert user2.email == email
예제 #4
0
def test_retrieve_users(client: TestClient, superuser_token_headers):
    username = random_lower_string()
    password = random_lower_string()
    user_in = UserCreate(email=username, password=password)
    user = crud.user.create(db_session, user_in=user_in)

    username2 = random_lower_string()
    password2 = random_lower_string()
    user_in2 = UserCreate(email=username2, password=password2)
    user2 = crud.user.create(db_session, user_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 user in all_users:
        assert "email" in user
def create_random_user(db: Session) -> User:
    email = random_email()
    firstname = random_lower_string()
    password = random_lower_string()
    user_in = UserCreate(username=email,
                         firstname=firstname,
                         email=email,
                         password=password)
    user = crud.user.create(db=db, obj_in=user_in)
    return user
예제 #6
0
def test_check_if_user_is_active_inactive(db: Session):
    email = random_lower_string()
    password = random_lower_string()
    user_in = UserCreate(email=email, password=password, disabled=True)
    print(user_in)
    user = crud.user.create(db, user_in=user_in)
    print(user)
    is_active = crud.user.is_active(user)
    print(is_active)
    assert is_active
예제 #7
0
def test_authenticate_user():
    email = random_lower_string()
    password = random_lower_string()
    user_in = UserCreate(email=email, password=password)
    user = crud.user.create(db_session, user_in=user_in)
    authenticated_user = crud.user.authenticate(
        db_session, email=email, password=password
    )
    assert authenticated_user
    assert user.email == authenticated_user.email
예제 #8
0
def test_authenticate_user(db: Session) -> None:
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(email=email, password=password)
    user = crud.user.create(db, obj_in=user_in)
    authenticated_user = crud.user.authenticate(db,
                                                email=email,
                                                password=password)
    assert authenticated_user
    assert user.email == authenticated_user.email
예제 #9
0
def init() -> None:
    user_in = UserCreate(
        email=settings.FIRST_SUPERUSER_EMAIL,
        name=settings.FIRST_SUPERUSER_NAME,
        password=settings.FIRST_SUPERUSER_PASSWORD,
    )
    try:
        user_services.create_user(create_obj=user_in, uow=get_sqlalchemy_uow())
    except user_services.UserAlreadyExists:
        pass
예제 #10
0
def test_retrieve_users(superuser_token_headers):
    server_api = get_server_api()
    username = random_email()
    password = random_lower_string()
    user_in = UserCreate(email=username, password=password)
    user = crud.user.create(db_session, obj_in=user_in)

    username2 = random_email()
    password2 = random_lower_string()
    user_in2 = UserCreate(email=username2, password=password2)
    crud.user.create(db_session, obj_in=user_in2)

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

    assert len(all_users) > 1
    for user in all_users:
        assert "email" in user
예제 #11
0
def test_retrieve_users(
    client: TestClient, superuser_token_headers: dict, db: Session
) -> None:
    username = random_email()
    password = random_lower_string()
    user_in = UserCreate(email=username, password=password)
    crud.user.create(db, obj_in=user_in)

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

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

    assert len(all_users) > 1
    for item in all_users:
        assert "email" in item
def test_check_if_user_is_superuser(db: Session) -> None:
    username = random_lower_string()
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(username=username,
                         email=email,
                         password=password,
                         is_superuser=True)
    user = crud.user.create(db, obj_in=user_in)
    assert user.is_superuser
def test_get_users_by_admin(client: TestClient, superuser_token_headers: dict,
                            db: Session) -> None:
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(email=email, password=password)
    crud.user.create(db, obj_in=user_in)

    email2 = random_email()
    password2 = random_lower_string()
    user_in2 = UserCreate(email=email2, password=password2)
    crud.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
예제 #14
0
def test_retrieve_users(db: Session, client: TestClient,
                        superuser_token_headers: dict):
    username = random_email()
    password = random_lower_string()
    user_in = UserCreate(email=username, password=password)
    crud.user.create(db, obj_in=user_in)

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

    res = client.get(f"{config.API_V1_STR}/users/",
                     headers=superuser_token_headers)

    all_users = res.json()

    assert len(all_users) > 1
    for item in all_users:
        assert "email" in item
예제 #15
0
def test_check_if_user_is_active_inactive(db: Session) -> None:
    email = random_email()
    password = random_lower_string()
    username = random_lower_string()
    user_in = UserCreate(email=email,
                         username=username,
                         password=password,
                         is_active=False)
    user = crud.user.create(db, obj_in=user_in)
    is_active = crud.user.is_active(user)
    assert is_active is False
예제 #16
0
def test_get_access_token(client: TestClient, db: Session,
                          user_data: Dict[str, str]) -> None:
    user_in = UserCreate(**user_data)
    crud.user.create(db, obj_in=user_in)

    r = get_token_response(client, user_data["username"],
                           user_data["password"])
    tokens = r.json()
    assert r.status_code == 200
    assert "access_token" in tokens
    assert tokens["access_token"]
예제 #17
0
def test_create_user_by_normal_user(client: TestClient):
    username = random_lower_string()
    password = random_lower_string()
    user_in = UserCreate(email=username, password=password)
    user = crud.user.create(db_session, user_in=user_in)
    user_token_headers = user_authentication_headers(client, username, password)
    data = {"email": username, "password": password}
    r = client.post(
        f"{settings.API_V1_STR}/users/", headers=user_token_headers, json=data
    )
    assert r.status_code == 400
예제 #18
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_user_by_email(email)
    if not user:
        user_in_create = UserCreate(email=email, password=password)
        user = services.user.create_user(user_in_create.to_dto())
    else:
        user_in_update = UserUpdate(password=password)
        user = services.user.update_user(
            user.provider_uuid, user.uuid, user_in_update.to_dto()
        )

    return user_authentication_headers(client=client, email=email, password=password)
async def test_cachedb_create_user(db: Session, redis: aioredis.Redis):
    username = random_lower_string()
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(username=username, email=email, password=password)
    user = await crud.user_cachedb.create(db, redis, obj_in=user_in)
    db_user = crud.user.get(db, id=user.id)
    assert user.username == db_user.username == username
    assert user.email == db_user.email == email
    assert hasattr(user, "hashed_password")
    assert hasattr(db_user, "hashed_password")
예제 #20
0
파일: init_db.py 프로젝트: vinceyxl/LuWu
def init_db(db_session):
    init_user = user.get_by_username(db_session,
                                     username=config.FIRST_SUPERUSER_USERNAME)
    if not init_user:
        user_in = UserCreate(
            username=config.FIRST_SUPERUSER_USERNAME,
            email=config.FIRST_SUPERUSER_EMAIL,
            password=config.FIRST_SUPERUSER_PASSWORD,
            is_superuser=True,
        )
        init_user = user.create(db_session, obj_in=user_in)
예제 #21
0
def create_user(db: Session,
                repetition_model: Optional[Repetition] = None) -> User:
    email = random_email()
    password = random_lower_string()
    username = random_lower_string()
    user_in = UserCreate(email=email,
                         username=username,
                         password=password,
                         is_active=False,
                         repetition_model=repetition_model)
    return crud.user.create(db, obj_in=user_in)
def test_update_user():
    password = random_lower_string()
    username = random_lower_string()
    user_in = UserCreate(email=username, password=password, is_superuser=True)
    user = crud.user.create(db_session, obj_in=user_in)
    new_password = random_lower_string()
    user_in = UserUpdate(password=new_password, is_superuser=True)
    crud.user.update(db_session, db_obj=user, obj_in=user_in)
    user_2 = crud.user.get(db_session, id=user.id)
    assert user.email == user_2.email
    assert verify_password(new_password, user_2.hashed_password)
예제 #23
0
def test_check_if_user_is_superuser() -> None:
    full_name = random_lower_string()
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(full_name=full_name,
                         email=email,
                         password=password,
                         is_superuser=True)
    user = crud.user.create(obj_in=user_in)
    is_superuser = crud.user.is_superuser(user)
    assert is_superuser is True
예제 #24
0
def test_check_if_user_is_active_inactive() -> None:
    full_name = random_lower_string()
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(full_name=full_name,
                         email=email,
                         password=password,
                         disabled=True)
    user = crud.user.create(obj_in=user_in)
    is_active = crud.user.is_active(user)
    assert is_active
예제 #25
0
def test_update_user(db: Session) -> None:
    password = random_lower_string()
    email = random_email()
    user_in = UserCreate(email=email, password=password, is_superuser=True)
    user = crud.user.create(db, obj_in=user_in)
    new_password = random_lower_string()
    user_in_update = UserUpdate(password=new_password, is_superuser=True)
    crud.user.update(db, db_obj=user, obj_in=user_in_update)
    user_2 = crud.user.get(db, id=user.id)
    assert user_2
    assert user.email == user_2.email
    assert verify_password(new_password, user_2.hashed_password)
예제 #26
0
def test_update_user_not_password(db: Session) -> None:
    password = random_lower_string()
    email = random_email()
    user_in = UserCreate(email=email, password=password, is_superuser=True)
    user = crud.user.create(db, obj_in=user_in)
    full_name = random_lower_string()
    user_in_update = UserUpdate(full_name=full_name, is_superuser=True)
    crud.user.update(db, db_obj=user, obj_in=user_in_update)
    user_2 = crud.user.get(db, id=user.id)
    assert user_2
    assert user.email == user_2.email
    assert user.full_name == user_2.full_name
예제 #27
0
def test_login_access_token(client: TestClient, db: Session) -> None:
    user = UserCreate(name="test", password="******")
    user_db = user_crud.create_user(db, user)
    r = client.post(f"{settings.API_STR}/login/access-token",
                    data={
                        "username": "******",
                        "password": "******"
                    })
    result = r.json()
    assert r.status_code == 200
    assert 'access_token' in result
    assert result['access_token']
예제 #28
0
def test_login_successful(db: Session, client: TestClient) -> None:
    email = '*****@*****.**'
    password = '******'
    user_in = UserCreate(email=email, password=password)
    operations.user_operations.create(db, obj_in=user_in)

    payload = {"username": email, "password": password}
    response = client.post('/login/', data=payload)
    token = response.json()
    assert response.status_code == 200
    assert 'access_token' in token
    assert token['access_token']
예제 #29
0
def create(*, obj_in: UserCreate) -> User:
    passwordhash = get_password_hash(obj_in.password)
    user = UserInDB(**obj_in.dict(by_alias=True), hashed_password=passwordhash)
    doc_data = jsonable_encoder(user)
    db = get_default_bucket()
    collection = db["users"]
    res = collection.insert_one(doc_data)

    user_db = collection.find_one({"_id": ObjectId(res.inserted_id)})
    user = UserInDB(**user_db)

    return user
def test_get_user_by_user(client: TestClient, normal_user_token_headers: dict,
                          db: Session) -> None:
    email = random_email()
    password = random_lower_string()
    user_in = UserCreate(email=email, password=password)
    user = crud.user.create(db, obj_in=user_in)
    user_id = user.id
    r = client.get(
        f"{settings.API_V1_STR}/users/{user_id}",
        headers=normal_user_token_headers,
    )
    assert r.status_code == 400