diff options
author | Milo Casagrande <milo.casagrande@linaro.org> | 2015-03-06 14:33:29 +0100 |
---|---|---|
committer | Milo Casagrande <milo.casagrande@linaro.org> | 2015-03-06 14:33:29 +0100 |
commit | 438af4f1b2e97e96f0774b2786f030897d9de252 (patch) | |
tree | 585254f91648e9d39c92253bcb94b4f4bc99a409 | |
parent | 2a6d8bc208463a0f903a2b62021abb913ab510c5 (diff) |
Add test suite URL rule and handler tests.
-rw-r--r-- | app/handlers/tests/test_test_suite_handler.py | 192 | ||||
-rw-r--r-- | app/tests/__init__.py | 1 | ||||
-rw-r--r-- | app/urls.py | 9 |
3 files changed, 201 insertions, 1 deletions
diff --git a/app/handlers/tests/test_test_suite_handler.py b/app/handlers/tests/test_test_suite_handler.py new file mode 100644 index 0000000..b26da19 --- /dev/null +++ b/app/handlers/tests/test_test_suite_handler.py @@ -0,0 +1,192 @@ +# Copyright (C) 2014 Linaro Ltd. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +"""Test module for the JobHandler handler.""" + +import concurrent.futures +import json +import mock +import mongomock +import tornado +import tornado.testing + +import handlers.app +import urls + +# Default Content-Type header returned by Tornado. +DEFAULT_CONTENT_TYPE = 'application/json; charset=UTF-8' + + +class TestTestSuiteHandler( + tornado.testing.AsyncHTTPTestCase, tornado.testing.LogTrapTestCase): + + def setUp(self): + self.mongodb_client = mongomock.Connection() + + super(TestTestSuiteHandler, self).setUp() + + patched_find_token = mock.patch( + "handlers.base.BaseHandler._find_token") + self.find_token = patched_find_token.start() + self.find_token.return_value = "token" + + patched_validate_token = mock.patch("handlers.common.validate_token") + self.validate_token = patched_validate_token.start() + self.validate_token.return_value = True + + self.addCleanup(patched_find_token.stop) + self.addCleanup(patched_validate_token.stop) + + def get_app(self): + dboptions = { + "dbpassword": "", + "dbuser": "" + } + + mailoptions = {} + + settings = { + "dboptions": dboptions, + "mailoptions": mailoptions, + "senddelay": 5, + "client": self.mongodb_client, + "executor": concurrent.futures.ThreadPoolExecutor(max_workers=2), + "default_handler_class": handlers.app.AppHandler, + "debug": False + } + + return tornado.web.Application([urls._TEST_SUITE_URL], **settings) + + def get_new_ioloop(self): + return tornado.ioloop.IOLoop.instance() + + @mock.patch("utils.db.find_and_count") + def test_get(self, mock_find): + mock_find.return_value = ([{"foo": "bar"}], 1) + + headers = {"Authorization": "foo"} + response = self.fetch("/test/suite/", headers=headers) + + self.assertEqual(response.code, 200) + self.assertEqual( + response.headers["Content-Type"], DEFAULT_CONTENT_TYPE) + + @mock.patch("bson.objectid.ObjectId") + @mock.patch("handlers.test_suite.TestSuiteHandler.collection") + def test_get_by_id_not_found(self, collection, mock_id): + mock_id.return_value = "suite-id" + collection.find_one = mock.MagicMock() + collection.find_one.return_value = None + + headers = {"Authorization": "foo"} + response = self.fetch("/test/suite/suite-id", headers=headers) + + self.assertEqual(response.code, 404) + self.assertEqual( + response.headers["Content-Type"], DEFAULT_CONTENT_TYPE) + + @mock.patch("bson.objectid.ObjectId") + @mock.patch("handlers.test_suite.TestSuiteHandler.collection") + def test_get_by_id_not_found_empty_list(self, collection, mock_id): + mock_id.return_value = "suite-id" + collection.find_one = mock.MagicMock() + collection.find_one.return_value = [] + + headers = {"Authorization": "foo"} + response = self.fetch("/test/suite/suite-id", headers=headers) + + self.assertEqual(response.code, 404) + self.assertEqual( + response.headers["Content-Type"], DEFAULT_CONTENT_TYPE) + + @mock.patch("bson.objectid.ObjectId") + @mock.patch("handlers.test_suite.TestSuiteHandler.collection") + def test_get_by_id_found(self, collection, mock_id): + mock_id.return_value = "suite-id" + collection.find_one = mock.MagicMock() + collection.find_one.return_value = {"_id": "suite-id"} + + headers = {"Authorization": "foo"} + response = self.fetch("/test/suite/suite-id", headers=headers) + + self.assertEqual(response.code, 200) + self.assertEqual( + response.headers["Content-Type"], DEFAULT_CONTENT_TYPE) + + def test_post_without_token(self): + body = json.dumps(dict(name="suite", version="1.0")) + + response = self.fetch("/test/suite", method="POST", body=body) + + self.assertEqual(response.code, 403) + self.assertEqual( + response.headers["Content-Type"], DEFAULT_CONTENT_TYPE) + + def test_post_not_json_content(self): + headers = {"Authorization": "foo", "Content-Type": "application/json"} + + response = self.fetch( + "/test/suite", method="POST", body="", headers=headers + ) + + self.assertEqual(response.code, 422) + self.assertEqual( + response.headers["Content-Type"], DEFAULT_CONTENT_TYPE) + + def test_post_wrong_content_type(self): + headers = {"Authorization": "foo"} + + response = self.fetch( + "/test/suite", method="POST", body="", headers=headers + ) + + self.assertEqual(response.code, 415) + self.assertEqual( + response.headers["Content-Type"], DEFAULT_CONTENT_TYPE) + + def test_post_wrong_json(self): + headers = {"Authorization": "foo", "Content-Type": "application/json"} + + body = json.dumps(dict(foo="foo", bar="bar")) + + response = self.fetch( + "/test/suite", method="POST", body=body, headers=headers + ) + + self.assertEqual(response.code, 400) + self.assertEqual( + response.headers["Content-Type"], DEFAULT_CONTENT_TYPE) + + @mock.patch("utils.db.save") + def test_post_correct(self, mock_save): + mock_save.return_value = (201, "test-suite-id") + headers = { + "Authorization": "foo", + "Content-Type": "application/json", + } + + body = json.dumps( + dict( + name="test", lab_name="lab_name", version="1.0", + defconfig_id="defconfig_id") + ) + + response = self.fetch( + "/test/suite", method="POST", headers=headers, body=body + ) + + self.assertEqual(response.code, 201) + self.assertEqual( + response.headers["Content-Type"], DEFAULT_CONTENT_TYPE) diff --git a/app/tests/__init__.py b/app/tests/__init__.py index 4bf8d3e..67cb946 100644 --- a/app/tests/__init__.py +++ b/app/tests/__init__.py @@ -31,6 +31,7 @@ def test_modules(): "handlers.tests.test_lab_handler", "handlers.tests.test_report_handler", "handlers.tests.test_send_handler", + "handlers.tests.test_test_suite_handler", "handlers.tests.test_token_handler", "handlers.tests.test_upload_handler", "handlers.tests.test_version_handler", diff --git a/app/urls.py b/app/urls.py index 22e74ca..c06d228 100644 --- a/app/urls.py +++ b/app/urls.py @@ -25,6 +25,7 @@ import handlers.lab import handlers.report import handlers.send import handlers.subscription +import handlers.test_suite import handlers.token import handlers.upload import handlers.version @@ -81,6 +82,11 @@ _SEND_URL = tornado.web.url( handlers.send.SendHandler, name="send" ) +_TEST_SUITE_URL = tornado.web.url( + r"/test[s]?/suite[s]?/?(?P<id>.*)", + handlers.test_suite.TestSuiteHandler, + name="test-suite" +) APP_URLS = [ _BATCH_URL, @@ -95,5 +101,6 @@ APP_URLS = [ _VERSION_URL, _REPORT_URL, _UPLOAD_URL, - _SEND_URL + _SEND_URL, + _TEST_SUITE_URL ] |