# 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/password/models.py
# class PasswordsManager(object):
# 
#     def __init__(self, db):
#         self.db = db
# 
#     def create(self, user, password):
#         """Creates and returns a new password or a set of passwords.
# 
#         Stores the password in the database for this specific user
#         and returns a new dict with the 'owner' and '_id' fields
#         filled.
# 
#         If password is a list, do the same with each password in
#         this list.
#         """
#         if isinstance(password, dict):
#             if password:
#                 new_password = dict(password)  # copy since we are changing this object
#                 new_password['owner'] = user['_id']
#                 _id = self.db.passwords.insert(new_password, safe=True)
#                 new_password['_id'] = _id
#                 return new_password
#         else:
#             new_passwords = []  # copy since we are changing this object
#             for p in password:
#                 if p:
#                     p = dict(p)
#                     p['owner'] = user['_id']
#                     new_passwords.append(p)
# 
#             if new_passwords:
# 
#                 _ids = self.db.passwords.insert(new_passwords, safe=True)
# 
#                 for i in range(len(new_passwords)):
#                     new_passwords[i]['_id'] = _ids[i]
# 
#                 return new_passwords
# 
#     def retrieve(self, user, _id=None):
#         """Return the user's passwords or just one.
# 
#         If _id is None return the whole set of passwords for this
#         user. Otherwise, it returns the password with that _id.
#         """
#         if _id is None:
#             return self.db.passwords.find({'owner': user['_id']})
#         else:
#             return self.db.passwords.find_one({
#                     '_id': _id,
#                     'owner': user['_id'],
#                     })
# 
#     def update(self, user, _id, password):
#         """Update a password in the database.
# 
#         Return the updated password on success or None if the original
#         password does not exist.
#         """
#         new_password = dict(password)  # copy since we are changing this object
#         new_password['owner'] = user['_id']
#         result = self.db.passwords.update({
#                 '_id': _id,
#                 'owner': user['_id'],
#                 }, new_password, safe=True)
#         new_password['_id'] = _id
# 
#         # result['n'] is the number of documents updated
#         # See <http://www.mongodb.org/display/DOCS/getLastError+Command#getLastErrorCommand-ReturnValue
#         if result['n'] == 1:
#             return new_password
#         else:
#             return None
# 
#     def delete(self, user, _id=None):
#         """Deletes a password from the database or the whole set for this user.
# 
#         Returns True if the delete is succesfull or False otherwise.
#         """
#         query = {'owner': user['_id']}
#         if _id is not None:
#             query['_id'] = _id
# 
#         result = self.db.passwords.remove(query, safe=True)
#         return result['n'] > 0
# 
# Path: yithlibraryserver/testing.py
# MONGO_URI = 'mongodb://localhost:27017/test-yith-library'


import unittest
from pyramid import testing
from yithlibraryserver.db import MongoDB
from yithlibraryserver.password.models import PasswordsManager
from yithlibraryserver.testing import MONGO_URI
                }, {
                'secret3': 'ghi',
                }]
        created_passwords = self.pm.create(self.user, passwords)
        self.assertEqual(created_passwords[0]['owner'], self.user_id)
        self.assertEqual(created_passwords[1]['owner'], self.user_id)
        self.assertEqual(created_passwords[2]['owner'], self.user_id)
        self.assertTrue('_id' in created_passwords[0])
        self.assertTrue('_id' in created_passwords[1])
        self.assertTrue('_id' in created_passwords[2])
        self.assertEqual(n_passwords + 4, self.db.passwords.count())

    def test_retrieve(self):
        p1 = self.db.passwords.insert({
                'secret': 'secret1',
                'owner': self.user_id,
                }, safe=True)

        password = self.pm.retrieve(self.user, p1)
        self.assertEqual(password, {
                'secret': 'secret1',
                'owner': self.user_id,
                '_id': p1,
                })

        p2 = self.db.passwords.insert({
                'secret': 'secret2',
                'owner': self.user_id,
                }, safe=True)
        passwords = self.pm.retrieve(self.user)

