From 9a855645e465c6b06b49ba74bce12ef774b597e4 Mon Sep 17 00:00:00 2001 From: Loic Dachary Date: Thu, 28 Jan 2021 21:07:07 +0100 Subject: [PATCH] gitlab: create/get/delete user --- fedeproxy/common/gitlab.py | 35 +++++++++++++++++++++++++++ tests/fedeproxy/common/test_gitlab.py | 16 ++++++++++++ 2 files changed, 51 insertions(+) diff --git a/fedeproxy/common/gitlab.py b/fedeproxy/common/gitlab.py index 607de88..64ff629 100644 --- a/fedeproxy/common/gitlab.py +++ b/fedeproxy/common/gitlab.py @@ -59,6 +59,41 @@ class GitLab(object): user = r.json() return self.is_member_of_group(group, user['username']) + def user_delete(self, user): + info = self.user_get(user) + if info is None: + return False + while True: + r = self.s.delete(f'{self.s.api}/users/{info["id"]}') + if r.status_code == 404: + break + r.raise_for_status() + return True + + def user_get(self, user): + r = self.s.get(f'{self.s.api}/users?username={user}') + r.raise_for_status() + found = r.json() + if found: + return found[0] + else: + return None + + def user_create(self, user, email): + info = self.user_get(user) + if info is None: + r = self.s.post(f'{self.s.api}/users', data={ + "name": user, + "username": user, + "email": email, + "password": "something", + "force_random_password": True, + }) + logger.debug(r.text) + r.raise_for_status() + info = r.json() + return info + def project_delete(self, namespace, project): info = self.project_get(namespace, project) if info is None: diff --git a/tests/fedeproxy/common/test_gitlab.py b/tests/fedeproxy/common/test_gitlab.py index bf3d70a..f1d575c 100644 --- a/tests/fedeproxy/common/test_gitlab.py +++ b/tests/fedeproxy/common/test_gitlab.py @@ -14,6 +14,22 @@ def test_project_create(): assert gitlab.project_get('root', 'testproject') is None p = gitlab.project_create('root', 'testproject') assert p['id'] == gitlab.project_create('root', 'testproject')['id'] + assert gitlab.project_delete('root', 'testproject') is True + assert gitlab.project_delete('root', 'testproject') is False + + +@pytest.mark.gitlab +def test_user_create(): + ip = os.environ.get('FEDEPROXY_IP', '0.0.0.0') + gitlab = GitLab(f'http://{ip}:8181') + gitlab.login('root', 'Wrobyak4') + user = 'testuser1' + email = 'testuser1@example.com' + gitlab.user_delete(user) + u = gitlab.user_create(user, email) + assert u['id'] == gitlab.user_create(user, email)['id'] + assert gitlab.user_delete(user) is True + assert gitlab.user_delete(user) is False @pytest.mark.gitlab