Repository: incubator-airflow Updated Branches: refs/heads/master f1ff09d07 -> df9a10b26
[AIRFLOW-1244] Forbid creation of a pool with empty name Closes #2324 from skudriashev/airflow-1244 Project: http://git-wip-us.apache.org/repos/asf/incubator-airflow/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-airflow/commit/df9a10b2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-airflow/tree/df9a10b2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-airflow/diff/df9a10b2 Branch: refs/heads/master Commit: df9a10b26fda546d0e8124f3d5cd9aefa6c0a81f Parents: f1ff09d Author: Stanislav Kudriashev <[email protected]> Authored: Mon Jun 5 21:48:32 2017 +0200 Committer: Bolke de Bruin <[email protected]> Committed: Mon Jun 5 21:48:32 2017 +0200 ---------------------------------------------------------------------- airflow/www/views.py | 7 ++++ tests/www/test_views.py | 91 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-airflow/blob/df9a10b2/airflow/www/views.py ---------------------------------------------------------------------- diff --git a/airflow/www/views.py b/airflow/www/views.py index 1e9d49d..e250111 100644 --- a/airflow/www/views.py +++ b/airflow/www/views.py @@ -1986,6 +1986,13 @@ class PoolModelView(wwwutils.SuperUserMixin, AirflowModelView): column_formatters = dict( pool=pool_link, used_slots=fused_slots, queued_slots=fqueued_slots) named_filter_urls = True + form_args = { + 'pool': { + 'validators': [ + validators.DataRequired(), + ] + } + } class SlaMissModelView(wwwutils.SuperUserMixin, ModelViewOnly): http://git-wip-us.apache.org/repos/asf/incubator-airflow/blob/df9a10b2/tests/www/test_views.py ---------------------------------------------------------------------- diff --git a/tests/www/test_views.py b/tests/www/test_views.py new file mode 100644 index 0000000..a8823e6 --- /dev/null +++ b/tests/www/test_views.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +from airflow import configuration +from airflow.models import Pool +from airflow.settings import Session +from airflow.www import app as application + + +class TestPoolModelView(unittest.TestCase): + + CREATE_ENDPOINT = '/admin/pool/new/?url=/admin/pool/' + + @classmethod + def setUpClass(cls): + super(TestPoolModelView, cls).setUpClass() + session = Session() + session.query(Pool).delete() + session.commit() + session.close() + + def setUp(self): + super(TestPoolModelView, self).setUp() + configuration.load_test_config() + app = application.create_app(testing=True) + app.config['WTF_CSRF_METHODS'] = [] + self.app = app.test_client() + self.session = Session() + self.pool = { + 'pool': 'test-pool', + 'slots': 777, + 'description': 'test-pool-description', + } + + def tearDown(self): + self.session.query(Pool).delete() + self.session.commit() + self.session.close() + super(TestPoolModelView, self).tearDown() + + def test_create_pool(self): + response = self.app.post( + self.CREATE_ENDPOINT, + data=self.pool, + follow_redirects=True, + ) + self.assertEqual(response.status_code, 200) + self.assertEqual(self.session.query(Pool).count(), 1) + + def test_create_pool_with_same_name(self): + # create test pool + self.app.post( + self.CREATE_ENDPOINT, + data=self.pool, + follow_redirects=True, + ) + # create pool with the same name + response = self.app.post( + self.CREATE_ENDPOINT, + data=self.pool, + follow_redirects=True, + ) + self.assertIn('Already exists.', response.data.decode('utf-8')) + self.assertEqual(self.session.query(Pool).count(), 1) + + def test_create_pool_with_empty_name(self): + self.pool['pool'] = '' + response = self.app.post( + self.CREATE_ENDPOINT, + data=self.pool, + follow_redirects=True, + ) + self.assertIn('This field is required.', response.data.decode('utf-8')) + self.assertEqual(self.session.query(Pool).count(), 0) + + +if __name__ == '__main__': + unittest.main()
