now added (working) tests

pull/1/head
fkarg 2019-04-17 14:44:51 +02:00
rodzic 11bed416c3
commit 2c82cd2983
3 zmienionych plików z 144 dodań i 45 usunięć

Wyświetl plik

@ -1,6 +1,9 @@
import os
from gitea.gitea import *
from gitea import Gitea, User, Organization, Team, Repository, version
from gitea import NotFoundException, AlreadyExistsException
assert version >= '0.4.0'
# Testing a localhost instance for API-functionality.
@ -82,6 +85,7 @@ def test_before_team():
def test_create_user():
user = gitea.create_user('test-user', 'testmail@example.org', 'pw1234')
user.update_mail()
assert user.username == 'test-user'
assert user.login == 'test-user'
assert user.email == 'testmail@example.org'
@ -89,7 +93,9 @@ def test_create_user():
def test_create_org():
org = gitea.create_org(gitea.get_user(), 'test-org', 'some-desc', 'loc')
user = gitea.get_user()
org = gitea.create_org(user, 'test-org', 'some-desc', 'loc')
assert org.get_members() == [user]
assert org.description == 'some-desc'
assert org.username == 'test-org'
assert org.location == 'loc'
@ -98,47 +104,66 @@ def test_create_org():
def test_create_repo():
pass
org = Organization(gitea, 'test-org')
repo = gitea.create_repo(org, 'test-repo', 'descr')
assert repo.description == 'descr'
assert repo.owner == org
assert repo.name == 'test-repo'
assert not repo.private
def test_create_team():
pass
org = Organization(gitea, 'test-org')
team = gitea.create_team(org, 'test-team', 'descr')
assert team.name == 'test-team'
assert team.description == 'descr'
assert team.organization == org
def test_full():
user = User(gitea, 'test-user')
user.update_mail()
org = Organization(gitea, 'test-org')
team = Team(org, 'test-team')
assert team.get_members() == []
team.add(user)
assert team.get_members() == [user]
repo = Repository(gitea, org, 'test-repo')
assert team.get_repos() == []
team.add(repo)
assert team.get_repos() == [repo]
def test_delete_repo():
org = Organization(gitea, 'test-org')
repo = Repository(gitea, org, 'test-repo')
repo.delete()
assert expect_not_exist(
lambda: Repository(gitea, User(gitea, "test-user"), "test-repo"),
(NotFoundException),
), "Repository test-repo should not exist"
def test_delete_team():
org = Organization(gitea, 'test-org')
team = Team(org, 'test-team')
team.delete()
assert expect_not_exist(
lambda: Team(org, "test-team"),
(NotFoundException),
), "Team test-team should not exist"
def test_delete_org():
org = Organization(gitea, 'test-org')
org.delete()
assert expect_not_exist(
lambda: Organization(gitea, "test-org"), (NotFoundException)
), "Organization test-org should not exist"
def test_delete_user():
user = User(gitea, 'test-user')
user.delete()
assert expect_not_exist(
lambda: User(gitea, "test-user"), (NotFoundException)
), "User test-user should not exist"
#
# print(org.username)
# org.set_value({"location": "a-place"})
# print([user.username for user in org.get_members()])
# ## try getting an organization that does not exists
# try:
# org = Organization(gitea, "non-existent-org")
# except:
# pass
#
# ##get a User that does exist
# user = User(gitea, "a-user")
# print(user.username)
# ##try getting organization that does not exist
# try:
# user = User(gitea, "non-existent-user")
# except:
# pass
# user.set_value(user.email, {"location": "an-location"})
# ##get repositories set under a organization
# repos = org.get_repositories()
# print("org " + org.username + " has repositories " + str(repos))
# ##get repositories of a user
# userRepos = user.get_repositories()
# print("user " + user.username + " has repositories " + str(repos))
# ##get branches
# repo = Repository(gitea, org,"playground")
# print([b.name for b in repo.get_branches()])
#
# ##create Repository
# gitea.create_repo(user , "test-repo-api", "this is an api test repo, delete this", True, True)
# gitea.create_repo(org , "test-repo-api-org", "this is an api test repo, delete this", True, True)
#
# ## create user
# newUser = gitea.create_user("test-test", "test@testing.de","Torben der Tester", str(os.urandom(32)), False)
# print(newUser.username)
# newUser.delete()

Wyświetl plik

@ -1 +1 @@
from .gitea import Gitea, User, Organization, Repository, Branch
from .gitea import Gitea, User, Organization, Team, Repository, Branch, version, NotFoundException, AlreadyExistsException

Wyświetl plik

