Skip to content

Commit

Permalink
Fix tests and warnings in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKs committed Apr 4, 2022
1 parent 8c4c366 commit 3702e93
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 86 deletions.
10 changes: 5 additions & 5 deletions demo_project/api/api_v1/endpoints/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
operation_id='helloGraph',
dependencies=[Depends(azure_scheme)],
)
async def graph_world(request: Request) -> Any:
async def graph_world(request: Request) -> Any: # noqa: ANN401
"""
An example on how to use "on behalf of"-flow to fetch a graph token and then fetch data from graph.
"""
Expand All @@ -37,17 +37,17 @@ async def graph_world(request: Request) -> Any:

if obo_response.is_success:
# Call the graph `/me` endpoint to fetch more information about the current user, using the new token
graph_response = httpx.Response = await client.get(
graph_response: httpx.Response = await client.get(
'https://graph.microsoft.com/v1.0/me',
headers={'Authorization': f'Bearer {obo_response.json()["access_token"]}'},
)
graph_response = graph_response.json()
graph = graph_response.json()
else:
graph_response = 'skipped'
graph = 'skipped'

# Return all the information to the end user
return (
{'claims': jwt.get_unverified_claims(token=request.state.user.access_token)}
| {'obo_response': obo_response.json()}
| {'graph_response': graph_response}
| {'graph_response': graph}
)
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ addopts = --allow-hosts=localhost,127.0.0.1,::1
env_files =
tests/.env.test
env_override_existing_values = 1
asyncio_mode=auto
Empty file.
197 changes: 118 additions & 79 deletions tests/test_openapi_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,124 +3,163 @@
from fastapi.testclient import TestClient

openapi_schema = {
'openapi': '3.0.2',
'info': {
'title': 'My Project',
'description': '## Welcome to my API! \n This is my description, written in `markdown`',
'version': '1.0.0',
},
'paths': {
'/api/v1/hello': {
'get': {
'tags': ['hello'],
'summary': 'Say hello',
'description': 'Wonder who we say hello to?',
'operationId': 'helloWorld',
'responses': {
'200': {
'description': 'Successful Response',
'content': {
'application/json': {'schema': {'$ref': '#/components/schemas/HelloWorldResponse'}}
},
}
},
'security': [{'Azure AD - PKCE, Single-tenant': []}],
}
},
'/api/v1/hello-multi-auth': {
'get': {
'tags': ['hello'],
'summary': 'Say hello with an API key',
'description': 'Wonder how this auth is done?',
'operationId': 'helloWorldApiKey',
'responses': {
'200': {
'description': 'Successful Response',
'content': {'application/json': {'schema': {'$ref': '#/components/schemas/TokenType'}}},
}
},
'security': [{'Azure AD - PKCE, Multi-tenant': []}, {'APIKeyHeader': []}],
}
},
},
'components': {
'schemas': {
'HelloWorldResponse': {
'title': 'HelloWorldResponse',
'required': ['hello', 'user'],
'type': 'object',
'properties': {
'hello': {'title': 'Hello', 'type': 'string', 'description': 'What we\'re saying hello to'},
'hello': {
'description': 'What ' "we're " 'saying ' 'hello ' 'to',
'title': 'Hello',
'type': 'string',
},
'user': {
'title': 'User',
'allOf': [{'$ref': '#/components/schemas/User'}],
'description': 'The user object',
'description': 'The ' 'user ' 'object',
'title': 'User',
},
},
'required': ['hello', 'user'],
'title': 'HelloWorldResponse',
'type': 'object',
},
'TokenType': {
'title': 'TokenType',
'required': ['api_key', 'azure_auth'],
'type': 'object',
'properties': {
'api_key': {'title': 'Api Key', 'type': 'boolean', 'description': 'API key was used'},
'azure_auth': {'title': 'Azure Auth', 'type': 'boolean', 'description': 'Azure auth was used'},
'api_key': {'description': 'API ' 'key ' 'was ' 'used', 'title': 'Api ' 'Key', 'type': 'boolean'},
'azure_auth': {
'description': 'Azure ' 'auth ' 'was ' 'used',
'title': 'Azure ' 'Auth',
'type': 'boolean',
},
},
'required': ['api_key', 'azure_auth'],
'title': 'TokenType',
'type': 'object',
},
'User': {
'title': 'User',
'required': ['aud', 'tid', 'claims', 'access_token'],
'type': 'object',
'properties': {
'aud': {'title': 'Aud', 'type': 'string', 'description': 'Audience'},
'tid': {'title': 'Tid', 'type': 'string', 'description': 'Tenant ID'},
'access_token': {
'description': 'The '
'access_token. '
'Can '
'be '
'used '
'for '
'fetching '
'the '
'Graph '
'API',
'title': 'Access ' 'Token',
'type': 'string',
},
'aud': {'description': 'Audience', 'title': 'Aud', 'type': 'string'},
'claims': {'description': 'The ' 'entire ' 'decoded ' 'token', 'title': 'Claims', 'type': 'object'},
'name': {'description': 'Name', 'title': 'Name', 'type': 'string'},
'roles': {
'default': [],
'description': 'Roles ' '(Groups) ' 'the ' 'user ' 'has ' 'for ' 'this ' 'app',
'items': {'type': 'string'},
'title': 'Roles',
'type': 'array',
'items': {'type': 'string'},
'description': 'Roles (Groups) the user has for this app',
'default': [],
},
'claims': {'title': 'Claims', 'type': 'object', 'description': 'The entire decoded token'},
'scp': {'title': 'Scp', 'type': 'string', 'description': 'Scope'},
'name': {'title': 'Name', 'type': 'string', 'description': 'Name'},
'access_token': {
'title': 'Access Token',
'type': 'string',
'description': 'The access_token. Can be used for fetching the Graph API',
},
'scp': {'description': 'Scope', 'title': 'Scp', 'type': 'string'},
'tid': {'description': 'Tenant ' 'ID', 'title': 'Tid', 'type': 'string'},
},
'required': ['aud', 'tid', 'claims', 'access_token'],
'title': 'User',
'type': 'object',
},
},
'securitySchemes': {
'Azure AD - PKCE, Single-tenant': {
'type': 'oauth2',
'description': '`Leave client_secret blank`',
'APIKeyHeader': {'in': 'header', 'name': 'TEST-API-KEY', 'type': 'apiKey'},
'Azure AD - PKCE, Multi-tenant': {
'description': '`Leave ' 'client_secret ' 'blank`',
'flows': {
'authorizationCode': {
'authorizationUrl': 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
'scopes': {
'api://oauth299-9999-9999-abcd-efghijkl1234567890/user_impersonation': '**No client secret needed, leave blank**'
'api://oauth299-9999-9999-abcd-efghijkl1234567890/user_impersonation': 'User '
'impersonation'
},
'authorizationUrl': 'https://login.microsoftonline.com/intility_tenant_id/oauth2/v2.0/authorize',
'tokenUrl': 'https://login.microsoftonline.com/intility_tenant_id/oauth2/v2.0/token',
'tokenUrl': 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
}
},
'type': 'oauth2',
},
'Azure AD - PKCE, Multi-tenant': {
'Azure AD - PKCE, Single-tenant': {
'description': '`Leave ' 'client_secret ' 'blank`',
'flows': {
'authorizationCode': {
'authorizationUrl': 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
'authorizationUrl': 'https://login.microsoftonline.com/intility_tenant_id/oauth2/v2.0/authorize',
'scopes': {
'api://oauth299-9999-9999-abcd-efghijkl1234567890/user_impersonation': 'User '
'impersonation'
'api://oauth299-9999-9999-abcd-efghijkl1234567890/user_impersonation': '**No '
'client '
'secret '
'needed, '
'leave '
'blank**'
},
'tokenUrl': 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
'tokenUrl': 'https://login.microsoftonline.com/intility_tenant_id/oauth2/v2.0/token',
}
},
'type': 'oauth2',
},
'APIKeyHeader': {'type': 'apiKey', 'in': 'header', 'name': 'TEST-API-KEY'},
},
},
'info': {
'description': '## Welcome to my API! \n' ' This is my description, written in `markdown`',
'title': 'My Project',
'version': '1.0.0',
},
'openapi': '3.0.2',
'paths': {
'/api/v1/hello': {
'get': {
'description': 'Wonder who we say hello ' 'to?',
'operationId': 'helloWorld',
'responses': {
'200': {
'content': {
'application/json': {'schema': {'$ref': '#/components/schemas/HelloWorldResponse'}}
},
'description': 'Successful ' 'Response',
}
},
'security': [{'Azure AD - PKCE, Single-tenant': []}],
'summary': 'Say hello',
'tags': ['hello'],
}
},
'/api/v1/hello-graph': {
'get': {
'description': 'An example on how '
'to use "on behalf '
'of"-flow to fetch a '
'graph token and '
'then fetch data '
'from graph.',
'operationId': 'helloGraph',
'responses': {
'200': {'content': {'application/json': {'schema': {}}}, 'description': 'Successful ' 'Response'}
},
'security': [{'Azure AD - PKCE, Single-tenant': []}],
'summary': 'Fetch graph API using ' 'OBO',
'tags': ['graph'],
}
},
'/api/v1/hello-multi-auth': {
'get': {
'description': 'Wonder how ' 'this auth is ' 'done?',
'operationId': 'helloWorldApiKey',
'responses': {
'200': {
'content': {'application/json': {'schema': {'$ref': '#/components/schemas/TokenType'}}},
'description': 'Successful ' 'Response',
}
},
'security': [{'Azure AD - PKCE, Multi-tenant': []}, {'APIKeyHeader': []}],
'summary': 'Say hello with an ' 'API key',
'tags': ['hello'],
}
},
},
}
Expand Down
8 changes: 6 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ def build_openid_keys(empty_keys: bool = False, no_valid_keys: bool = False) ->
encryption_algorithm=serialization.NoEncryption(),
),
'RS256',
).to_dict(),
)
.public_key()
.to_dict(),
},
{
'use': 'sig',
Expand All @@ -189,7 +191,9 @@ def build_openid_keys(empty_keys: bool = False, no_valid_keys: bool = False) ->
encryption_algorithm=serialization.NoEncryption(),
),
'RS256',
).to_dict(),
)
.public_key()
.to_dict(),
},
]
}
Expand Down

0 comments on commit 3702e93

Please sign in to comment.