From 1d419d03121dd211f8ea88b1df2e6add76a0da65 Mon Sep 17 00:00:00 2001 From: Alexander Bokovoy 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. NOTE: cherry-picked from commit 9170669 and adapted to the Python 2 environment by adding absolute_import and treating empty_response as a unicode string. Fixes: https://pagure.io/freeipa/issue/8567 Signed-off-by: Alexander Bokovoy Reviewed-By: Florence Blanc-Renaud --- install/wsgi/plugins.py | 5 +- ipatests/test_ipaserver/test_jsplugins.py | 70 +++++++++++++++++++++++ 2 files changed, 74 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 f80cfb9febae251e64a4c912be47798d59d0933d..4c43e7f87907b6a70aa22b0a9f4b6be205fd71f2 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 0000000000000000000000000000000000000000..d8e755ccb7d9c542223b959c9ab5c02554ae0d8f --- /dev/null +++ b/ipatests/test_ipaserver/test_jsplugins.py @@ -0,0 +1,70 @@ +# Copyright (C) 2020 FreeIPA Contributors see COPYING for license + +from __future__ import absolute_import + +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 = u"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().decode(encoding='utf-8') + 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 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().decode(encoding='utf-8') + assert_not_equal(response_data, empty_response) + for (d, f) in self.jsplugins: + if f: + assert "'" + d + "'" in response_data + else: + assert "'" + d + "'" not 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 additional plugins + response = self.send_request(method='GET') + assert_equal(response.status, 200) + response_data = response.read().decode(encoding='utf-8') + assert_equal(response_data, empty_response) -- 2.26.2