diff options
author | Milo Casagrande <milo.casagrande@linaro.org> | 2015-01-20 10:18:36 +0100 |
---|---|---|
committer | Milo Casagrande <milo.casagrande@linaro.org> | 2015-01-20 10:18:36 +0100 |
commit | 3e369350588db0c8605b1ac6119977fe126e58c6 (patch) | |
tree | 1a940e908c894b7c8c18d93ec753a39a27ab13f3 | |
parent | dedc04c79a6721cc7cbebaf9a917a28867fd1d7c (diff) |
Make sure the delay value is valid.
Change-Id: Ia61658298143a7ca2a518d187abb57f7cd66452b
-rw-r--r-- | app/handlers/send.py | 29 | ||||
-rw-r--r-- | app/handlers/tests/test_send_handler.py | 53 |
2 files changed, 62 insertions, 20 deletions
diff --git a/app/handlers/send.py b/app/handlers/send.py index 51de4b7..c6f9bf5 100644 --- a/app/handlers/send.py +++ b/app/handlers/send.py @@ -45,17 +45,24 @@ class SendHandler(hbase.BaseHandler): if countdown is None: countdown = self.settings["senddelay"] - when = ( - datetime.datetime.now(tz=bson.tz_util.utc) + - datetime.timedelta(seconds=countdown) - ) - response.reason = ( - "Email report scheduled to be sent at '%s' UTC" % - when.isoformat() - ) - - taskq.schedule_boot_report.apply_async( - [json_obj, db_options, mail_options, countdown]) + try: + countdown = abs(int(countdown)) + when = ( + datetime.datetime.now(tz=bson.tz_util.utc) + + datetime.timedelta(seconds=countdown) + ) + response.reason = ( + "Email report scheduled to be sent at '%s' UTC" % + when.isoformat() + ) + + taskq.schedule_boot_report.apply_async( + [json_obj, db_options, mail_options, countdown]) + except (TypeError, ValueError): + response.status_code = 400 + response.reason = ( + "Wrong value specified for 'delay': %s" % countdown + ) return response diff --git a/app/handlers/tests/test_send_handler.py b/app/handlers/tests/test_send_handler.py index 98565d4..ecd33bd 100644 --- a/app/handlers/tests/test_send_handler.py +++ b/app/handlers/tests/test_send_handler.py @@ -35,6 +35,11 @@ class TestSendHandler(testing.AsyncHTTPTestCase, testing.LogTrapTestCase): def setUp(self): self.mongodb_client = mongomock.Connection() + self.dboptions = { + "dbpassword": "", + "dbuser": "" + } + self.mailoptions = {} super(TestSendHandler, self).setUp() @@ -51,21 +56,15 @@ class TestSendHandler(testing.AsyncHTTPTestCase, testing.LogTrapTestCase): self.addCleanup(patched_validate_token.stop) def get_app(self): - dboptions = { - "dbpassword": "", - "dbuser": "" - } - - mailoptions = {} settings = { - "dboptions": dboptions, + "dboptions": self.dboptions, "client": self.mongodb_client, "executor": ThreadPoolExecutor(max_workers=2), "default_handler_class": AppHandler, "master_key": "bar", "debug": False, - "mailoptions": mailoptions, + "mailoptions": self.mailoptions, "senddelay": 60*60 } @@ -141,9 +140,45 @@ class TestSendHandler(testing.AsyncHTTPTestCase, testing.LogTrapTestCase): "Authorization": "foo", "Content-Type": "application/json", } - body = json.dumps(dict(job="job", kernel="kernel", delay=None)) + data = dict(job="job", kernel="kernel", delay=None) + body = json.dumps(data) + response = self.fetch( + "/send", method="POST", headers=headers, body=body) + self.assertEqual(response.code, 202) + self.assertEqual( + response.headers["Content-Type"], DEFAULT_CONTENT_TYPE) + mock_schedule.apply_async.assert_called_with( + [data, self.dboptions, self.mailoptions, 60*60] + ) + + @mock.patch("taskqueue.tasks.schedule_boot_report") + def test_post_wrong_delay(self, mock_schedule): + mock_schedule.apply_async = mock.MagicMock() + headers = { + "Authorization": "foo", + "Content-Type": "application/json", + } + body = json.dumps(dict(job="job", kernel="kernel", delay="foo")) + response = self.fetch( + "/send", method="POST", headers=headers, body=body) + self.assertEqual(response.code, 400) + self.assertEqual( + response.headers["Content-Type"], DEFAULT_CONTENT_TYPE) + + @mock.patch("taskqueue.tasks.schedule_boot_report") + def test_post_negative_delay(self, mock_schedule): + mock_schedule.apply_async = mock.MagicMock() + headers = { + "Authorization": "foo", + "Content-Type": "application/json", + } + data = dict(job="job", kernel="kernel", delay=-100) + body = json.dumps(data) response = self.fetch( "/send", method="POST", headers=headers, body=body) self.assertEqual(response.code, 202) self.assertEqual( response.headers["Content-Type"], DEFAULT_CONTENT_TYPE) + mock_schedule.apply_async.assert_called_with( + [data, self.dboptions, self.mailoptions, 100] + ) |