Add very basic tests

This commit is contained in:
Tobias Kunze 2025-03-23 00:03:04 +01:00
parent 832763cb2a
commit 1424fdf3bb
5 changed files with 137 additions and 3 deletions

24
src/tests/conftest.py Normal file
View file

@ -0,0 +1,24 @@
import pytest
from servala.core.models import Organization, OrganizationMembership, User, OrganizationOrigin
@pytest.fixture
def origin():
return OrganizationOrigin.objects.create(name="TESTORIGIN")
@pytest.fixture
def organization(origin):
return Organization.objects.create(name="Test Org", origin=origin)
@pytest.fixture
def other_organization(origin):
return Organization.objects.create(name="Test Org Alternate", origin=origin)
@pytest.fixture
def org_owner(organization):
user = User.objects.create(email="user@example.org", password="example")
OrganizationMembership.objects.create(organization=organization, user=user, role="owner")
return user

41
src/tests/test_views.py Normal file
View file

@ -0,0 +1,41 @@
import pytest
@pytest.mark.parametrize("url,redirect", (
("/", "/accounts/login/?next=/"),
("/accounts/profile/", "/accounts/login/?next=/accounts/profile/")
))
def test_root_view_redirects_valid_urls(client, url, redirect):
response = client.get(url)
assert response.status_code == 302
assert response.url == redirect
def test_root_view_invalid_urls_404(client):
response = client.get("/foobarbaz/")
assert response.status_code == 404
@pytest.mark.django_db
def test_owner_can_access_dashboard(client, org_owner, organization):
client.force_login(org_owner)
response = client.get(organization.urls.base)
assert response.status_code == 200
assert organization.name in response.content.decode()
@pytest.mark.django_db
def test_user_cannot_see_other_organization(client, org_owner, other_organization):
client.force_login(org_owner)
response = client.get(other_organization.urls.base)
assert response.status_code == 403
assert other_organization.name not in response.content.decode()
@pytest.mark.django_db
def test_organization_linked_in_sidebar(client, org_owner, organization, other_organization):
client.force_login(org_owner)
response = client.get("/", follow=True)
assert response.status_code == 200
assert organization.name in response.content.decode()
assert other_organization.name not in response.content.decode()