Exemplo n.º 1
0
def test_create_client(db: Session) -> None:
    email = random_email()
    password = random_lower_string()
    client_in = ClientCreate(email=email, password=password)
    client = crud.client.create(db, obj_in=client_in)
    assert client.email == email
    assert hasattr(client, "hashed_password")
Exemplo n.º 2
0
def test_authenticate_client(db: Session) -> None:
    email = random_email()
    password = random_lower_string()
    client_in = ClientCreate(email=email, password=password)
    client = crud.client.create(db, obj_in=client_in)
    authenticated_client = crud.client.authenticate(db,
                                                    email=email,
                                                    password=password)
    assert authenticated_client
    assert client.email == authenticated_client.email
Exemplo n.º 3
0
def test_get_access_token_incorrect_data(test_client: TestClient,
                                         db: Session) -> None:
    username = random_email()
    password = random_lower_string()
    login_data = {
        "username": username,
        "password": password,
    }
    r = test_client.post(f"{settings.API_V1_STR}/login/access-token",
                         data=login_data)
    assert r.status_code == 400
Exemplo n.º 4
0
def test_get_client(db: Session) -> None:
    password = random_lower_string()
    username = random_email()
    client_in = ClientCreate(email=username,
                             password=password,
                             is_superuser=True)
    client = crud.client.create(db, obj_in=client_in)
    client_2 = crud.client.get(db, id=client.id)
    assert client_2
    assert client.email == client_2.email
    assert jsonable_encoder(client) == jsonable_encoder(client_2)
Exemplo n.º 5
0
def test_create_client_by_normal_client(
        test_client: TestClient,
        normal_client_token_headers: Dict[str, str]) -> None:
    username = random_email()
    password = random_lower_string()
    data = {"email": username, "password": password}
    r = test_client.post(
        f"{settings.API_V1_STR}/clients/",
        headers=normal_client_token_headers,
        json=data,
    )
    assert r.status_code == 200
Exemplo n.º 6
0
def test_update_client(db: Session) -> None:
    password = random_lower_string()
    email = random_email()
    client_in = ClientCreate(email=email, password=password)
    client = crud.client.create(db, obj_in=client_in)
    new_password = random_lower_string()
    client_in_update = ClientUpdate(password=new_password)
    crud.client.update(db, db_obj=client, obj_in=client_in_update)
    client_2 = crud.client.get(db, id=client.id)
    assert client_2
    assert client.email == client_2.email
    assert verify_password(new_password, client_2.hashed_password)
Exemplo n.º 7
0
def test_get_access_token(test_client: TestClient, db: Session) -> None:
    username = random_email()
    password = random_lower_string()
    client_in = ClientCreate(email=username, password=password)
    crud.client.create(db, obj_in=client_in)
    login_data = {
        "username": username,
        "password": password,
    }
    r = test_client.post(f"{settings.API_V1_STR}/login/access-token",
                         data=login_data)
    tokens = r.json()
    assert r.status_code == 200
    assert "access_token" in tokens
    assert tokens["access_token"]
Exemplo n.º 8
0
def test_create_client_existing_username(test_client: TestClient,
                                         normal_client_token_headers: dict,
                                         db: Session) -> None:
    username = random_email()
    password = random_lower_string()
    user_in = ClientCreate(email=username, password=password)
    crud.client.create(db, obj_in=user_in)
    data = {"email": username, "password": password}
    r = test_client.post(
        f"{settings.API_V1_STR}/clients/",
        headers=normal_client_token_headers,
        json=data,
    )
    created_user = r.json()
    assert r.status_code == 400
    assert "_id" not in created_user
Exemplo n.º 9
0
def test_create_user_new_email(test_client: TestClient,
                               normal_client_token_headers: dict,
                               db: Session) -> None:
    username = random_email()
    password = random_lower_string()
    data = {"email": username, "password": password}
    r = test_client.post(
        f"{settings.API_V1_STR}/clients/",
        headers=normal_client_token_headers,
        json=data,
    )
    assert 200 <= r.status_code < 300
    created_client = r.json()
    client = crud.client.get_by_email(db, email=username)
    assert client
    assert client.email == created_client["email"]
Exemplo n.º 10
0
def create_random_client(db: Session) -> Client:
    email = random_email()
    password = random_lower_string()
    client_in = ClientCreate(username=email, email=email, password=password)
    client = crud.client.create(db=db, obj_in=client_in)
    return client
Exemplo n.º 11
0
def test_not_authenticate_client(db: Session) -> None:
    email = random_email()
    password = random_lower_string()
    client = crud.client.authenticate(db, email=email, password=password)
    assert client is None