# 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/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/analytics.py
# USER_ATTR = 'allow_google_analytics'


import datetime
import os
from deform import ValidationFailure
from mock import patch
from pyramid_mailer import get_mailer
from yithlibraryserver.backups.email import get_day_to_send
from yithlibraryserver.compat import url_quote
from yithlibraryserver.testing import TestCase
from yithlibraryserver.user.analytics import USER_ATTR
        self.assertEqual(res.status, '200 OK')
        self.assertEqual(res.json, {'allow': False})
        self.assertEqual(self.get_session(res)[USER_ATTR], False)

        # Authenticated users save the preference in the database
        # Log in
        user_id = self.db.users.insert({
                'twitter_id': 'twitter1',
                'screen_name': 'John Doe',
                'first_name': 'John',
                'last_name': 'Doe',
                'email': 'john@example.com',
                'email_verified': True,
                'authorized_apps': ['app1', 'app2'],
                }, safe=True)
        self.set_user_cookie(str(user_id))

        res = self.testapp.post('/google-analytics-preference', {'yes': 'Yes'})
        self.assertEqual(res.status, '200 OK')
        self.assertEqual(res.json, {'allow': True})
        user_refreshed = self.db.users.find_one({'_id': user_id}, safe=True)
        self.assertEqual(user_refreshed[USER_ATTR], True)

        res = self.testapp.post('/google-analytics-preference', {'no': 'No'})
        self.assertEqual(res.status, '200 OK')
        self.assertEqual(res.json, {'allow': False})
        user_refreshed = self.db.users.find_one({'_id': user_id}, safe=True)
        self.assertEqual(user_refreshed[USER_ATTR], False)



# 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/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/analytics.py
# USER_ATTR = 'allow_google_analytics'


# Path: yithlibraryserver/user/tests/test_views.py
import datetime
import os
from deform import ValidationFailure
from mock import patch
from pyramid_mailer import get_mailer
from yithlibraryserver.backups.email import get_day_to_send
from yithlibraryserver.compat import url_quote
from yithlibraryserver.testing import TestCase
from yithlibraryserver.user.analytics import USER_ATTR

        self.assertEqual(res.status, '200 OK')
        self.assertEqual(res.json, {'allow': False})
        self.assertEqual(self.get_session(res)[USER_ATTR], False)

        # Authenticated users save the preference in the database
        # Log in
        user_id = self.db.users.insert({
                'twitter_id': 'twitter1',
                'screen_name': 'John Doe',
                'first_name': 'John',
                'last_name': 'Doe',
                'email': 'john@example.com',
                'email_verified': True,
                'authorized_apps': ['app1', 'app2'],
                }, safe=True)
        self.set_user_cookie(str(user_id))

        res = self.testapp.post('/google-analytics-preference', {'yes': 'Yes'})
        self.assertEqual(res.status, '200 OK')
        self.assertEqual(res.json, {'allow': True})
        user_refreshed = self.db.users.find_one({'_id': user_id}, safe=True)
        self.assertEqual(user_refreshed[USER_ATTR], True)

        res = self.testapp.post('/google-analytics-preference', {'no': 'No'})
        self.assertEqual(res.status, '200 OK')
        self.assertEqual(res.json, {'allow': False})
        user_refreshed = self.db.users.find_one({'_id': user_id}, safe=True)
        self.assertEqual(user_refreshed[USER_ATTR], False)



class RESTViewTests(TestCase):