# Path: yithlibraryserver/email.py
# def create_message(request, template, context, subject, recipients,
#                    attachments=None, extra_headers=None):
#     text_body = render(template + '.txt', context, request=request)
#     # chamaleon txt templates are rendered as utf-8 bytestrings
#     text_body = text_body.decode('utf-8')
# 
#     html_body = render(template + '.pt', context, request=request)
# 
#     extra_headers = extra_headers or {}
# 
#     message = Message(
#         subject=subject,
#         recipients=recipients,
#         body=text_body,
#         html=html_body,
#         extra_headers=extra_headers,
#     )
# 
#     if attachments is not None:
#         for attachment in attachments:
#             message.attach(attachment)
# 
#     return message
# 
# def send_email(request, template, context, subject, recipients,
#                attachments=None, extra_headers=None):
#     message = create_message(request, template, context, subject, recipients,
#                              attachments, extra_headers)
#     return get_mailer(request).send(message)
# 
# Path: yithlibraryserver/email.py
# def send_email_to_admins(request, template, context, subject,
#                          attachments=None, extra_headers=None):
#     admin_emails = request.registry.settings['admin_emails']
#     if admin_emails:
#         return send_email(request, template, context, subject, admin_emails,
#                           attachments, extra_headers)
# 
# 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 unittest
from pyramid import testing
from pyramid_mailer import get_mailer
from pyramid_mailer.message import Attachment
from yithlibraryserver.email import create_message, send_email
from yithlibraryserver.email import send_email_to_admins
from yithlibraryserver.testing import TestCase
            request,
            'yithlibraryserver.tests:templates/email_test',
            {'name': 'John', 'email': 'john@example.com'},
            'Testing message',
        )

        self.assertEqual(len(mailer.outbox), 1)
        message = mailer.outbox[0]
        self.assertEqual(message.subject, 'Testing message')
        self.assertEqual(message.html, '<p>Hello John,</p>\n\n<p>this is your email address: john@example.com</p>')
        self.assertEqual(message.body, 'Hello John,\n\nthis is your email address: john@example.com\n')
        self.assertEqual(message.recipients, self.admin_emails)
        self.assertEqual(message.attachments, [])
        self.assertEqual(message.extra_headers, {})


class SendEmailNoAdminsTests(TestCase):

    def setUp(self):
        self.config = testing.setUp(settings={
                'admin_emails': [],
                })
        self.config.include('pyramid_mailer.testing')
        self.config.include('yithlibraryserver')
        super(SendEmailNoAdminsTests, self).setUp()

    def test_send_email_to_admins(self):
        request = testing.DummyRequest()
        mailer = get_mailer(request)


# Path: yithlibraryserver/email.py
# def create_message(request, template, context, subject, recipients,
#                    attachments=None, extra_headers=None):
#     text_body = render(template + '.txt', context, request=request)
#     # chamaleon txt templates are rendered as utf-8 bytestrings
#     text_body = text_body.decode('utf-8')
# 
#     html_body = render(template + '.pt', context, request=request)
# 
#     extra_headers = extra_headers or {}
# 
#     message = Message(
#         subject=subject,
#         recipients=recipients,
#         body=text_body,
#         html=html_body,
#         extra_headers=extra_headers,
#     )
# 
#     if attachments is not None:
#         for attachment in attachments:
#             message.attach(attachment)
# 
#     return message
# 
# def send_email(request, template, context, subject, recipients,
#                attachments=None, extra_headers=None):
#     message = create_message(request, template, context, subject, recipients,
#                              attachments, extra_headers)
#     return get_mailer(request).send(message)
# 
# Path: yithlibraryserver/email.py
# def send_email_to_admins(request, template, context, subject,
#                          attachments=None, extra_headers=None):
#     admin_emails = request.registry.settings['admin_emails']
#     if admin_emails:
#         return send_email(request, template, context, subject, admin_emails,
#                           attachments, extra_headers)
# 
# 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/tests/test_email.py
import unittest
from pyramid import testing
from pyramid_mailer import get_mailer
from pyramid_mailer.message import Attachment
from yithlibraryserver.email import create_message, send_email
from yithlibraryserver.email import send_email_to_admins
from yithlibraryserver.testing import TestCase

            request,
            'yithlibraryserver.tests:templates/email_test',
            {'name': 'John', 'email': 'john@example.com'},
            'Testing message',
        )

        self.assertEqual(len(mailer.outbox), 1)
        message = mailer.outbox[0]
        self.assertEqual(message.subject, 'Testing message')
        self.assertEqual(message.html, '<p>Hello John,</p>\n\n<p>this is your email address: john@example.com</p>')
        self.assertEqual(message.body, 'Hello John,\n\nthis is your email address: john@example.com\n')
        self.assertEqual(message.recipients, self.admin_emails)
        self.assertEqual(message.attachments, [])
        self.assertEqual(message.extra_headers, {})


class SendEmailNoAdminsTests(TestCase):

    def setUp(self):
        self.config = testing.setUp(settings={
                'admin_emails': [],
                })
        self.config.include('pyramid_mailer.testing')
        self.config.include('yithlibraryserver')
        super(SendEmailNoAdminsTests, self).setUp()

    def test_send_email_to_admins(self):
        request = testing.DummyRequest()
        mailer = get_mailer(request)


        send_email_to_admins(