# Path: pushmanager/core/db.py
# def UnsignedInteger():
# def init_db():
# def finalize_db():
# def execute_cb(query, callback_fn):
# def execute_transaction_cb(queries, callback_fn, condition=None):
# def prefix_insert_ignore(insert_ignore, compiler, **kw):
# def prefix_insert_ignore_sqlite(insert_ignore, compiler, **kw):
# class DatabaseError(Exception):
# class PushCheckList(Base):
# class PushPlans(Base):
# class PushPushContents(Base):
# class PushPushes(Base):
# class PushRemovals(Base):
# class PushRequests(Base):
# class InsertIgnore(Insert):
# 
# Path: pushmanager/servlets/livepush.py
# class LivePushServlet(RequestHandler):
# 
#     def _arg(self, key):
#         return pushmanager.core.util.get_str_arg(self.request, key, '')
# 
#     def post(self):
#         if not self.current_user:
#             return self.send_error(403)
#         self.pushid = pushmanager.core.util.get_int_arg(self.request, 'id')
#         push_query = db.push_pushes.update().where(db.push_pushes.c.id == self.pushid).values({
#             'state': 'live',
#             'modified': time.time(),
#             })
#         request_query = db.push_requests.update().where(SA.and_(
#             db.push_requests.c.state == 'blessed',
#             SA.exists(
#                 [1],
#                 SA.and_(
#                     db.push_pushcontents.c.push == self.pushid,
#                     db.push_pushcontents.c.request == db.push_requests.c.id,
#                 )
#             ))).values({
#                 'state': 'live',
#                 'modified': time.time(),
#             })
#         reset_query = db.push_requests.update().where(
#             SA.exists(
#                 [1],
#                 SA.and_(
#                     db.push_requests.c.state == 'pickme',
#                     db.push_pushcontents.c.push == self.pushid,
#                     db.push_pushcontents.c.request == db.push_requests.c.id,
#                 )
#             )).values({
#                 'state': 'requested',
#             })
#         delete_query = db.push_pushcontents.delete().where(
#             SA.exists([1], SA.and_(
#                 db.push_pushcontents.c.push == self.pushid,
#                 db.push_pushcontents.c.request == db.push_requests.c.id,
#                 db.push_requests.c.state == 'requested',
#             )))
#         live_query = db.push_requests.select().where(
#             SA.and_(db.push_requests.c.state == 'live',
#                     db.push_pushcontents.c.push == self.pushid,
#                     db.push_pushcontents.c.request == db.push_requests.c.id)
#             )
#         db.execute_transaction_cb(
#             [push_query, request_query, reset_query, delete_query, live_query],
#             self.on_db_complete,
#         )
# 
#     def on_db_complete(self, success, db_results):
#         self.check_db_results(success, db_results)
# 
#         _, _, _, _, live_requests = db_results
#         for req in live_requests:
#             if req['reviewid']:
#                 review_id = int(req['reviewid'])
#                 RBQueue.enqueue_review(review_id)
# 
#             if req['watchers']:
#                 user_string = '%s (%s)' % (req['user'], req['watchers'])
#                 users = [req['user']] + req['watchers'].split(',')
#             else:
#                 user_string = req['user']
#                 users = [req['user']]
# 
#             msg = (
#                 """
#                 <p>
#                     %(pushmaster)s has certified request for %(user)s as stable in production:
#                 </p>
#                 <p>
#                     <strong>%(user)s - %(title)s</strong><br />
#                     <em>%(repo)s/%(branch)s</em>
#                 </p>
#                 <p>
#                     Regards,<br />
#                     PushManager
#                 </p>"""
#                 ) % pushmanager.core.util.EscapedDict({
#                     'pushmaster': self.current_user,
#                     'user': user_string,
#                     'title': req['title'],
#                     'repo': req['repo'],
#                     'branch': req['branch'],
#                 })
#             subject = "[push] %s - %s" % (user_string, req['title'])
#             MailQueue.enqueue_user_email(users, msg, subject)
# 
# Path: pushmanager/testing/mocksettings.py



import types
import mock
import testify as T
from contextlib import nested
from pushmanager.core import db
from pushmanager.core.util import get_servlet_urlspec
from pushmanager.servlets.livepush import LivePushServlet
from pushmanager.testing.mocksettings import MockedSettings
from pushmanager.testing.testservlet import ServletTestMixin



class LivePushServletTest(T.TestCase, ServletTestMixin):

    @T.class_setup_teardown
    def mock_servlet_env(self):
        self.results = []
        with nested(
            mock.patch.dict(db.Settings, MockedSettings),
            mock.patch.object(
                LivePushServlet,
                "get_current_user",
                return_value="testuser"
            )
        ):
            yield

    def get_handlers(self):

