From c499ef049d3291425d2a5283e0b7d8e89a2190d1 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 17 Jul 2023 12:00:03 +0300 Subject: [PATCH 1/8] Test fixing --- src/Database/Helpers/Permission.php | 4 ++ tests/Database/PermissionTest.php | 58 +++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/Database/Helpers/Permission.php b/src/Database/Helpers/Permission.php index 82113223b..6e468f629 100644 --- a/src/Database/Helpers/Permission.php +++ b/src/Database/Helpers/Permission.php @@ -89,6 +89,10 @@ public static function parse(string $permission): self } $permission = $permissionParts[0]; + if(!in_array($permission, Database::PERMISSIONS)){ + throw new DatabaseException('Invalid permission Value'); + } + $fullRole = \str_replace('")', '', $permissionParts[1]); $roleParts = \explode(':', $fullRole); $role = $roleParts[0]; diff --git a/tests/Database/PermissionTest.php b/tests/Database/PermissionTest.php index 2e332f1b3..dd6926b36 100644 --- a/tests/Database/PermissionTest.php +++ b/tests/Database/PermissionTest.php @@ -260,20 +260,41 @@ public function testInputFromRoles(): void public function testInvalidFormats(): void { - $this->expectException(\Exception::class); - Permission::parse('read'); + try { + Permission::parse('read'); + $this->fail('Failed to throw Exception'); + } catch (\Exception $e) { + $this->assertEquals('Invalid permission string format: "read".', $e->getMessage()); + } + + try { + Permission::parse('read(("any")'); + $this->fail('Failed to throw Exception'); + } catch (\Exception $e) { + $this->assertEquals('Invalid permission Value', $e->getMessage()); + } + + try { + Permission::parse('read("users/un/verified")'); + $this->fail('Failed to throw Exception'); + } catch (\Exception $e) { + $this->assertEquals('Only one dimension can be provided', $e->getMessage()); + } + + try { + Permission::parse('read("users/")'); + $this->fail('Failed to throw Exception'); + } catch (\Exception $e) { + $this->assertEquals('Dimension must not be empty', $e->getMessage()); + } + + try { + Permission::parse('read("label:alphanumeric-only")'); + $this->fail('Failed to throw Exception'); + } catch (\Exception $e) { + $this->assertEquals('bla', $e->getMessage()); + } - $this->expectException(\Exception::class); - Permission::parse('read(("any")'); - - $this->expectException(\Exception::class); - Permission::parse('read("users/un/verified")'); - - $this->expectException(\Exception::class); - Permission::parse('read("users/")'); - - $this->expectException(\Exception::class); - Permission::parse('read("label:alphanumeric-only")'); } /** @@ -281,12 +302,17 @@ public function testInvalidFormats(): void */ public function testAggregation(): void { - $permissions = ['write("any")']; + $permissions = ['create("any")']; $parsed = Permission::aggregate($permissions); - $this->assertEquals(['create("any")', 'update("any")', 'delete("any")'], $parsed); + $this->assertEquals(['create("any")'], $parsed); $parsed = Permission::aggregate($permissions, [Database::PERMISSION_UPDATE, Database::PERMISSION_DELETE]); - $this->assertEquals(['update("any")', 'delete("any")'], $parsed); + + // TODO looks like something here is not working proper + var_dump($parsed); + die; + + $this->assertEquals(['create("any")', 'update("any")', 'delete("any")'], $parsed); $permissions = [ 'read("any")', From 0e80dc9077924bc3be638fe917e8145d2f889e2a Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 17 Jul 2023 12:00:27 +0300 Subject: [PATCH 2/8] Test fixing --- tests/Database/PermissionTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Database/PermissionTest.php b/tests/Database/PermissionTest.php index dd6926b36..994ab2f36 100644 --- a/tests/Database/PermissionTest.php +++ b/tests/Database/PermissionTest.php @@ -294,7 +294,6 @@ public function testInvalidFormats(): void } catch (\Exception $e) { $this->assertEquals('bla', $e->getMessage()); } - } /** From 5ce693bcb8f830c7c56bfeb60ebe451df447e692 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 25 Jul 2023 10:53:35 +0300 Subject: [PATCH 3/8] Revert test --- tests/Database/PermissionTest.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/tests/Database/PermissionTest.php b/tests/Database/PermissionTest.php index 994ab2f36..7c1413b2c 100644 --- a/tests/Database/PermissionTest.php +++ b/tests/Database/PermissionTest.php @@ -301,17 +301,12 @@ public function testInvalidFormats(): void */ public function testAggregation(): void { - $permissions = ['create("any")']; + $permissions = ['write("any")']; $parsed = Permission::aggregate($permissions); - $this->assertEquals(['create("any")'], $parsed); + $this->assertEquals(['create("any")', 'update("any")', 'delete("any")'], $parsed); $parsed = Permission::aggregate($permissions, [Database::PERMISSION_UPDATE, Database::PERMISSION_DELETE]); - - // TODO looks like something here is not working proper - var_dump($parsed); - die; - - $this->assertEquals(['create("any")', 'update("any")', 'delete("any")'], $parsed); + $this->assertEquals(['update("any")', 'delete("any")'], $parsed); $permissions = [ 'read("any")', From 0cd84882a60d27c2c4b2eb865ab8fef93603b04f Mon Sep 17 00:00:00 2001 From: Shmuel Fogel Date: Tue, 25 Jul 2023 10:54:14 +0300 Subject: [PATCH 4/8] Update src/Database/Helpers/Permission.php Co-authored-by: Jake Barnby --- src/Database/Helpers/Permission.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Helpers/Permission.php b/src/Database/Helpers/Permission.php index 6e468f629..675ccb2eb 100644 --- a/src/Database/Helpers/Permission.php +++ b/src/Database/Helpers/Permission.php @@ -90,7 +90,7 @@ public static function parse(string $permission): self $permission = $permissionParts[0]; if(!in_array($permission, Database::PERMISSIONS)){ - throw new DatabaseException('Invalid permission Value'); + throw new DatabaseException('Invalid permission type: "' . $permission . '".'); } $fullRole = \str_replace('")', '', $permissionParts[1]); From aefc111caf8ad9df024a791bcbc6bb8af7d3a1de Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 31 Jul 2023 17:53:13 +0300 Subject: [PATCH 5/8] Changing whiteList --- src/Database/Helpers/Permission.php | 2 +- tests/Database/PermissionTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Database/Helpers/Permission.php b/src/Database/Helpers/Permission.php index 675ccb2eb..4bbe78c67 100644 --- a/src/Database/Helpers/Permission.php +++ b/src/Database/Helpers/Permission.php @@ -89,7 +89,7 @@ public static function parse(string $permission): self } $permission = $permissionParts[0]; - if(!in_array($permission, Database::PERMISSIONS)){ + if(!in_array($permission, array_merge(Database::PERMISSIONS, [Database::PERMISSION_WRITE]))){ throw new DatabaseException('Invalid permission type: "' . $permission . '".'); } diff --git a/tests/Database/PermissionTest.php b/tests/Database/PermissionTest.php index 7c1413b2c..ef726032e 100644 --- a/tests/Database/PermissionTest.php +++ b/tests/Database/PermissionTest.php @@ -271,7 +271,7 @@ public function testInvalidFormats(): void Permission::parse('read(("any")'); $this->fail('Failed to throw Exception'); } catch (\Exception $e) { - $this->assertEquals('Invalid permission Value', $e->getMessage()); + $this->assertEquals('Invalid permission type: "read(".', $e->getMessage()); } try { @@ -292,7 +292,7 @@ public function testInvalidFormats(): void Permission::parse('read("label:alphanumeric-only")'); $this->fail('Failed to throw Exception'); } catch (\Exception $e) { - $this->assertEquals('bla', $e->getMessage()); + $this->assertEquals('Do we want this to fail?', $e->getMessage()); } } From 25f711ae53478b668c1e924af835976e705db374 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 31 Jul 2023 17:55:02 +0300 Subject: [PATCH 6/8] lint --- src/Database/Helpers/Permission.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Helpers/Permission.php b/src/Database/Helpers/Permission.php index 4bbe78c67..9e3e56422 100644 --- a/src/Database/Helpers/Permission.php +++ b/src/Database/Helpers/Permission.php @@ -89,7 +89,7 @@ public static function parse(string $permission): self } $permission = $permissionParts[0]; - if(!in_array($permission, array_merge(Database::PERMISSIONS, [Database::PERMISSION_WRITE]))){ + if (!in_array($permission, array_merge(Database::PERMISSIONS, [Database::PERMISSION_WRITE]))) { throw new DatabaseException('Invalid permission type: "' . $permission . '".'); } From 7d2e0a79136a511641f6cefba408e7e0f16b1a71 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 10 Aug 2023 18:37:48 -0400 Subject: [PATCH 7/8] Remove redundant check --- tests/Database/PermissionTest.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/Database/PermissionTest.php b/tests/Database/PermissionTest.php index ef726032e..a436d743a 100644 --- a/tests/Database/PermissionTest.php +++ b/tests/Database/PermissionTest.php @@ -287,13 +287,6 @@ public function testInvalidFormats(): void } catch (\Exception $e) { $this->assertEquals('Dimension must not be empty', $e->getMessage()); } - - try { - Permission::parse('read("label:alphanumeric-only")'); - $this->fail('Failed to throw Exception'); - } catch (\Exception $e) { - $this->assertEquals('Do we want this to fail?', $e->getMessage()); - } } /** From 6374579a65aeacb94901d1ec7e123cf091ef2bb6 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 10 Aug 2023 18:44:58 -0400 Subject: [PATCH 8/8] Update src/Database/Helpers/Permission.php --- src/Database/Helpers/Permission.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Helpers/Permission.php b/src/Database/Helpers/Permission.php index 9e3e56422..df0f1a5c0 100644 --- a/src/Database/Helpers/Permission.php +++ b/src/Database/Helpers/Permission.php @@ -89,10 +89,10 @@ public static function parse(string $permission): self } $permission = $permissionParts[0]; - if (!in_array($permission, array_merge(Database::PERMISSIONS, [Database::PERMISSION_WRITE]))) { + + if (!\in_array($permission, array_merge(Database::PERMISSIONS, [Database::PERMISSION_WRITE]))) { throw new DatabaseException('Invalid permission type: "' . $permission . '".'); } - $fullRole = \str_replace('")', '', $permissionParts[1]); $roleParts = \explode(':', $fullRole); $role = $roleParts[0];