intent/tests/test_project_access.py
Alec Perkins b9378b8397 Add projects and features with revision history (Phase 2)
Projects under organizations with fixed URL name and feature ID
prefix, three visibility levels, open contribution, per-project
roles, settings and typed-confirmation delete. Hidden projects 404.
Leaving an org drops its project roles. Project access rules live in
intent/access.py alongside org access.

Features numbered per project (INT-12) in a tree. Every change is a
revision with a required note; history, old revisions, and
side-by-side field and line diffs between any two. Cycles and
superseded-without-replacement are rejected. Markdown renders safely
with source-line attributes for Phase 3 line comments and headings
shifted below the page h1. Server-side preview works without JS.

Structural accessibility check runs on every page in tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 07:58:16 -04:00

89 lines
3.5 KiB
Python

import pytest
from django.contrib.auth.models import AnonymousUser
from intent.access import project_access, visible_projects
from intent.orgs.models import OrgMembership
from intent.projects.models import ProjectMembership, Visibility
def _flags(user, project):
a = project_access(user, project)
return (a.can_view, a.can_contribute, a.can_edit, a.can_admin)
@pytest.fixture
def people(make_user, org, project, owner, collaborator, editor):
project_owner = make_user("pam")
OrgMembership.objects.create(org=org, user=project_owner)
ProjectMembership.objects.create(project=project, user=project_owner, role="owner")
return {
"anonymous": AnonymousUser(),
"outsider": make_user(),
"staff": make_user(is_staff=True),
"org collaborator": collaborator,
"project collaborator": editor,
"project owner": project_owner,
"org owner": owner,
}
# (view, contribute, edit, admin) per visibility, without open contribution.
EXPECTED = {
Visibility.PRIVATE: {
"anonymous": (False, False, False, False),
"outsider": (False, False, False, False),
"staff": (False, False, False, False),
"org collaborator": (False, False, False, False),
"project collaborator": (True, True, True, False),
"project owner": (True, True, True, True),
"org owner": (True, True, True, True),
},
Visibility.AUTHENTICATED: {
"anonymous": (False, False, False, False),
"outsider": (True, False, False, False),
"staff": (False, False, False, False),
"org collaborator": (True, False, False, False),
"project collaborator": (True, True, True, False),
"project owner": (True, True, True, True),
"org owner": (True, True, True, True),
},
Visibility.PUBLIC: {
"anonymous": (True, False, False, False),
"outsider": (True, False, False, False),
"staff": (True, False, False, False),
"org collaborator": (True, False, False, False),
"project collaborator": (True, True, True, False),
"project owner": (True, True, True, True),
"org owner": (True, True, True, True),
},
}
@pytest.mark.parametrize("visibility", list(EXPECTED))
def test_access_matrix(people, project, visibility):
project.visibility = visibility
project.save()
for who, user in people.items():
assert _flags(user, project) == EXPECTED[visibility][who], who
def test_open_contribution_lets_signed_in_viewers_contribute(people, project):
project.visibility = Visibility.PUBLIC
project.open_contribution = True
project.save()
assert project_access(people["outsider"], project).can_contribute
assert project_access(people["org collaborator"], project).can_contribute
assert not project_access(people["anonymous"], project).can_contribute
assert not project_access(people["staff"], project).can_contribute
assert not project_access(people["outsider"], project).can_edit
def test_visible_projects(people, org, project):
assert list(visible_projects(people["org owner"], org)) == [project]
assert list(visible_projects(people["project collaborator"], org)) == [project]
assert list(visible_projects(people["org collaborator"], org)) == []
assert list(visible_projects(people["anonymous"], org)) == []
project.visibility = Visibility.AUTHENTICATED
project.save()
assert list(visible_projects(people["outsider"], org)) == [project]
assert list(visible_projects(people["anonymous"], org)) == []