Skip to content

Commit

Permalink
Add test for flask for throwing exception from security function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruwann committed Apr 13, 2022
1 parent b9d93a6 commit e44b416
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/api/test_secure_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def test_security(oauth_requests, secure_endpoint_app):
assert response.data == b'"Unauthenticated"\n'
assert response.status_code == 200

# security function throws exception
response = app_client.get('/v1.0/auth-exception', headers={'X-Api-Key': 'foo'})
assert response.status_code == 401


def test_checking_that_client_token_has_all_necessary_scopes(
oauth_requests, secure_endpoint_app):
Expand Down
9 changes: 9 additions & 0 deletions tests/fakeapi/hello/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from connexion import NoContent, ProblemException, context, request
from flask import jsonify, redirect, send_file

from connexion.exceptions import OAuthProblem


class DummyClass:
@classmethod
Expand Down Expand Up @@ -463,6 +465,9 @@ def optional_auth(**kwargs):
return "Authenticated"


def auth_exception():
return 'foo'

def test_args_kwargs(*args, **kwargs):
return kwargs

Expand Down Expand Up @@ -569,6 +574,10 @@ def jwt_info(token):
return None


def apikey_exception(token):
raise OAuthProblem()


def get_add_operation_on_http_methods_only():
return ""

Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/secure_endpoint/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ paths:
responses:
'200':
description: some response
/auth-exception:
get:
summary: Test security handler function that raises an exception
description: Throw error from security function
operationId: fakeapi.hello.auth_exception
security:
- auth_exception: []
responses:
'200':
description: some response

servers:
- url: /v1.0
components:
Expand All @@ -161,3 +172,8 @@ components:
scheme: bearer
bearerFormat: JWT
x-bearerInfoFunc: fakeapi.hello.jwt_info
auth_exception:
type: apiKey
name: X-Api-Key
in: header
x-apikeyInfoFunc: fakeapi.hello.apikey_exception
17 changes: 17 additions & 0 deletions tests/fixtures/secure_endpoint/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ securityDefinitions:
x-authentication-scheme: Bearer
x-bearerInfoFunc: fakeapi.hello.jwt_info

auth_exception:
type: apiKey
name: X-Api-Key
in: header
x-apikeyInfoFunc: fakeapi.hello.apikey_exception

paths:
/byesecure/{name}:
get:
Expand Down Expand Up @@ -171,3 +177,14 @@ paths:
responses:
'200':
description: some response

/auth-exception:
get:
summary: Test security handler function that raises an exception
description: Throw error from security function
operationId: fakeapi.hello.auth_exception
security:
- auth_exception: []
responses:
'200':
description: some response

0 comments on commit e44b416

Please sign in to comment.