# Path: atlassian_jwt_auth/contrib/tests/utils.py
# def get_static_retriever_class(keys):
# 
#     class StaticPublicKeyRetriever(object):
#         """ Retrieves a key from a static list of public keys
#         (for use in tests only) """
# 
#         def __init__(self, *args, **kwargs):
#             self.keys = keys
# 
#         def retrieve(self, key_identifier, **requests_kwargs):
#             return self.keys[key_identifier.key_id]
# 
#     return StaticPublicKeyRetriever
# 
# Path: atlassian_jwt_auth/frameworks/wsgi/middleware.py
# class ASAPMiddleware(object):
#     def __init__(self, handler, settings):
#         self._next = handler
#         self._backend = WSGIBackend(settings)
#         self._verifier = self._backend.get_verifier()
# 
#     def __call__(self, environ, start_response):
#         settings = self._backend.settings
#         request = Request(environ, start_response)
#         error_response = _process_asap_token(
#             request, self._backend, settings, verifier=self._verifier
#         )
#         if error_response is not None:
#             return error_response
# 
#         return self._next(environ, start_response)
# 
# Path: atlassian_jwt_auth/tests/utils.py
# def get_new_rsa_private_key_in_pem_format():
# def get_public_key_pem_for_private_key_pem(private_key_pem):
# def get_example_jwt_auth_signer(**kwargs):
# def create_token(issuer, audience, key_id, private_key, subject=None):
#     def get_new_private_key_in_pem_format(self):
#     def algorithm(self):
#     def get_new_private_key_in_pem_format(self):
#     def algorithm(self):
#     def get_new_private_key_in_pem_format(self):
# class BaseJWTAlgorithmTestMixin(object):
# class RS256KeyTestMixin(object):
# class ES256KeyTestMixin(object):
# 
# Path: atlassian_jwt_auth/tests/utils.py
# def create_token(issuer, audience, key_id, private_key, subject=None):
#     """" returns a token based upon the supplied parameters. """
#     signer = atlassian_jwt_auth.create_signer(
#         issuer, key_id, private_key, subject=subject)
#     return signer.generate_jwt(audience)


import unittest
from atlassian_jwt_auth.contrib.tests.utils import get_static_retriever_class
from atlassian_jwt_auth.frameworks.wsgi.middleware import ASAPMiddleware
from atlassian_jwt_auth.tests import utils
from atlassian_jwt_auth.tests.utils import (
    create_token,
)
    def test_request_with_duplicate_jti_is_accepted_as_per_setting(self):
        self.config['ASAP_CHECK_JTI_UNIQUENESS'] = False
        self._assert_request_with_duplicate_jti_is_accepted()

    def test_request_with_invalid_audience_is_rejected(self):
        token = create_token(
            'client-app', 'invalid-audience',
            'client-app/key01', self._private_key_pem
        )
        body, resp_info, environ = self.send_request(token=token)
        self.assertEqual(resp_info['status'], '401 Unauthorized')
        self.assertNotIn('ATL_ASAP_CLAIMS', environ)

    def test_request_with_invalid_token_is_rejected(self):
        body, resp_info, environ = self.send_request(token=b'notavalidtoken')
        self.assertEqual(resp_info['status'], '401 Unauthorized')
        self.assertNotIn('ATL_ASAP_CLAIMS', environ)

    def test_request_subject_and_issue_not_matching(self):
        token = create_token(
            'client-app', 'server-app',
            'client-app/key01', self._private_key_pem,
            subject='different'
        )
        body, resp_info, environ = self.send_request(token=token)
        self.assertEqual(resp_info['status'], '401 Unauthorized')
        self.assertNotIn('ATL_ASAP_CLAIMS', environ)

    def test_request_subject_does_not_need_to_match_issuer_from_settings(self):
        self.config['ASAP_SUBJECT_SHOULD_MATCH_ISSUER'] = False

