Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix permission test #290

Merged
merged 10 commits into from
Aug 10, 2023
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 . '".');
}

abnegate marked this conversation as resolved.
Show resolved Hide resolved
$fullRole = \str_replace('")', '', $permissionParts[1]);
$roleParts = \explode(':', $fullRole);
$role = $roleParts[0];
Expand Down
48 changes: 34 additions & 14 deletions tests/Database/PermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,20 +260,40 @@ 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());
}

try {
abnegate marked this conversation as resolved.
Show resolved Hide resolved
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());
abnegate marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
Expand Down