public function testSetPassphrase() { $this->sessionMock->expects($this->exactly(4))->method('getPrivateKey')->willReturnOnConsecutiveCalls(true, false); $this->cryptMock->expects($this->exactly(4))->method('encryptPrivateKey')->willReturn(true); $this->cryptMock->expects($this->any())->method('generateHeader')->willReturn(Crypt::HEADER_START . ':Cipher:test:' . Crypt::HEADER_END); $this->keyManagerMock->expects($this->exactly(4))->method('setPrivateKey')->willReturnCallback(function ($user, $key) { $header = substr($key, 0, strlen(Crypt::HEADER_START)); $this->assertSame(Crypt::HEADER_START, $header, 'every encrypted file should start with a header'); }); $this->assertNull($this->instance->setPassphrase($this->params)); $this->params['recoveryPassword'] = '******'; $this->recoveryMock->expects($this->exactly(3))->method('isRecoveryEnabledForUser')->with('testUser')->willReturnOnConsecutiveCalls(true, false); $this->instance = $this->getMockBuilder('OCA\\Encryption\\Hooks\\UserHooks')->setConstructorArgs([$this->keyManagerMock, $this->userManagerMock, $this->loggerMock, $this->userSetupMock, $this->userSessionMock, $this->utilMock, $this->sessionMock, $this->cryptMock, $this->recoveryMock])->setMethods(['initMountPoints'])->getMock(); $this->instance->expects($this->exactly(3))->method('initMountPoints'); // Test first if statement $this->assertNull($this->instance->setPassphrase($this->params)); // Test Second if conditional $this->keyManagerMock->expects($this->exactly(2))->method('userHasKeys')->with('testUser')->willReturn(true); $this->assertNull($this->instance->setPassphrase($this->params)); // Test third and final if condition $this->utilMock->expects($this->once())->method('userHasFiles')->with('testUser')->willReturn(false); $this->cryptMock->expects($this->once())->method('createKeyPair'); $this->keyManagerMock->expects($this->once())->method('setPrivateKey'); $this->recoveryMock->expects($this->once())->method('recoverUsersFiles')->with('password', 'testUser'); $this->assertNull($this->instance->setPassphrase($this->params)); }
public function testSetPassphrase() { $this->sessionMock->expects($this->exactly(4))->method('getPrivateKey')->willReturnOnConsecutiveCalls(true, false); $this->cryptMock->expects($this->exactly(4))->method('symmetricEncryptFileContent')->willReturn(true); $this->keyManagerMock->expects($this->exactly(4))->method('setPrivateKey'); $this->assertNull($this->instance->setPassphrase($this->params)); $this->params['recoveryPassword'] = '******'; $this->recoveryMock->expects($this->exactly(3))->method('isRecoveryEnabledForUser')->with('testUser')->willReturnOnConsecutiveCalls(true, false); // Test first if statement $this->assertNull($this->instance->setPassphrase($this->params)); // Test Second if conditional $this->keyManagerMock->expects($this->exactly(2))->method('userHasKeys')->with('testUser')->willReturn(true); $this->assertNull($this->instance->setPassphrase($this->params)); // Test third and final if condition $this->utilMock->expects($this->once())->method('userHasFiles')->with('testUser')->willReturn(false); $this->cryptMock->expects($this->once())->method('createKeyPair'); $this->keyManagerMock->expects($this->once())->method('setPrivateKey'); $this->recoveryMock->expects($this->once())->method('recoverUsersFiles')->with('password', 'testUser'); $this->assertNull($this->instance->setPassphrase($this->params)); }
public function testSetPasswordNoUser() { $this->sessionMock->expects($this->once())->method('getPrivateKey')->willReturn(true); $userSessionMock = $this->getMockBuilder('OCP\\IUserSession')->disableOriginalConstructor()->getMock(); $userSessionMock->expects($this->any())->method('getUser')->will($this->returnValue(null)); $this->recoveryMock->expects($this->once())->method('isRecoveryEnabledForUser')->with('testUser')->willReturn(false); $userHooks = new UserHooks($this->keyManagerMock, $this->userManagerMock, $this->loggerMock, $this->userSetupMock, $userSessionMock, $this->utilMock, $this->sessionMock, $this->cryptMock, $this->recoveryMock); $this->assertNull($userHooks->setPassphrase($this->params)); }