# Path: yithlibraryserver/testing.py
# class TestCase(unittest.TestCase):
# 
#     clean_collections = tuple()
# 
#     def setUp(self):
#         settings = {
#             'mongo_uri': MONGO_URI,
#             'auth_tk_secret': '123456',
#             'twitter_consumer_key': 'key',
#             'twitter_consumer_secret': 'secret',
#             'facebook_app_id': 'id',
#             'facebook_app_secret': 'secret',
#             'google_client_id': 'id',
#             'google_client_secret': 'secret',
#             'paypal_user': 'sdk-three_api1.sdk.com',
#             'paypal_password': 'QFZCWN5HZM8VBG7Q',
#             'paypal_signature': 'A-IzJhZZjhg29XQ2qnhapuwxIDzyAZQ92FRP5dqBzVesOkzbdUONzmOU',
#             'testing': 'True',
#             'pyramid_mailer.prefix': 'mail_',
#             'mail_default_sender': 'no-reply@yithlibrary.com',
#             'admin_emails': 'admin1@example.com admin2@example.com',
#             'public_url_root': 'http://localhost:6543/',
#             }
#         app = main({}, **settings)
#         self.testapp = TestApp(app)
#         self.db = app.registry.settings['db_conn']['test-yith-library']
# 
#     def tearDown(self):
#         for col in self.clean_collections:
#             self.db.drop_collection(col)
# 
#         self.testapp.reset()
# 
#     def set_user_cookie(self, user_id):
#         request = TestRequest.blank('', {})
#         request.registry = self.testapp.app.registry
#         remember_headers = remember(request, user_id)
#         cookie_value = remember_headers[0][1].split('"')[1]
#         self.testapp.cookies['auth_tkt'] = cookie_value
# 
#     def add_to_session(self, data):
#         queryUtility = self.testapp.app.registry.queryUtility
#         session_factory = queryUtility(ISessionFactory)
#         request = DummyRequest()
#         session = session_factory(request)
#         for key, value in data.items():
#             session[key] = value
#         session.persist()
#         self.testapp.cookies['beaker.session.id'] = session._sess.id
# 
#     def get_session(self, response):
#         queryUtility = self.testapp.app.registry.queryUtility
#         session_factory = queryUtility(ISessionFactory)
#         request = response.request
# 
#         if not hasattr(request, 'add_response_callback'):
#             request.add_response_callback = lambda r: r
# 
#         if 'Set-Cookie' in response.headers:
#             request.environ['HTTP_COOKIE'] = response.headers['Set-Cookie']
# 
#         return session_factory(request)
# 
# Path: yithlibraryserver/user/email_verification.py
# class EmailVerificationCode(object):
# 
#     def __init__(self, code=None):
#         if code is None:
#             self.code = self._generate_code()
#         else:
#             self.code = code
# 
#     def _generate_code(self):
#         return str(uuid.uuid4())
# 
#     def store(self, db, user):
#         result = db.users.update({'_id': user['_id']}, {
#                 '$set': {'email_verification_code': self.code},
#                 }, safe=True)
#         return result['n'] == 1
# 
#     def remove(self, db, email, verified):
#         result = db.users.update({
#                 'email_verification_code': self.code,
#                 'email': email,
#                 }, {
#                 '$unset': {'email_verification_code': 1},
#                 '$set': {'email_verified': verified},
#                 }, safe=True)
#         return result['n'] == 1
# 
#     def verify(self, db, email):
#         result = db.users.find_one({
#                 'email': email,
#                 'email_verification_code': self.code,
#                 })
#         return result is not None
# 
#     def send(self, request, user, url):
#         context = {
#             'link': '%s?code=%s&email=%s' % (url, self.code, user['email']),
#             'user': user,
#         }
#         return send_email(
#             request,
#             'yithlibraryserver.user:templates/email_verification_code',
#             context,
#             'Please verify your email address',
#             [user['email']],
#         )


from pyramid import testing
from pyramid.testing import DummyRequest
from pyramid_mailer import get_mailer
from yithlibraryserver.testing import TestCase
from yithlibraryserver.user.email_verification import EmailVerificationCode
# along with Yith Library Server.  If not, see <http://www.gnu.org/licenses/>.




class EmailVerificationCodeTests(TestCase):

    clean_collections = ('users', )

    def setUp(self):
        self.config = testing.setUp()
        self.config.include('pyramid_mailer.testing')
        super(EmailVerificationCodeTests, self).setUp()

    def test_email_verification_code(self):
        evc = EmailVerificationCode()

        self.assertNotEqual(evc.code, None)

        user_id = self.db.users.insert({
                'first_name': 'John',
                'last_name': 'Doe',
                'email': 'john@example.com',
                }, safe=True)
        user = self.db.users.find_one({'_id': user_id})
        evc.store(self.db, user)

        user = self.db.users.find_one({'_id': user_id})
        self.assertEqual(user['email_verification_code'], evc.code)


