Commit aaae1d88 authored by Loïc Dachary's avatar Loïc Dachary
Browse files

Merge branch 'wip-authenticate' into 'master'

forge: create user with a password

See merge request fedeproxy/fedeproxy!11
parents 809dec6d 4ceaf8ce
Pipeline #23779 failed with stage
......@@ -36,37 +36,37 @@ class Gitea(Forge):
def set_token(self, token):
self.s.headers["Authorization"] = f"token {token}"
def user_delete(self, user):
info = self.user_get(user)
def user_delete(self, username):
info = self.user_get(username)
if info is None:
return False
while True:
r = self.s.delete(f"{self.s.api}/admin/users/{user}")
r = self.s.delete(f"{self.s.api}/admin/users/{username}")
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/search?q={user}")
def user_get(self, username):
r = self.s.get(f"{self.s.api}/users/search?q={username}")
r.raise_for_status()
found = r.json()
if found and found["ok"] and len(found["data"]):
info = found["data"][0]
info["username"] = user
info["username"] = username
return info
else:
return None
def user_create(self, user, email):
info = self.user_get(user)
def user_create(self, username, password, email):
info = self.user_get(username)
if info is None:
r = self.s.post(
f"{self.s.api}/admin/users",
data={
"username": user,
"username": username,
"email": email,
"password": "something",
"password": password,
},
)
logger.debug(r.text)
......@@ -74,18 +74,18 @@ class Gitea(Forge):
info = r.json()
return info
def project_delete(self, user, namespace, project):
info = self.project_get(user, namespace, project)
def project_delete(self, username, namespace, project):
info = self.project_get(username, namespace, project)
if info is None:
return False
r = self.s.delete(f"{self.s.api}/repos/{user}/{project}")
r = self.s.delete(f"{self.s.api}/repos/{username}/{project}")
r.raise_for_status()
while self.project_get(user, namespace, project) is not None:
while self.project_get(username, namespace, project) is not None:
time.sleep(1)
return True
def project_get(self, user, namespace, project):
r = self.s.get(f"{self.s.api}/repos/{user}/{project}")
def project_get(self, username, namespace, project):
r = self.s.get(f"{self.s.api}/repos/{username}/{project}")
if r.status_code == requests.codes.ok:
return r.json()
else:
......@@ -95,21 +95,21 @@ class Gitea(Forge):
pass
@retry(DeletionInProgress, tries=5)
def _project_create(self, admin, user, namespace, project, **data):
def _project_create(self, admin, username, namespace, project, **data):
data.update(
{
"name": project,
}
)
r = self.s.post(f"{self.s.api}/admin/users/{user}/repos", data=data)
r = self.s.post(f"{self.s.api}/admin/users/{username}/repos", data=data)
logger.info(r.text)
if r.status_code == 201:
return r.json()
r.raise_for_status()
def project_create(self, admin, user, namespace, project, **data):
info = self.project_get(user, namespace, project)
def project_create(self, admin, username, namespace, project, **data):
info = self.project_get(username, namespace, project)
if info is None:
return self._project_create(admin, user, namespace, project, **data)
return self._project_create(admin, username, namespace, project, **data)
else:
return info
......@@ -94,8 +94,8 @@ class GitLab(Forge):
r.raise_for_status()
return r.json()
def user_delete(self, user):
info = self.user_get(user)
def user_delete(self, username):
info = self.user_get(username)
if info is None:
return False
while True:
......@@ -105,8 +105,8 @@ class GitLab(Forge):
r.raise_for_status()
return True
def user_get(self, user):
r = self.s.get(f"{self.s.api}/users?username={user}")
def user_get(self, username):
r = self.s.get(f"{self.s.api}/users?username={username}")
r.raise_for_status()
found = r.json()
if found:
......@@ -114,16 +114,16 @@ class GitLab(Forge):
else:
return None
def user_create(self, user, email):
info = self.user_get(user)
def user_create(self, username, password, email):
info = self.user_get(username)
if info is None:
r = self.s.post(
f"{self.s.api}/users",
data={
"name": user,
"username": user,
"name": username,
"username": username,
"email": email,
"password": "something",
"password": password,
"force_random_password": True,
},
)
......@@ -132,17 +132,17 @@ class GitLab(Forge):
info = r.json()
return info
def project_delete(self, user, namespace, project):
info = self.project_get(user, namespace, project)
def project_delete(self, username, namespace, project):
info = self.project_get(username, namespace, project)
if info is None:
return False
r = self.s.delete(f'{self.s.api}/projects/{info["id"]}')
r.raise_for_status()
while self.project_get(user, namespace, project) is not None:
while self.project_get(username, namespace, project) is not None:
time.sleep(1)
return True
def project_get(self, user, namespace, project):
def project_get(self, username, namespace, project):
r = self.s.get(f"{self.s.api}/projects/{namespace}%2F{project}")
if r.status_code == requests.codes.ok:
return r.json()
......@@ -153,8 +153,8 @@ class GitLab(Forge):
pass
@retry(DeletionInProgress, tries=5)
def _project_create(self, admin, user, namespace, project, **data):
user_id = self.user_get(user)["id"]
def _project_create(self, admin, username, namespace, project, **data):
user_id = self.user_get(username)["id"]
admin_id = self.user_get(admin)["id"]
namespace_id = self.get_namespace_id(namespace)
data.update(
......@@ -173,14 +173,14 @@ class GitLab(Forge):
raise GitLab.DeletionInProgress()
r.raise_for_status()
def project_create(self, admin, user, namespace, project, **data):
info = self.project_get(user, namespace, project)
def project_create(self, admin, username, namespace, project, **data):
info = self.project_get(username, namespace, project)
if info is None:
return self._project_create(admin, user, namespace, project, **data)
return self._project_create(admin, username, namespace, project, **data)
else:
return info
def project_export(self, user, namespace, project, filename):
def project_export(self, username, namespace, project, filename):
url = f"{self.s.api}/projects/{namespace}%2F{project}/export"
r = self.s.post(url)
assert r.status_code == 202, f"{r.status_code} {r.text}"
......
def verify_project_create(forge):
forge.authenticate(username="root", password="Wrobyak4")
user = "testuser1"
username = "testuser1"
email = "testuser1@example.com"
forge.user_create(user, email)
forge.project_delete(user, user, "testproject")
assert forge.project_get(user, user, "testproject") is None
p = forge.project_create("root", user, user, "testproject")
assert p["id"] == forge.project_create("root", user, user, "testproject")["id"]
assert forge.project_delete(user, user, "testproject") is True
assert forge.project_delete(user, user, "testproject") is False
assert forge.user_delete(user) is True
password = "Wrobyak4"
forge.user_create(username, password, email)
forge.project_delete(username, username, "testproject")
assert forge.project_get(username, username, "testproject") is None
p = forge.project_create("root", username, username, "testproject")
assert p["id"] == forge.project_create("root", username, username, "testproject")["id"]
assert forge.project_delete(username, username, "testproject") is True
assert forge.project_delete(username, username, "testproject") is False
assert forge.user_delete(username) is True
def verify_user_create(forge):
forge.authenticate(username="root", password="Wrobyak4")
user = "testuser1"
username = "testuser1"
email = "testuser1@example.com"
forge.user_delete(user)
u = forge.user_create(user, email)
assert u["id"] == forge.user_create(user, email)["id"]
assert forge.user_delete(user) is True
assert forge.user_get(user) is None
assert forge.user_delete(user) is False
password = "Wrobyak4"
forge.user_delete(username)
u = forge.user_create(username, password, email)
assert u["id"] == forge.user_create(username, password, email)["id"]
assert forge.user_delete(username) is True
assert forge.user_get(username) is None
assert forge.user_delete(username) is False
......@@ -7,25 +7,25 @@ class Forge(abc.ABC):
...
@abc.abstractmethod
def user_get(self, user):
def user_get(self, username):
...
@abc.abstractmethod
def user_create(self, user):
def user_create(self, username, password, email):
...
@abc.abstractmethod
def user_delete(self, user):
def user_delete(self, username):
...
@abc.abstractmethod
def project_get(self, user, namespace, project):
def project_get(self, username, namespace, project):
...
@abc.abstractmethod
def project_create(self, admin, user, namespace, project, **data):
def project_create(self, admin, username, namespace, project, **data):
...
@abc.abstractmethod
def project_delete(self, user, namespace, project):
def project_delete(self, username, namespace, project):
...
Markdown is supported
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