From 61db9a0e2a7674bcc6dfe308089bc3b04d3f7239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 30 May 2026 22:37:16 +0200 Subject: [PATCH 1/2] Improve extensibility of policies tests --- tests/e2e/Services/Project/PoliciesBase.php | 62 ++++++++++----------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/tests/e2e/Services/Project/PoliciesBase.php b/tests/e2e/Services/Project/PoliciesBase.php index 43c09b55c3..8f9bc1fccb 100644 --- a/tests/e2e/Services/Project/PoliciesBase.php +++ b/tests/e2e/Services/Project/PoliciesBase.php @@ -7,13 +7,12 @@ use Utopia\Database\Query; trait PoliciesBase { - // ========================================================================= - // Get Policy - // ========================================================================= - - public function testGetPolicy(): void + /** + * @return array> + */ + protected function getExpectedPolicies(): array { - $expectedFields = [ + return [ 'password-dictionary' => ['enabled'], 'password-history' => ['total'], 'password-personal-data' => ['enabled'], @@ -24,8 +23,15 @@ trait PoliciesBase 'user-limit' => ['total'], 'membership-privacy' => ['userId', 'userEmail', 'userPhone', 'userName', 'userMFA'], ]; + } - foreach ($expectedFields as $policyId => $fields) { + // ========================================================================= + // Get Policy + // ========================================================================= + + public function testGetPolicy(): void + { + foreach ($this->getExpectedPolicies() as $policyId => $fields) { $response = $this->getPolicy($policyId); $this->assertSame(200, $response['headers']['status-code']); @@ -125,25 +131,21 @@ trait PoliciesBase { $response = $this->listPolicies(); + $expected = $this->getExpectedPolicies(); + $this->assertSame(200, $response['headers']['status-code']); $this->assertArrayHasKey('policies', $response['body']); $this->assertArrayHasKey('total', $response['body']); $this->assertIsArray($response['body']['policies']); $this->assertIsInt($response['body']['total']); - $this->assertSame(9, $response['body']['total']); - $this->assertCount(9, $response['body']['policies']); + $this->assertSame(\count($expected), $response['body']['total']); + $this->assertCount(\count($expected), $response['body']['policies']); $policyIds = \array_column($response['body']['policies'], '$id'); - $this->assertContains('password-dictionary', $policyIds); - $this->assertContains('password-history', $policyIds); - $this->assertContains('password-personal-data', $policyIds); - $this->assertContains('session-alert', $policyIds); - $this->assertContains('session-duration', $policyIds); - $this->assertContains('session-invalidation', $policyIds); - $this->assertContains('session-limit', $policyIds); - $this->assertContains('user-limit', $policyIds); - $this->assertContains('membership-privacy', $policyIds); + foreach (\array_keys($expected) as $policyId) { + $this->assertContains($policyId, $policyIds); + } } public function testListPoliciesResponseModel(): void @@ -161,19 +163,13 @@ trait PoliciesBase $byId[$policy['$id']] = $policy; } - $this->assertArrayHasKey('enabled', $byId['password-dictionary']); - $this->assertArrayHasKey('total', $byId['password-history']); - $this->assertArrayHasKey('enabled', $byId['password-personal-data']); - $this->assertArrayHasKey('enabled', $byId['session-alert']); - $this->assertArrayHasKey('duration', $byId['session-duration']); - $this->assertArrayHasKey('enabled', $byId['session-invalidation']); - $this->assertArrayHasKey('total', $byId['session-limit']); - $this->assertArrayHasKey('total', $byId['user-limit']); - $this->assertArrayHasKey('userId', $byId['membership-privacy']); - $this->assertArrayHasKey('userEmail', $byId['membership-privacy']); - $this->assertArrayHasKey('userPhone', $byId['membership-privacy']); - $this->assertArrayHasKey('userName', $byId['membership-privacy']); - $this->assertArrayHasKey('userMFA', $byId['membership-privacy']); + foreach ($this->getExpectedPolicies() as $policyId => $fields) { + $this->assertArrayHasKey($policyId, $byId); + + foreach ($fields as $field) { + $this->assertArrayHasKey($field, $byId[$policyId]); + } + } } public function testListPoliciesReflectsUpdates(): void @@ -226,7 +222,7 @@ trait PoliciesBase $this->assertSame(200, $response['headers']['status-code']); $this->assertSame(0, $response['body']['total']); - $this->assertCount(9, $response['body']['policies']); + $this->assertCount(\count($this->getExpectedPolicies()), $response['body']['policies']); } public function testListPoliciesWithLimit(): void @@ -237,7 +233,7 @@ trait PoliciesBase $this->assertSame(200, $response['headers']['status-code']); $this->assertCount(1, $response['body']['policies']); - $this->assertSame(9, $response['body']['total']); + $this->assertSame(\count($this->getExpectedPolicies()), $response['body']['total']); } public function testListPoliciesWithOffset(): void From 2ae4209a99cad7fe253bb1dc3dd39fdf832e216c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 30 May 2026 22:52:38 +0200 Subject: [PATCH 2/2] Improve downstream tests --- tests/e2e/Services/Project/PoliciesBase.php | 22 +++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tests/e2e/Services/Project/PoliciesBase.php b/tests/e2e/Services/Project/PoliciesBase.php index 8f9bc1fccb..71fc85919f 100644 --- a/tests/e2e/Services/Project/PoliciesBase.php +++ b/tests/e2e/Services/Project/PoliciesBase.php @@ -138,8 +138,12 @@ trait PoliciesBase $this->assertArrayHasKey('total', $response['body']); $this->assertIsArray($response['body']['policies']); $this->assertIsInt($response['body']['total']); - $this->assertSame(\count($expected), $response['body']['total']); - $this->assertCount(\count($expected), $response['body']['policies']); + + // Unpaginated list: total reflects exactly what was returned. + $this->assertSame($response['body']['total'], \count($response['body']['policies'])); + + // At least the known policies are present; downstream may add more. + $this->assertGreaterThanOrEqual(\count($expected), $response['body']['total']); $policyIds = \array_column($response['body']['policies'], '$id'); @@ -222,7 +226,15 @@ trait PoliciesBase $this->assertSame(200, $response['headers']['status-code']); $this->assertSame(0, $response['body']['total']); - $this->assertCount(\count($this->getExpectedPolicies()), $response['body']['policies']); + + // total=false skips the count but still returns every policy; downstream may add more. + $this->assertGreaterThanOrEqual(\count($this->getExpectedPolicies()), \count($response['body']['policies'])); + + $policyIds = \array_column($response['body']['policies'], '$id'); + + foreach (\array_keys($this->getExpectedPolicies()) as $policyId) { + $this->assertContains($policyId, $policyIds); + } } public function testListPoliciesWithLimit(): void @@ -233,7 +245,9 @@ trait PoliciesBase $this->assertSame(200, $response['headers']['status-code']); $this->assertCount(1, $response['body']['policies']); - $this->assertSame(\count($this->getExpectedPolicies()), $response['body']['total']); + + // limit caps the page, not the total; downstream may add more policies. + $this->assertGreaterThanOrEqual(\count($this->getExpectedPolicies()), $response['body']['total']); } public function testListPoliciesWithOffset(): void