コード例 #1
0
def test_create(client: TestClient):
    token = create_access_token(data={"sub": "test", "scopes": ["default"]})
    response = client.post(
        "api/users",
        headers={"Authorization": f"bearer {token}"},
        json={
            "name": "Alexandre Fialho",
            "document": "390.430.138-11",
            "birth_date": "1995-10-05",
            "address": {
                "postal_code": "08412-070"
            }
        },
    )
    assert response.status_code == 201
    assert response.json() == {
        "name": "Alexandre Fialho",
        "document": "390.430.138-11",
        "birth_date": "1995-10-05",
        "id": 1,
        "address": {
            "city": "São Paulo",
            "postal_code": "08412-070",
            "street": "Rua Antônio Silvestre Ferreira",
            "neighborhoods": "Vila Cruzeiro",
            "state": "Sp",
            "id": 1,
        }
    }
コード例 #2
0
def test_put_inexistent(client: TestClient):
    token = create_access_token(data={"sub": "test", "scopes": ["default"]})
    response = client.put(
        "api/users/2",
        headers={"Authorization": f"bearer {token}"},
        json={
            "name": "Alexandre Fialho de Araujo",
        },
    )
    assert response.status_code == 404
    assert response.json() == {"detail": "User not found"}
コード例 #3
0
def test_create_existing(client: TestClient):
    token = create_access_token(data={"sub": "test", "scopes": ["default"]})
    response = client.post(
        "api/users",
        headers={"Authorization": f"bearer {token}"},
        json={
            "name": "Alexandre Fialho",
            "document": "390.430.138-11",
            "birth_date": "1995-10-05",
        },
    )
    assert response.status_code == 400
    assert response.json() == {"detail": "User already exists"}
コード例 #4
0
async def login_for_access_token(
        form_data: OAuth2PasswordRequestForm = Depends(),
        db_session: Session = Depends(DbSession),
):
    user = authenticate_user(db_session=db_session,
                             username=form_data.username,
                             password=form_data.password)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )
    access_token_expires = timedelta(
        minutes=int(settings.ACCESS_TOKEN_EXPIRE_MINUTES))
    access_token = create_access_token(
        data={
            "sub": user.username,
            "scopes": form_data.scopes
        },
        expires_delta=access_token_expires,
    )
    return {"access_token": access_token, "token_type": "bearer"}
コード例 #5
0
def test_delete_inexistent(client: TestClient):
    token = create_access_token(data={"sub": "test", "scopes": ["default"]})
    response = client.delete("api/users/32",
                             headers={"Authorization": f"bearer {token}"})
    assert response.status_code == 404
    assert response.json() == {"detail": "User not found"}
コード例 #6
0
def test_delete(client: TestClient):
    token = create_access_token(data={"sub": "test", "scopes": ["default"]})
    response = client.delete("api/users/1",
                             headers={"Authorization": f"bearer {token}"})
    assert response.status_code == 204