|
19 | 19 | use OCA\Libresign\Db\UserElementMapper; |
20 | 20 | use OCA\Libresign\Exception\LibresignException; |
21 | 21 | use OCA\Libresign\Helper\ValidateHelper; |
| 22 | +use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod; |
| 23 | +use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\ISignatureMethod; |
22 | 24 | use OCA\Libresign\Service\IdentifyMethodService; |
23 | 25 | use OCA\Libresign\Service\SignerElementsService; |
24 | 26 | use OCP\Files\IMimeTypeDetector; |
|
30 | 32 | use OCP\IUser; |
31 | 33 | use OCP\IUserManager; |
32 | 34 | use OCP\Security\IHasher; |
| 35 | +use PHPUnit\Framework\Attributes\DataProvider; |
33 | 36 | use PHPUnit\Framework\MockObject\MockObject; |
34 | 37 |
|
35 | 38 | final class ValidateHelperTest extends \OCA\Libresign\Tests\Unit\TestCase { |
@@ -621,4 +624,119 @@ public static function datavalidateIfIdentifyMethodExists(): array { |
621 | 624 | ['telegram', false], |
622 | 625 | ]; |
623 | 626 | } |
| 627 | + |
| 628 | + #[DataProvider('providerValidateSignersDataStructure')] |
| 629 | + public function testValidateSignersDataStructure(array $data, ?string $expectedException, ?string $expectedMessage): void { |
| 630 | + $validateHelper = $this->getValidateHelper(); |
| 631 | + |
| 632 | + if ($expectedException) { |
| 633 | + $this->expectException($expectedException); |
| 634 | + if ($expectedMessage) { |
| 635 | + $this->expectExceptionMessage($expectedMessage); |
| 636 | + } |
| 637 | + } |
| 638 | + |
| 639 | + // Use reflection to test private method |
| 640 | + $method = new \ReflectionMethod($validateHelper, 'validateSignersDataStructure'); |
| 641 | + $method->invoke($validateHelper, $data); |
| 642 | + |
| 643 | + if (!$expectedException) { |
| 644 | + $this->assertTrue(true); |
| 645 | + } |
| 646 | + } |
| 647 | + |
| 648 | + public static function providerValidateSignersDataStructure(): array { |
| 649 | + return [ |
| 650 | + 'Empty data' => [[], LibresignException::class, 'No signers'], |
| 651 | + 'No users key' => [['invalid' => 'data'], LibresignException::class, 'No signers'], |
| 652 | + 'Users is not array' => [['users' => 'invalid'], LibresignException::class, 'No signers'], |
| 653 | + 'Valid structure' => [['users' => []], null, null], |
| 654 | + ]; |
| 655 | + } |
| 656 | + |
| 657 | + #[DataProvider('providerValidateSignerData')] |
| 658 | + public function testValidateSignerData($signer, ?string $expectedException, ?string $expectedMessage): void { |
| 659 | + $validateHelper = $this->getValidateHelper(); |
| 660 | + |
| 661 | + if ($expectedException) { |
| 662 | + $this->expectException($expectedException); |
| 663 | + if ($expectedMessage) { |
| 664 | + $this->expectExceptionMessage($expectedMessage); |
| 665 | + } |
| 666 | + } |
| 667 | + |
| 668 | + $method = new \ReflectionMethod($validateHelper, 'validateSignerData'); |
| 669 | + $method->invoke($validateHelper, $signer); |
| 670 | + |
| 671 | + if (!$expectedException) { |
| 672 | + $this->assertTrue(true); |
| 673 | + } |
| 674 | + } |
| 675 | + |
| 676 | + public static function providerValidateSignerData(): array { |
| 677 | + return [ |
| 678 | + 'Signer is not array' => ['invalid', LibresignException::class, 'No signers'], |
| 679 | + 'Empty signer' => [[], LibresignException::class, 'No signers'], |
| 680 | + 'No identify methods' => [['name' => 'User'], LibresignException::class, 'No identify methods for signer'], |
| 681 | + 'Identify is not array' => [['identify' => 'invalid'], LibresignException::class, 'No identify methods for signer'], |
| 682 | + ]; |
| 683 | + } |
| 684 | + |
| 685 | + public function testValidateIdentifyMethodForRequestWithNoSignatureMethods(): void { |
| 686 | + $identifyMethod = $this->createMock(IIdentifyMethod::class); |
| 687 | + $identifyMethod->method('getSignatureMethods')->willReturn([]); |
| 688 | + $identifyMethod->method('validateToRequest'); |
| 689 | + |
| 690 | + $this->identifyMethodService |
| 691 | + ->method('getInstanceOfIdentifyMethod') |
| 692 | + ->willReturn($identifyMethod); |
| 693 | + |
| 694 | + $validateHelper = $this->getValidateHelper(); |
| 695 | + |
| 696 | + $this->expectException(LibresignException::class); |
| 697 | + $this->expectExceptionMessage('No signature methods for identify method account'); |
| 698 | + |
| 699 | + $method = new \ReflectionMethod($validateHelper, 'validateIdentifyMethodForRequest'); |
| 700 | + $method-> invoke( $validateHelper, 'account', '[email protected]'); |
| 701 | + } |
| 702 | + |
| 703 | + public function testValidateIdentifyMethodForRequestWithValidSignatureMethods(): void { |
| 704 | + $signatureMethod = $this->createMock(ISignatureMethod::class); |
| 705 | + $identifyMethod = $this->createMock(IIdentifyMethod::class); |
| 706 | + $identifyMethod->method('getSignatureMethods')->willReturn([$signatureMethod]); |
| 707 | + $identifyMethod->method('validateToRequest'); |
| 708 | + |
| 709 | + $this->identifyMethodService |
| 710 | + ->method('getInstanceOfIdentifyMethod') |
| 711 | + ->willReturn($identifyMethod); |
| 712 | + |
| 713 | + $validateHelper = $this->getValidateHelper(); |
| 714 | + |
| 715 | + $method = new \ReflectionMethod($validateHelper, 'validateIdentifyMethodForRequest'); |
| 716 | + $result = $method-> invoke( $validateHelper, 'account', '[email protected]'); |
| 717 | + |
| 718 | + $this->assertNull($result); |
| 719 | + } |
| 720 | + |
| 721 | + public function testValidateIdentifySignersIntegration(): void { |
| 722 | + $signatureMethod = $this->createMock(ISignatureMethod::class); |
| 723 | + $identifyMethod = $this->createMock(IIdentifyMethod::class); |
| 724 | + $identifyMethod->method('getSignatureMethods')->willReturn([$signatureMethod]); |
| 725 | + $identifyMethod->method('validateToRequest'); |
| 726 | + |
| 727 | + $this->identifyMethodService |
| 728 | + ->method('getInstanceOfIdentifyMethod') |
| 729 | + ->willReturn($identifyMethod); |
| 730 | + |
| 731 | + $data = [ |
| 732 | + 'users' => [ |
| 733 | + [ 'identify' => [ 'account' => '[email protected]']] |
| 734 | + ] |
| 735 | + ]; |
| 736 | + |
| 737 | + $validateHelper = $this->getValidateHelper(); |
| 738 | + $result = $validateHelper->validateIdentifySigners($data); |
| 739 | + |
| 740 | + $this->assertNull($result); |
| 741 | + } |
624 | 742 | } |
0 commit comments