# Path: yithlibraryserver/datetimeservice/testing.py
# class FakeDateService(object):
# 
#     def __init__(self, request):
#         self.request = request
# 
#     def today(self):
#         fake = os.environ['YITH_FAKE_DATE']
#         parts = [int(p) for p in fake.split('-')]
#         return datetime.date(*parts)
# 
# Path: yithlibraryserver/backups/email.py
# def get_day_to_send(user, days_of_month):
#     """Sum the ordinal of each character in user._id % days_of_month"""
#     return sum([ord(chr) for chr in str(user['_id'])]) % days_of_month
# 
# def send_passwords(request, user, preferences_link, backups_link):
#     passwords = get_user_passwords(request.db, user)
#     if not passwords:
#         return False
# 
#     context = {
#         'user': user,
#         'preferences_link': preferences_link,
#         'backups_link': backups_link,
#     }
# 
#     today = request.date_service.today()
#     attachment = Attachment(get_backup_filename(today),
#                             "application/yith",
#                             compress(passwords))
# 
#     send_email(
#         request,
#         'yithlibraryserver.backups:templates/email_passwords',
#         context,
#         "Your Yith Library's passwords",
#         [user['email']],
#         attachments=[attachment],
#     )
# 
#     return True
# 
# 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)


import os
import unittest
import bson
from pyramid import testing
from pyramid.testing import DummyRequest
from pyramid_mailer import get_mailer
from yithlibraryserver.datetimeservice.testing import FakeDateService
from yithlibraryserver.backups.email import get_day_to_send, send_passwords
from yithlibraryserver.testing import TestCase
# Yith Library Server is a password storage server.
# Copyright (C) 2013 Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>
#
# This file is part of Yith Library Server.
#
# Yith Library Server is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Yith Library Server is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# 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 GetDayToSendTests(unittest.TestCase):

    def test_get_day_to_send(self):
        user = {}
        user['_id'] = bson.objectid.ObjectId('000000000000000000000001')
        self.assertEqual(5, get_day_to_send(user, 28))

# Path: yithlibraryserver/datetimeservice/testing.py
# class FakeDateService(object):
# 
#     def __init__(self, request):
#         self.request = request
# 
#     def today(self):
#         fake = os.environ['YITH_FAKE_DATE']
#         parts = [int(p) for p in fake.split('-')]
#         return datetime.date(*parts)
# 
# Path: yithlibraryserver/backups/email.py
# def get_day_to_send(user, days_of_month):
#     """Sum the ordinal of each character in user._id % days_of_month"""
#     return sum([ord(chr) for chr in str(user['_id'])]) % days_of_month
# 
# def send_passwords(request, user, preferences_link, backups_link):
#     passwords = get_user_passwords(request.db, user)
#     if not passwords:
#         return False
# 
#     context = {
#         'user': user,
#         'preferences_link': preferences_link,
#         'backups_link': backups_link,
#     }
# 
#     today = request.date_service.today()
#     attachment = Attachment(get_backup_filename(today),
#                             "application/yith",
#                             compress(passwords))
# 
#     send_email(
#         request,
#         'yithlibraryserver.backups:templates/email_passwords',
#         context,
#         "Your Yith Library's passwords",
#         [user['email']],
#         attachments=[attachment],
#     )
# 
#     return True
# 
# 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/backups/tests/test_email.py
import os
import unittest
import bson
from pyramid import testing
from pyramid.testing import DummyRequest
from pyramid_mailer import get_mailer
from yithlibraryserver.datetimeservice.testing import FakeDateService
from yithlibraryserver.backups.email import get_day_to_send, send_passwords
from yithlibraryserver.testing import TestCase

# Yith Library Server is a password storage server.
# Copyright (C) 2013 Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>
#
# This file is part of Yith Library Server.
#
# Yith Library Server is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Yith Library Server is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# 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 GetDayToSendTests(unittest.TestCase):

    def test_get_day_to_send(self):
        user = {}
        user['_id'] = bson.objectid.ObjectId('000000000000000000000001')
        self.assertEqual(5, get_day_to_send(user, 28))

        self.assertEqual(13, get_day_to_send(user, 30))