Verified Commit 5ae6d4de authored by Peter Stanko's avatar Peter Stanko
Browse files

Extract credentials from header and cookie; Fixed gitlab namespaces

parent 70581fcb
...@@ -20,12 +20,20 @@ class GitlabFacade(GeneralFacade): ...@@ -20,12 +20,20 @@ class GitlabFacade(GeneralFacade):
@property @property
def user_gl_cred(self): def user_gl_cred(self):
return dict(token=self._get_cookie('token'), return dict(token=self._get_credential('token'),
token_type=self._get_cookie('token_type')) token_type=self._get_credential('token_type'))
def _get_cookie(self, name, prefix='gitlab'): def _get_cookie(self, name, prefix='gitlab'):
return self._request.cookies.get(f'{prefix}_{name}') return self._request.cookies.get(f'{prefix}_{name}')
def _get_header(self, name, prefix='gitlab'):
full_name = f'{prefix}_{name}'.upper()
return self._request.headers.get(full_name)
def _get_credential(self, name, prefix='gitlab'):
return self._get_cookie(name, prefix=prefix) or self._get_header(name, prefix=prefix)
def check_gl_enabled(self): def check_gl_enabled(self):
if not self.is_enabled(): if not self.is_enabled():
raise errors.GitlabIsNotEnabledError() raise errors.GitlabIsNotEnabledError()
......
...@@ -8,6 +8,7 @@ from gitlab.v4 import objects ...@@ -8,6 +8,7 @@ from gitlab.v4 import objects
from portal import logger, oauth from portal import logger, oauth
from portal.rest import rest_api from portal.rest import rest_api
from portal.rest.custom_resource import CustomResource from portal.rest.custom_resource import CustomResource
from portal.service import errors
log = logger.get_logger(__name__) log = logger.get_logger(__name__)
...@@ -79,24 +80,31 @@ class GitlabProjects(CustomResource): ...@@ -79,24 +80,31 @@ class GitlabProjects(CustomResource):
return jsonify([proj.attributes for proj in projects]) return jsonify([proj.attributes for proj in projects])
@gitlab_namespace.route('/projects/<string:pname>') def _get_project_name() -> str:
@gitlab_namespace.param('pname', 'Project name') pname = request.args.get('project')
if not pname:
raise errors.PortalAPIError(code=400, message="Gitlab project name is missing.")
return pname
@gitlab_namespace.route('/project')
class GitlabProjects(CustomResource): class GitlabProjects(CustomResource):
@jwt_required @jwt_required
# @courses_namespace.response(200, 'Course found', model=course_schema) # @courses_namespace.response(200, 'Course found', model=course_schema)
def get(self, pname: str): def get(self):
self.facades.gitlab.check_gl_enabled() self.facades.gitlab.check_gl_enabled()
pname = _get_project_name()
project: objects.Project = self.facades.gitlab.get_project(project_name=pname) project: objects.Project = self.facades.gitlab.get_project(project_name=pname)
return project.attributes return project.attributes
@gitlab_namespace.route('/projects/<string:pname>/members') @gitlab_namespace.route('/project/members')
@gitlab_namespace.param('pname', 'Project name')
class GitlabProjectMembers(CustomResource): class GitlabProjectMembers(CustomResource):
@jwt_required @jwt_required
# @courses_namespace.response(200, 'Course found', model=course_schema) # @courses_namespace.response(200, 'Course found', model=course_schema)
def get(self, pname: str): def get(self):
self.facades.gitlab.check_gl_enabled() self.facades.gitlab.check_gl_enabled()
pname = _get_project_name()
return self.facades.gitlab.list_members(project_name=pname) return self.facades.gitlab.list_members(project_name=pname)
@jwt_required @jwt_required
......
...@@ -93,9 +93,8 @@ class GitlabService(GeneralService): ...@@ -93,9 +93,8 @@ class GitlabService(GeneralService):
def get_project(self, project_name, gl_client=None, **kwargs) -> objects.Project: def get_project(self, project_name, gl_client=None, **kwargs) -> objects.Project:
gl_client = gl_client or self.gl_client gl_client = gl_client or self.gl_client
projects: objects.ProjectManager = gl_client.projects projects: objects.ProjectManager = gl_client.projects
full_project_name = gl_client.user.username + '/' + project_name log.debug(f"[GL_PROJ] Getting gitlab project: {project_name}")
log.debug(f"[GL_PROJ] Getting gitlab project: {full_project_name}") project: objects.Project = projects.get(project_name, **kwargs)
project: objects.Project = projects.get(full_project_name, **kwargs)
return project return project
def tag_submission(self, submission: Submission, **kwargs) -> objects.ProjectTag: def tag_submission(self, submission: Submission, **kwargs) -> objects.ProjectTag:
......
import logging import logging
from urllib.parse import urlencode
import gitlab import gitlab
import pytest import pytest
...@@ -192,13 +193,13 @@ def test_list_gitlab_projects(client, gl_api_url, data_gl_projects, gl_cookies, ...@@ -192,13 +193,13 @@ def test_list_gitlab_projects(client, gl_api_url, data_gl_projects, gl_cookies,
@responses.activate @responses.activate
def test_get_gitlab_project(client, gl_api_url, data_gl_projects, gl_cookies, gl_user): def test_get_gitlab_project(client, gl_api_url, data_gl_projects, gl_cookies, gl_user):
project = data_gl_projects[0] project = data_gl_projects[0]
pname = project['name'] pname: str = project['path_with_namespace']
namespace = f"{gl_user['username']}%2F{pname}" namespace = pname.replace('/', '%2F')
responses.add(responses.GET, f"{gl_api_url}/user", json=gl_user, status=200) responses.add(responses.GET, f"{gl_api_url}/user", json=gl_user, status=200)
responses.add(responses.GET, f"{gl_api_url}/projects/{namespace}", responses.add(responses.GET, f"{gl_api_url}/projects/{namespace}",
json=project, status=200) json=project, status=200)
# 'https://gitlab.local/api/v4/user' # 'https://gitlab.local/api/v4/user'
response = rest_tools.make_request(client, f'/gitlab/projects/{pname}', response = rest_tools.make_request(client, f'/gitlab/project?project={pname}',
cookies=gl_cookies) cookies=gl_cookies)
assert_response(response) assert_response(response)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment