SetupTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Encryption\Tests\Users;
  8. use OCA\Encryption\Crypto\Crypt;
  9. use OCA\Encryption\KeyManager;
  10. use OCA\Encryption\Users\Setup;
  11. use Test\TestCase;
  12. class SetupTest extends TestCase {
  13. /**
  14. * @var \OCA\Encryption\KeyManager|\PHPUnit\Framework\MockObject\MockObject
  15. */
  16. private $keyManagerMock;
  17. /**
  18. * @var \OCA\Encryption\Crypto\Crypt|\PHPUnit\Framework\MockObject\MockObject
  19. */
  20. private $cryptMock;
  21. /**
  22. * @var Setup
  23. */
  24. private $instance;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->cryptMock = $this->getMockBuilder(Crypt::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $this->keyManagerMock = $this->getMockBuilder(KeyManager::class)
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $this->instance = new Setup(
  34. $this->cryptMock,
  35. $this->keyManagerMock);
  36. }
  37. public function testSetupSystem() {
  38. $this->keyManagerMock->expects($this->once())->method('validateShareKey');
  39. $this->keyManagerMock->expects($this->once())->method('validateMasterKey');
  40. $this->instance->setupSystem();
  41. }
  42. /**
  43. * @dataProvider dataTestSetupUser
  44. *
  45. * @param bool $hasKeys
  46. * @param bool $expected
  47. */
  48. public function testSetupUser($hasKeys, $expected) {
  49. $this->keyManagerMock->expects($this->once())->method('userHasKeys')
  50. ->with('uid')->willReturn($hasKeys);
  51. if ($hasKeys) {
  52. $this->keyManagerMock->expects($this->never())->method('storeKeyPair');
  53. } else {
  54. $this->cryptMock->expects($this->once())->method('createKeyPair')->willReturn(['publicKey' => 'publicKey', 'privateKey' => 'privateKey']);
  55. $this->keyManagerMock->expects($this->once())->method('storeKeyPair')
  56. ->with('uid', 'password', ['publicKey' => 'publicKey', 'privateKey' => 'privateKey'])->willReturn(true);
  57. }
  58. $this->assertSame($expected,
  59. $this->instance->setupUser('uid', 'password')
  60. );
  61. }
  62. public function dataTestSetupUser() {
  63. return [
  64. [true, true],
  65. [false, true]
  66. ];
  67. }
  68. }