1
0

SetupTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Clark Tomlinson <fallen013@gmail.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Encryption\Tests\Users;
  27. use OCA\Encryption\Crypto\Crypt;
  28. use OCA\Encryption\KeyManager;
  29. use OCA\Encryption\Users\Setup;
  30. use Test\TestCase;
  31. class SetupTest extends TestCase {
  32. /**
  33. * @var \OCA\Encryption\KeyManager|\PHPUnit\Framework\MockObject\MockObject
  34. */
  35. private $keyManagerMock;
  36. /**
  37. * @var \OCA\Encryption\Crypto\Crypt|\PHPUnit\Framework\MockObject\MockObject
  38. */
  39. private $cryptMock;
  40. /**
  41. * @var Setup
  42. */
  43. private $instance;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->cryptMock = $this->getMockBuilder(Crypt::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->keyManagerMock = $this->getMockBuilder(KeyManager::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->instance = new Setup(
  53. $this->cryptMock,
  54. $this->keyManagerMock);
  55. }
  56. public function testSetupSystem() {
  57. $this->keyManagerMock->expects($this->once())->method('validateShareKey');
  58. $this->keyManagerMock->expects($this->once())->method('validateMasterKey');
  59. $this->instance->setupSystem();
  60. }
  61. /**
  62. * @dataProvider dataTestSetupUser
  63. *
  64. * @param bool $hasKeys
  65. * @param bool $expected
  66. */
  67. public function testSetupUser($hasKeys, $expected) {
  68. $this->keyManagerMock->expects($this->once())->method('userHasKeys')
  69. ->with('uid')->willReturn($hasKeys);
  70. if ($hasKeys) {
  71. $this->keyManagerMock->expects($this->never())->method('storeKeyPair');
  72. } else {
  73. $this->cryptMock->expects($this->once())->method('createKeyPair')->willReturn(['publicKey' => 'publicKey', 'privateKey' => 'privateKey']);
  74. $this->keyManagerMock->expects($this->once())->method('storeKeyPair')
  75. ->with('uid', 'password', ['publicKey' => 'publicKey', 'privateKey' => 'privateKey'])->willReturn(true);
  76. }
  77. $this->assertSame($expected,
  78. $this->instance->setupUser('uid', 'password')
  79. );
  80. }
  81. public function dataTestSetupUser() {
  82. return [
  83. [true, true],
  84. [false, true]
  85. ];
  86. }
  87. }