diff --git a/gitea/gitea.py b/gitea/gitea.py index b6d838c..1721c9a 100644 --- a/gitea/gitea.py +++ b/gitea/gitea.py @@ -165,6 +165,12 @@ class User(GiteaApiObject): results = self.gitea.requests_get(url) return [Organization.parse_response(self.gitea, result) for result in results] + def get_teams(self) -> List['Team']: + """ Get all Organizations this user is a member of.""" + url = f"/user/teams" + results = self.gitea.requests_get(url, sudo=self) + return [Team.parse_response(self.gitea, result) for result in results] + def __request_emails(self): result = self.gitea.requests_get(User.USER_MAIL % self.login) # report if the adress changed by this @@ -635,7 +641,9 @@ class Gitea: return json.loads(result.text) return {} - def requests_get(self, endpoint, params={}, requests=None): + def requests_get(self, endpoint, params={}, requests=None, sudo=None): + if sudo: + params["sudo"] = sudo.username if not requests: request = self.requests.get( self.__get_url(endpoint), headers=self.headers, params=params diff --git a/tests/test_api.py b/tests/test_api.py index ea0a67e..e9ac124 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -54,7 +54,7 @@ def test_fail_get_non_existent_repo(instance): def test_create_user(instance): email = test_user + "@example.org" - user = instance.create_user(test_user, email, "abcdefg1.23AB") + user = instance.create_user(test_user, email, "abcdefg1.23AB", send_notify=False) assert user.username == test_user assert user.login == test_user assert email in user.emails @@ -147,6 +147,13 @@ def test_create_team(instance): assert team.description == "descr" assert team.organization == org +def test_user_teams(instance): + org = Organization.request(instance, test_org) + team = org.get_team(test_team) + user = instance.get_user_by_name(test_user) + team.add_user(user) + teams = user.get_teams() + assert team in teams def test_create_issue(instance): org = Organization.request(instance, test_org)