From 36326e38a08a4269416b6defe460e2a65609ad9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Thu, 19 Apr 2018 00:12:06 +0200 Subject: [PATCH] Add optional "displayName" parameter to add user endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Calviño Sánchez --- .../lib/Controller/UsersController.php | 6 ++ .../tests/Controller/UsersControllerTest.php | 71 +++++++++++++++++-- 2 files changed, 70 insertions(+), 7 deletions(-) diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php index 52021ec248..d5e568b918 100644 --- a/apps/provisioning_api/lib/Controller/UsersController.php +++ b/apps/provisioning_api/lib/Controller/UsersController.php @@ -199,6 +199,7 @@ class UsersController extends AUserData { * * @param string $userid * @param string $password + * @param string $displayName * @param string $email * @param array $groups * @param array $subadmins @@ -209,6 +210,7 @@ class UsersController extends AUserData { */ public function addUser(string $userid, string $password = '', + string $displayName = '', string $email = '', array $groups = [], array $subadmin = [], @@ -282,6 +284,10 @@ class UsersController extends AUserData { $subAdminManager->createSubAdmin($newUser, $group); } + if ($displayName !== '') { + $this->editUser($userid, 'display', $displayName); + } + if ($quota !== '') { $this->editUser($userid, 'quota', $quota); } diff --git a/apps/provisioning_api/tests/Controller/UsersControllerTest.php b/apps/provisioning_api/tests/Controller/UsersControllerTest.php index af4d5958b5..adbdb9d868 100644 --- a/apps/provisioning_api/tests/Controller/UsersControllerTest.php +++ b/apps/provisioning_api/tests/Controller/UsersControllerTest.php @@ -247,7 +247,7 @@ class UsersControllerTest extends TestCase { ->with('adminUser') ->willReturn(true); - $this->api->addUser('AlreadyExistingUser', 'password', '', []); + $this->api->addUser('AlreadyExistingUser', 'password', '', '', []); } /** @@ -283,7 +283,7 @@ class UsersControllerTest extends TestCase { ->with('NonExistingGroup') ->willReturn(false); - $this->api->addUser('NewUser', 'pass', '', ['NonExistingGroup']); + $this->api->addUser('NewUser', 'pass', '', '', ['NonExistingGroup']); } /** @@ -325,7 +325,7 @@ class UsersControllerTest extends TestCase { ['NonExistingGroup', false] ])); - $this->api->addUser('NewUser', 'pass', '', ['ExistingGroup', 'NonExistingGroup']); + $this->api->addUser('NewUser', 'pass', '', '', ['ExistingGroup', 'NonExistingGroup']); } public function testAddUserSuccessful() { @@ -362,6 +362,63 @@ class UsersControllerTest extends TestCase { $this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser')->getData()); } + public function testAddUserSuccessfulWithDisplayName() { + $api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController') + ->setConstructorArgs([ + 'provisioning_api', + $this->request, + $this->userManager, + $this->config, + $this->appManager, + $this->groupManager, + $this->userSession, + $this->accountManager, + $this->logger, + $this->l10nFactory, + $this->newUserMailHelper, + $this->federatedFileSharingFactory, + $this->secureRandom + ]) + ->setMethods(['editUser']) + ->getMock(); + + $this->userManager + ->expects($this->once()) + ->method('userExists') + ->with('NewUser') + ->will($this->returnValue(false)); + $this->userManager + ->expects($this->once()) + ->method('createUser') + ->with('NewUser', 'PasswordOfTheNewUser'); + $this->logger + ->expects($this->once()) + ->method('info') + ->with('Successful addUser call with userid: NewUser', ['app' => 'ocs_api']); + $loggedInUser = $this->getMockBuilder(IUser::class) + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('adminUser')); + $this->userSession + ->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('adminUser') + ->willReturn(true); + $api + ->expects($this->once()) + ->method('editUser') + ->with('NewUser', 'display', 'DisplayNameOfTheNewUser'); + + $this->assertEquals([], $api->addUser('NewUser', 'PasswordOfTheNewUser', 'DisplayNameOfTheNewUser')->getData()); + } + public function testAddUserExistingGroup() { $this->userManager ->expects($this->once()) @@ -417,7 +474,7 @@ class UsersControllerTest extends TestCase { ['Added userid NewUser to group ExistingGroup', ['app' => 'ocs_api']] ); - $this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', ['ExistingGroup'])->getData()); + $this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', ['ExistingGroup'])->getData()); } /** @@ -495,7 +552,7 @@ class UsersControllerTest extends TestCase { ->with() ->willReturn($subAdminManager); - $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', []); + $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', []); } /** @@ -544,7 +601,7 @@ class UsersControllerTest extends TestCase { ->with('ExistingGroup') ->willReturn(true); - $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', ['ExistingGroup'])->getData(); + $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', ['ExistingGroup'])->getData(); } public function testAddUserAsSubAdminExistingGroups() { @@ -635,7 +692,7 @@ class UsersControllerTest extends TestCase { ) ->willReturn(true); - $this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', ['ExistingGroup1', 'ExistingGroup2'])->getData()); + $this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', ['ExistingGroup1', 'ExistingGroup2'])->getData()); } /**