FossilRepo

fossilrepo / conftest.py
Blame History Raw 123 lines
1
import pytest
2
from django.contrib.auth.models import Group, Permission, User
3
4
from organization.models import Organization, OrganizationMember, Team
5
from pages.models import Page
6
from projects.models import Project, ProjectTeam
7
8
9
@pytest.fixture(autouse=True)
10
def _fossil_data_dir(tmp_path, settings):
11
"""Point FOSSIL_DATA_DIR to a temp directory so tests don't need /data/repos."""
12
data_dir = tmp_path / "fossil-repos"
13
data_dir.mkdir()
14
# Override the constance default via Django's CONSTANCE_CONFIG setting
15
settings.CONSTANCE_CONFIG = {
16
**settings.CONSTANCE_CONFIG,
17
"FOSSIL_DATA_DIR": (str(data_dir), "Directory where .fossil repository files are stored"),
18
}
19
# Also set via constance DB backend if already cached
20
try:
21
from constance import config
22
23
config.FOSSIL_DATA_DIR = str(data_dir)
24
except Exception:
25
pass
26
27
28
@pytest.fixture
29
def admin_user(db):
30
user = User.objects.create_superuser(username="admin", email="[email protected]", password="testpass123")
31
return user
32
33
34
@pytest.fixture
35
def viewer_user(db):
36
user = User.objects.create_user(username="viewer", email="[email protected]", password="testpass123")
37
group, _ = Group.objects.get_or_create(name="Viewers")
38
view_perms = Permission.objects.filter(
39
content_type__app_label__in=["organization", "projects", "pages"],
40
codename__startswith="view_",
41
)
42
group.permissions.set(view_perms)
43
user.groups.add(group)
44
return user
45
46
47
@pytest.fixture
48
def no_perm_user(db):
49
return User.objects.create_user(username="noperm", email="[email protected]", password="testpass123")
50
51
52
@pytest.fixture
53
def org(db, admin_user):
54
org = Organization.objects.create(name="Test Org", created_by=admin_user)
55
OrganizationMember.objects.create(member=admin_user, organization=org)
56
return org
57
58
59
@pytest.fixture
60
def sample_team(db, org, admin_user):
61
team = Team.objects.create(name="Core Devs", organization=org, created_by=admin_user)
62
team.members.add(admin_user)
63
return team
64
65
66
@pytest.fixture
67
def sample_project(db, org, admin_user, sample_team):
68
project = Project.objects.create(name="Frontend App", organization=org, visibility="private", created_by=admin_user)
69
ProjectTeam.objects.create(project=project, team=sample_team, role="write", created_by=admin_user)
70
return project
71
72
73
@pytest.fixture
74
def sample_page(db, org, admin_user):
75
return Page.objects.create(
76
name="Getting Started",
77
content="# Getting Started\n\nWelcome to the docs.",
78
organization=org,
79
created_by=admin_user,
80
)
81
82
83
@pytest.fixture
84
def fossil_repo(db, sample_project, admin_user, tmp_path):
85
"""Create a FossilRepository with a real .fossil file for testing."""
86
import shutil
87
88
from fossil.models import FossilRepository
89
90
# Copy a test repo to tmp_path
91
src = "/tmp/fossil-setup/frontend-app.fossil"
92
dest = tmp_path / "test-project.fossil"
93
shutil.copy2(src, dest)
94
95
# Override FOSSIL_DATA_DIR for this test
96
97
repo = FossilRepository.objects.create(
98
project=sample_project,
99
filename="test-project.fossil",
100
created_by=admin_user,
101
)
102
# Patch the full_path property to point to our tmp file
103
repo._test_path = dest
104
return repo
105
106
107
@pytest.fixture
108
def admin_client(client, admin_user):
109
client.login(username="admin", password="testpass123")
110
return client
111
112
113
@pytest.fixture
114
def viewer_client(client, viewer_user):
115
client.login(username="viewer", password="testpass123")
116
return client
117
118
119
@pytest.fixture
120
def no_perm_client(client, no_perm_user):
121
client.login(username="noperm", password="testpass123")
122
return client
123

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button