|
3 | 3 | namespace Tests\Unit; |
4 | 4 |
|
5 | 5 | use PHPUnit\Framework\TestCase; |
| 6 | +use Utopia\Cache\Adapter\None as NoCache; |
| 7 | +use Utopia\Cache\Cache; |
| 8 | +use Utopia\Database\Adapter\Memory; |
| 9 | +use Utopia\Database\Database; |
6 | 10 | use Utopia\Database\Document; |
7 | 11 | use Utopia\Database\Exception as DatabaseException; |
8 | 12 | use Utopia\Database\Helpers\Permission; |
9 | 13 | use Utopia\Database\Helpers\Role; |
| 14 | +use Utopia\Database\Validator\Authorization; |
10 | 15 | use Utopia\Database\Validator\Permissions; |
11 | 16 |
|
12 | 17 | class ColumnPermissionTest extends TestCase |
@@ -95,34 +100,48 @@ public function testWildcardColumnIsRejected(): void |
95 | 100 | } |
96 | 101 |
|
97 | 102 | /** |
98 | | - * A column-scoped permission must still resolve to a bare role, or every |
99 | | - * existing document-level authorization check silently breaks. |
| 103 | + * A column-scoped permission still grants its role ordinary row-level access -- |
| 104 | + * the column narrows what is returned, it does not withhold the row. Asserted |
| 105 | + * through a read rather than through the shape of the extracted permission list. |
100 | 106 | */ |
101 | | - public function testDocumentPermissionsByTypeReturnsRolesOnly(): void |
| 107 | + public function testColumnScopedGrantStillGrantsTheRowToThatRole(): void |
102 | 108 | { |
103 | | - $document = new Document(['$permissions' => [ |
104 | | - 'read("any")', |
105 | | - 'read("user:1", "salary")', |
106 | | - 'update("user:1", "name")', |
107 | | - 'delete("user:1")', |
108 | | - ]]); |
| 109 | + $authorization = new Authorization(); |
109 | 110 |
|
110 | | - $this->assertSame(['any', 'user:1'], \array_values($document->getRead())); |
111 | | - $this->assertSame(['user:1'], \array_values($document->getUpdate())); |
112 | | - $this->assertSame(['user:1'], \array_values($document->getDelete())); |
113 | | - } |
| 111 | + $database = new Database(new Memory(), new Cache(new NoCache())); |
| 112 | + $database |
| 113 | + ->setAuthorization($authorization) |
| 114 | + ->setDatabase('columnPermissions') |
| 115 | + ->setNamespace('cpt_' . \uniqid()); |
114 | 116 |
|
115 | | - public function testDocumentPermissionsByTypeWithColumns(): void |
116 | | - { |
117 | | - $document = new Document(['$permissions' => [ |
118 | | - 'read("any")', |
119 | | - 'read("user:1", "salary")', |
120 | | - ]]); |
| 117 | + $database->create(); |
| 118 | + |
| 119 | + $authorization->skip(function () use ($database) { |
| 120 | + $database->createCollection('employees', documentSecurity: true, columnSecurity: true, permissions: []); |
| 121 | + $database->createAttribute('employees', 'name', Database::VAR_STRING, 128, false); |
| 122 | + $database->createAttribute('employees', 'salary', Database::VAR_INTEGER, 8, false); |
| 123 | + |
| 124 | + $database->createDocument('employees', new Document([ |
| 125 | + '$id' => 'e1', |
| 126 | + '$permissions' => [Permission::read(Role::user('hr'), 'salary')], |
| 127 | + 'name' => 'Bob', |
| 128 | + 'salary' => 100000, |
| 129 | + ])); |
| 130 | + }); |
| 131 | + |
| 132 | + $authorization->cleanRoles(); |
| 133 | + $authorization->addRole('user:hr'); |
| 134 | + |
| 135 | + $document = $database->getDocument('employees', 'e1'); |
| 136 | + |
| 137 | + $this->assertFalse($document->isEmpty(), 'a column-scoped grant must still make the row visible'); |
| 138 | + $this->assertSame(100000, $document->getAttribute('salary')); |
| 139 | + $this->assertNull($document->getAttribute('name')); |
| 140 | + |
| 141 | + $authorization->cleanRoles(); |
| 142 | + $authorization->addRole('user:other'); |
121 | 143 |
|
122 | | - $this->assertSame([ |
123 | | - ['role' => 'any', 'column' => Permission::COLUMN_ALL], |
124 | | - ['role' => 'user:1', 'column' => 'salary'], |
125 | | - ], $document->getPermissionsByTypeWithColumns('read')); |
| 144 | + $this->assertTrue($database->getDocument('employees', 'e1')->isEmpty()); |
126 | 145 | } |
127 | 146 |
|
128 | 147 | public function testValidatorAcceptsColumnScopedReadCreateUpdate(): void |
|
0 commit comments