# Path: yithlibraryserver/testing.py
# MONGO_URI = 'mongodb://localhost:27017/test-yith-library'
# class FakeRequest(DummyRequest):
# class TestCase(unittest.TestCase):
#     def __init__(self, *args, **kwargs):
#     def setUp(self):
#     def tearDown(self):
#     def set_user_cookie(self, user_id):
#     def add_to_session(self, data):
#     def get_session(self, response):
# 
# Path: yithlibraryserver/cors.py
# class CORSManager(object):
# 
#     def __init__(self, global_allowed_origins):
#         self.global_allowed_origins = global_allowed_origins.split(' ')
# 
#     def add_cors_header(self, request, response):
#         if 'Origin' in request.headers:
#             origin = request.headers['Origin']
# 
#             client_id = request.GET.get('client_id')
#             if client_id is None:
#                 allowed_origins = self.global_allowed_origins
#             else:
#                 allowed_origins = self._get_allowed_origins_for_client(
#                     request, client_id)
# 
#             if origin in allowed_origins:
#                 log.debug('Origin %s is allowed: %s' %
#                           (origin, ' '.join(allowed_origins)))
#                 response.headers['Access-Control-Allow-Origin'] = origin
#             else:
#                 log.debug('Origin %s is not allowed: %s' %
#                           (origin, ' '.join(allowed_origins)))
# 
#     def _get_allowed_origins_for_client(self, request, client_id):
#         app = request.db.applications.find_one({'client_id': client_id})
#         if app is None:
#             return []
#         else:
#             return app['authorized_origins']


from pyramid.testing import DummyRequest
from yithlibraryserver import testing
from yithlibraryserver.cors import CORSManager
    clean_collections = ('applications', )

    def test_cors_headers_global_origins_access_denied(self):
        cm = CORSManager('')

        request = DummyRequest(headers={'Origin': 'foo'})
        response = request.response

        cm.add_cors_header(request, response)

        self.assertEqual(response.headers, {
                'Content-Type': 'text/html; charset=UTF-8',
                'Content-Length': '0',
                })

    def test_cors_headers_global_origins(self):
        cm = CORSManager('http://localhost')

        request = DummyRequest(headers={'Origin': 'http://localhost'})
        response = request.response

        cm.add_cors_header(request, response)

        self.assertEqual(response.headers, {
                'Content-Type': 'text/html; charset=UTF-8',
                'Content-Length': '0',
                'Access-Control-Allow-Origin': 'http://localhost',
                })

    def test_cors_headers_app_origins_access_denied(self):

# Path: yithlibraryserver/testing.py
# MONGO_URI = 'mongodb://localhost:27017/test-yith-library'
# class FakeRequest(DummyRequest):
# class TestCase(unittest.TestCase):
#     def __init__(self, *args, **kwargs):
#     def setUp(self):
#     def tearDown(self):
#     def set_user_cookie(self, user_id):
#     def add_to_session(self, data):
#     def get_session(self, response):
# 
# Path: yithlibraryserver/cors.py
# class CORSManager(object):
# 
#     def __init__(self, global_allowed_origins):
#         self.global_allowed_origins = global_allowed_origins.split(' ')
# 
#     def add_cors_header(self, request, response):
#         if 'Origin' in request.headers:
#             origin = request.headers['Origin']
# 
#             client_id = request.GET.get('client_id')
#             if client_id is None:
#                 allowed_origins = self.global_allowed_origins
#             else:
#                 allowed_origins = self._get_allowed_origins_for_client(
#                     request, client_id)
# 
#             if origin in allowed_origins:
#                 log.debug('Origin %s is allowed: %s' %
#                           (origin, ' '.join(allowed_origins)))
#                 response.headers['Access-Control-Allow-Origin'] = origin
#             else:
#                 log.debug('Origin %s is not allowed: %s' %
#                           (origin, ' '.join(allowed_origins)))
# 
#     def _get_allowed_origins_for_client(self, request, client_id):
#         app = request.db.applications.find_one({'client_id': client_id})
#         if app is None:
#             return []
#         else:
#             return app['authorized_origins']


# Path: yithlibraryserver/tests/test_cors.py
from pyramid.testing import DummyRequest
from yithlibraryserver import testing
from yithlibraryserver.cors import CORSManager

    clean_collections = ('applications', )

    def test_cors_headers_global_origins_access_denied(self):
        cm = CORSManager('')

        request = DummyRequest(headers={'Origin': 'foo'})
        response = request.response

        cm.add_cors_header(request, response)

        self.assertEqual(response.headers, {
                'Content-Type': 'text/html; charset=UTF-8',
                'Content-Length': '0',
                })

    def test_cors_headers_global_origins(self):
        cm = CORSManager('http://localhost')

        request = DummyRequest(headers={'Origin': 'http://localhost'})
        response = request.response

        cm.add_cors_header(request, response)

        self.assertEqual(response.headers, {
                'Content-Type': 'text/html; charset=UTF-8',
                'Content-Length': '0',
                'Access-Control-Allow-Origin': 'http://localhost',
                })

    def test_cors_headers_app_origins_access_denied(self):

        cm = CORSManager('')