Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Archival #538

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Prev Previous commit
Next Next commit
Add archive_users stub and test
  • Loading branch information
lukasjuhrich committed Sep 5, 2023
commit 014464685e7be706ea3e66623955f8ce5a360703
12 changes: 6 additions & 6 deletions pycroft/lib/user_deletion.py
Original file line number Diff line number Diff line change
@@ -85,10 +85,10 @@ def get_archivable_members(session: Session, delta: timedelta = timedelta(days=1
return cast(list[ArchivableMemberInfo], session.execute(stmt).unique().all())


def get_invalidated_archive_memberships() -> list[Membership]:
"""Get all memberships in `to_be_archived` of users who have an active `do-not-archive` property.

This can happen if archivability is detected, and later the user becomes a member again,
or if for some reason the user shall not be archived.
"""
def archive_users(session: Session, user_ids: Sequence[int]) -> None:
# todo remove hosts
# todo remove tasks
# todo remove log entries
# todo insert these users into an archival log
# todo add membership in archival group
pass
2 changes: 2 additions & 0 deletions tests/factories/log.py
Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@ class Meta:
message = factory.Faker('paragraph')
author = factory.SubFactory(UserFactory)
user = factory.SubFactory(UserFactory)
created_at = None


class RoomLogEntryFactory(BaseFactory):
class Meta:
9 changes: 7 additions & 2 deletions tests/factories/task.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime

import factory
import factory.fuzzy

from pycroft.model.task import UserTask, TaskType, Task, TaskStatus
from tests.factories.base import BaseFactory
@@ -14,7 +14,7 @@ class TaskFactory(BaseFactory):
class Meta:
model = Task

type: TaskType = None
type: TaskType = factory.fuzzy.FuzzyChoice(TaskType)
due = None
parameters_json = None
created = None
@@ -34,3 +34,8 @@ class Meta:
model = UserTask

user = factory.SubFactory('tests.factories.UserFactory')

class Params:
self_created = factory.Trait(
creator=factory.SelfAttribute('user')
)
14 changes: 14 additions & 0 deletions tests/factories/user.py
Original file line number Diff line number Diff line change
@@ -65,6 +65,20 @@ class Params:
room=None,
address=factory.SubFactory('tests.factories.address.AddressFactory'),
)
with_creation_log_entry = factory.Trait(
log_entry=factory.RelatedFactory(
'tests.factories.log.UserLogEntryFactory', 'user',
created_at=factory.SelfAttribute('..registered_at'),
message="User created",
)
)
with_random_task = factory.Trait(
task=factory.RelatedFactory(
'tests.factories.task.UserTaskFactory', 'user',
self_created=True,
due_yesterday=True,
)
)

@factory.post_generation
def room_history_entries(self, create, extracted, **kwargs):
30 changes: 29 additions & 1 deletion tests/lib/user/test_deletion.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
import pytest

from pycroft.helpers.interval import closed, closedopen
from pycroft.lib.user_deletion import get_archivable_members
from pycroft.lib.user_deletion import get_archivable_members, archive_users
from tests.factories import UserFactory, ConfigFactory, MembershipFactory, \
PropertyGroupFactory, \
HostFactory
@@ -91,3 +91,31 @@ def test_user_with_room_in_list(self, session, old_user):
session.add(old_user)
members = get_archivable_members(session)
assert_archivable_members(members, old_user, date(2020, 3, 1))


class TestUserArchival:
@pytest.fixture(scope='class')
def archivable_users(self, class_session, config):
return UserFactory.create_batch(
3,
with_membership=True,
membership__active_during=closed(datetime(2020, 1, 1), datetime(2020, 3, 1)),
membership__group=config.member_group,
with_host=True, patched=True,
with_creation_log_entry=True,
with_random_task=True,
)

@pytest.fixture(scope='class')
def archived_users(self, class_session, archivable_users):
archive_users(class_session, [u.id for u in archivable_users])
return archivable_users

@pytest.mark.parametrize('index', [0, 1, 2])
def test_user_archival(self, archived_users, index):
user = archived_users[index]
assert user.tasks == [], "archival did not delete tasks"
assert [le for le in user.log_entries
if le.created_at == user.registered_at] == [], "archival did not delete logs"
assert user.hosts == [], "archival did not delete hosts"
assert 'archived' in user.current_properties