forked from nextcloud/server
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(admin-delegation): Prevent delegation to group if delegation alre… #144
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\Settings\Service; | ||
|
|
||
| class ConflictException extends ServiceException { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
apps/settings/tests/Integration/DuplicateAssignmentIntegrationTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 STRATO GmbH | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\Settings\Tests\Integration; | ||
|
|
||
| use OC\Settings\AuthorizedGroup; | ||
| use OC\Settings\AuthorizedGroupMapper; | ||
| use OCA\Settings\Service\AuthorizedGroupService; | ||
| use OCA\Settings\Service\ConflictException; | ||
| use OCP\AppFramework\Db\DoesNotExistException; | ||
| use Test\TestCase; | ||
|
|
||
| /** | ||
| * Integration test for duplicate prevention in AuthorizedGroupService | ||
| * This test verifies the complete flow of duplicate detection and prevention | ||
| */ | ||
| class DuplicateAssignmentIntegrationTest extends TestCase { | ||
|
|
||
| private AuthorizedGroupService $service; | ||
| private AuthorizedGroupMapper $mapper; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
|
|
||
| // Use real mapper for integration testing | ||
| $this->mapper = \OC::$server->get(AuthorizedGroupMapper::class); | ||
| $this->service = new AuthorizedGroupService($this->mapper); | ||
| } | ||
|
|
||
| protected function tearDown(): void { | ||
| // Clean up any test data | ||
| try { | ||
| $allGroups = $this->mapper->findAll(); | ||
| foreach ($allGroups as $group) { | ||
| if (str_starts_with($group->getGroupId(), 'test_') || | ||
| str_starts_with($group->getClass(), 'TestClass')) { | ||
| $this->mapper->delete($group); | ||
| } | ||
| } | ||
| } catch (\Exception $e) { | ||
| // Ignore cleanup errors | ||
| } | ||
| parent::tearDown(); | ||
| } | ||
|
|
||
| public function testDuplicateAssignmentPrevention(): void { | ||
| $groupId = 'test_duplicate_group'; | ||
| $class = 'TestClass\\DuplicateTest'; | ||
|
|
||
| // First assignment should succeed | ||
| $result1 = $this->service->create($groupId, $class); | ||
| $this->assertInstanceOf(AuthorizedGroup::class, $result1); | ||
| $this->assertEquals($groupId, $result1->getGroupId()); | ||
| $this->assertEquals($class, $result1->getClass()); | ||
| $this->assertNotNull($result1->getId()); | ||
|
|
||
| // Second assignment of same group to same class should throw ConflictException | ||
| $this->expectException(ConflictException::class); | ||
| $this->expectExceptionMessage('Group is already assigned to this class'); | ||
|
|
||
| $this->service->create($groupId, $class); | ||
| } | ||
|
|
||
| public function testDifferentGroupsSameClassAllowed(): void { | ||
| $groupId1 = 'test_group_1'; | ||
| $groupId2 = 'test_group_2'; | ||
| $class = 'TestClass\\MultiGroup'; | ||
|
|
||
| // Both assignments should succeed | ||
| $result1 = $this->service->create($groupId1, $class); | ||
| $result2 = $this->service->create($groupId2, $class); | ||
|
|
||
| $this->assertEquals($groupId1, $result1->getGroupId()); | ||
| $this->assertEquals($groupId2, $result2->getGroupId()); | ||
| $this->assertEquals($class, $result1->getClass()); | ||
| $this->assertEquals($class, $result2->getClass()); | ||
| $this->assertNotEquals($result1->getId(), $result2->getId()); | ||
| } | ||
|
|
||
| public function testSameGroupDifferentClassesAllowed(): void { | ||
| $groupId = 'test_multi_class_group'; | ||
| $class1 = 'TestClass\\First'; | ||
| $class2 = 'TestClass\\Second'; | ||
|
|
||
| // Both assignments should succeed | ||
| $result1 = $this->service->create($groupId, $class1); | ||
| $result2 = $this->service->create($groupId, $class2); | ||
|
|
||
| $this->assertEquals($groupId, $result1->getGroupId()); | ||
| $this->assertEquals($groupId, $result2->getGroupId()); | ||
| $this->assertEquals($class1, $result1->getClass()); | ||
| $this->assertEquals($class2, $result2->getClass()); | ||
| $this->assertNotEquals($result1->getId(), $result2->getId()); | ||
| } | ||
|
|
||
| public function testCreateAfterDelete(): void { | ||
| $groupId = 'test_recreate_group'; | ||
| $class = 'TestClass\\Recreate'; | ||
|
|
||
| // Create initial assignment | ||
| $result1 = $this->service->create($groupId, $class); | ||
| $initialId = $result1->getId(); | ||
|
|
||
| // Delete the assignment | ||
| $this->service->delete($initialId); | ||
|
|
||
| // Verify it's deleted by trying to find it | ||
| $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class); | ||
| try { | ||
| $this->service->find($initialId); | ||
| } catch (\OCA\Settings\Service\NotFoundException $e) { | ||
| // Expected - now create the same assignment again, which should succeed | ||
| $result2 = $this->service->create($groupId, $class); | ||
|
|
||
| $this->assertEquals($groupId, $result2->getGroupId()); | ||
| $this->assertEquals($class, $result2->getClass()); | ||
| $this->assertNotEquals($initialId, $result2->getId()); | ||
| return; | ||
| } | ||
|
|
||
| $this->fail('Expected NotFoundException when finding deleted group'); | ||
| } | ||
|
|
||
| /** | ||
| * Test the mapper's findByGroupIdAndClass method behavior with duplicates | ||
| */ | ||
| public function testMapperFindByGroupIdAndClassBehavior(): void { | ||
| $groupId = 'test_mapper_group'; | ||
| $class = 'TestClass\\MapperTest'; | ||
|
|
||
| // Initially should throw DoesNotExistException | ||
| $this->expectException(DoesNotExistException::class); | ||
| $this->mapper->findByGroupIdAndClass($groupId, $class); | ||
| } | ||
|
|
||
| /** | ||
| * Test that mapper returns existing record after creation | ||
| */ | ||
| public function testMapperFindsExistingRecord(): void { | ||
| $groupId = 'test_existing_group'; | ||
| $class = 'TestClass\\Existing'; | ||
|
|
||
| // Create the record first | ||
| $created = $this->service->create($groupId, $class); | ||
|
|
||
| // Now mapper should find it | ||
| $found = $this->mapper->findByGroupIdAndClass($groupId, $class); | ||
|
|
||
| $this->assertEquals($created->getId(), $found->getId()); | ||
| $this->assertEquals($groupId, $found->getGroupId()); | ||
| $this->assertEquals($class, $found->getClass()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test expects
DoesNotExistExceptionbut catchesNotFoundException. This mismatch will cause the test to fail ifDoesNotExistExceptionis thrown. Either remove theexpectExceptioncall since you're using try-catch, or catch the expected exception type.