# 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/oauth2/authentication.py
# def authenticate_client(request):
#     """Returns the client application representing this request.
# 
#     Uses the Authorization header in Basic format to identify
#     the client of this request against the set of registered
#     applications on the server.
#     """
#     authorization = request.headers.get('Authorization')
#     if authorization is None:
#         raise HTTPUnauthorized()
# 
#     method, credentials = request.authorization
#     if method.lower() != 'basic':
#         raise HTTPUnauthorized()
# 
#     credentials = decodebytes(credentials.encode('utf-8'))
#     credentials = credentials.decode('utf-8')
#     client_id, client_secret = credentials.split(':')
# 
#     application = request.db.applications.find_one({
#             'client_id': client_id,
#             'client_secret': client_secret
#             })
# 
#     if application is None:
#         raise HTTPUnauthorized()
# 
#     return application
# 
# Path: yithlibraryserver/oauth2/authentication.py
# def auth_basic_encode(user, password):
#     value = '%s:%s' % (user, password)
#     value = 'Basic ' + encodebytes(value.encode('utf-8')).decode('utf-8')
#     return encode_header(value)


from pyramid.httpexceptions import HTTPUnauthorized
from yithlibraryserver import testing
from yithlibraryserver.oauth2.authentication import authenticate_client
from yithlibraryserver.oauth2.authentication import auth_basic_encode
# You should have received a copy of the GNU Affero General Public License
# along with Yith Library Server.  If not, see <http://www.gnu.org/licenses/>.




class AuthenticationTests(testing.TestCase):

    clean_collections = ('applications', )

    def test_authenticate_client(self):
        request = testing.FakeRequest(headers={})
        # The authorization header is required
        self.assertRaises(HTTPUnauthorized, authenticate_client, request)

        request = testing.FakeRequest(
            headers={'Authorization': 'Advanced foobar'})
        # Only the basic method is allowed
        self.assertRaises(HTTPUnauthorized, authenticate_client, request)

        request = testing.FakeRequest(headers={
                'Authorization': auth_basic_encode('foo', 'bar'),
                }, db=self.db)
        # Invalid user:password
        self.assertRaises(HTTPUnauthorized, authenticate_client, request)

        self.db.applications.insert({
                'client_id': '123456',
                'client_secret': 'secret',
                })

# 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/oauth2/authentication.py
# def authenticate_client(request):
#     """Returns the client application representing this request.
# 
#     Uses the Authorization header in Basic format to identify
#     the client of this request against the set of registered
#     applications on the server.
#     """
#     authorization = request.headers.get('Authorization')
#     if authorization is None:
#         raise HTTPUnauthorized()
# 
#     method, credentials = request.authorization
#     if method.lower() != 'basic':
#         raise HTTPUnauthorized()
# 
#     credentials = decodebytes(credentials.encode('utf-8'))
#     credentials = credentials.decode('utf-8')
#     client_id, client_secret = credentials.split(':')
# 
#     application = request.db.applications.find_one({
#             'client_id': client_id,
#             'client_secret': client_secret
#             })
# 
#     if application is None:
#         raise HTTPUnauthorized()
# 
#     return application
# 
# Path: yithlibraryserver/oauth2/authentication.py
# def auth_basic_encode(user, password):
#     value = '%s:%s' % (user, password)
#     value = 'Basic ' + encodebytes(value.encode('utf-8')).decode('utf-8')
#     return encode_header(value)


# Path: yithlibraryserver/oauth2/tests/test_authentication.py
from pyramid.httpexceptions import HTTPUnauthorized
from yithlibraryserver import testing
from yithlibraryserver.oauth2.authentication import authenticate_client
from yithlibraryserver.oauth2.authentication import auth_basic_encode

# You should have received a copy of the GNU Affero General Public License
# along with Yith Library Server.  If not, see <http://www.gnu.org/licenses/>.




class AuthenticationTests(testing.TestCase):

    clean_collections = ('applications', )

    def test_authenticate_client(self):
        request = testing.FakeRequest(headers={})
        # The authorization header is required
        self.assertRaises(HTTPUnauthorized, authenticate_client, request)

        request = testing.FakeRequest(
            headers={'Authorization': 'Advanced foobar'})
        # Only the basic method is allowed
        self.assertRaises(HTTPUnauthorized, authenticate_client, request)

        request = testing.FakeRequest(headers={
                'Authorization': auth_basic_encode('foo', 'bar'),
                }, db=self.db)
        # Invalid user:password
        self.assertRaises(HTTPUnauthorized, authenticate_client, request)

        self.db.applications.insert({
                'client_id': '123456',
                'client_secret': 'secret',
                })

        request = testing.FakeRequest(headers={