# Path: pushmanager/core/db.py
# def UnsignedInteger():
# def init_db():
# def finalize_db():
# def execute_cb(query, callback_fn):
# def execute_transaction_cb(queries, callback_fn, condition=None):
# def prefix_insert_ignore(insert_ignore, compiler, **kw):
# def prefix_insert_ignore_sqlite(insert_ignore, compiler, **kw):
# class DatabaseError(Exception):
# class PushCheckList(Base):
# class PushPlans(Base):
# class PushPushContents(Base):
# class PushPushes(Base):
# class PushRemovals(Base):
# class PushRequests(Base):
# class InsertIgnore(Insert):
# 
# Path: pushmanager/servlets/livepush.py
# class LivePushServlet(RequestHandler):
# 
#     def _arg(self, key):
#         return pushmanager.core.util.get_str_arg(self.request, key, '')
# 
#     def post(self):
#         if not self.current_user:
#             return self.send_error(403)
#         self.pushid = pushmanager.core.util.get_int_arg(self.request, 'id')
#         push_query = db.push_pushes.update().where(db.push_pushes.c.id == self.pushid).values({
#             'state': 'live',
#             'modified': time.time(),
#             })
#         request_query = db.push_requests.update().where(SA.and_(
#             db.push_requests.c.state == 'blessed',
#             SA.exists(
#                 [1],
#                 SA.and_(
#                     db.push_pushcontents.c.push == self.pushid,
#                     db.push_pushcontents.c.request == db.push_requests.c.id,
#                 )
#             ))).values({
#                 'state': 'live',
#                 'modified': time.time(),
#             })
#         reset_query = db.push_requests.update().where(
#             SA.exists(
#                 [1],
#                 SA.and_(
#                     db.push_requests.c.state == 'pickme',
#                     db.push_pushcontents.c.push == self.pushid,
#                     db.push_pushcontents.c.request == db.push_requests.c.id,
#                 )
#             )).values({
#                 'state': 'requested',
#             })
#         delete_query = db.push_pushcontents.delete().where(
#             SA.exists([1], SA.and_(
#                 db.push_pushcontents.c.push == self.pushid,
#                 db.push_pushcontents.c.request == db.push_requests.c.id,
#                 db.push_requests.c.state == 'requested',
#             )))
#         live_query = db.push_requests.select().where(
#             SA.and_(db.push_requests.c.state == 'live',
#                     db.push_pushcontents.c.push == self.pushid,
#                     db.push_pushcontents.c.request == db.push_requests.c.id)
#             )
#         db.execute_transaction_cb(
#             [push_query, request_query, reset_query, delete_query, live_query],
#             self.on_db_complete,
#         )
# 
#     def on_db_complete(self, success, db_results):
#         self.check_db_results(success, db_results)
# 
#         _, _, _, _, live_requests = db_results
#         for req in live_requests:
#             if req['reviewid']:
#                 review_id = int(req['reviewid'])
#                 RBQueue.enqueue_review(review_id)
# 
#             if req['watchers']:
#                 user_string = '%s (%s)' % (req['user'], req['watchers'])
#                 users = [req['user']] + req['watchers'].split(',')
#             else:
#                 user_string = req['user']
#                 users = [req['user']]
# 
#             msg = (
#                 """
#                 <p>
#                     %(pushmaster)s has certified request for %(user)s as stable in production:
#                 </p>
#                 <p>
#                     <strong>%(user)s - %(title)s</strong><br />
#                     <em>%(repo)s/%(branch)s</em>
#                 </p>
#                 <p>
#                     Regards,<br />
#                     PushManager
#                 </p>"""
#                 ) % pushmanager.core.util.EscapedDict({
#                     'pushmaster': self.current_user,
#                     'user': user_string,
#                     'title': req['title'],
#                     'repo': req['repo'],
#                     'branch': req['branch'],
#                 })
#             subject = "[push] %s - %s" % (user_string, req['title'])
#             MailQueue.enqueue_user_email(users, msg, subject)
# 
# Path: pushmanager/testing/mocksettings.py



# Path: pushmanager/tests/test_servlet_livepush.py
import types
import mock
import testify as T
from contextlib import nested
from pushmanager.core import db
from pushmanager.core.util import get_servlet_urlspec
from pushmanager.servlets.livepush import LivePushServlet
from pushmanager.testing.mocksettings import MockedSettings
from pushmanager.testing.testservlet import ServletTestMixin




class LivePushServletTest(T.TestCase, ServletTestMixin):

    @T.class_setup_teardown
    def mock_servlet_env(self):
        self.results = []
        with nested(
            mock.patch.dict(db.Settings, MockedSettings),
            mock.patch.object(
                LivePushServlet,
                "get_current_user",
                return_value="testuser"
            )
        ):
            yield

    def get_handlers(self):

        return [get_servlet_urlspec(LivePushServlet)]