# 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/security.py
# def authorize_user(request):
#     authorization = request.headers.get('Authorization')
#     if authorization is None:
#         raise HTTPUnauthorized()
# 
#     method, credentials = request.authorization
#     if method.lower() != 'bearer':
#         raise HTTPBadRequest('Authorization method not supported')
# 
#     access_code = AccessCodes(request.db).find(credentials)
#     if access_code is None:
#         raise HTTPUnauthorized()
# 
#     user_id = access_code['user']
#     user = request.db.users.find_one(user_id)
#     if user is None:
#         raise HTTPUnauthorized()
# 
#     return user


from pyramid.httpexceptions import HTTPBadRequest, HTTPUnauthorized
from yithlibraryserver import testing
from yithlibraryserver.security import authorize_user




class AuthorizationTests(testing.TestCase):

    clean_collections = ('access_codes', 'users')

    def test_authorize_user(self):

        request = testing.FakeRequest(headers={})

        # The authorization header is required
        self.assertRaises(HTTPUnauthorized, authorize_user, request)

        request = testing.FakeRequest(
            headers={'Authorization': 'Basic foobar'})
        # Only the bearer method is allowed
        self.assertRaises(HTTPBadRequest, authorize_user, request)

        request = testing.FakeRequest(headers={
                'Authorization': 'Bearer 1234',
                }, db=self.db)
        # Invalid code
        self.assertRaises(HTTPUnauthorized, authorize_user, request)

        access_code_id = self.db.access_codes.insert({
                'code': '1234',
                'user': 'user1',
                }, safe=True)

# 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/security.py
# def authorize_user(request):
#     authorization = request.headers.get('Authorization')
#     if authorization is None:
#         raise HTTPUnauthorized()
# 
#     method, credentials = request.authorization
#     if method.lower() != 'bearer':
#         raise HTTPBadRequest('Authorization method not supported')
# 
#     access_code = AccessCodes(request.db).find(credentials)
#     if access_code is None:
#         raise HTTPUnauthorized()
# 
#     user_id = access_code['user']
#     user = request.db.users.find_one(user_id)
#     if user is None:
#         raise HTTPUnauthorized()
# 
#     return user


# Path: yithlibraryserver/tests/test_security.py
from pyramid.httpexceptions import HTTPBadRequest, HTTPUnauthorized
from yithlibraryserver import testing
from yithlibraryserver.security import authorize_user





class AuthorizationTests(testing.TestCase):

    clean_collections = ('access_codes', 'users')

    def test_authorize_user(self):

        request = testing.FakeRequest(headers={})

        # The authorization header is required
        self.assertRaises(HTTPUnauthorized, authorize_user, request)

        request = testing.FakeRequest(
            headers={'Authorization': 'Basic foobar'})
        # Only the bearer method is allowed
        self.assertRaises(HTTPBadRequest, authorize_user, request)

        request = testing.FakeRequest(headers={
                'Authorization': 'Bearer 1234',
                }, db=self.db)
        # Invalid code
        self.assertRaises(HTTPUnauthorized, authorize_user, request)

        access_code_id = self.db.access_codes.insert({
                'code': '1234',
                'user': 'user1',
                }, safe=True)

        request = testing.FakeRequest(headers={