aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorFerdinand Thiessen <opensource@fthiessen.de>2024-09-10 22:50:16 +0200
committerFerdinand Thiessen <opensource@fthiessen.de>2024-09-26 20:48:37 +0200
commit16833aff863290e4b298a2e69015d97cd230be47 (patch)
treefe2da4b61695b27ad56be71e335abc80ca97c9cf /tests
parentc8a907fc8c1cddad7de9e9e453ede52d392ee2bd (diff)
downloadnextcloud-server-16833aff863290e4b298a2e69015d97cd230be47.tar.gz
nextcloud-server-16833aff863290e4b298a2e69015d97cd230be47.zip
fix: Make user removal more resilient
Currently there is a problem if an exception is thrown in `User::delete`, because at that point the user is already removed from the backend, but not all data is deleted. There is no way to recover from this state, as the user is gone no information is available anymore. This means the data is still available on the server but can not removed by any API anymore. The solution here is to first set a flag and backup the user home, this can be used to recover failed user deletions in a way the delete can be re-tried. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/User/UserTest.php81
1 files changed, 67 insertions, 14 deletions
diff --git a/tests/lib/User/UserTest.php b/tests/lib/User/UserTest.php
index 55230b83667..e79aaadd593 100644
--- a/tests/lib/User/UserTest.php
+++ b/tests/lib/User/UserTest.php
@@ -513,14 +513,25 @@ class UserTest extends TestCase {
$test = $this;
/**
- * @var Backend | MockObject $backend
+ * @var UserInterface&MockObject $backend
*/
$backend = $this->createMock(\Test\Util\User\Dummy::class);
$backend->expects($this->once())
->method('deleteUser')
->willReturn($result);
+
+ $config = $this->createMock(IConfig::class);
+ $config->method('getSystemValue')
+ ->willReturnArgument(1);
+ $config->method('getSystemValueString')
+ ->willReturnArgument(1);
+ $config->method('getSystemValueBool')
+ ->willReturnArgument(1);
+ $config->method('getSystemValueInt')
+ ->willReturnArgument(1);
+
$emitter = new PublicEmitter();
- $user = new User('foo', $backend, $this->dispatcher, $emitter);
+ $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
/**
* @param User $user
@@ -533,21 +544,11 @@ class UserTest extends TestCase {
$emitter->listen('\OC\User', 'preDelete', $hook);
$emitter->listen('\OC\User', 'postDelete', $hook);
- $config = $this->createMock(IConfig::class);
$commentsManager = $this->createMock(ICommentsManager::class);
$notificationManager = $this->createMock(INotificationManager::class);
- $config->method('getSystemValue')
- ->willReturnArgument(1);
- $config->method('getSystemValueString')
- ->willReturnArgument(1);
- $config->method('getSystemValueBool')
- ->willReturnArgument(1);
- $config->method('getSystemValueInt')
- ->willReturnArgument(1);
-
if ($result) {
- $config->expects($this->once())
+ $config->expects($this->atLeastOnce())
->method('deleteAllUserValues')
->with('foo');
@@ -586,7 +587,6 @@ class UserTest extends TestCase {
$this->overwriteService(\OCP\Notification\IManager::class, $notificationManager);
$this->overwriteService(\OCP\Comments\ICommentsManager::class, $commentsManager);
- $this->overwriteService(AllConfig::class, $config);
$this->assertSame($result, $user->delete());
@@ -597,6 +597,59 @@ class UserTest extends TestCase {
$this->assertEquals($expectedHooks, $hooksCalled);
}
+ public function testDeleteRecoverState() {
+ $backend = $this->createMock(\Test\Util\User\Dummy::class);
+ $backend->expects($this->once())
+ ->method('deleteUser')
+ ->willReturn(true);
+
+ $config = $this->createMock(IConfig::class);
+ $config->method('getSystemValue')
+ ->willReturnArgument(1);
+ $config->method('getSystemValueString')
+ ->willReturnArgument(1);
+ $config->method('getSystemValueBool')
+ ->willReturnArgument(1);
+ $config->method('getSystemValueInt')
+ ->willReturnArgument(1);
+
+ $userConfig = [];
+ $config->expects(self::atLeast(2))
+ ->method('setUserValue')
+ ->willReturnCallback(function () {
+ $userConfig[] = func_get_args();
+ });
+
+ $commentsManager = $this->createMock(ICommentsManager::class);
+ $commentsManager->expects($this->once())
+ ->method('deleteReferencesOfActor')
+ ->willThrowException(new \Error('Test exception'));
+
+ $this->overwriteService(\OCP\Comments\ICommentsManager::class, $commentsManager);
+ $this->expectException(\Error::class);
+
+ $user = $this->getMockBuilder(User::class)
+ ->onlyMethods(['getHome'])
+ ->setConstructorArgs(['foo', $backend, $this->dispatcher, null, $config])
+ ->getMock();
+
+ $user->expects(self::atLeastOnce())
+ ->method('getHome')
+ ->willReturn('/home/path');
+
+ $user->delete();
+
+ $this->assertEqualsCanonicalizing(
+ [
+ ['foo', 'core', 'deleted', 'true', null],
+ ['foo', 'core', 'deleted.backup-home', '/home/path', null],
+ ],
+ $userConfig,
+ );
+
+ $this->restoreService(\OCP\Comments\ICommentsManager::class);
+ }
+
public function dataGetCloudId(): array {
return [
['https://localhost:8888/nextcloud', 'foo@localhost:8888/nextcloud'],