diff --git a/lib/SP/Repositories/UserProfile/UserProfileRepository.php b/lib/SP/Repositories/UserProfile/UserProfileRepository.php index bcd8c605..e1e2685d 100644 --- a/lib/SP/Repositories/UserProfile/UserProfileRepository.php +++ b/lib/SP/Repositories/UserProfile/UserProfileRepository.php @@ -49,7 +49,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac * * @param $id int El id del perfil * - * @return array + * @return QueryResult * @throws ConstraintException * @throws QueryException */ @@ -59,7 +59,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery('SELECT login FROM User WHERE userProfileId = ?'); $queryData->addParam($id); - return $this->db->doSelect($queryData)->getDataAsArray(); + return $this->db->doSelect($queryData); } /** @@ -104,7 +104,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac * * @param int $id * - * @return UserProfileData + * @return QueryResult * @throws ConstraintException * @throws QueryException */ @@ -115,13 +115,13 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery('SELECT id, `name`, `profile` FROM UserProfile WHERE id = ? LIMIT 1'); $queryData->addParam($id); - return $this->db->doSelect($queryData)->getData(); + return $this->db->doSelect($queryData); } /** * Returns all the items * - * @return UserProfileData[] + * @return QueryResult * @throws ConstraintException * @throws QueryException */ @@ -131,7 +131,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac $queryData->setMapClassName(UserProfileData::class); $queryData->setQuery('SELECT id, `name` FROM UserProfile ORDER BY `name`'); - return $this->db->doSelect($queryData)->getDataAsArray(); + return $this->db->doSelect($queryData); } /** diff --git a/lib/SP/Services/UserPassRecover/UserPassRecoverService.php b/lib/SP/Services/UserPassRecover/UserPassRecoverService.php index cbaf9894..cac3d680 100644 --- a/lib/SP/Services/UserPassRecover/UserPassRecoverService.php +++ b/lib/SP/Services/UserPassRecover/UserPassRecoverService.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -56,6 +56,7 @@ class UserPassRecoverService extends Service /** * @param $hash + * * @return MailMessage */ public static function getMailMessage($hash) @@ -75,6 +76,7 @@ class UserPassRecoverService extends Service /** * @param $hash + * * @return void * @throws ServiceException * @throws \SP\Core\Exceptions\SPException @@ -88,6 +90,7 @@ class UserPassRecoverService extends Service /** * @param int $id + * * @return string * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -111,6 +114,7 @@ class UserPassRecoverService extends Service * Comprobar el límite de recuperaciones de clave. * * @param int $userId + * * @return bool * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -123,6 +127,7 @@ class UserPassRecoverService extends Service /** * @param $userId * @param $hash + * * @return bool * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException diff --git a/lib/SP/Services/UserProfile/UserProfileService.php b/lib/SP/Services/UserProfile/UserProfileService.php index 94c4faa8..29d7f26f 100644 --- a/lib/SP/Services/UserProfile/UserProfileService.php +++ b/lib/SP/Services/UserProfile/UserProfileService.php @@ -28,6 +28,7 @@ use SP\Core\Exceptions\SPException; use SP\DataModel\ItemSearchData; use SP\DataModel\ProfileData; use SP\DataModel\UserProfileData; +use SP\Repositories\NoSuchItemException; use SP\Repositories\UserProfile\UserProfileRepository; use SP\Services\Service; use SP\Services\ServiceException; @@ -54,10 +55,17 @@ class UserProfileService extends Service * @return UserProfileData * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException + * @throws NoSuchItemException */ public function getById($id) { - $userProfileData = $this->userProfileRepository->getById($id); + $result = $this->userProfileRepository->getById($id); + + if ($result->getNumRows() === 0) { + throw new NoSuchItemException(__u('Perfil no encontrado')); + } + + $userProfileData = $result->getData(); $userProfileData->setProfile(Util::unserialize(ProfileData::class, $userProfileData->getProfile())); return $userProfileData; @@ -79,12 +87,14 @@ class UserProfileService extends Service * @param $id * * @return $this - * @throws SPException + * @throws NoSuchItemException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function delete($id) { if ($this->userProfileRepository->delete($id) === 0) { - throw new ServiceException(__u('Perfil no encontrado'), ServiceException::INFO); + throw new NoSuchItemException(__u('Perfil no encontrado'), NoSuchItemException::INFO); } return $this; @@ -121,14 +131,15 @@ class UserProfileService extends Service /** * @param $itemData * - * @return bool * @throws SPException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function update($itemData) { - return $this->userProfileRepository->update($itemData); + if ($this->userProfileRepository->update($itemData) === 0) { + throw new ServiceException(__u('Error al modificar perfil')); + } } /** @@ -140,19 +151,19 @@ class UserProfileService extends Service */ public function getUsersForProfile($id) { - return $this->userProfileRepository->getUsersForProfile($id); + return $this->userProfileRepository->getUsersForProfile($id)->getDataAsArray(); } /** * Get all items from the service's repository * - * @return array + * @return UserProfileData[] * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function getAllBasic() { - return $this->userProfileRepository->getAll(); + return $this->userProfileRepository->getAll()->getDataAsArray(); } /** diff --git a/phpunit.xml b/phpunit.xml index d6f87e8d..912d6120 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -20,5 +20,8 @@ + + \ No newline at end of file diff --git a/test/SP/Repositories/UserProfileRepositoryTest.php b/test/SP/Repositories/UserProfileRepositoryTest.php index fb632053..3096ff47 100644 --- a/test/SP/Repositories/UserProfileRepositoryTest.php +++ b/test/SP/Repositories/UserProfileRepositoryTest.php @@ -76,13 +76,20 @@ class UserProfileRepositoryTest extends DatabaseTestCase */ public function testGetAll() { - $profiles = self::$repository->getAll(); + $result = self::$repository->getAll(); - $this->assertCount(3, $profiles); - $this->assertInstanceOf(UserProfileData::class, $profiles[0]); - $this->assertEquals('Admin', $profiles[0]->getName()); - $this->assertInstanceOf(UserProfileData::class, $profiles[1]); - $this->assertEquals('Demo', $profiles[1]->getName()); + $this->assertEquals(3, $result->getNumRows()); + + /** @var UserProfileData[] $data */ + $data = $result->getDataAsArray(); + + $this->assertCount(3, $data); + + $this->assertInstanceOf(UserProfileData::class, $data[0]); + $this->assertEquals('Admin', $data[0]->getName()); + + $this->assertInstanceOf(UserProfileData::class, $data[1]); + $this->assertEquals('Demo', $data[1]->getName()); } /** @@ -124,17 +131,17 @@ class UserProfileRepositoryTest extends DatabaseTestCase */ public function testUpdate() { - $userProfileData = new UserProfileData(); - $userProfileData->setId(2); - $userProfileData->setName('Perfil Demo'); + $data = new UserProfileData(); + $data->setId(2); + $data->setName('Test Profile Demo'); - $this->assertEquals(1, self::$repository->update($userProfileData)); + $this->assertEquals(1, self::$repository->update($data)); $this->expectException(DuplicatedItemException::class); - $userProfileData->setName('Admin'); + $data->setName('Admin'); - self::$repository->update($userProfileData); + self::$repository->update($data); } /** @@ -153,7 +160,6 @@ class UserProfileRepositoryTest extends DatabaseTestCase $this->expectException(ConstraintException::class); self::$repository->delete(1); - self::$repository->delete(2); } /** @@ -183,20 +189,36 @@ class UserProfileRepositoryTest extends DatabaseTestCase $profileData->setAccDelete(true); $profileData->setConfigBackup(true); - $userProfileData = new UserProfileData(); - $userProfileData->setName('Prueba'); - $userProfileData->setProfile($profileData); + $data = new UserProfileData(); + $data->setId(4); + $data->setName('Prueba'); + $data->setProfile($profileData); - $result = self::$repository->create($userProfileData); + $result = self::$repository->create($data); - $this->assertEquals(4, $result); + $this->assertEquals($data->getId(), $result); $this->assertEquals(4, $this->conn->getRowCount('UserProfile')); + /** @var UserProfileData $resultData */ + $resultData = self::$repository->getById($result)->getData(); + + $this->assertEquals($data->getId(), $resultData->getId()); + $this->assertEquals($data->getName(), $resultData->getName()); + $this->assertEquals(serialize($data->getProfile()), $resultData->getProfile()); + } + + /** + * @throws \SP\Core\Exceptions\SPException + */ + public function testCreateDuplicated() + { + $data = new UserProfileData(); + $data->setName('Admin'); + $data->setProfile(new ProfileData()); + $this->expectException(DuplicatedItemException::class); - $userProfileData->setName('Demo'); - - self::$repository->create($userProfileData); + self::$repository->create($data); } /** @@ -207,13 +229,18 @@ class UserProfileRepositoryTest extends DatabaseTestCase */ public function testGetById() { - $profile = self::$repository->getById(2); + $result = self::$repository->getById(2); - $this->assertInstanceOf(UserProfileData::class, $profile); - $this->assertEquals('Demo', $profile->getName()); - $this->assertNotEmpty($profile->getProfile()); + $this->assertEquals(1, $result->getNumRows()); - $this->assertNull(self::$repository->getById(4)); + /** @var UserProfileData $data */ + $data = $result->getData(); + + $this->assertInstanceOf(UserProfileData::class, $data); + $this->assertEquals('Demo', $data->getName()); + $this->assertNotEmpty($data->getProfile()); + + $this->assertEquals(0, self::$repository->getById(4)->getNumRows()); } /** @@ -224,8 +251,9 @@ class UserProfileRepositoryTest extends DatabaseTestCase */ public function testGetUsersForProfile() { - $this->assertCount(1, self::$repository->getUsersForProfile(2)); - $this->assertCount(0, self::$repository->getUsersForProfile(3)); + $this->assertEquals(1, self::$repository->getUsersForProfile(2)->getNumRows()); + + $this->assertEquals(0, self::$repository->getUsersForProfile(3)->getNumRows()); } /** diff --git a/test/SP/Services/UserPassRecover/UserPassRecoverServiceTest.php b/test/SP/Services/UserPassRecover/UserPassRecoverServiceTest.php new file mode 100644 index 00000000..d531e165 --- /dev/null +++ b/test/SP/Services/UserPassRecover/UserPassRecoverServiceTest.php @@ -0,0 +1,161 @@ +. + */ + +namespace SP\Tests\SP\Services\UserPassRecover; + +use SP\Core\Exceptions\ConstraintException; +use SP\Services\ServiceException; +use SP\Services\UserPassRecover\UserPassRecoverService; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Test\DatabaseTestCase; +use SP\Util\Util; +use function SP\Test\setupContext; + +/** + * Class UserPassRecoverServiceTest + * + * @package SP\Tests\SP\Services\UserPassRecover + */ +class UserPassRecoverServiceTest extends DatabaseTestCase +{ + /** + * @var UserPassRecoverService + */ + private static $service; + + /** + * @throws \DI\NotFoundException + * @throws \SP\Core\Context\ContextException + * @throws \DI\DependencyException + * @throws \SP\Core\Exceptions\SPException + */ + public static function setUpBeforeClass() + { + $dic = setupContext(); + + self::$dataset = 'syspass.xml'; + + // Datos de conexión a la BBDD + self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); + + // Inicializar el servicio + self::$service = $dic->get(UserPassRecoverService::class); + } + + /** + * @throws ConstraintException + * @throws ServiceException + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\SPException + */ + public function testToggleUsedByHash() + { + self::$service->toggleUsedByHash(self::$service->requestForUserId(2)); + + $this->expectException(ServiceException::class); + + self::$service->toggleUsedByHash(Util::generateRandomBytes()); + } + + /** + * @throws ServiceException + * @throws \SP\Core\Exceptions\SPException + */ + public function testToggleUsedByHashExpired () + { + $this->expectException(ServiceException::class); + + self::$service->toggleUsedByHash(pack('H*', '3038366162313036303866363838346566383031396134353237333561633066')); + } + + /** + * @throws ConstraintException + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testAdd() + { + $this->assertEquals(3, self::$service->add(2, Util::generateRandomBytes())); + + $this->expectException(ConstraintException::class); + + self::$service->add(10, Util::generateRandomBytes()); + } + + /** + * @throws ConstraintException + * @throws ServiceException + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testRequestForUserId() + { + $hash = self::$service->requestForUserId(2); + + $this->assertNotEmpty($hash); + + $this->assertEquals(2, self::$service->getUserIdForHash($hash)); + + $this->expectException(ConstraintException::class); + + self::$service->requestForUserId(10); + } + + /** + * @throws ConstraintException + * @throws ServiceException + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testCheckAttemptsByUserId() + { + $this->assertFalse(self::$service->checkAttemptsByUserId(2)); + + for ($i = 1; $i <= UserPassRecoverService::MAX_PASS_RECOVER_LIMIT; $i++) { + self::$service->requestForUserId(2); + } + + $this->assertTrue(self::$service->checkAttemptsByUserId(2)); + + $this->assertFalse(self::$service->checkAttemptsByUserId(10)); + } + + /** + * @throws ConstraintException + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Services\ServiceException + */ + public function testGetUserIdForHash() + { + $result = self::$service->getUserIdForHash(self::$service->requestForUserId(2)); + + $this->assertEquals(2, $result); + + $this->expectException(ServiceException::class); + + self::$service->getUserIdForHash(Util::generateRandomBytes()); + } +} diff --git a/test/SP/Services/UserProfile/UserProfileServiceTest.php b/test/SP/Services/UserProfile/UserProfileServiceTest.php new file mode 100644 index 00000000..19ae2fcc --- /dev/null +++ b/test/SP/Services/UserProfile/UserProfileServiceTest.php @@ -0,0 +1,286 @@ +. + */ + +namespace SP\Tests\SP\Services\UserProfile; + +use SP\Core\Exceptions\ConstraintException; +use SP\DataModel\ItemSearchData; +use SP\DataModel\ProfileData; +use SP\DataModel\UserProfileData; +use SP\Repositories\DuplicatedItemException; +use SP\Repositories\NoSuchItemException; +use SP\Services\ServiceException; +use SP\Services\UserProfile\UserProfileService; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Test\DatabaseTestCase; +use function SP\Test\setupContext; + +/** + * Class UserProfileServiceTest + * + * @package SP\Tests\SP\Services\UserProfile + */ +class UserProfileServiceTest extends DatabaseTestCase +{ + /** + * @var UserProfileService + */ + private static $service; + + /** + * @throws \DI\NotFoundException + * @throws \SP\Core\Context\ContextException + * @throws \DI\DependencyException + * @throws \SP\Core\Exceptions\SPException + */ + public static function setUpBeforeClass() + { + $dic = setupContext(); + + self::$dataset = 'syspass.xml'; + + // Datos de conexión a la BBDD + self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); + + // Inicializar el servicio + self::$service = $dic->get(UserProfileService::class); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testSearch() + { + $itemSearchData = new ItemSearchData(); + $itemSearchData->setLimitCount(10); + $itemSearchData->setSeachString('Demo'); + + $result = self::$service->search($itemSearchData); + $data = $result->getDataAsArray(); + + $this->assertEquals(1, $result->getNumRows()); + $this->assertCount(1, $data); + $this->assertInstanceOf(\stdClass::class, $data[0]); + $this->assertEquals(2, $data[0]->id); + $this->assertEquals('Demo', $data[0]->name); + + // Nueva búsqueda de perfil no existente + $itemSearchData->setSeachString('prueba'); + + $result = self::$service->search($itemSearchData); + + $this->assertEquals(0, $result->getNumRows()); + $this->assertCount(0, $result->getDataAsArray()); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetAllBasic() + { + $data = self::$service->getAllBasic(); + + $this->assertCount(3, $data); + + $this->assertInstanceOf(UserProfileData::class, $data[0]); + $this->assertEquals('Admin', $data[0]->getName()); + + $this->assertInstanceOf(UserProfileData::class, $data[1]); + $this->assertEquals('Demo', $data[1]->getName()); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetUsersForProfile() + { + $this->assertCount(1, self::$service->getUsersForProfile(2)); + + $this->assertCount(0, self::$service->getUsersForProfile(3)); + + $this->assertCount(0, self::$service->getUsersForProfile(10)); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\SPException + */ + public function testUpdate() + { + $data = new UserProfileData(); + $data->setId(2); + $data->setName('Test Profile'); + + self::$service->update($data); + + $this->assertTrue(true); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\SPException + */ + public function testUpdateUnknown() + { + $data = new UserProfileData(); + $data->setId(10); + $data->setName('Test Profile'); + + $this->expectException(ServiceException::class); + + self::$service->update($data); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\SPException + */ + public function testUpdateDuplicated() + { + $data = new UserProfileData(); + $data->setId(2); + $data->setName('Admin'); + + $this->expectException(DuplicatedItemException::class); + + self::$service->update($data); + } + + /** + * @throws ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Services\ServiceException + */ + public function testDeleteByIdBatch() + { + $this->assertEquals(1, self::$service->deleteByIdBatch([3])); + + $this->assertEquals(2, $this->conn->getRowCount('UserProfile')); + } + + /** + * @throws ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Services\ServiceException + */ + public function testDeleteByIdBatchUsed() + { + $this->expectException(ConstraintException::class); + + self::$service->deleteByIdBatch([1, 2]); + + $this->assertEquals(3, $this->conn->getRowCount('UserProfile')); + } + + /** + * @throws ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Services\ServiceException + */ + public function testDeleteByIdBatchUnknown() + { + $this->expectException(ServiceException::class); + + self::$service->deleteByIdBatch([3, 10]); + + $this->assertEquals(2, $this->conn->getRowCount('UserProfile')); + } + + /** + * @throws ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws NoSuchItemException + */ + public function testGetById() + { + $result = self::$service->getById(2); + + $this->assertInstanceOf(UserProfileData::class, $result); + $this->assertInstanceOf(ProfileData::class, $result->getProfile()); + + $this->expectException(NoSuchItemException::class); + + self::$service->getById(10); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testDelete() + { + self::$service->delete(3); + + $this->assertEquals(2, $this->conn->getRowCount('UserProfile')); + + $this->expectException(ConstraintException::class); + + self::$service->delete(1); + } + + /** + * @throws \SP\Core\Exceptions\SPException + */ + public function testCreate() + { + $profileData = new ProfileData(); + $profileData->setAccAdd(true); + $profileData->setAccDelete(true); + $profileData->setConfigBackup(true); + + $data = new UserProfileData(); + $data->setId(4); + $data->setName('Prueba'); + $data->setProfile($profileData); + + $result = self::$service->create($data); + + $this->assertEquals($data->getId(), $result); + + $this->assertEquals(4, $this->conn->getRowCount('UserProfile')); + + $this->assertEquals($data, self::$service->getById($result)); + } + + /** + * @throws \SP\Core\Exceptions\SPException + */ + public function testCreateDuplicated() + { + $data = new UserProfileData(); + $data->setName('Admin'); + $data->setProfile(new ProfileData()); + + $this->expectException(DuplicatedItemException::class); + + self::$service->create($data); + } +} diff --git a/test/res/config/config.xml b/test/res/config/config.xml index 12ec3786..cb50b044 100644 --- a/test/res/config/config.xml +++ b/test/res/config/config.xml @@ -9,11 +9,11 @@ 1 1 - e4ec7e7979447922d90ef4280172177256c162b8 + 3a2b2fe9e1edb1c98c29c6e454e7d0b942382717 0 0 - 1532379104 - 5819af570a83290a50935bb1e8397d24c74b87aa + 1532557463 + 9d6f26441038788146ac0b36a21d2e299120518f @@ -32,7 +32,7 @@ 0 - 25cdc5c691636d478367f88a5e9bb3fc043097d2 + ec6de9dab12089f334fdfa283bd8ba00938696d2 PDF JPG