From 80d1db22bd74bc98ff63f49bd8a9a11042fe39ac Mon Sep 17 00:00:00 2001 From: Simaris Date: Tue, 16 Apr 2019 18:59:23 +0200 Subject: [PATCH] more verbose message for missing configs on update-queries --- quit/web/modules/endpoint.py | 17 ++++++++++++ quit/web/modules/git.py | 16 ++++++++++- tests/test_endpoint.py | 51 ++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/quit/web/modules/endpoint.py b/quit/web/modules/endpoint.py index 58b8ccd8..2f5b14de 100644 --- a/quit/web/modules/endpoint.py +++ b/quit/web/modules/endpoint.py @@ -160,6 +160,23 @@ def sparql(branch_or_ref): else: response.headers["X-CurrentCommit"] = commitid return response + except KeyError as e: + logger.exception(e) + print(e.args) + if "config value 'user.name' was not found" in e.args: + message = ("Unable to process query: " + "git config value 'user.name' was not found.\n" + "Please use the following command in your data repo:" + "\n\n git config user.name ") + return make_response(message, 400) + if "config value 'user.email' was not found" in e.args: + message = ("Unable to process query: " + "git config value 'user.email' was not found.\n" + "Please use the following command in your data repo:" + "\n\n git config user.email ") + return make_response(message, 400) + # KeyError has many sources -> it could be caused by other problems + return make_response('Error after executing the update query.', 400) except Exception as e: # query ok, but unsupported query type or other problem during commit logger.exception(e) diff --git a/quit/web/modules/git.py b/quit/web/modules/git.py index b59928ee..62c56648 100644 --- a/quit/web/modules/git.py +++ b/quit/web/modules/git.py @@ -258,7 +258,21 @@ def merge(refspec): return response else: return "
Unsupported Mimetype: {}
".format(mimetype), 406 - + except KeyError as e: + message = "\n" + if "config value 'user.name' was not found" in e.args: + message += ("Unable to process query: " + "git config value 'user.name' was not found.\n" + "Please use the following command in your data repo:" + "\n\n git config user.name ") + if "config value 'user.email' was not found" in e.args: + message += ("Unable to process query: " + "git config value 'user.email' was not found.\n" + "Please use the following command in your data repo:" + "\n\n git config user.email ") + logger.error(e) + logger.error(traceback.format_exc()) + return "
" + traceback.format_exc() + message + "
", 400 except Exception as e: logger.error(e) logger.error(traceback.format_exc()) diff --git a/tests/test_endpoint.py b/tests/test_endpoint.py index 16b19d44..debb86c9 100644 --- a/tests/test_endpoint.py +++ b/tests/test_endpoint.py @@ -5,6 +5,7 @@ import quit.application as quitApp from quit.web.app import create_app from tempfile import TemporaryDirectory +from pygit2 import config as gitconfig import json class EndpointTests(unittest.TestCase): @@ -28,6 +29,7 @@ def testInsertDataNoSnapshotIsolation(self): config = objects['config'] app = create_app(config).test_client() + # execute INSERT DATA query update = """INSERT DATA { GRAPH { @@ -412,6 +414,55 @@ def testInsertDataOverlappingWithMerge(self): "p": {'type': 'uri', 'value': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'}, "o": {'type': 'uri', 'value': 'http://ex.org/Todo'}}) + def testInsertDataWithNoGitConfigUser(self): + with TemporaryDirectory() as repo: + + # Start Quit + args = quitApp.parseArgs(['-t', repo]) + objects = quitApp.initialize(args) + config = objects['config'] + app = create_app(config).test_client() + gitGlobalConfig = gitconfig.Config.get_global_config() + username = gitGlobalConfig.__getitem__("user.name") + email = gitGlobalConfig.__getitem__("user.email") + try: + gitconfig.Config.get_global_config().__delitem__("user.name") + # execute INSERT DATA query + update = """INSERT DATA { + GRAPH { + a ; + "Take out the organic waste" . + }} + """ + response = app.post('/sparql', data=dict(update=update)) + message = response.get_data().decode("utf-8") + self.assertEqual( + message, + ("Unable to process query: git config value 'user.name' " + "was not found.\nPlease use the following command in your" + " data repo:\n\n git config user.name ")) + # Uncommenting the second line below and commenting the first + # causes the test to fail with an KeyError. + # "username" and username are both str s + # Even more, the same code line still correctly reset user.name + # and user.email + gitGlobalConfig.__setitem__("user.name", "username") + # gitGlob alConfig.__setitem__("user.name", username) + gitGlobalConfig = gitconfig.Config.get_global_config() + gitGlobalConfig.__delitem__("user.email") + response = app.post('/sparql', data=dict(update=update)) + gitGlobalConfig.__setitem__("user.name", username) + gitGlobalConfig.__setitem__("user.email", email) + message = response.get_data().decode("utf-8") + self.assertEqual( + message, + ("Unable to process query: git config value 'user.email' " + "was not found.\nPlease use the following command in your" + " data repo:\n\n git config user.email ")) + except Exception as e: + gitGlobalConfig.__setitem__("user.name", username) + gitGlobalConfig.__setitem__("user.email", email) + raise e if __name__ == '__main__': unittest.main()