# Path: yithlibraryserver/db.py
# class MongoDB(object):
#     """Simple wrapper to get pymongo real objects from the settings uri"""
# 
#     def __init__(self, db_uri=DEFAULT_MONGODB_URI,
#                  connection_factory=pymongo.Connection):
#         self.db_uri = urlparse.urlparse(db_uri)
#         self.connection = connection_factory(
#             host=self.db_uri.hostname or DEFAULT_MONGODB_HOST,
#             port=self.db_uri.port or DEFAULT_MONGODB_PORT,
#             tz_aware=True)
# 
#         if self.db_uri.path:
#             self.database_name = self.db_uri.path[1:]
#         else:
#             self.database_name = DEFAULT_MONGODB_NAME
# 
#     def get_connection(self):
#         return self.connection
# 
#     def get_database(self):
#         database = self.connection[self.database_name]
#         if self.db_uri.username and self.db_uri.password:
#             database.authenticate(self.db_uri.username, self.db_uri.password)
# 
#         return database
# 
# Path: yithlibraryserver/testing.py
# MONGO_URI = 'mongodb://localhost:27017/test-yith-library'
# 
# Path: yithlibraryserver/user/security.py
# def get_user(request):
#     user_id = unauthenticated_userid(request)
#     if user_id is None:
#         return user_id
# 
#     try:
#         user = request.db.users.find_one(bson.ObjectId(user_id))
#     except bson.errors.InvalidId:
#         return None
# 
#     return User(user)
# 
# def assert_authenticated_user_is_registered(request):
#     user_id = authenticated_userid(request)
#     try:
#         user = request.db.users.find_one(bson.ObjectId(user_id))
#     except bson.errors.InvalidId:
#         raise HTTPFound(location=request.route_path('register_new_user'))
#     else:
#         return User(user)


import unittest
from pyramid import testing
from pyramid.httpexceptions import HTTPFound
from yithlibraryserver.db import MongoDB
from yithlibraryserver.testing import MONGO_URI
from yithlibraryserver.user.security import (
    get_user,
    assert_authenticated_user_is_registered,
    )
        self.db = mdb.get_database()

    def tearDown(self):
        testing.tearDown()
        self.db.drop_collection('users')

    def test_get_user(self):
        request = testing.DummyRequest()
        request.db = self.db

        self.assertEqual(None, get_user(request))

        self.config.testing_securitypolicy(userid='john')
        self.assertEqual(None, get_user(request))

        user_id = self.db.users.insert({'screen_name': 'John Doe'}, safe=True)
        self.config.testing_securitypolicy(userid=str(user_id))
        self.assertEqual(get_user(request), {
                '_id': user_id,
                'screen_name': 'John Doe',
                })

    def test_assert_authenticated_user_is_registered(self):
        self.config.testing_securitypolicy(userid='john')

        request = testing.DummyRequest()
        request.db = self.db

        self.assertRaises(HTTPFound, assert_authenticated_user_is_registered, request)
        try:

# Path: yithlibraryserver/db.py
# class MongoDB(object):
#     """Simple wrapper to get pymongo real objects from the settings uri"""
# 
#     def __init__(self, db_uri=DEFAULT_MONGODB_URI,
#                  connection_factory=pymongo.Connection):
#         self.db_uri = urlparse.urlparse(db_uri)
#         self.connection = connection_factory(
#             host=self.db_uri.hostname or DEFAULT_MONGODB_HOST,
#             port=self.db_uri.port or DEFAULT_MONGODB_PORT,
#             tz_aware=True)
# 
#         if self.db_uri.path:
#             self.database_name = self.db_uri.path[1:]
#         else:
#             self.database_name = DEFAULT_MONGODB_NAME
# 
#     def get_connection(self):
#         return self.connection
# 
#     def get_database(self):
#         database = self.connection[self.database_name]
#         if self.db_uri.username and self.db_uri.password:
#             database.authenticate(self.db_uri.username, self.db_uri.password)
# 
#         return database
# 
# Path: yithlibraryserver/testing.py
# MONGO_URI = 'mongodb://localhost:27017/test-yith-library'
# 
# Path: yithlibraryserver/user/security.py
# def get_user(request):
#     user_id = unauthenticated_userid(request)
#     if user_id is None:
#         return user_id
# 
#     try:
#         user = request.db.users.find_one(bson.ObjectId(user_id))
#     except bson.errors.InvalidId:
#         return None
# 
#     return User(user)
# 
# def assert_authenticated_user_is_registered(request):
#     user_id = authenticated_userid(request)
#     try:
#         user = request.db.users.find_one(bson.ObjectId(user_id))
#     except bson.errors.InvalidId:
#         raise HTTPFound(location=request.route_path('register_new_user'))
#     else:
#         return User(user)


# Path: yithlibraryserver/user/tests/test_security.py
import unittest
from pyramid import testing
from pyramid.httpexceptions import HTTPFound
from yithlibraryserver.db import MongoDB
from yithlibraryserver.testing import MONGO_URI
from yithlibraryserver.user.security import (
    get_user,
    assert_authenticated_user_is_registered,
    )

        self.db = mdb.get_database()

    def tearDown(self):
        testing.tearDown()
        self.db.drop_collection('users')

    def test_get_user(self):
        request = testing.DummyRequest()
        request.db = self.db

        self.assertEqual(None, get_user(request))

        self.config.testing_securitypolicy(userid='john')
        self.assertEqual(None, get_user(request))

        user_id = self.db.users.insert({'screen_name': 'John Doe'}, safe=True)
        self.config.testing_securitypolicy(userid=str(user_id))
        self.assertEqual(get_user(request), {
                '_id': user_id,
                'screen_name': 'John Doe',
                })

    def test_assert_authenticated_user_is_registered(self):
        self.config.testing_securitypolicy(userid='john')

        request = testing.DummyRequest()
        request.db = self.db

        self.assertRaises(HTTPFound, assert_authenticated_user_is_registered, request)
        try:

            assert_authenticated_user_is_registered(request)