Skip to content

Commit

Permalink
Merge pull request #290 from utopia-php/fix-permission-test
Browse files Browse the repository at this point in the history
Fix permission test
  • Loading branch information
abnegate authored Aug 10, 2023
2 parents 318ee12 + 6374579 commit 5631f15
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/Database/Helpers/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public static function parse(string $permission): self
}

$permission = $permissionParts[0];

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];
Expand Down
41 changes: 27 additions & 14 deletions tests/Database/PermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,20 +260,33 @@ public function testInputFromRoles(): void

public function testInvalidFormats(): void
{
$this->expectException(\Exception::class);
Permission::parse('read');

$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")');
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 type: "read(".', $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());
}
}

/**
Expand Down

0 comments on commit 5631f15

Please sign in to comment.