From 80d1db22bd74bc98ff63f49bd8a9a11042fe39ac Mon Sep 17 00:00:00 2001 From: Simaris Date: Tue, 16 Apr 2019 18:59:23 +0200 Subject: [PATCH 1/4] 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() From 5dc3e45c350bb5c21ba03825e7a2ac7a0fb063fe Mon Sep 17 00:00:00 2001 From: username Date: Mon, 29 Apr 2019 22:40:06 +0200 Subject: [PATCH 2/4] added failing test for quit.web.modules.git --- tests/test_app.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tests/test_app.py b/tests/test_app.py index 5a416cdc..93f88418 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -5,6 +5,7 @@ from urllib.parse import quote_plus from datetime import datetime from pygit2 import GIT_SORT_TOPOLOGICAL, Signature +from pygit2 import config as gitconfig import quit.application as quitApp from quit.web.app import create_app import unittest @@ -3449,6 +3450,67 @@ def testWithOnInsertUsing(self): with open(path.join(repo.workdir, 'graph_1.nt'), 'r') as f: self.assertEqual('\n', f.read()) + def testMergeFailureByUnconfiguredAuthorName(self): + """Test merging two commits without a set git config user.name """ + # Prepate a git Repository + content = " ." + with TemporaryRepositoryFactory().withGraph("http://example.org/", content) as repo: + gitGlobalConfig = gitconfig.Config.get_global_config() + username = gitGlobalConfig.__getitem__("user.name") + email = gitGlobalConfig.__getitem__("user.email") + # Start Quit + args = quitApp.parseArgs(['-t', repo.workdir]) + objects = quitApp.initialize(args) + config = objects['config'] + app = create_app(config).test_client() + + app.post("/branch", data={"oldbranch": "master", "newbranch": "develop"}) + app.post("/branch", data={"oldbranch": "master", "newbranch": "target"}) + + # execute INSERT DATA query + update = "INSERT DATA {graph { .}}" + app.post('/sparql/target', data={"query": update}) + + app = create_app(config).test_client() + # start new app to syncAll() + + update = "INSERT DATA {graph { .}}" + app.post('/sparql/develop', data={"query": update}) + try: + gitGlobalConfig.__delitem__("user.name") + # execute INSERT DATA query + # execute merge + app = create_app(config).test_client() + response = app.post("/merge", data={"target": "target", "branch": "develop", "method": "context"}) + 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_xdg_config() + gitGlobalConfig.__delitem__("user.email") + response = app.post("/merge", data={"target": "master", "branch": "develop", "method": "context"}) + 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 + class FileHandlingTests(unittest.TestCase): def testNewNamedGraph(self): From d382574b197ae087d57756d31531a1184999ba60 Mon Sep 17 00:00:00 2001 From: Natanael Arndt Date: Tue, 30 Apr 2019 17:06:34 +0200 Subject: [PATCH 3/4] Use dictionary operators and use the same config object all the time --- tests/test_endpoint.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/tests/test_endpoint.py b/tests/test_endpoint.py index debb86c9..8db375e9 100644 --- a/tests/test_endpoint.py +++ b/tests/test_endpoint.py @@ -423,10 +423,10 @@ def testInsertDataWithNoGitConfigUser(self): 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") + username = gitGlobalConfig["user.name"] + email = gitGlobalConfig["user.email"] try: - gitconfig.Config.get_global_config().__delitem__("user.name") + del gitGlobalConfig["user.name"] # execute INSERT DATA query update = """INSERT DATA { GRAPH { @@ -446,13 +446,11 @@ def testInsertDataWithNoGitConfigUser(self): # "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") + gitGlobalConfig["user.name"] = username + del gitGlobalConfig["user.email"] response = app.post('/sparql', data=dict(update=update)) - gitGlobalConfig.__setitem__("user.name", username) - gitGlobalConfig.__setitem__("user.email", email) + gitGlobalConfig["user.name"] = username + gitGlobalConfig["user.email"] = email message = response.get_data().decode("utf-8") self.assertEqual( message, @@ -460,8 +458,8 @@ def testInsertDataWithNoGitConfigUser(self): "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) + gitGlobalConfig["user.name"] = username + gitGlobalConfig["user.email"] = email raise e if __name__ == '__main__': From 7fa6babd1b7411b2c1020965e9a3747d1a37981e Mon Sep 17 00:00:00 2001 From: Natanael Arndt Date: Tue, 30 Apr 2019 18:54:12 +0200 Subject: [PATCH 4/4] Set update parameter properly and use default merge method to not produce conflicts --- tests/test_app.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_app.py b/tests/test_app.py index 93f88418..445c7dd6 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -3469,19 +3469,19 @@ def testMergeFailureByUnconfiguredAuthorName(self): # execute INSERT DATA query update = "INSERT DATA {graph { .}}" - app.post('/sparql/target', data={"query": update}) + app.post('/sparql/target', data={"update": update}) app = create_app(config).test_client() # start new app to syncAll() update = "INSERT DATA {graph { .}}" - app.post('/sparql/develop', data={"query": update}) + app.post('/sparql/develop', data={"update": update}) try: gitGlobalConfig.__delitem__("user.name") # execute INSERT DATA query # execute merge app = create_app(config).test_client() - response = app.post("/merge", data={"target": "target", "branch": "develop", "method": "context"}) + response = app.post("/merge", data={"target": "target", "branch": "develop"}) message = response.get_data().decode("utf-8") self.assertEqual( message,