# Path: yithlibraryserver/testing.py
# class TestCase(unittest.TestCase):
# 
#     clean_collections = tuple()
# 
#     def setUp(self):
#         settings = {
#             'mongo_uri': MONGO_URI,
#             'auth_tk_secret': '123456',
#             'twitter_consumer_key': 'key',
#             'twitter_consumer_secret': 'secret',
#             'facebook_app_id': 'id',
#             'facebook_app_secret': 'secret',
#             'google_client_id': 'id',
#             'google_client_secret': 'secret',
#             'paypal_user': 'sdk-three_api1.sdk.com',
#             'paypal_password': 'QFZCWN5HZM8VBG7Q',
#             'paypal_signature': 'A-IzJhZZjhg29XQ2qnhapuwxIDzyAZQ92FRP5dqBzVesOkzbdUONzmOU',
#             'testing': 'True',
#             'pyramid_mailer.prefix': 'mail_',
#             'mail_default_sender': 'no-reply@yithlibrary.com',
#             'admin_emails': 'admin1@example.com admin2@example.com',
#             'public_url_root': 'http://localhost:6543/',
#             }
#         app = main({}, **settings)
#         self.testapp = TestApp(app)
#         self.db = app.registry.settings['db_conn']['test-yith-library']
# 
#     def tearDown(self):
#         for col in self.clean_collections:
#             self.db.drop_collection(col)
# 
#         self.testapp.reset()
# 
#     def set_user_cookie(self, user_id):
#         request = TestRequest.blank('', {})
#         request.registry = self.testapp.app.registry
#         remember_headers = remember(request, user_id)
#         cookie_value = remember_headers[0][1].split('"')[1]
#         self.testapp.cookies['auth_tkt'] = cookie_value
# 
#     def add_to_session(self, data):
#         queryUtility = self.testapp.app.registry.queryUtility
#         session_factory = queryUtility(ISessionFactory)
#         request = DummyRequest()
#         session = session_factory(request)
#         for key, value in data.items():
#             session[key] = value
#         session.persist()
#         self.testapp.cookies['beaker.session.id'] = session._sess.id
# 
#     def get_session(self, response):
#         queryUtility = self.testapp.app.registry.queryUtility
#         session_factory = queryUtility(ISessionFactory)
#         request = response.request
# 
#         if not hasattr(request, 'add_response_callback'):
#             request.add_response_callback = lambda r: r
# 
#         if 'Set-Cookie' in response.headers:
#             request.environ['HTTP_COOKIE'] = response.headers['Set-Cookie']
# 
#         return session_factory(request)
# 
# Path: yithlibraryserver/user/email_verification.py
# class EmailVerificationCode(object):
# 
#     def __init__(self, code=None):
#         if code is None:
#             self.code = self._generate_code()
#         else:
#             self.code = code
# 
#     def _generate_code(self):
#         return str(uuid.uuid4())
# 
#     def store(self, db, user):
#         result = db.users.update({'_id': user['_id']}, {
#                 '$set': {'email_verification_code': self.code},
#                 }, safe=True)
#         return result['n'] == 1
# 
#     def remove(self, db, email, verified):
#         result = db.users.update({
#                 'email_verification_code': self.code,
#                 'email': email,
#                 }, {
#                 '$unset': {'email_verification_code': 1},
#                 '$set': {'email_verified': verified},
#                 }, safe=True)
#         return result['n'] == 1
# 
#     def verify(self, db, email):
#         result = db.users.find_one({
#                 'email': email,
#                 'email_verification_code': self.code,
#                 })
#         return result is not None
# 
#     def send(self, request, user, url):
#         context = {
#             'link': '%s?code=%s&email=%s' % (url, self.code, user['email']),
#             'user': user,
#         }
#         return send_email(
#             request,
#             'yithlibraryserver.user:templates/email_verification_code',
#             context,
#             'Please verify your email address',
#             [user['email']],
#         )


# Path: yithlibraryserver/user/tests/test_email_verification.py
from pyramid import testing
from pyramid.testing import DummyRequest
from pyramid_mailer import get_mailer
from yithlibraryserver.testing import TestCase
from yithlibraryserver.user.email_verification import EmailVerificationCode

# along with Yith Library Server.  If not, see <http://www.gnu.org/licenses/>.




class EmailVerificationCodeTests(TestCase):

    clean_collections = ('users', )

    def setUp(self):
        self.config = testing.setUp()
        self.config.include('pyramid_mailer.testing')
        super(EmailVerificationCodeTests, self).setUp()

    def test_email_verification_code(self):
        evc = EmailVerificationCode()

        self.assertNotEqual(evc.code, None)

        user_id = self.db.users.insert({
                'first_name': 'John',
                'last_name': 'Doe',
                'email': 'john@example.com',
                }, safe=True)
        user = self.db.users.find_one({'_id': user_id})
        evc.store(self.db, user)

        user = self.db.users.find_one({'_id': user_id})
        self.assertEqual(user['email_verification_code'], evc.code)


        evc2 = EmailVerificationCode(evc.code)