# 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
# 
# Path: yithlibraryserver/compat.py
# PY3 = sys.version_info[0] == 3
#     def encode_header(obj):  # pragma: no cover
#     def encode_header(obj):  # pragma: no cover
# 
# Path: yithlibraryserver/scripts/backups.py
# def send_backups_via_email():
#     result = setup_simple_command(
#         "send_backups_via_email",
#         "Report information about users and their passwords.",
#         )
#     if isinstance(result, int):
#         return result
#     else:
#         settings, closer, env, args = result
# 
#     try:
#         request = env['request']
# 
#         if len(args) == 0:
#             user_iterator = get_all_users(request)
#         else:
#             user_iterator = get_selected_users(request, *args)
# 
#         tx = transaction.begin()
# 
#         public_url_root = settings['public_url_root']
#         preferences_link = urlparse.urljoin(
#             public_url_root,
#             request.route_path('user_preferences'))
#         backups_link = urlparse.urljoin(
#             public_url_root,
#             request.route_path('backups_index'))
# 
#         for user in user_iterator:
#             if user['email']:
#                 sent = send_passwords(request, user,
#                                       preferences_link, backups_link)
#                 if sent:
#                     safe_print('Passwords sent to %s' %
#                                get_user_display_name(user))
# 
#         tx.commit()
# 
#     finally:
#         closer()
# 
# Path: yithlibraryserver/scripts/testing.py
# class ScriptTests(unittest.TestCase):
# 
#     clean_collections = tuple()
# 
#     def setUp(self):
#         super(ScriptTests, self).setUp()
# 
#         fd, self.conf_file_path = tempfile.mkstemp()
#         os.write(fd, CONFIG.encode('ascii'))
#         mdb = MongoDB(MONGO_URI)
#         self.db = mdb.get_database()
# 
#     def tearDown(self):
#         super(ScriptTests, self).tearDown()
#         os.unlink(self.conf_file_path)
#         for col in self.clean_collections:
#             self.db.drop_collection(col)
# 
#     def add_passwords(self, user, n):
#         for i in range(n):
#             self.db.passwords.insert({
#                     'service': 'service-%d' % (i + 1),
#                     'secret': 's3cr3t',
#                     'owner': user,
#                     })
#         return user


import datetime
import os
import sys
from yithlibraryserver.backups.email import get_day_to_send
from yithlibraryserver.compat import StringIO
from yithlibraryserver.scripts.backups import send_backups_via_email
from yithlibraryserver.scripts.testing import ScriptTests

    def test_several_users(self):
        date_joined = datetime.datetime(2012, 12, 12, 12, 12)
        # Add some users
        self.add_passwords(self.db.users.insert({
                    'first_name': 'John1',
                    'last_name': 'Doe',
                    'date_joined': date_joined,
                    'email': '',
                    'email_verified': False,
                    'send_passwords_periodically': False,
                    }), 10)

        i = 1
        while True:
            user_id = self.add_passwords(self.db.users.insert({
                        'first_name': 'John%d' % i,
                        'last_name': 'Doe',
                        'date_joined': date_joined,
                        'email': 'john%d@example.com' % i,
                        'email_verified': True,
                        'send_passwords_periodically': True,
                        }), 10)
            day = get_day_to_send({'_id': user_id}, 28)
            if day == 10:
                break

            i += 1

        sys.argv = ['notused', self.conf_file_path]

# 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
# 
# Path: yithlibraryserver/compat.py
# PY3 = sys.version_info[0] == 3
#     def encode_header(obj):  # pragma: no cover
#     def encode_header(obj):  # pragma: no cover
# 
# Path: yithlibraryserver/scripts/backups.py
# def send_backups_via_email():
#     result = setup_simple_command(
#         "send_backups_via_email",
#         "Report information about users and their passwords.",
#         )
#     if isinstance(result, int):
#         return result
#     else:
#         settings, closer, env, args = result
# 
#     try:
#         request = env['request']
# 
#         if len(args) == 0:
#             user_iterator = get_all_users(request)
#         else:
#             user_iterator = get_selected_users(request, *args)
# 
#         tx = transaction.begin()
# 
#         public_url_root = settings['public_url_root']
#         preferences_link = urlparse.urljoin(
#             public_url_root,
#             request.route_path('user_preferences'))
#         backups_link = urlparse.urljoin(
#             public_url_root,
#             request.route_path('backups_index'))
# 
#         for user in user_iterator:
#             if user['email']:
#                 sent = send_passwords(request, user,
#                                       preferences_link, backups_link)
#                 if sent:
#                     safe_print('Passwords sent to %s' %
#                                get_user_display_name(user))
# 
#         tx.commit()
# 
#     finally:
#         closer()
# 
# Path: yithlibraryserver/scripts/testing.py
# class ScriptTests(unittest.TestCase):
# 
#     clean_collections = tuple()
# 
#     def setUp(self):
#         super(ScriptTests, self).setUp()
# 
#         fd, self.conf_file_path = tempfile.mkstemp()
#         os.write(fd, CONFIG.encode('ascii'))
#         mdb = MongoDB(MONGO_URI)
#         self.db = mdb.get_database()
# 
#     def tearDown(self):
#         super(ScriptTests, self).tearDown()
#         os.unlink(self.conf_file_path)
#         for col in self.clean_collections:
#             self.db.drop_collection(col)
# 
#     def add_passwords(self, user, n):
#         for i in range(n):
#             self.db.passwords.insert({
#                     'service': 'service-%d' % (i + 1),
#                     'secret': 's3cr3t',
#                     'owner': user,
#                     })
#         return user


# Path: yithlibraryserver/scripts/tests/test_backups.py
import datetime
import os
import sys
from yithlibraryserver.backups.email import get_day_to_send
from yithlibraryserver.compat import StringIO
from yithlibraryserver.scripts.backups import send_backups_via_email
from yithlibraryserver.scripts.testing import ScriptTests


    def test_several_users(self):
        date_joined = datetime.datetime(2012, 12, 12, 12, 12)
        # Add some users
        self.add_passwords(self.db.users.insert({
                    'first_name': 'John1',
                    'last_name': 'Doe',
                    'date_joined': date_joined,
                    'email': '',
                    'email_verified': False,
                    'send_passwords_periodically': False,
                    }), 10)

        i = 1
        while True:
            user_id = self.add_passwords(self.db.users.insert({
                        'first_name': 'John%d' % i,
                        'last_name': 'Doe',
                        'date_joined': date_joined,
                        'email': 'john%d@example.com' % i,
                        'email_verified': True,
                        'send_passwords_periodically': True,
                        }), 10)
            day = get_day_to_send({'_id': user_id}, 28)
            if day == 10:
                break

            i += 1

        sys.argv = ['notused', self.conf_file_path]

        sys.stdout = StringIO()