From bc44699d85ae2ad6e3486dedf3e9c39757b8c82b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Tulach?= Date: Wed, 10 Jul 2024 09:11:22 +0200 Subject: [PATCH 1/4] Tests for project-settings --- tests/mocks_gitlab_api.py | 13 ++++++++++++- tests/test_settings.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/test_settings.py diff --git a/tests/mocks_gitlab_api.py b/tests/mocks_gitlab_api.py index 1be3ca9..dccdc65 100644 --- a/tests/mocks_gitlab_api.py +++ b/tests/mocks_gitlab_api.py @@ -52,7 +52,18 @@ def register_project(self, numerical_id, full_project_path): 'projects/' + self.escape_path_in_url(full_project_path), response_json={ 'id': numerical_id, - 'path_with_namespace': full_project_path + 'path_with_namespace': full_project_path, + }, + helper=True, + ) + + def register_project_with_mr(self, numerical_id, full_project_path): + self.on_api_get( + 'projects/' + self.escape_path_in_url(full_project_path), + response_json={ + 'id': numerical_id, + 'path_with_namespace': full_project_path, + 'mr_default_target_self' : 'self' }, helper=True, ) diff --git a/tests/test_settings.py b/tests/test_settings.py new file mode 100644 index 0000000..f6a0bf3 --- /dev/null +++ b/tests/test_settings.py @@ -0,0 +1,38 @@ +import logging + +import teachers_gitlab.main as tg +import gitlab + +def test_project_settings(mock_gitlab): + entries = [ + {'login': 'alpha'}, + ] + + mock_gitlab.register_project_with_mr(42, 'student/alpha') + + tg.action_project_settings( + mock_gitlab.get_python_gitlab(), + logging.getLogger("settings"), + tg.ActionEntries(entries), + True, + 'student/{login}', + 'self', + 'The best project' + ) + +def test_project_settings_with_none(mock_gitlab): + entries = [ + {'login': 'beta'}, + ] + + mock_gitlab.register_project_with_mr(38, 'student/beta') + + tg.action_project_settings( + mock_gitlab.get_python_gitlab(), + logging.getLogger("settings"), + tg.ActionEntries(entries), + True, + 'student/{login}', + None, + 'The best project' + ) From c86f6527a2657a14b8b184b604786ee1ad051745 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Tulach?= Date: Thu, 11 Jul 2024 09:36:29 +0200 Subject: [PATCH 2/4] Tests: Populate response JSON from kwargs --- tests/mocks_gitlab_api.py | 19 ++++--------------- tests/test_settings.py | 4 ++-- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/tests/mocks_gitlab_api.py b/tests/mocks_gitlab_api.py index dccdc65..2d33c00 100644 --- a/tests/mocks_gitlab_api.py +++ b/tests/mocks_gitlab_api.py @@ -47,28 +47,17 @@ def escape_path_in_url(self, path_with_namespace): def get_python_gitlab(self): return gitlab.Gitlab(self.base_url, oauth_token="mock_token") - def register_project(self, numerical_id, full_project_path): - self.on_api_get( - 'projects/' + self.escape_path_in_url(full_project_path), - response_json={ + def register_project(self, numerical_id, full_project_path, **kwargs): + response_json_base = { 'id': numerical_id, 'path_with_namespace': full_project_path, - }, - helper=True, - ) - - def register_project_with_mr(self, numerical_id, full_project_path): + } self.on_api_get( 'projects/' + self.escape_path_in_url(full_project_path), - response_json={ - 'id': numerical_id, - 'path_with_namespace': full_project_path, - 'mr_default_target_self' : 'self' - }, + response_json = response_json_base | kwargs, helper=True, ) - def on_api_get(self, url, response_json=None, response_404=False, helper=False, *args, **kwargs): full_url = self.make_api_url_(url) diff --git a/tests/test_settings.py b/tests/test_settings.py index f6a0bf3..4f00904 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -8,7 +8,7 @@ def test_project_settings(mock_gitlab): {'login': 'alpha'}, ] - mock_gitlab.register_project_with_mr(42, 'student/alpha') + mock_gitlab.register_project(42, 'student/alpha', mr_default_target_self='self') tg.action_project_settings( mock_gitlab.get_python_gitlab(), @@ -25,7 +25,7 @@ def test_project_settings_with_none(mock_gitlab): {'login': 'beta'}, ] - mock_gitlab.register_project_with_mr(38, 'student/beta') + mock_gitlab.register_project(38, 'student/beta', mr_default_target_self='self') tg.action_project_settings( mock_gitlab.get_python_gitlab(), From b38ed752f0d03c3b1a4e385f849ac035d1be49f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Tulach?= Date: Thu, 1 Aug 2024 10:09:15 +0200 Subject: [PATCH 3/4] Tests: capture PUT calls --- tests/mocks_gitlab_api.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/mocks_gitlab_api.py b/tests/mocks_gitlab_api.py index 2d33c00..7716eb4 100644 --- a/tests/mocks_gitlab_api.py +++ b/tests/mocks_gitlab_api.py @@ -17,12 +17,12 @@ def __init__(self, rsps): def report_unknown(self): def dumping_callback(req): - log_line = f"{req.method} {req.url}" + log_line = f"{req.method} {req.url} {req.body}" logging.getLogger('DUMPER').error("URL not mocked: %s", log_line) self.unknown_urls.append(log_line) return (404, {}, json.dumps({"error": "not implemented"})) - methods = [responses.GET, responses.POST, responses.DELETE] + methods = [responses.GET, responses.POST, responses.DELETE, responses.PUT] for m in methods: self.responses.add_callback( m, @@ -97,3 +97,16 @@ def on_api_delete(self, url, *args, **kwargs): *args, **kwargs, ) + + def on_api_put(self, url, request_json, response_json, *args, **kwargs): + kwargs['body'] = json.dumps(response_json) + kwargs['match'] = [ + responses.matchers.json_params_matcher(request_json) + ] + kwargs['content_type'] = 'application/json' + + return self.responses.put( + self.make_api_url_(url), + *args, + **kwargs, + ) From 8094157ab8bb9245e24907e869bdf3b30b50a743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Tulach?= Date: Thu, 1 Aug 2024 10:13:04 +0200 Subject: [PATCH 4/4] Tests: project-settings --- tests/test_settings.py | 61 +++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/tests/test_settings.py b/tests/test_settings.py index 4f00904..cc32ce8 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -1,37 +1,80 @@ import logging import teachers_gitlab.main as tg -import gitlab -def test_project_settings(mock_gitlab): + +def test_project_settings_changing_everything(mock_gitlab): entries = [ - {'login': 'alpha'}, + { + 'login': 'alpha', + 'name': 'Alpha Able' + }, ] mock_gitlab.register_project(42, 'student/alpha', mr_default_target_self='self') + mock_gitlab.on_api_put( + 'projects/42', + request_json= { + 'mr_default_target_self': True, + }, + response_json={ + 'id': 42, + 'path_with_namespace': 'student/alpha', + } + ) + + mock_gitlab.on_api_put( + 'projects/42', + request_json= { + 'description': 'Semestral project for Alpha Able', + }, + response_json={ + 'id': 42, + 'path_with_namespace': 'student/alpha', + } + ) + + mock_gitlab.report_unknown() + tg.action_project_settings( mock_gitlab.get_python_gitlab(), - logging.getLogger("settings"), + logging.getLogger('settings'), tg.ActionEntries(entries), - True, + False, 'student/{login}', 'self', - 'The best project' + 'Semestral project for {name}' ) -def test_project_settings_with_none(mock_gitlab): + + +def test_project_settings_changing_only_name(mock_gitlab): entries = [ {'login': 'beta'}, ] mock_gitlab.register_project(38, 'student/beta', mr_default_target_self='self') + mock_gitlab.on_api_put( + 'projects/38', + request_json= { + 'description': 'The best project', + }, + response_json={ + 'id': 38, + 'path_with_namespace': 'student/beta', + } + ) + + mock_gitlab.report_unknown() + + tg.action_project_settings( mock_gitlab.get_python_gitlab(), - logging.getLogger("settings"), + logging.getLogger('settings'), tg.ActionEntries(entries), - True, + False, 'student/{login}', None, 'The best project'