@ -3,6 +3,7 @@ import requests
import logging
logging = logging.getLogger("gitea")
version = '0.4.0'
class AlreadyExistsException(Exception):
@ -66,6 +67,12 @@ class Organization:
"""
return "Organization: %s (%s)" % (self.username, self.id)
def __eq__(self, other):
if not other is None:
if isinstance(other, Organization):
return other.id == self.id
return False
def get_repositories(self):
""" Get the Repositories of this Organization.
@ -206,6 +213,12 @@ class User:
for i, v in result.items():
setattr(self, i, v)
def __eq__(self, other):
if other is not None:
if isinstance(other, User):
return other.id == self.id
return False
def update_mail(self):
""" Update the mail of this user instance to one that is \
actually correct.
@ -265,6 +278,7 @@ class Repository:
REPO_SEARCH = """/repos/search/%s""" # <reponame>
REPO_BRANCHES = """/repos/%s/%s/branches""" # <owner>, <reponame>
REPO_DELETE = """/repos/%s/%s""" # <owner>, <reponame>
REPO_USER_TIME = """/repos/%s/%s/times/%s""" # <owner>, <reponame>, <username>
def __init__(self, gitea, repoOwner, repoName: str, initJson: json = None):
""" Initializing a Repository.
@ -305,6 +319,12 @@ class Repository:
setattr(self, i, v)
self.owner = repoOwner
def __eq__(self, other):
if not other is None:
if isinstance(other, Repository):
return other.id == self.id
return False
def __repr__(self):
""" Representation of a Repository. Consisting of path and id.
"""
@ -321,6 +341,18 @@ class Repository:
)
return [Branch(self, result["name"], result) for result in results]
def get_user_time(self, username):
"""Get the time a user spent working on this Repository.
Args:
username (str): Username of the user
Returns: int
Accumulated time the user worked in this Repository.
"""
results = self.gitea.requests_get(Repository.REPO_USER_TIME % (self.owner.username, self.name, username))
return results["time"]
def delete(self):
""" Deletes this Repository.
@ -392,6 +424,8 @@ class Team:
ADD_USER = """/teams/%s/members/%s""" # <id, username to add>
ADD_REPO = """/teams/%s/repos/%s/%s""" # <id, org, repo>
TEAM_DELETE = """/teams/%s""" # <id>
GET_MEMBERS = """/teams/%s/members""" # <id>
GET_REPOS = """/teams/%s/repos""" # <id>
def __init__(self, org: Organization, name: str, initJson: json = None):
""" Initializes Team.
@ -428,7 +462,7 @@ class Team:
logging.debug("Team found: %s/%s" % (org.username, name))
if not result:
logging.warning("Failed to find Team: %s/%s" % (org.username, name))
raise NotFoundException()
raise NotFoundException("Team could not be Found")
for i, v in result.items():
setattr(self, i, v)
self.organization = org
@ -438,6 +472,27 @@ class Team:
"""
return "Team: %s/%s (%s)" % (self.organization.username, self.name, self.id)
def __eq__(self, other):
if other is not None:
if isinstance(other, Team):
return other.id == self.id
return False
def add(self, toAdd):
""" Adding User or Repository to Team.
Args:
toAdd (Repository/User): the object to add to this Team
"""
if isinstance(toAdd, Repository):
self.add_repo(toAdd)
return
if isinstance(toAdd, User):
self.add_user(toAdd)
return
logging.error("Could not add %s" % str(toAdd))
raise Exception("Could not add" + str(type(toAdd)))
def add_user(self, user: User):
""" Adding a User to this Team.
@ -456,6 +511,26 @@ class Team:
Team.ADD_REPO % (self.id, self.organization.username, repo.name)
)
def get_members(self):
""" Get all members of this Team.
Returns: [User]
A list of Users in this Team.
"""
results = self.gitea.requests_get(Team.GET_MEMBERS % self.id)
return [User(self.gitea, result["username"], initJson=result) for result in results]
def get_repos(self):
""" Get all repos of this Team.
Returns: [Repository]
A list of Repositories of this Team
"""
results = self.gitea.requests_get(Team.GET_REPOS % self.id)
return [Repository(self.gitea, self.organization, result["name"], initJson=result) for result in results]
def delete(self):
""" Delete this Team.
"""
@ -833,7 +908,6 @@ class Gitea:
repoOwner,
repoName: str,
description: str = "",
# private: bool=False, autoInit=True, gitignores='C#',
private: bool = False,
autoInit=True,
gitignores=None,
@ -927,7 +1001,7 @@ class Gitea:
raise Exception(
"Organization not created... (gitea: %s)" % result["message"]
)
return Organization(owner, orgName, initJson=result)
return Organization(self, orgName, initJson=result)
def create_team(
self,