# 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/password/models.py
# class PasswordsManager(object):
# 
#     def __init__(self, db):
#         self.db = db
# 
#     def create(self, user, password):
#         """Creates and returns a new password or a set of passwords.
# 
#         Stores the password in the database for this specific user
#         and returns a new dict with the 'owner' and '_id' fields
#         filled.
# 
#         If password is a list, do the same with each password in
#         this list.
#         """
#         if isinstance(password, dict):
#             if password:
#                 new_password = dict(password)  # copy since we are changing this object
#                 new_password['owner'] = user['_id']
#                 _id = self.db.passwords.insert(new_password, safe=True)
#                 new_password['_id'] = _id
#                 return new_password
#         else:
#             new_passwords = []  # copy since we are changing this object
#             for p in password:
#                 if p:
#                     p = dict(p)
#                     p['owner'] = user['_id']
#                     new_passwords.append(p)
# 
#             if new_passwords:
# 
#                 _ids = self.db.passwords.insert(new_passwords, safe=True)
# 
#                 for i in range(len(new_passwords)):
#                     new_passwords[i]['_id'] = _ids[i]
# 
#                 return new_passwords
# 
#     def retrieve(self, user, _id=None):
#         """Return the user's passwords or just one.
# 
#         If _id is None return the whole set of passwords for this
#         user. Otherwise, it returns the password with that _id.
#         """
#         if _id is None:
#             return self.db.passwords.find({'owner': user['_id']})
#         else:
#             return self.db.passwords.find_one({
#                     '_id': _id,
#                     'owner': user['_id'],
#                     })
# 
#     def update(self, user, _id, password):
#         """Update a password in the database.
# 
#         Return the updated password on success or None if the original
#         password does not exist.
#         """
#         new_password = dict(password)  # copy since we are changing this object
#         new_password['owner'] = user['_id']
#         result = self.db.passwords.update({
#                 '_id': _id,
#                 'owner': user['_id'],
#                 }, new_password, safe=True)
#         new_password['_id'] = _id
# 
#         # result['n'] is the number of documents updated
#         # See <http://www.mongodb.org/display/DOCS/getLastError+Command#getLastErrorCommand-ReturnValue
#         if result['n'] == 1:
#             return new_password
#         else:
#             return None
# 
#     def delete(self, user, _id=None):
#         """Deletes a password from the database or the whole set for this user.
# 
#         Returns True if the delete is succesfull or False otherwise.
#         """
#         query = {'owner': user['_id']}
#         if _id is not None:
#             query['_id'] = _id
# 
#         result = self.db.passwords.remove(query, safe=True)
#         return result['n'] > 0
# 
# Path: yithlibraryserver/testing.py
# MONGO_URI = 'mongodb://localhost:27017/test-yith-library'


# Path: yithlibraryserver/password/tests/test_models.py
import unittest
from pyramid import testing
from yithlibraryserver.db import MongoDB
from yithlibraryserver.password.models import PasswordsManager
from yithlibraryserver.testing import MONGO_URI

                }, {
                'secret3': 'ghi',
                }]
        created_passwords = self.pm.create(self.user, passwords)
        self.assertEqual(created_passwords[0]['owner'], self.user_id)
        self.assertEqual(created_passwords[1]['owner'], self.user_id)
        self.assertEqual(created_passwords[2]['owner'], self.user_id)
        self.assertTrue('_id' in created_passwords[0])
        self.assertTrue('_id' in created_passwords[1])
        self.assertTrue('_id' in created_passwords[2])
        self.assertEqual(n_passwords + 4, self.db.passwords.count())

    def test_retrieve(self):
        p1 = self.db.passwords.insert({
                'secret': 'secret1',
                'owner': self.user_id,
                }, safe=True)

        password = self.pm.retrieve(self.user, p1)
        self.assertEqual(password, {
                'secret': 'secret1',
                'owner': self.user_id,
                '_id': p1,
                })

        p2 = self.db.passwords.insert({
                'secret': 'secret2',
                'owner': self.user_id,
                }, safe=True)
        passwords = self.pm.retrieve(self.user)

        self.assertEqual(list(passwords), [{