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-10-08 21:02:07 +0200
commit7dd85e15b98c1f5a12edad1d441ad2575f18c90e (patch)
tree14d0a9437e82a396bb7271e92ac7b98d05b2e1db /tests
parent3b51990d5b077f4a34e920959f7d1ea1f4fa52f8 (diff)
downloadnextcloud-server-7dd85e15b98c1f5a12edad1d441ad2575f18c90e.tar.gz
nextcloud-server-7dd85e15b98c1f5a12edad1d441ad2575f18c90e.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 8e62c06e1ba..fbbf4366c05 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'],