Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Max authored and Riuzaky77 committed Jul 15, 2022
1 parent 09c5bb9 commit f02c4aa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
17 changes: 13 additions & 4 deletions tests/test_security_http_client_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ def __call__(self, r):

@app.get("/users/me")
def read_current_user(credentials: HTTPClientCredentials = Security(security)):
return {
"client_id": credentials.client_id,
"client_secret": credentials.client_secret,
}
if credentials:
return {
"client_id": credentials.client_id,
"client_secret": credentials.client_secret,
}


client = TestClient(app)
Expand Down Expand Up @@ -84,3 +85,11 @@ def test_security_http_basic_non_basic_credentials():
assert response.status_code == 401, response.text
assert response.headers["WWW-Authenticate"] == "Basic"
assert response.json() == {"detail": "Invalid authentication credentials"}


def test_no_return_none():
security.auto_error = False
response = client.get("/users/me")
assert response.status_code == 200, response.text
assert response.json() is None
security.auto_error = True
26 changes: 1 addition & 25 deletions tests/test_security_oauth2_client_credentials.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,20 @@
from typing import Optional

from fastapi import FastAPI, Security
from fastapi.security import (
HTTPBasicClientCredentials,
OAuth2ClientCredentials,
OAuth2ClientCredentialsRequestForm,
)
from fastapi.security import OAuth2ClientCredentials, OAuth2ClientCredentialsRequestForm
from fastapi.testclient import TestClient

app = FastAPI()

oauth2_scheme = OAuth2ClientCredentials(tokenUrl="token", auto_error=True)

token_scheme = HTTPBasicClientCredentials(
auto_error=False, scheme_name="oAuth2ClientCredentials"
)


@app.get("/items/")
async def read_items(token: Optional[str] = Security(oauth2_scheme)):
if token:
return {"token": token}


# @app.post("/token")
# def create_access_token(
# form: OAuth2ClientCredentialsRequestForm = Depends(),
# basic_credentials: Optional[HTTPClientCredentials] = Depends(token_scheme),
# ):
# if form.client_id and form.client_secret:
# client_id = form.client_id
# client_secret = form.client_secret
# elif basic_credentials:
# client_id = basic_credentials.client_id
# client_secret = basic_credentials.client_secret
# else:
# HTTPException(status_code=400, detail="Client credentials not provided")
# return {"token": "jwt_token"}


client = TestClient(app)

openapi_schema = {
Expand Down

0 comments on commit f02c4aa

Please sign in to comment.