212 lines
5.5 KiB
Python
212 lines
5.5 KiB
Python
import os
|
|
from pprint import PrettyPrinter
|
|
|
|
import database.setup_db
|
|
from database import SessionLocal
|
|
from services import OrganizationService, UserService, FileService
|
|
|
|
database.setup_db.setup_db()
|
|
|
|
db = SessionLocal()
|
|
org_service = OrganizationService(db)
|
|
user_service = UserService(db)
|
|
file_service = FileService(db)
|
|
|
|
def test_create_organization():
|
|
org = org_service.create_organization(
|
|
name="Test Org",
|
|
username="testuser",
|
|
full_name="Test User",
|
|
email="test@mail.com",
|
|
public_key="abc123"
|
|
)
|
|
|
|
assert org.name == "Test Org"
|
|
assert org.owner.username == "testuser"
|
|
assert org.owner.full_name == "Test User"
|
|
assert org.owner.email == "test@mail.com"
|
|
assert org.owner.public_key == "abc123"
|
|
assert org.owner.orgs[0].name == "Test Org"
|
|
|
|
db.delete(org.owner)
|
|
db.delete(org)
|
|
db.commit()
|
|
|
|
|
|
def test_create_user():
|
|
user = user_service.create_user(
|
|
username="testuser",
|
|
full_name="Test User",
|
|
email="test@mail.com",
|
|
public_key="abc123"
|
|
)
|
|
|
|
assert user.username == "testuser"
|
|
assert user.full_name == "Test User"
|
|
assert user.email == "test@mail.com"
|
|
assert user.public_key == "abc123"
|
|
|
|
db.delete(user)
|
|
db.commit()
|
|
|
|
|
|
def test_get_organization():
|
|
org = org_service.create_organization(
|
|
name="Test Org",
|
|
username="testuser",
|
|
full_name="Test User",
|
|
email="test@mail.com",
|
|
public_key="abc123"
|
|
)
|
|
|
|
org2 = org_service.get_organization(org.id)
|
|
org3 = org_service.get_organization_by_name("Test Org")
|
|
|
|
assert org2.name == "Test Org"
|
|
assert org3.name == "Test Org"
|
|
assert org2.owner.username == "testuser"
|
|
assert org3.owner.username == "testuser"
|
|
|
|
db.delete(org.owner)
|
|
db.delete(org)
|
|
db.commit()
|
|
|
|
|
|
def test_list_organizations():
|
|
org = org_service.create_organization(
|
|
name="Test Org",
|
|
username="testuser",
|
|
full_name="Test User",
|
|
email="test@mail.com",
|
|
public_key="abc123"
|
|
)
|
|
org2 = org_service.create_organization(
|
|
name="Test Org2",
|
|
username="testuser2",
|
|
full_name="Test User2",
|
|
email="mail2@test.com",
|
|
public_key="def456"
|
|
)
|
|
|
|
orgs = org_service.list_organizations()
|
|
|
|
assert len(orgs) == 2
|
|
assert orgs[0].name == "Test Org"
|
|
assert orgs[1].name == "Test Org2"
|
|
assert orgs == [org, org2]
|
|
|
|
db.delete(org.owner)
|
|
db.delete(org)
|
|
db.delete(org2.owner)
|
|
db.delete(org2)
|
|
db.commit()
|
|
|
|
|
|
def test_get_user():
|
|
user = user_service.create_user(
|
|
username="testuser",
|
|
full_name="Test User",
|
|
email="test@mail.com",
|
|
public_key="abc123"
|
|
)
|
|
|
|
user2 = user_service.get_user(user.id)
|
|
user3 = user_service.get_user_by_username("testuser")
|
|
|
|
assert user2.username == "testuser"
|
|
assert user3.username == "testuser"
|
|
assert user2.full_name == "Test User"
|
|
assert user3.full_name == "Test User"
|
|
assert user2.email == "test@mail.com"
|
|
assert user3.email == "test@mail.com"
|
|
assert user2.public_key == "abc123"
|
|
assert user3.public_key == "abc123"
|
|
|
|
org = org_service.create_organization(
|
|
name="Test Org",
|
|
username="testuser",
|
|
full_name="Test User",
|
|
email="test@mail.com",
|
|
public_key="abc123"
|
|
)
|
|
user4 = user_service.get_user(user.id)
|
|
|
|
assert user4.orgs[0].name == "Test Org"
|
|
|
|
db.delete(org)
|
|
db.delete(user)
|
|
db.commit()
|
|
|
|
def test_create_dummy_file():
|
|
|
|
org = org_service.create_organization(
|
|
name="org",
|
|
username="testuser",
|
|
full_name="Test User",
|
|
email="test@mail.com",
|
|
public_key="abc123"
|
|
)
|
|
|
|
file = file_service.create_dummy_file(org, org.owner)
|
|
|
|
assert file.file_handle == "dummy_file"
|
|
assert file.document_handle == "org/dummy_file.txt"
|
|
assert file.name == "dummy_file"
|
|
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
repos = os.path.join(project_root, "repository")
|
|
with open(os.path.join(repos, file.document_handle), "r") as f:
|
|
assert f.read() == "Dummy file content"
|
|
|
|
|
|
os.remove(os.path.join(repos, file.document_handle))
|
|
db.delete(file)
|
|
db.delete(org.owner)
|
|
db.delete(org)
|
|
db.commit()
|
|
|
|
|
|
def test_get_file():
|
|
org = org_service.create_organization(
|
|
name="org",
|
|
username="testuser",
|
|
full_name="Test User",
|
|
email="test@mail.com",
|
|
public_key="abc123"
|
|
)
|
|
|
|
file = file_service.create_dummy_file(org, org.owner)
|
|
file2 = file_service.get_file_by_file_handle("dummy_file")
|
|
|
|
assert file2.file_handle == "dummy_file"
|
|
assert file2.document_handle == "org/dummy_file.txt"
|
|
assert file2.name == "dummy_file"
|
|
|
|
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
repos = os.path.join(project_root, "repository")
|
|
os.remove(os.path.join(repos, file.document_handle))
|
|
db.delete(file)
|
|
db.delete(org.owner)
|
|
db.delete(org)
|
|
db.commit()
|
|
|
|
|
|
def test_delete_file():
|
|
org = org_service.create_organization(
|
|
name="org",
|
|
username="testuser",
|
|
full_name="Test User",
|
|
email="test@mail.com",
|
|
public_key="abc123"
|
|
)
|
|
|
|
file = file_service.create_dummy_file(org, org.owner)
|
|
file2 = file_service.delete_file(file)
|
|
|
|
assert file2.file_handle == "dummy_file"
|
|
assert file2.document_handle is None
|
|
assert os.path.exists(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "repository", "org", "dummy_file.txt")) == False
|
|
|
|
db.delete(file)
|
|
db.delete(org.owner)
|
|
db.delete(org)
|
|
db.commit() |