# 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/authorization.py
# class AuthorizationCodes(Codes):
# 
#     collection_name = 'authorization_codes'
# 
#     def get_redirect_url(self, code, uri, state=None):
#         parameters = ['code=%s' % code]
#         if state:
#             parameters.append('state=%s' % state)
#         return '%s?%s' % (uri, '&'.join(parameters))
# 
#     def create(self, user_id, client_id, scope):
#         return super(AuthorizationCodes, self).create(user_id,
#                                                       scope=scope,
#                                                       client_id=client_id)
# 
# Path: yithlibraryserver/oauth2/authorization.py
# class AccessCodes(Codes):
# 
#     collection_name = 'access_codes'
# 
#     def create(self, user_id, grant):
#         return super(AccessCodes, self).create(user_id,
#                                                scope=grant['scope'],
#                                                client_id=grant['client_id'])
# 
# Path: yithlibraryserver/oauth2/authorization.py
# class Authorizator(object):
# 
#     def __init__(self, db, app):
#         self.db = db
#         self.app = app
#         self.auth_codes = AuthorizationCodes(db)
#         self.access_codes = AccessCodes(db)
# 
#     def is_app_authorized(self, user):
#         return self.app['_id'] in user['authorized_apps']
# 
#     def store_user_authorization(self, user):
#         self.db.users.update(
#             {'_id': user['_id']},
#             {'$addToSet': {'authorized_apps': self.app['_id']}},
#             safe=True,
#             )
# 
#     def remove_user_authorization(self, user):
#         self.db.users.update(
#             {'_id': user['_id']},
#             {'$pull': {'authorized_apps': self.app['_id']}},
#             safe=True,
#             )


from yithlibraryserver import testing
from yithlibraryserver.oauth2.authorization import AuthorizationCodes
from yithlibraryserver.oauth2.authorization import AccessCodes
from yithlibraryserver.oauth2.authorization import Authorizator

        self.assertNotEqual(None, codes.find(code2))
        self.assertEqual(None, codes.find(code1))

        codes.remove(codes.find(code1))
        self.assertEqual(self.db.authorization_codes.count(), 0)

    def test_access_codes(self):
        codes = AccessCodes(self.db)
        self.assertEqual(self.db.access_codes.count(), 0)
        grant = {'scope': 'passwords', 'client_id': 'client1'}
        code1 = codes.create('user1', grant)
        self.assertEqual(self.db.access_codes.count(), 1)

        # creating a code with same arguments replace the old one
        code2 = codes.create('user1', grant)
        self.assertEqual(self.db.access_codes.count(), 1)

        self.assertNotEqual(code1, code2)

        self.assertNotEqual(None, codes.find(code2))
        self.assertEqual(None, codes.find(code1))

        codes.remove(codes.find(code1))
        self.assertEqual(self.db.access_codes.count(), 0)

    def test_authorizator(self):
        app = {'_id': 'app1'}
        authorizator = Authorizator(self.db, app)
        self.assertTrue(isinstance(authorizator.auth_codes,

# 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/authorization.py
# class AuthorizationCodes(Codes):
# 
#     collection_name = 'authorization_codes'
# 
#     def get_redirect_url(self, code, uri, state=None):
#         parameters = ['code=%s' % code]
#         if state:
#             parameters.append('state=%s' % state)
#         return '%s?%s' % (uri, '&'.join(parameters))
# 
#     def create(self, user_id, client_id, scope):
#         return super(AuthorizationCodes, self).create(user_id,
#                                                       scope=scope,
#                                                       client_id=client_id)
# 
# Path: yithlibraryserver/oauth2/authorization.py
# class AccessCodes(Codes):
# 
#     collection_name = 'access_codes'
# 
#     def create(self, user_id, grant):
#         return super(AccessCodes, self).create(user_id,
#                                                scope=grant['scope'],
#                                                client_id=grant['client_id'])
# 
# Path: yithlibraryserver/oauth2/authorization.py
# class Authorizator(object):
# 
#     def __init__(self, db, app):
#         self.db = db
#         self.app = app
#         self.auth_codes = AuthorizationCodes(db)
#         self.access_codes = AccessCodes(db)
# 
#     def is_app_authorized(self, user):
#         return self.app['_id'] in user['authorized_apps']
# 
#     def store_user_authorization(self, user):
#         self.db.users.update(
#             {'_id': user['_id']},
#             {'$addToSet': {'authorized_apps': self.app['_id']}},
#             safe=True,
#             )
# 
#     def remove_user_authorization(self, user):
#         self.db.users.update(
#             {'_id': user['_id']},
#             {'$pull': {'authorized_apps': self.app['_id']}},
#             safe=True,
#             )


# Path: yithlibraryserver/oauth2/tests/test_authorization.py
from yithlibraryserver import testing
from yithlibraryserver.oauth2.authorization import AuthorizationCodes
from yithlibraryserver.oauth2.authorization import AccessCodes
from yithlibraryserver.oauth2.authorization import Authorizator


        self.assertNotEqual(None, codes.find(code2))
        self.assertEqual(None, codes.find(code1))

        codes.remove(codes.find(code1))
        self.assertEqual(self.db.authorization_codes.count(), 0)

    def test_access_codes(self):
        codes = AccessCodes(self.db)
        self.assertEqual(self.db.access_codes.count(), 0)
        grant = {'scope': 'passwords', 'client_id': 'client1'}
        code1 = codes.create('user1', grant)
        self.assertEqual(self.db.access_codes.count(), 1)

        # creating a code with same arguments replace the old one
        code2 = codes.create('user1', grant)
        self.assertEqual(self.db.access_codes.count(), 1)

        self.assertNotEqual(code1, code2)

        self.assertNotEqual(None, codes.find(code2))
        self.assertEqual(None, codes.find(code1))

        codes.remove(codes.find(code1))
        self.assertEqual(self.db.access_codes.count(), 0)

    def test_authorizator(self):
        app = {'_id': 'app1'}
        authorizator = Authorizator(self.db, app)
        self.assertTrue(isinstance(authorizator.auth_codes,

                                   AuthorizationCodes))