Skip to content

Commit 3da9d8f

Browse files
committed
test: update to handle new interface
1 parent 1e07dc9 commit 3da9d8f

File tree

5 files changed

+51
-19
lines changed

5 files changed

+51
-19
lines changed

.pre-commit-config.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,15 @@ repos:
4141
rev: "1.7.0"
4242
hooks:
4343
- id: bandit
44-
entry: bandit -c .bandit.yml
44+
entry: bandit -c .bandit.yml
45+
46+
- repo: local
47+
hooks:
48+
- id: pytest
49+
name: pytest
50+
entry: .venv/bin/pytest
51+
language: script
52+
pass_filenames: false
53+
# alternatively you could `types: [python]` so it only runs when python files change
54+
# though tests might be invalidated if you were to say change a data file
55+
always_run: true

fastapi_oidc/auth.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ def test_auth(authenticated_user: AuthenticatedUser = Depends(authenticate_user)
4040
from fastapi_oidc import discovery
4141
from fastapi_oidc.types import IDToken
4242

43-
# class AuthBearer(HTTPBearer):
44-
# async def __call__(self, request: Request):
45-
# return await super().__call__(request)
46-
4743

4844
class OAuth2Facade(OAuth2):
4945
async def __call__(self, request: Request) -> Optional[str]:
@@ -232,7 +228,9 @@ def authenticate_user(
232228
else:
233229
return None
234230

235-
if not set(security_scopes.scopes).issubset(id_token["scope"].split(" ")):
231+
if not set(security_scopes.scopes).issubset(
232+
id_token.get("scope", "").split(" ")
233+
):
236234
if auto_error:
237235
raise HTTPException(
238236
status.HTTP_401_UNAUTHORIZED,

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,7 @@ class functions:
137137
auth_server = lambda **_: oidc_discovery
138138
public_keys = lambda _: public_key
139139
signing_algos = lambda x: x["id_token_signing_alg_values_supported"]
140+
authorization_url = lambda x: x["authorization_endpoint"]
141+
token_url = lambda x: x["token_endpoint"]
140142

141143
return lambda *args, **kwargs: functions

tests/test_auth.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from fastapi_oidc import auth
1+
from fastapi.security import HTTPAuthorizationCredentials
2+
from fastapi.security import SecurityScopes
3+
4+
import fastapi_oidc
5+
from fastapi_oidc import Auth
26
from fastapi_oidc.types import IDToken
37

48

@@ -9,13 +13,17 @@ def test__authenticate_user(
913
config_w_aud,
1014
test_email,
1115
):
12-
13-
monkeypatch.setattr(auth.discovery, "configure", mock_discovery)
16+
monkeypatch.setattr(fastapi_oidc.auth.discovery, "configure", mock_discovery)
1417

1518
token = token_with_audience
1619

17-
authenticate_user = auth.get_auth(**config_w_aud)
18-
id_token = IDToken(**authenticate_user(auth_header=f"Bearer {token}"))
20+
auth = Auth(**config_w_aud)
21+
id_token = auth.required(
22+
security_scopes=SecurityScopes(scopes=[]),
23+
authorization_credentials=HTTPAuthorizationCredentials(
24+
scheme="hello", credentials=token
25+
),
26+
)
1927

2028
assert id_token.email == test_email # nosec
2129
assert id_token.aud == config_w_aud["audience"]
@@ -30,13 +38,18 @@ def test__authenticate_user_no_aud(
3038
test_email,
3139
):
3240

33-
monkeypatch.setattr(auth.discovery, "configure", mock_discovery)
41+
monkeypatch.setattr(fastapi_oidc.auth.discovery, "configure", mock_discovery)
3442

3543
token = token_without_audience
3644

37-
authenticate_user = auth.get_auth(**no_audience_config)
45+
auth = Auth(**no_audience_config)
3846

39-
id_token = IDToken(**authenticate_user(auth_header=f"Bearer {token}"))
47+
id_token = auth.required(
48+
security_scopes=SecurityScopes(scopes=[]),
49+
authorization_credentials=HTTPAuthorizationCredentials(
50+
scheme="hello", credentials=token
51+
),
52+
)
4053

4154
assert id_token.email == test_email # nosec
4255

@@ -47,12 +60,20 @@ def test__authenticate_user_returns_custom_tokens(
4760
class CustomToken(IDToken):
4861
custom_field: str = "OnlySlightlyBent"
4962

50-
monkeypatch.setattr(auth.discovery, "configure", mock_discovery)
63+
monkeypatch.setattr(fastapi_oidc.auth.discovery, "configure", mock_discovery)
5164

5265
token = token_without_audience
5366

54-
authenticate_user = auth.get_auth(**no_audience_config)
55-
56-
custom_token = CustomToken(**authenticate_user(auth_header=f"Bearer {token}"))
67+
auth = Auth(
68+
**no_audience_config,
69+
idtoken_model=CustomToken,
70+
)
71+
72+
custom_token = auth.required(
73+
security_scopes=SecurityScopes(scopes=[]),
74+
authorization_credentials=HTTPAuthorizationCredentials(
75+
scheme="hello", credentials=token
76+
),
77+
)
5778

5879
assert custom_token.custom_field == "OnlySlightlyBent"

tests/test_import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
def test_can_import_things_from_project_root():
55
assert fastapi_oidc.IDToken
66
assert fastapi_oidc.OktaIDToken
7-
assert fastapi_oidc.get_auth
7+
assert fastapi_oidc.Auth

0 commit comments

Comments
 (0)