|
| 1 | +from faker import Faker |
| 2 | + |
| 3 | +from basetest import BaseTest |
| 4 | +from scalekit.v1.organizations.organizations_pb2 import ( |
| 5 | + CreateOrganization, |
| 6 | + SessionPolicyType, |
| 7 | +) |
| 8 | +from scalekit.v1.commons.commons_pb2 import TimeUnit |
| 9 | + |
| 10 | + |
| 11 | +class TestOrganizationSessionPolicy(BaseTest): |
| 12 | + """Test cases for organization session policy management.""" |
| 13 | + |
| 14 | + def setUp(self): |
| 15 | + self.org_id = None |
| 16 | + |
| 17 | + def _create_org(self): |
| 18 | + organization = CreateOrganization(display_name=Faker().company(), external_id=Faker().uuid4()) |
| 19 | + response = self.scalekit_client.organization.create_organization(organization=organization) |
| 20 | + self.org_id = response[0].organization.id |
| 21 | + return self.org_id |
| 22 | + |
| 23 | + def test_get_default_policy(self): |
| 24 | + """New org should inherit APPLICATION policy by default.""" |
| 25 | + org_id = self._create_org() |
| 26 | + |
| 27 | + policy = self.scalekit_client.organization.get_organization_session_policy( |
| 28 | + organization_id=org_id |
| 29 | + )[0].policy |
| 30 | + |
| 31 | + self.assertIsNotNone(policy) |
| 32 | + self.assertEqual(policy.policy_source, SessionPolicyType.APPLICATION) |
| 33 | + |
| 34 | + def test_set_custom_policy(self): |
| 35 | + """Setting a custom policy should persist and be retrievable.""" |
| 36 | + org_id = self._create_org() |
| 37 | + |
| 38 | + policy = self.scalekit_client.organization.update_organization_session_policy( |
| 39 | + organization_id=org_id, |
| 40 | + policy_source=SessionPolicyType.CUSTOM, |
| 41 | + absolute_session_timeout=360, |
| 42 | + absolute_session_timeout_unit=TimeUnit.MINUTES, |
| 43 | + idle_session_timeout_enabled=True, |
| 44 | + idle_session_timeout=60, |
| 45 | + idle_session_timeout_unit=TimeUnit.MINUTES, |
| 46 | + )[0].policy |
| 47 | + |
| 48 | + self.assertIsNotNone(policy) |
| 49 | + self.assertEqual(policy.policy_source, SessionPolicyType.CUSTOM) |
| 50 | + |
| 51 | + fetched = self.scalekit_client.organization.get_organization_session_policy( |
| 52 | + organization_id=org_id |
| 53 | + )[0].policy |
| 54 | + self.assertEqual(fetched.policy_source, SessionPolicyType.CUSTOM) |
| 55 | + self.assertTrue(fetched.HasField("absolute_session_timeout")) |
| 56 | + self.assertEqual(fetched.absolute_session_timeout.value, 360) |
| 57 | + self.assertTrue(fetched.HasField("idle_session_timeout_enabled")) |
| 58 | + self.assertTrue(fetched.idle_session_timeout_enabled.value) |
| 59 | + |
| 60 | + def test_revert_to_application_policy(self): |
| 61 | + """Setting policy source to APPLICATION should revert to application defaults.""" |
| 62 | + org_id = self._create_org() |
| 63 | + |
| 64 | + self.scalekit_client.organization.update_organization_session_policy( |
| 65 | + organization_id=org_id, |
| 66 | + policy_source=SessionPolicyType.CUSTOM, |
| 67 | + absolute_session_timeout=120, |
| 68 | + absolute_session_timeout_unit=TimeUnit.MINUTES, |
| 69 | + ) |
| 70 | + |
| 71 | + reverted = self.scalekit_client.organization.update_organization_session_policy( |
| 72 | + organization_id=org_id, |
| 73 | + policy_source=SessionPolicyType.APPLICATION, |
| 74 | + )[0].policy |
| 75 | + |
| 76 | + self.assertIsNotNone(reverted) |
| 77 | + self.assertEqual(reverted.policy_source, SessionPolicyType.APPLICATION) |
| 78 | + |
| 79 | + fetched = self.scalekit_client.organization.get_organization_session_policy( |
| 80 | + organization_id=org_id |
| 81 | + )[0].policy |
| 82 | + self.assertEqual(fetched.policy_source, SessionPolicyType.APPLICATION) |
| 83 | + |
| 84 | + def test_set_idle_timeout_disabled(self): |
| 85 | + """Setting idle_session_timeout_enabled=False should persist as false.""" |
| 86 | + org_id = self._create_org() |
| 87 | + |
| 88 | + policy = self.scalekit_client.organization.update_organization_session_policy( |
| 89 | + organization_id=org_id, |
| 90 | + policy_source=SessionPolicyType.CUSTOM, |
| 91 | + absolute_session_timeout=480, |
| 92 | + absolute_session_timeout_unit=TimeUnit.MINUTES, |
| 93 | + idle_session_timeout_enabled=False, |
| 94 | + )[0].policy |
| 95 | + |
| 96 | + self.assertIsNotNone(policy) |
| 97 | + self.assertEqual(policy.policy_source, SessionPolicyType.CUSTOM) |
| 98 | + |
| 99 | + fetched = self.scalekit_client.organization.get_organization_session_policy( |
| 100 | + organization_id=org_id |
| 101 | + )[0].policy |
| 102 | + self.assertTrue(fetched.HasField("idle_session_timeout_enabled")) |
| 103 | + self.assertFalse(fetched.idle_session_timeout_enabled.value) |
| 104 | + |
| 105 | + def tearDown(self): |
| 106 | + if self.org_id: |
| 107 | + self.scalekit_client.organization.delete_organization(organization_id=self.org_id) |
0 commit comments