Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing project settings #4

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions tests/mocks_gitlab_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -47,17 +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):
def register_project(self, numerical_id, full_project_path, **kwargs):
response_json_base = {
'id': numerical_id,
'path_with_namespace': 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
},
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)

Expand Down Expand Up @@ -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,
)
81 changes: 81 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import logging

import teachers_gitlab.main as tg


def test_project_settings_changing_everything(mock_gitlab):
entries = [
{
'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'),
tg.ActionEntries(entries),
False,
'student/{login}',
'self',
'Semestral project for {name}'
)



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'),
tg.ActionEntries(entries),
False,
'student/{login}',
None,
'The best project'
)