# Path: atlassian_jwt_auth/contrib/tests/utils.py
# def get_static_retriever_class(keys):
# 
#     class StaticPublicKeyRetriever(object):
#         """ Retrieves a key from a static list of public keys
#         (for use in tests only) """
# 
#         def __init__(self, *args, **kwargs):
#             self.keys = keys
# 
#         def retrieve(self, key_identifier, **requests_kwargs):
#             return self.keys[key_identifier.key_id]
# 
#     return StaticPublicKeyRetriever
# 
# Path: atlassian_jwt_auth/frameworks/wsgi/middleware.py
# class ASAPMiddleware(object):
#     def __init__(self, handler, settings):
#         self._next = handler
#         self._backend = WSGIBackend(settings)
#         self._verifier = self._backend.get_verifier()
# 
#     def __call__(self, environ, start_response):
#         settings = self._backend.settings
#         request = Request(environ, start_response)
#         error_response = _process_asap_token(
#             request, self._backend, settings, verifier=self._verifier
#         )
#         if error_response is not None:
#             return error_response
# 
#         return self._next(environ, start_response)
# 
# Path: atlassian_jwt_auth/tests/utils.py
# def get_new_rsa_private_key_in_pem_format():
# def get_public_key_pem_for_private_key_pem(private_key_pem):
# def get_example_jwt_auth_signer(**kwargs):
# def create_token(issuer, audience, key_id, private_key, subject=None):
#     def get_new_private_key_in_pem_format(self):
#     def algorithm(self):
#     def get_new_private_key_in_pem_format(self):
#     def algorithm(self):
#     def get_new_private_key_in_pem_format(self):
# class BaseJWTAlgorithmTestMixin(object):
# class RS256KeyTestMixin(object):
# class ES256KeyTestMixin(object):
# 
# Path: atlassian_jwt_auth/tests/utils.py
# def create_token(issuer, audience, key_id, private_key, subject=None):
#     """" returns a token based upon the supplied parameters. """
#     signer = atlassian_jwt_auth.create_signer(
#         issuer, key_id, private_key, subject=subject)
#     return signer.generate_jwt(audience)


# Path: atlassian_jwt_auth/frameworks/wsgi/tests/test_wsgi.py
import unittest
from atlassian_jwt_auth.contrib.tests.utils import get_static_retriever_class
from atlassian_jwt_auth.frameworks.wsgi.middleware import ASAPMiddleware
from atlassian_jwt_auth.tests import utils
from atlassian_jwt_auth.tests.utils import (
    create_token,
)

    def test_request_with_duplicate_jti_is_accepted_as_per_setting(self):
        self.config['ASAP_CHECK_JTI_UNIQUENESS'] = False
        self._assert_request_with_duplicate_jti_is_accepted()

    def test_request_with_invalid_audience_is_rejected(self):
        token = create_token(
            'client-app', 'invalid-audience',
            'client-app/key01', self._private_key_pem
        )
        body, resp_info, environ = self.send_request(token=token)
        self.assertEqual(resp_info['status'], '401 Unauthorized')
        self.assertNotIn('ATL_ASAP_CLAIMS', environ)

    def test_request_with_invalid_token_is_rejected(self):
        body, resp_info, environ = self.send_request(token=b'notavalidtoken')
        self.assertEqual(resp_info['status'], '401 Unauthorized')
        self.assertNotIn('ATL_ASAP_CLAIMS', environ)

    def test_request_subject_and_issue_not_matching(self):
        token = create_token(
            'client-app', 'server-app',
            'client-app/key01', self._private_key_pem,
            subject='different'
        )
        body, resp_info, environ = self.send_request(token=token)
        self.assertEqual(resp_info['status'], '401 Unauthorized')
        self.assertNotIn('ATL_ASAP_CLAIMS', environ)

    def test_request_subject_does_not_need_to_match_issuer_from_settings(self):
        self.config['ASAP_SUBJECT_SHOULD_MATCH_ISSUER'] = False

        token = create_token(