def test_get_cron_jobs_no_cron_yaml(self): cron_handler.pytz = None handler = cron_handler.CronHandler(None, None) self.mox.StubOutWithMock(handler, '_parse_cron_yaml') handler._parse_cron_yaml().AndReturn(None) self.mox.ReplayAll() self.assertEqual([], handler._get_cron_jobs()) self.mox.VerifyAll()
def test_get_cron_jobs_without_pytz(self): cron_handler.pytz = None handler = cron_handler.CronHandler(None, None) self.mox.StubOutWithMock(handler, '_parse_cron_yaml') handler._parse_cron_yaml().AndReturn(CRON_INFO_EXTERNAL) self.mox.ReplayAll() cron_jobs = handler._get_cron_jobs() self.mox.VerifyAll() self.assertEqual(2, len(cron_jobs)) self.assertEqual(CRON_INFO_EXTERNAL.cron[0].ToDict(), cron_jobs[0]) next_run = cron_jobs[1].pop('times') self.assertEqual(CRON_INFO_EXTERNAL.cron[1].ToDict(), cron_jobs[1]) self.assertEqual(3, len(next_run)) for run in next_run: datetime.datetime.strptime(run['runtime'], '%Y-%m-%d %H:%M:%SZ')
def test_get_with_pytz(self, pytz=object()): cron_handler.pytz = pytz jobs = object() request = webapp2.Request.blank('/cron') response = webapp2.Response() handler = cron_handler.CronHandler(request, response) self.mox.StubOutWithMock(handler, '_get_cron_jobs') self.mox.StubOutWithMock(handler, 'render') handler._get_cron_jobs().AndReturn(jobs) handler.render('cron.html', {'has_pytz': bool(pytz), 'cronjobs': jobs}).AndReturn( 'template') self.mox.ReplayAll() handler.get() self.mox.VerifyAll() self.assertEqual('template', response.body)
def test_get_with_invalid_cron_yaml(self): cron_handler.pytz = None request = webapp2.Request.blank('/cron') response = webapp2.Response() handler = cron_handler.CronHandler(request, response) self.mox.StubOutWithMock(handler, '_get_cron_jobs') self.mox.StubOutWithMock(handler, 'render') self.mox.StubOutWithMock(traceback, 'format_exc') handler._get_cron_jobs().AndRaise(yaml_errors.Error) traceback.format_exc().AndReturn('traceback') handler.render( 'cron.html', {'has_pytz': False, 'cron_error': 'traceback'}).AndReturn('template') self.mox.ReplayAll() handler.get() self.mox.VerifyAll() self.assertEqual('template', response.body)
def test_post(self): self.mox.StubOutWithMock(cron_handler.CronHandler, 'dispatcher') request = webapp2.Request.blank('/cron', POST={'url': '/url'}) response = webapp2.Response() handler = cron_handler.CronHandler(request, response) handler.dispatcher = self.mox.CreateMock(dispatcher.Dispatcher) handler.dispatcher.add_request( method='GET', relative_url='/url', headers=[('X-AppEngine-Cron', 'true')], body='', source_ip='0.1.0.1').AndReturn( dispatcher.ResponseTuple('500 Internal Server Error', [], '')) self.mox.ReplayAll() handler.post() self.mox.VerifyAll() self.assertEqual(500, response.status_int)