from django.contrib.auth import get_user_model from django.test import Client, TestCase from django.utils import timezone from workflows.branding import get_portal_trial_config from workflows.roles import ROLE_PLATFORM_OWNER, ROLE_STAFF, assign_user_role from workflows.services import is_email_test_mode, is_nextcloud_enabled class TrialLifecycleTests(TestCase): def setUp(self): user_model = get_user_model() self.platform_owner = user_model.objects.create_user(username='trial_platform_owner', password='secret123') assign_user_role(self.platform_owner, ROLE_PLATFORM_OWNER) self.staff = user_model.objects.create_user(username='trial_staff', password='secret123') assign_user_role(self.staff, ROLE_STAFF) self.trial = get_portal_trial_config() self.original_values = ( self.trial.is_trial_mode, self.trial.trial_started_at, self.trial.trial_expires_at, self.trial.restrict_production_integrations, self.trial.auto_cleanup_enabled, ) def tearDown(self): ( self.trial.is_trial_mode, self.trial.trial_started_at, self.trial.trial_expires_at, self.trial.restrict_production_integrations, self.trial.auto_cleanup_enabled, ) = self.original_values self.trial.save() def test_staff_is_blocked_after_trial_expiry(self): self.trial.is_trial_mode = True self.trial.trial_started_at = timezone.now() - timezone.timedelta(days=10) self.trial.trial_expires_at = timezone.now() - timezone.timedelta(days=1) self.trial.save() client = Client(HTTP_HOST='127.0.0.1') client.force_login(self.staff) response = client.get('/requests/') self.assertEqual(response.status_code, 403) self.assertIn('Trial abgelaufen', response.content.decode('utf-8')) def test_platform_owner_keeps_access_after_trial_expiry(self): self.trial.is_trial_mode = True self.trial.trial_started_at = timezone.now() - timezone.timedelta(days=10) self.trial.trial_expires_at = timezone.now() - timezone.timedelta(days=1) self.trial.save() client = Client(HTTP_HOST='127.0.0.1') client.force_login(self.platform_owner) response = client.get('/requests/') self.assertEqual(response.status_code, 200) def test_trial_restriction_forces_safe_integration_modes(self): self.trial.is_trial_mode = True self.trial.trial_started_at = timezone.now() - timezone.timedelta(days=1) self.trial.trial_expires_at = timezone.now() + timezone.timedelta(days=2) self.trial.restrict_production_integrations = True self.trial.save() self.assertFalse(is_nextcloud_enabled()) self.assertTrue(is_email_test_mode())