From ed8a98eaa1e44d172b838c5c9caa74261ac27eb1 Mon Sep 17 00:00:00 2001
From: Lukas Reschke <lukas@statuscode.ch>
Date: Thu, 17 Aug 2017 12:08:40 +0200
Subject: Prevent SQL error message in case of error

`\OC\User\Database::createUser` can throw a PHP exception in case the UID is longer than
permitted in the database. This is against it's PHPDocs and we should cast this to `false`,
so that the regular error handling triggers in.

The easiest way to reproduce is on MySQL:

1. Create user `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa` in admin panel
2. Create user `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa` in admin panel again
3. See SQL exception as error message

Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
---
 tests/lib/User/ManagerTest.php | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

(limited to 'tests/lib')

diff --git a/tests/lib/User/ManagerTest.php b/tests/lib/User/ManagerTest.php
index cf725aae671..9520cd640fd 100644
--- a/tests/lib/User/ManagerTest.php
+++ b/tests/lib/User/ManagerTest.php
@@ -9,6 +9,7 @@
 
 namespace Test\User;
 use OC\User\Database;
+use OC\User\Manager;
 use OCP\IConfig;
 use OCP\IUser;
 use Test\TestCase;
@@ -304,7 +305,6 @@ class ManagerTest extends TestCase {
 
 		$this->setExpectedException(\InvalidArgumentException::class, $exception);
 		$manager->createUser($uid, $password);
-
 	}
 
 	public function testCreateUserSingleBackendNotExists() {
@@ -385,6 +385,25 @@ class ManagerTest extends TestCase {
 		$this->assertFalse($manager->createUser('foo', 'bar'));
 	}
 
+	/**
+	 * @expectedException \InvalidArgumentException
+	 * @expectedExceptionMessage Could not create user
+	 */
+	public function testCreateUserFromBackendWithBackendError() {
+		/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject $config */
+		$config = $this->createMock(IConfig::class);
+		/** @var \Test\Util\User\Dummy|\PHPUnit_Framework_MockObject_MockObject $backend */
+		$backend = $this->createMock(\Test\Util\User\Dummy::class);
+		$backend
+			->expects($this->once())
+			->method('createUser')
+			->with('MyUid', 'MyPassword')
+			->willReturn(false);
+
+		$manager = new Manager($config);
+		$manager->createUserFromBackend('MyUid', 'MyPassword', $backend);
+	}
+
 	/**
 	 * @expectedException \Exception
 	 */
-- 
cgit v1.2.3