URL: https://github.com/freeipa/freeipa/pull/5230 Author: abbra Title: #5230: wgi/plugins.py: ignore empty plugin directories Action: opened
PR body: """ Dynamic plugin registry returns as a plugin any folder within the plugins directory. Web UI then attempts to load for each plugin 'foo' a JavaScript file named 'foo/foo.js'. The problem is that if 'foo/foo.js' does not exist, Web UI breaks and it is impossible to recover until the empty folder is removed or 'foo/foo.js' (even empty) is created at the server side. Check that 'foo/foo.js' actual exists when including a plugin into the registry. Test the registry generator by creating fake plugins and removing them during the test. Signed-off-by: Alexander Bokovoy <aboko...@redhat.com> """ To pull the PR as Git branch: git remote add ghfreeipa https://github.com/freeipa/freeipa git fetch ghfreeipa pull/5230/head:pr5230 git checkout pr5230
From 58126b696aadac7b8c05077089e34aa717343502 Mon Sep 17 00:00:00 2001 From: Alexander Bokovoy <aboko...@redhat.com> Date: Fri, 6 Nov 2020 09:53:35 +0200 Subject: [PATCH] wgi/plugins.py: ignore empty plugin directories Dynamic plugin registry returns as a plugin any folder within the plugins directory. Web UI then attempts to load for each plugin 'foo' a JavaScript file named 'foo/foo.js'. The problem is that if 'foo/foo.js' does not exist, Web UI breaks and it is impossible to recover until the empty folder is removed or 'foo/foo.js' (even empty) is created at the server side. Check that 'foo/foo.js' actual exists when including a plugin into the registry. Test the registry generator by creating fake plugins and removing them during the test. Signed-off-by: Alexander Bokovoy <aboko...@redhat.com> --- install/wsgi/plugins.py | 5 +- ipatests/test_ipaserver/test_jsplugins.py | 82 +++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 ipatests/test_ipaserver/test_jsplugins.py diff --git a/install/wsgi/plugins.py b/install/wsgi/plugins.py index f80cfb9feba..4c43e7f8790 100644 --- a/install/wsgi/plugins.py +++ b/install/wsgi/plugins.py @@ -36,7 +36,10 @@ def get_plugin_index(): dirs = os.listdir(paths.IPA_JS_PLUGINS_DIR) index = 'define([],function(){return[' - index += ','.join("'"+x+"'" for x in dirs) + for x in dirs: + p = os.path.join(paths.IPA_JS_PLUGINS_DIR, x, x + '.js') + if os.path.exists(p): + index += "'" + x + "'," index += '];});' return index.encode('utf-8') diff --git a/ipatests/test_ipaserver/test_jsplugins.py b/ipatests/test_ipaserver/test_jsplugins.py new file mode 100644 index 00000000000..9bf11c667ea --- /dev/null +++ b/ipatests/test_ipaserver/test_jsplugins.py @@ -0,0 +1,82 @@ +# Authors: +# Martin Kosek <mko...@redhat.com> +# +# Copyright (C) 2012 Red Hat +# see file 'COPYING' for use and warranty information +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import os +import pytest + +from ipatests.test_ipaserver.httptest import Unauthorized_HTTP_test +from ipatests.util import assert_equal, assert_not_equal +from ipaplatform.paths import paths + + +@pytest.mark.tier1 +class test_jsplugins(Unauthorized_HTTP_test): + app_uri = '/ipa/ui/js/freeipa/plugins.js' + jsplugins = (('foo', 'foo.js'), ('bar', '')) + content_type = 'application/javascript' + + def test_jsplugins(self): + empty_response = "define([],function(){return[];});" + + # Step 1: make sure default response has no additional plugins + response = self.send_request(method='GET') + assert_equal(response.status, 200) + response_data = response.read() + assert_equal(response_data, empty_response) + + # Step 2: add fake plugins + try: + for (d, f) in self.jsplugins: + dir = os.path.join(paths.IPA_JS_PLUGINS_DIR, d) + if not os.path.exists(dir): + os.mkdir(dir, 0o755) + if f: + with os.open(os.path.join(dir, f), 'w') as js: + js.write("/* test js plugin */") + + except OSError as e: + pytest.skip( + 'Cannot set up test JS plugin: %s' % e + ) + + # Step 3: query plugins to see if our plugins exist + response = self.send_request(method='GET') + assert_equal(response.status, 200) + response_data = response.read() + assert_not_equal(response_data, empty_response) + for p in self.jsplugins: + assert "'" + p[0] + "'" in response_data + + # Step 4: remove fake plugins + try: + for (d, f) in self.jsplugins: + dir = os.path.join(paths.IPA_JS_PLUGINS_DIR, d) + file = os.path.join(dir, f) + if f and os.path.exists(file): + os.unlink(file) + if os.path.exists(dir): + os.rmdir(dir) + except OSError: + pass + + # Step 5: make sure default response has no additonal plugins + response = self.send_request(method='GET') + assert_equal(response.status, 200) + response_data = response.read() + assert_equal(response_data, empty_response)
_______________________________________________ FreeIPA-devel mailing list -- freeipa-devel@lists.fedorahosted.org To unsubscribe send an email to freeipa-devel-le...@lists.fedorahosted.org Fedora Code of Conduct: https://docs.fedoraproject.org/en-US/project/code-of-conduct/ List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines List Archives: https://lists.fedorahosted.org/archives/list/freeipa-devel@lists.fedorahosted.org