From 465fec9935de5a986ebe0e90c643176aafc6388f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Tue, 9 Apr 2024 06:42:26 +0200 Subject: [PATCH] chore(tests): UT for User service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../AccessManager/IndexController.php | 34 +- .../Account/SaveRequestController.php | 10 +- .../AccountManager/BulkEditController.php | 20 +- .../AuthToken/AuthTokenViewBase.php | 4 +- .../ConfigManager/IndexController.php | 30 +- .../Helpers/Account/AccountHelper.php | 12 +- .../Controllers/Helpers/ItemPresetHelper.php | 16 +- .../Notification/NotificationViewBase.php | 8 +- .../web/Controllers/User/SearchController.php | 12 +- .../web/Controllers/User/UserSaveBase.php | 6 +- .../web/Controllers/User/UserViewBase.php | 8 +- .../UserGroup/UserGroupViewBase.php | 12 +- .../UserPassReset/UserPassResetSaveBase.php | 6 +- .../UserSettingsGeneral/SaveController.php | 8 +- .../Domain/Account/Services/AccountSearch.php | 4 +- lib/SP/Domain/Api/Services/Api.php | 6 +- lib/SP/Domain/Auth/Services/Login.php | 24 +- .../Domain/Common/Attributes/Hydratable.php | 6 +- .../Crypt/Services/TemporaryMasterPass.php | 12 +- lib/SP/Domain/Import/Services/LdapImport.php | 4 +- .../Install/Services/InstallerService.php | 4 +- .../{Services => Dtos}/UserLoginRequest.php | 55 +- ...erServiceInterface.php => UserService.php} | 32 +- .../User/Services/UpdatePassRequest.php | 67 -- .../Services/{UserService.php => User.php} | 208 +++--- .../Providers/Auth/Database/DatabaseAuth.php | 6 +- .../Account/Services/AccountSearchTest.php | 4 +- tests/SPT/Domain/Api/Services/ApiTest.php | 8 +- .../Services/TemporaryMasterPassTest.php | 8 +- .../Domain/Import/Services/LdapImportTest.php | 10 +- .../Domain/Install/Services/InstallerTest.php | 8 +- tests/SPT/Domain/User/Services/UserTest.php | 679 ++++++++++++++++++ .../Auth/Database/DatabaseAuthTest.php | 8 +- 33 files changed, 954 insertions(+), 385 deletions(-) rename lib/SP/Domain/User/{Services => Dtos}/UserLoginRequest.php (59%) rename lib/SP/Domain/User/Ports/{UserServiceInterface.php => UserService.php} (85%) delete mode 100644 lib/SP/Domain/User/Services/UpdatePassRequest.php rename lib/SP/Domain/User/Services/{UserService.php => User.php} (54%) create mode 100644 tests/SPT/Domain/User/Services/UserTest.php diff --git a/app/modules/web/Controllers/AccessManager/IndexController.php b/app/modules/web/Controllers/AccessManager/IndexController.php index 38e247df..5d078cee 100644 --- a/app/modules/web/Controllers/AccessManager/IndexController.php +++ b/app/modules/web/Controllers/AccessManager/IndexController.php @@ -35,7 +35,7 @@ use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserProfileService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Html\DataGrid\DataGridTab; use SP\Modules\Web\Controllers\ControllerBase; use SP\Modules\Web\Controllers\Helpers\Grid\AuthTokenGrid; @@ -59,27 +59,27 @@ final class IndexController extends ControllerBase private UserGroupGrid $userGroupGrid; private UserProfileGrid $userProfileGrid; private AuthTokenGrid $authTokenGrid; - private PublicLinkGrid $publicLinkGrid; - private UserServiceInterface $userService; - private UserGroupService $userGroupService; + private PublicLinkGrid $publicLinkGrid; + private UserService $userService; + private UserGroupService $userGroupService; private UserProfileService $userProfileService; private AuthTokenService $authTokenService; private PublicLinkService $publicLinkService; public function __construct( - Application $application, - WebControllerHelper $webControllerHelper, - TabsGridHelper $tabsGridHelper, - UserGrid $userGrid, - UserGroupGrid $userGroupGrid, - UserProfileGrid $userProfileGrid, - AuthTokenGrid $authTokenGrid, - PublicLinkGrid $publicLinkGrid, - UserServiceInterface $userService, - UserGroupService $userGroupService, - UserProfileService $userProfileService, - AuthTokenService $authTokenService, - PublicLinkService $publicLinkService + Application $application, + WebControllerHelper $webControllerHelper, + TabsGridHelper $tabsGridHelper, + UserGrid $userGrid, + UserGroupGrid $userGroupGrid, + UserProfileGrid $userProfileGrid, + AuthTokenGrid $authTokenGrid, + PublicLinkGrid $publicLinkGrid, + UserService $userService, + UserGroupService $userGroupService, + UserProfileService $userProfileService, + AuthTokenService $authTokenService, + PublicLinkService $publicLinkService ) { parent::__construct($application, $webControllerHelper); diff --git a/app/modules/web/Controllers/Account/SaveRequestController.php b/app/modules/web/Controllers/Account/SaveRequestController.php index a9677243..9aaea65d 100644 --- a/app/modules/web/Controllers/Account/SaveRequestController.php +++ b/app/modules/web/Controllers/Account/SaveRequestController.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -35,7 +35,7 @@ use SP\Core\Events\EventMessage; use SP\Domain\Account\Ports\AccountService; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Domain\Core\Exceptions\ValidationException; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Http\JsonMessage; use SP\Http\Uri; use SP\Modules\Web\Controllers\Traits\JsonTrait; @@ -50,14 +50,14 @@ final class SaveRequestController extends AccountControllerBase use ItemTrait; use JsonTrait; - private AccountService $accountService; - private UserServiceInterface $userService; + private AccountService $accountService; + private UserService $userService; public function __construct( Application $application, WebControllerHelper $webControllerHelper, AccountService $accountService, - UserServiceInterface $userService + UserService $userService ) { parent::__construct( $application, diff --git a/app/modules/web/Controllers/AccountManager/BulkEditController.php b/app/modules/web/Controllers/AccountManager/BulkEditController.php index 156b4149..7cabe1f4 100644 --- a/app/modules/web/Controllers/AccountManager/BulkEditController.php +++ b/app/modules/web/Controllers/AccountManager/BulkEditController.php @@ -38,7 +38,7 @@ use SP\Domain\Core\Acl\AclActionsInterface; use SP\Domain\CustomField\Ports\CustomFieldDataService; use SP\Domain\Tag\Ports\TagService; use SP\Domain\User\Ports\UserGroupService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Http\JsonMessage; use SP\Modules\Web\Controllers\ControllerBase; use SP\Modules\Web\Controllers\Helpers\Grid\AccountGrid; @@ -64,18 +64,18 @@ final class BulkEditController extends ControllerBase private CustomFieldDataService $customFieldService; private CategoryService $categoryService; private ClientService $clientService; - private TagService $tagService; - private UserServiceInterface $userService; + private TagService $tagService; + private UserService $userService; private UserGroupService $userGroupService; public function __construct( - Application $application, - WebControllerHelper $webControllerHelper, - CategoryService $categoryService, - ClientService $clientService, - TagService $tagService, - UserServiceInterface $userService, - UserGroupService $userGroupService + Application $application, + WebControllerHelper $webControllerHelper, + CategoryService $categoryService, + ClientService $clientService, + TagService $tagService, + UserService $userService, + UserGroupService $userGroupService ) { parent::__construct($application, $webControllerHelper); diff --git a/app/modules/web/Controllers/AuthToken/AuthTokenViewBase.php b/app/modules/web/Controllers/AuthToken/AuthTokenViewBase.php index 955fc1f7..be4b8b9a 100644 --- a/app/modules/web/Controllers/AuthToken/AuthTokenViewBase.php +++ b/app/modules/web/Controllers/AuthToken/AuthTokenViewBase.php @@ -37,7 +37,7 @@ use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Core\Exceptions\SessionTimeout; use SP\Domain\Core\Exceptions\SPException; use SP\Domain\CustomField\Ports\CustomFieldDataService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Modules\Web\Controllers\ControllerBase; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\ItemTrait; @@ -60,7 +60,7 @@ abstract class AuthTokenViewBase extends ControllerBase Application $application, WebControllerHelper $webControllerHelper, private readonly CustomFieldDataService $customFieldService, - private readonly UserServiceInterface $userService, + private readonly UserService $userService, private readonly AuthTokenService $authTokenService, private readonly AuthTokenActionInterface $authTokenAction, ) { diff --git a/app/modules/web/Controllers/ConfigManager/IndexController.php b/app/modules/web/Controllers/ConfigManager/IndexController.php index 2f39bd31..53503635 100644 --- a/app/modules/web/Controllers/ConfigManager/IndexController.php +++ b/app/modules/web/Controllers/ConfigManager/IndexController.php @@ -50,7 +50,7 @@ use SP\Domain\Export\Services\XmlExport; use SP\Domain\Task\Services\Task; use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserProfileService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Infrastructure\Database\DatabaseUtil; use SP\Infrastructure\File\FileException; @@ -73,9 +73,9 @@ use SP\Util\Util; */ final class IndexController extends ControllerBase { - protected TabsHelper $tabsHelper; - private UserServiceInterface $userService; - private UserGroupService $userGroupService; + protected TabsHelper $tabsHelper; + private UserService $userService; + private UserGroupService $userGroupService; private UserProfileService $userProfileService; private MimeTypesService $mimeTypes; private DatabaseUtil $databaseUtil; @@ -84,17 +84,17 @@ final class IndexController extends ControllerBase private PluginManager $pluginManager; public function __construct( - Application $application, - WebControllerHelper $webControllerHelper, - TabsHelper $tabsHelper, - UserServiceInterface $userService, - UserGroupService $userGroupService, - UserProfileService $userProfileService, - MimeTypesService $mimeTypes, - DatabaseUtil $databaseUtil, - ConfigService $configService, - AccountService $accountService, - PluginManager $pluginManager + Application $application, + WebControllerHelper $webControllerHelper, + TabsHelper $tabsHelper, + UserService $userService, + UserGroupService $userGroupService, + UserProfileService $userProfileService, + MimeTypesService $mimeTypes, + DatabaseUtil $databaseUtil, + ConfigService $configService, + AccountService $accountService, + PluginManager $pluginManager ) { parent::__construct($application, $webControllerHelper); diff --git a/app/modules/web/Controllers/Helpers/Account/AccountHelper.php b/app/modules/web/Controllers/Helpers/Account/AccountHelper.php index 216f575b..bdc26b1b 100644 --- a/app/modules/web/Controllers/Helpers/Account/AccountHelper.php +++ b/app/modules/web/Controllers/Helpers/Account/AccountHelper.php @@ -56,7 +56,7 @@ use SP\Domain\ItemPreset\Ports\ItemPresetInterface; use SP\Domain\ItemPreset\Ports\ItemPresetService; use SP\Domain\Tag\Ports\TagService; use SP\Domain\User\Ports\UserGroupService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Domain\User\Services\UpdatedMasterPassException; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Mvc\Controller\ItemTrait; @@ -83,9 +83,9 @@ final class AccountHelper extends AccountHelperBase private ClientService $clientService; private CustomFieldDataService $customFieldService; private ?AccountPermission $accountAcl = null; - private ?int $accountId = null; - private UserServiceInterface $userService; - private UserGroupService $userGroupService; + private ?int $accountId = null; + private UserService $userService; + private UserGroupService $userGroupService; private TagService $tagService; public function __construct( @@ -93,7 +93,7 @@ final class AccountHelper extends AccountHelperBase TemplateInterface $template, RequestInterface $request, AclInterface $acl, - AccountService $accountService, + AccountService $accountService, AccountHistoryService $accountHistoryService, PublicLinkService $publicLinkService, ItemPresetService $itemPresetService, @@ -103,7 +103,7 @@ final class AccountHelper extends AccountHelperBase CategoryService $categoryService, ClientService $clientService, CustomFieldDataService $customFieldService, - UserServiceInterface $userService, + UserService $userService, UserGroupService $userGroupService, TagService $tagService ) { diff --git a/app/modules/web/Controllers/Helpers/ItemPresetHelper.php b/app/modules/web/Controllers/Helpers/ItemPresetHelper.php index 6ae40dd8..56fb58a2 100644 --- a/app/modules/web/Controllers/Helpers/ItemPresetHelper.php +++ b/app/modules/web/Controllers/Helpers/ItemPresetHelper.php @@ -37,7 +37,7 @@ use SP\Domain\Http\RequestInterface; use SP\Domain\ItemPreset\Models\ItemPreset; use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserProfileService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Mvc\View\Components\SelectItemAdapter; use SP\Mvc\View\TemplateInterface; @@ -49,17 +49,17 @@ use SP\Mvc\View\TemplateInterface; final class ItemPresetHelper extends HelperBase { private ?SelectItemAdapter $users = null; - private ?SelectItemAdapter $userGroups = null; - private UserServiceInterface $userService; + private ?SelectItemAdapter $userGroups = null; + private UserService $userService; private UserGroupService $userGroupService; private UserProfileService $userProfileService; public function __construct( - Application $application, - TemplateInterface $template, - RequestInterface $request, - UserServiceInterface $userService, - UserGroupService $userGroupService, + Application $application, + TemplateInterface $template, + RequestInterface $request, + UserService $userService, + UserGroupService $userGroupService, UserProfileService $userProfileService ) { diff --git a/app/modules/web/Controllers/Notification/NotificationViewBase.php b/app/modules/web/Controllers/Notification/NotificationViewBase.php index 58bdb0d3..2f80de16 100644 --- a/app/modules/web/Controllers/Notification/NotificationViewBase.php +++ b/app/modules/web/Controllers/Notification/NotificationViewBase.php @@ -32,7 +32,7 @@ use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Notification\Models\Notification; use SP\Domain\Notification\Ports\NotificationService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Modules\Web\Controllers\ControllerBase; use SP\Mvc\Controller\WebControllerHelper; @@ -43,14 +43,14 @@ use SP\Mvc\View\Components\SelectItemAdapter; */ abstract class NotificationViewBase extends ControllerBase { - private NotificationService $notificationService; - private UserServiceInterface $userService; + private NotificationService $notificationService; + private UserService $userService; public function __construct( Application $application, WebControllerHelper $webControllerHelper, NotificationService $notificationService, - UserServiceInterface $userService + UserService $userService ) { parent::__construct($application, $webControllerHelper); diff --git a/app/modules/web/Controllers/User/SearchController.php b/app/modules/web/Controllers/User/SearchController.php index fac274eb..038fdfff 100644 --- a/app/modules/web/Controllers/User/SearchController.php +++ b/app/modules/web/Controllers/User/SearchController.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -30,7 +30,7 @@ use SP\Core\Application; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Html\DataGrid\DataGridInterface; use SP\Http\JsonMessage; use SP\Modules\Web\Controllers\ControllerBase; @@ -47,14 +47,14 @@ final class SearchController extends ControllerBase use ItemTrait; use JsonTrait; - private UserServiceInterface $userService; - private UserGrid $userGrid; + private UserService $userService; + private UserGrid $userGrid; public function __construct( Application $application, WebControllerHelper $webControllerHelper, - UserServiceInterface $userService, - UserGrid $userGrid + UserService $userService, + UserGrid $userGrid ) { parent::__construct($application, $webControllerHelper); diff --git a/app/modules/web/Controllers/User/UserSaveBase.php b/app/modules/web/Controllers/User/UserSaveBase.php index 85d4faea..8755253e 100644 --- a/app/modules/web/Controllers/User/UserSaveBase.php +++ b/app/modules/web/Controllers/User/UserSaveBase.php @@ -35,7 +35,7 @@ use SP\Domain\CustomField\Ports\CustomFieldDataService; use SP\Domain\Notification\Ports\MailService; use SP\Domain\User\Models\User; use SP\Domain\User\Ports\UserPassRecoverService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Domain\User\Services\UserPassRecover; use SP\Modules\Web\Controllers\ControllerBase; use SP\Modules\Web\Forms\UserForm; @@ -46,7 +46,7 @@ use SP\Mvc\Controller\WebControllerHelper; */ abstract class UserSaveBase extends ControllerBase { - protected UserServiceInterface $userService; + protected UserService $userService; protected CustomFieldDataService $customFieldService; protected UserForm $form; private MailService $mailService; @@ -55,7 +55,7 @@ abstract class UserSaveBase extends ControllerBase public function __construct( Application $application, WebControllerHelper $webControllerHelper, - UserServiceInterface $userService, + UserService $userService, CustomFieldDataService $customFieldService, MailService $mailService, UserPassRecoverService $userPassRecoverService diff --git a/app/modules/web/Controllers/User/UserViewBase.php b/app/modules/web/Controllers/User/UserViewBase.php index 3c47050e..5dfcd09b 100644 --- a/app/modules/web/Controllers/User/UserViewBase.php +++ b/app/modules/web/Controllers/User/UserViewBase.php @@ -36,7 +36,7 @@ use SP\Domain\CustomField\Ports\CustomFieldDataService; use SP\Domain\User\Models\User; use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserProfileService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Modules\Web\Controllers\ControllerBase; use SP\Mvc\Controller\ItemTrait; use SP\Mvc\Controller\WebControllerHelper; @@ -49,15 +49,15 @@ abstract class UserViewBase extends ControllerBase { use ItemTrait; - protected UserServiceInterface $userService; - private UserGroupService $userGroupService; + protected UserService $userService; + private UserGroupService $userGroupService; private UserProfileService $userProfileService; private CustomFieldDataService $customFieldService; public function __construct( Application $application, WebControllerHelper $webControllerHelper, - UserServiceInterface $userService, + UserService $userService, UserGroupService $userGroupService, UserProfileService $userProfileService, CustomFieldDataService $customFieldService diff --git a/app/modules/web/Controllers/UserGroup/UserGroupViewBase.php b/app/modules/web/Controllers/UserGroup/UserGroupViewBase.php index b35b5f22..9c205888 100644 --- a/app/modules/web/Controllers/UserGroup/UserGroupViewBase.php +++ b/app/modules/web/Controllers/UserGroup/UserGroupViewBase.php @@ -35,7 +35,7 @@ use SP\Domain\Core\Exceptions\SPException; use SP\Domain\CustomField\Ports\CustomFieldDataService; use SP\Domain\User\Models\UserGroup; use SP\Domain\User\Ports\UserGroupService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Modules\Web\Controllers\ControllerBase; use SP\Mvc\Controller\ItemTrait; @@ -51,13 +51,13 @@ abstract class UserGroupViewBase extends ControllerBase private UserGroupService $userGroupService; private CustomFieldDataService $customFieldService; - private UserServiceInterface $userService; + private UserService $userService; public function __construct( - Application $application, - WebControllerHelper $webControllerHelper, - UserGroupService $userGroupService, - UserServiceInterface $userService, + Application $application, + WebControllerHelper $webControllerHelper, + UserGroupService $userGroupService, + UserService $userService, CustomFieldDataService $customFieldService ) { parent::__construct($application, $webControllerHelper); diff --git a/app/modules/web/Controllers/UserPassReset/UserPassResetSaveBase.php b/app/modules/web/Controllers/UserPassReset/UserPassResetSaveBase.php index ef90ccc8..6d28c28b 100644 --- a/app/modules/web/Controllers/UserPassReset/UserPassResetSaveBase.php +++ b/app/modules/web/Controllers/UserPassReset/UserPassResetSaveBase.php @@ -35,7 +35,7 @@ use SP\Domain\Notification\Ports\MailService; use SP\Domain\Security\Dtos\TrackRequest; use SP\Domain\Security\Ports\TrackService; use SP\Domain\User\Ports\UserPassRecoverService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Modules\Web\Controllers\ControllerBase; use SP\Mvc\Controller\WebControllerHelper; @@ -45,7 +45,7 @@ use SP\Mvc\Controller\WebControllerHelper; abstract class UserPassResetSaveBase extends ControllerBase { protected UserPassRecoverService $userPassRecoverService; - protected UserServiceInterface $userService; + protected UserService $userService; protected MailService $mailService; private TrackService $trackService; private TrackRequest $trackRequest; @@ -59,7 +59,7 @@ abstract class UserPassResetSaveBase extends ControllerBase Application $application, WebControllerHelper $webControllerHelper, UserPassRecoverService $userPassRecoverService, - UserServiceInterface $userService, + UserService $userService, MailService $mailService, TrackService $trackService diff --git a/app/modules/web/Controllers/UserSettingsGeneral/SaveController.php b/app/modules/web/Controllers/UserSettingsGeneral/SaveController.php index defa6f91..73a2db38 100644 --- a/app/modules/web/Controllers/UserSettingsGeneral/SaveController.php +++ b/app/modules/web/Controllers/UserSettingsGeneral/SaveController.php @@ -30,8 +30,8 @@ use SP\Core\Application; use SP\Core\Events\Event; use SP\Domain\User\Dtos\UserDataDto; use SP\Domain\User\Models\UserPreferences; -use SP\Domain\User\Ports\UserServiceInterface; -use SP\Domain\User\Services\UserService; +use SP\Domain\User\Ports\UserService; +use SP\Domain\User\Services\User; use SP\Http\JsonMessage; use SP\Modules\Web\Controllers\SimpleControllerBase; use SP\Modules\Web\Controllers\Traits\JsonTrait; @@ -46,12 +46,12 @@ final class SaveController extends SimpleControllerBase { use JsonTrait; - private UserService $userService; + private User $userService; public function __construct( Application $application, SimpleControllerHelper $simpleControllerHelper, - UserServiceInterface $userService + UserService $userService ) { parent::__construct($application, $simpleControllerHelper); diff --git a/lib/SP/Domain/Account/Services/AccountSearch.php b/lib/SP/Domain/Account/Services/AccountSearch.php index fd4bbd19..6f308dc6 100644 --- a/lib/SP/Domain/Account/Services/AccountSearch.php +++ b/lib/SP/Domain/Account/Services/AccountSearch.php @@ -37,7 +37,7 @@ use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Core\Exceptions\SPException; use SP\Domain\User\Ports\UserGroupService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Infrastructure\Database\QueryResult; use SP\Util\Filter; @@ -50,7 +50,7 @@ final class AccountSearch extends Service implements AccountSearchService { public function __construct( Application $application, - private readonly UserServiceInterface $userService, + private readonly UserService $userService, private readonly UserGroupService $userGroupService, private readonly AccountSearchRepository $accountSearchRepository, private readonly AccountSearchDataBuilder $accountSearchDataBuilder diff --git a/lib/SP/Domain/Api/Services/Api.php b/lib/SP/Domain/Api/Services/Api.php index 94c96c1f..f5803332 100644 --- a/lib/SP/Domain/Api/Services/Api.php +++ b/lib/SP/Domain/Api/Services/Api.php @@ -48,7 +48,7 @@ use SP\Domain\Security\Dtos\TrackRequest; use SP\Domain\Security\Ports\TrackService; use SP\Domain\User\Dtos\UserDataDto; use SP\Domain\User\Ports\UserProfileService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Modules\Api\Controllers\Help\HelpInterface; use SP\Util\Filter; @@ -71,11 +71,11 @@ final class Api extends Service implements ApiService * @throws InvalidArgumentException */ public function __construct( - Application $application, + Application $application, private readonly TrackService $trackService, private readonly ApiRequestService $apiRequest, private readonly AuthTokenService $authTokenService, - private readonly UserServiceInterface $userService, + private readonly UserService $userService, private readonly UserProfileService $userProfileService ) { parent::__construct($application); diff --git a/lib/SP/Domain/Auth/Services/Login.php b/lib/SP/Domain/Auth/Services/Login.php index 9637d653..4d563313 100644 --- a/lib/SP/Domain/Auth/Services/Login.php +++ b/lib/SP/Domain/Auth/Services/Login.php @@ -47,11 +47,11 @@ use SP\Domain\Crypt\Ports\TemporaryMasterPassService; use SP\Domain\Http\RequestInterface; use SP\Domain\Security\Dtos\TrackRequest; use SP\Domain\Security\Ports\TrackService; +use SP\Domain\User\Dtos\UserLoginRequest; use SP\Domain\User\Ports\UserMasterPassService; use SP\Domain\User\Ports\UserPassRecoverService; use SP\Domain\User\Ports\UserProfileService; -use SP\Domain\User\Ports\UserServiceInterface; -use SP\Domain\User\Services\UserLoginRequest; +use SP\Domain\User\Ports\UserService; use SP\Domain\User\Services\UserMasterPassStatus; use SP\Http\Uri; use SP\Infrastructure\Common\Repositories\NoSuchItemException; @@ -88,16 +88,16 @@ final class Login extends Service implements LoginService * @throws InvalidArgumentException */ public function __construct( - Application $application, - private readonly AuthProviderInterface $authProvider, - private readonly LanguageInterface $language, - private readonly TrackService $trackService, - private readonly RequestInterface $request, - private readonly UserServiceInterface $userService, - private readonly UserPassRecoverService $userPassRecoverService, - private readonly TemporaryMasterPassService $temporaryMasterPassService, - private readonly UserMasterPassService $userMasterPassService, - private readonly UserProfileService $userProfileService + Application $application, + private readonly AuthProviderInterface $authProvider, + private readonly LanguageInterface $language, + private readonly TrackService $trackService, + private readonly RequestInterface $request, + private readonly UserService $userService, + private readonly UserPassRecoverService $userPassRecoverService, + private readonly TemporaryMasterPassService $temporaryMasterPassService, + private readonly UserMasterPassService $userMasterPassService, + private readonly UserProfileService $userProfileService ) { parent::__construct($application); diff --git a/lib/SP/Domain/Common/Attributes/Hydratable.php b/lib/SP/Domain/Common/Attributes/Hydratable.php index 9828a233..fef048e2 100644 --- a/lib/SP/Domain/Common/Attributes/Hydratable.php +++ b/lib/SP/Domain/Common/Attributes/Hydratable.php @@ -30,11 +30,11 @@ use Attribute; * Class Hydratable */ #[Attribute(Attribute::TARGET_CLASS)] -final class Hydratable +final readonly class Hydratable { - private readonly array $targetClass; + private array $targetClass; - public function __construct(private readonly string $sourceProperty, array $targetClass) + public function __construct(private string $sourceProperty, array $targetClass) { $this->targetClass = array_filter( $targetClass, diff --git a/lib/SP/Domain/Crypt/Services/TemporaryMasterPass.php b/lib/SP/Domain/Crypt/Services/TemporaryMasterPass.php index 8b55bec3..a0fe0dcb 100644 --- a/lib/SP/Domain/Crypt/Services/TemporaryMasterPass.php +++ b/lib/SP/Domain/Crypt/Services/TemporaryMasterPass.php @@ -41,7 +41,7 @@ use SP\Domain\Core\Exceptions\CryptException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Crypt\Ports\TemporaryMasterPassService; use SP\Domain\Notification\Ports\MailService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Util\PasswordUtil; @@ -73,11 +73,11 @@ final class TemporaryMasterPass extends Service implements TemporaryMasterPassSe private ?int $maxTime = null; public function __construct( - Application $application, - private readonly ConfigService $configService, - private readonly UserServiceInterface $userService, - private readonly MailService $mailService, - private readonly CryptInterface $crypt, + Application $application, + private readonly ConfigService $configService, + private readonly UserService $userService, + private readonly MailService $mailService, + private readonly CryptInterface $crypt, ) { parent::__construct($application); } diff --git a/lib/SP/Domain/Import/Services/LdapImport.php b/lib/SP/Domain/Import/Services/LdapImport.php index a6fe8e7a..60627457 100644 --- a/lib/SP/Domain/Import/Services/LdapImport.php +++ b/lib/SP/Domain/Import/Services/LdapImport.php @@ -38,7 +38,7 @@ use SP\Domain\Import\Ports\LdapImportService; use SP\Domain\User\Models\User; use SP\Domain\User\Models\UserGroup; use SP\Domain\User\Ports\UserGroupService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Providers\Auth\Ldap\LdapBase; use SP\Providers\Auth\Ldap\LdapException; use SP\Providers\Auth\Ldap\LdapParams; @@ -55,7 +55,7 @@ final class LdapImport extends Service implements LdapImportService { public function __construct( Application $application, - private readonly UserServiceInterface $userService, + private readonly UserService $userService, private readonly UserGroupService $userGroupService, private readonly LdapActionsService $ldapActionsService, private readonly LdapConnectionInterface $ldapConnection diff --git a/lib/SP/Domain/Install/Services/InstallerService.php b/lib/SP/Domain/Install/Services/InstallerService.php index a1fa7cff..8c041833 100644 --- a/lib/SP/Domain/Install/Services/InstallerService.php +++ b/lib/SP/Domain/Install/Services/InstallerService.php @@ -44,7 +44,7 @@ use SP\Domain\User\Models\UserGroup; use SP\Domain\User\Models\UserProfile; use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserProfileService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Infrastructure\Database\DatabaseConnectionData; use SP\Infrastructure\File\FileException; use SP\Util\VersionUtil; @@ -70,7 +70,7 @@ final class InstallerService implements InstallerServiceInterface public function __construct( RequestInterface $request, private readonly ConfigFileService $config, - private readonly UserServiceInterface $userService, + private readonly UserService $userService, private readonly UserGroupService $userGroupService, private readonly UserProfileService $userProfileService, private readonly ConfigService $configService, diff --git a/lib/SP/Domain/User/Services/UserLoginRequest.php b/lib/SP/Domain/User/Dtos/UserLoginRequest.php similarity index 59% rename from lib/SP/Domain/User/Services/UserLoginRequest.php rename to lib/SP/Domain/User/Dtos/UserLoginRequest.php index 19be1fb8..3f8bea7a 100644 --- a/lib/SP/Domain/User/Services/UserLoginRequest.php +++ b/lib/SP/Domain/User/Dtos/UserLoginRequest.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -22,68 +22,51 @@ * along with sysPass. If not, see . */ -namespace SP\Domain\User\Services; +namespace SP\Domain\User\Dtos; /** * Class UserLoginRequest - * - * @package SP\Domain\User\Services */ -final class UserLoginRequest +final readonly class UserLoginRequest { - private ?string $login = null; - private ?string $name = null; - private ?string $password = null; - private ?string $email = null; - private ?bool $isLdap = null; + /** + * @param string $login + * @param string|null $password + * @param string|null $name + * @param string|null $email + * @param bool|null $isLdap + */ + public function __construct( + private string $login, + private ?string $password = null, + private ?string $name = null, + private ?string $email = null, + private ?bool $isLdap = false + ) { + } public function getLogin(): ?string { return $this->login; } - public function setLogin(string $login): void - { - $this->login = $login; - } - public function getName(): ?string { return $this->name; } - public function setName(string $name): void - { - $this->name = $name; - } - public function getPassword(): ?string { return $this->password; } - public function setPassword(string $password): void - { - $this->password = $password; - } - public function getEmail(): ?string { return $this->email; } - public function setEmail(string $email): void - { - $this->email = $email; - } - public function getisLdap(): ?bool { return $this->isLdap; } - - public function setIsLdap(bool $isLdap): void - { - $this->isLdap = $isLdap; - } -} \ No newline at end of file +} diff --git a/lib/SP/Domain/User/Ports/UserServiceInterface.php b/lib/SP/Domain/User/Ports/UserService.php similarity index 85% rename from lib/SP/Domain/User/Ports/UserServiceInterface.php rename to lib/SP/Domain/User/Ports/UserService.php index d3af8bf6..49231a55 100644 --- a/lib/SP/Domain/User/Ports/UserServiceInterface.php +++ b/lib/SP/Domain/User/Ports/UserService.php @@ -30,10 +30,9 @@ use SP\Domain\Common\Services\ServiceException; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Core\Exceptions\SPException; -use SP\Domain\User\Models\User; +use SP\Domain\User\Dtos\UserLoginRequest; +use SP\Domain\User\Models\User as UserModel; use SP\Domain\User\Models\UserPreferences; -use SP\Domain\User\Services\UserLoginRequest; -use SP\Domain\User\Services\UserService; use SP\Infrastructure\Common\Repositories\DuplicatedItemException; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Infrastructure\Database\QueryResult; @@ -43,16 +42,16 @@ use SP\Infrastructure\Database\QueryResult; * * @package SP\Domain\User\Services */ -interface UserServiceInterface +interface UserService { /** - * Actualiza el último inicio de sesión del usuario en la BBDD. + * Update the last user log in * * @throws ConstraintException * @throws NoSuchItemException * @throws QueryException */ - public function updateLastLoginById(int $id): int; + public function updateLastLoginById(int $id): void; /** * @throws ConstraintException @@ -65,7 +64,7 @@ interface UserServiceInterface * * @throws SPException */ - public function getById(int $id): User; + public function getById(int $id): UserModel; /** * Returns the item for given id @@ -74,7 +73,7 @@ interface UserServiceInterface * @throws QueryException * @throws NoSuchItemException */ - public function getByLogin(string $login): User; + public function getByLogin(string $login): UserModel; /** * Deletes an item @@ -83,10 +82,10 @@ interface UserServiceInterface * @throws QueryException * @throws NoSuchItemException */ - public function delete(int $id): UserService; + public function delete(int $id): void; /** - * @param int[] $ids + * @param int[] $ids * * @throws ServiceException * @throws ConstraintException @@ -106,7 +105,7 @@ interface UserServiceInterface * * @throws SPException */ - public function create(User $itemData): int; + public function create(UserModel $user): int; /** * Creates an item @@ -114,11 +113,12 @@ interface UserServiceInterface * @throws SPException * @throws CryptoException */ - public function createWithMasterPass(User $itemData, string $userPass, string $masterPass): int; + public function createWithMasterPass(UserModel $user, string $userPass, string $masterPass): int; /** * Searches for items by a given filter * + * @return QueryResult * @throws ConstraintException * @throws QueryException */ @@ -132,7 +132,7 @@ interface UserServiceInterface * @throws DuplicatedItemException * @throws ServiceException */ - public function update(User $userData): void; + public function update(UserModel $user): void; /** * Updates a user's pass @@ -147,7 +147,7 @@ interface UserServiceInterface * @throws ConstraintException * @throws QueryException */ - public function updatePreferencesById(int $userId, UserPreferences $userPreferencesData): int; + public function updatePreferencesById(int $userId, UserPreferences $userPreferences): int; /** * @throws ConstraintException @@ -158,7 +158,7 @@ interface UserServiceInterface /** * Get all items from the service's repository * - * @return \SP\Domain\User\Models\User[] + * @return array * @throws ConstraintException * @throws QueryException */ @@ -183,7 +183,7 @@ interface UserServiceInterface /** * Return the email of the given user's id * - * @param int[] $ids + * @param int[] $ids * * @throws ConstraintException * @throws QueryException diff --git a/lib/SP/Domain/User/Services/UpdatePassRequest.php b/lib/SP/Domain/User/Services/UpdatePassRequest.php deleted file mode 100644 index 1aca643a..00000000 --- a/lib/SP/Domain/User/Services/UpdatePassRequest.php +++ /dev/null @@ -1,67 +0,0 @@ -. - */ - -namespace SP\Domain\User\Services; - -/** - * Class UpdatePassRequest - * - * @package SP\Domain\User\Services - */ -final class UpdatePassRequest -{ - private string $pass; - private bool $isChangePass = false; - private bool $isChangedPass = false; - - public function __construct(string $pass) - { - $this->pass = $pass; - } - - public function getPass(): string - { - return $this->pass; - } - - public function getisChangePass(): bool - { - return $this->isChangePass; - } - - public function setIsChangePass(bool $isChangePass): void - { - $this->isChangePass = $isChangePass; - } - - public function getisChangedPass(): bool - { - return $this->isChangedPass; - } - - public function setIsChangedPass(bool $isChangedPass): void - { - $this->isChangedPass = $isChangedPass; - } -} \ No newline at end of file diff --git a/lib/SP/Domain/User/Services/UserService.php b/lib/SP/Domain/User/Services/User.php similarity index 54% rename from lib/SP/Domain/User/Services/UserService.php rename to lib/SP/Domain/User/Services/User.php index 32f6e571..d7767f5d 100644 --- a/lib/SP/Domain/User/Services/UserService.php +++ b/lib/SP/Domain/User/Services/User.php @@ -24,77 +24,52 @@ namespace SP\Domain\User\Services; -use Defuse\Crypto\Exception\CryptoException; +use JsonException; use SP\Core\Application; use SP\Core\Crypt\Hash; use SP\DataModel\ItemSearchData; use SP\Domain\Common\Services\Service; use SP\Domain\Common\Services\ServiceException; -use SP\Domain\Common\Services\ServiceItemTrait; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Core\Exceptions\SPException; +use SP\Domain\User\Dtos\UserLoginRequest; use SP\Domain\User\Models\User as UserModel; use SP\Domain\User\Models\UserPreferences; use SP\Domain\User\Ports\UserMasterPassService; use SP\Domain\User\Ports\UserRepository; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Infrastructure\Common\Repositories\DuplicatedItemException; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Infrastructure\Database\QueryResult; -use SP\Util\Util; + +use function SP\__u; /** * Class UserService - * - * @package SP\Domain\User\Services */ -final class UserService extends Service implements UserServiceInterface +final class User extends Service implements UserService { - use ServiceItemTrait; - - private UserRepository $userRepository; - private UserMasterPassService $userPassService; - public function __construct( - Application $application, - UserRepository $userRepository, - UserMasterPassService $userPassService + Application $application, + private readonly UserRepository $userRepository, + private readonly UserMasterPassService $userMasterPassService ) { parent::__construct($application); - - $this->userRepository = $userRepository; - $this->userPassService = $userPassService; } /** - * Returns user's preferences object - */ - public static function getUserPreferences(?string $preferences): UserPreferences - { - if (!empty($preferences)) { - return Util::unserialize(UserPreferences::class, $preferences, 'SP\UserPreferences'); - } - - return new UserPreferences(); - } - - /** - * Actualiza el último inicio de sesión del usuario en la BBDD. + * Update the last user log in * * @throws ConstraintException * @throws NoSuchItemException * @throws QueryException */ - public function updateLastLoginById(int $id): int + public function updateLastLoginById(int $id): void { - $result = $this->userRepository->updateLastLoginById($id); - if ($this->userRepository->updateLastLoginById($id) === 0) { - throw new NoSuchItemException(__u('User does not exist')); + throw NoSuchItemException::info(__u('User not found')); } - - return $result; } /** @@ -116,14 +91,14 @@ final class UserService extends Service implements UserServiceInterface $result = $this->userRepository->getById($id); if ($result->getNumRows() === 0) { - throw new NoSuchItemException(__u('User does not exist')); + throw NoSuchItemException::error(__u('User does not exist')); } - return $result->getData(); + return $result->getData(UserModel::class); } /** - * Returns the item for given id + * Returns the item for given login * * @throws ConstraintException * @throws QueryException @@ -134,10 +109,10 @@ final class UserService extends Service implements UserServiceInterface $result = $this->userRepository->getByLogin($login); if ($result->getNumRows() === 0) { - throw new NoSuchItemException(__u('User does not exist')); + throw NoSuchItemException::error(__u('User not found')); } - return $result->getData(); + return $result->getData(UserModel::class); } /** @@ -147,13 +122,11 @@ final class UserService extends Service implements UserServiceInterface * @throws QueryException * @throws NoSuchItemException */ - public function delete(int $id): UserService + public function delete(int $id): void { - if ($this->userRepository->delete($id) === 0) { - throw new NoSuchItemException(__u('User not found'), SPException::INFO); + if ($this->userRepository->delete($id)->getAffectedNumRows() === 0) { + throw NoSuchItemException::info(__u('User not found')); } - - return $this; } /** @@ -165,13 +138,10 @@ final class UserService extends Service implements UserServiceInterface */ public function deleteByIdBatch(array $ids): int { - $count = $this->userRepository->deleteByIdBatch($ids); + $count = $this->userRepository->deleteByIdBatch($ids)->getAffectedNumRows(); if ($count !== count($ids)) { - throw new ServiceException( - __u('Error while deleting the users'), - SPException::WARNING - ); + throw ServiceException::warning(__u('Error while deleting the users')); } return $count; @@ -184,24 +154,26 @@ final class UserService extends Service implements UserServiceInterface */ public function createOnLogin(UserLoginRequest $userLoginRequest): int { - $userData = new UserModel(); - $userData->setLogin($userLoginRequest->getLogin()); - $userData->setName($userLoginRequest->getName()); - $userData->setEmail($userLoginRequest->getEmail()); - $userData->setIsLdap($userLoginRequest->getisLdap() ?? false); - $userData->setPass($userLoginRequest->getPassword()); + $userData = [ + 'login' => $userLoginRequest->getLogin(), + 'name' => $userLoginRequest->getName(), + 'email' => $userLoginRequest->getEmail(), + 'isLdap' => $userLoginRequest->getisLdap() ?? false, + 'pass' => $userLoginRequest->getPassword() + + ]; $configData = $this->config->getConfigData(); if ($userLoginRequest->getisLdap() === true) { - $userData->setUserGroupId($configData->getLdapDefaultGroup()); - $userData->setUserProfileId($configData->getLdapDefaultProfile()); + $userData['userGroupId'] = $configData->getLdapDefaultGroup(); + $userData['userProfileId'] = $configData->getLdapDefaultProfile(); } else { - $userData->setUserGroupId($configData->getSsoDefaultGroup()); - $userData->setUserProfileId($configData->getSsoDefaultProfile()); + $userData['userGroupId'] = $configData->getSsoDefaultGroup(); + $userData['userProfileId'] = $configData->getSsoDefaultProfile(); } - return $this->create($userData); + return $this->create(new UserModel($userData)); } /** @@ -209,33 +181,41 @@ final class UserService extends Service implements UserServiceInterface * * @throws SPException */ - public function create(UserModel $itemData): int + public function create(UserModel $user): int { - $itemData->setPass(Hash::hashKey($itemData->getPass())); - - return $this->userRepository->create($itemData); + return $this->userRepository + ->create($user->mutate(['pass' => Hash::hashKey($user->getPass())])) + ->getLastId(); } /** * Creates an item * + * @param UserModel $user + * @param string $userPass + * @param string $masterPass + * @return int * @throws SPException - * @throws CryptoException + * @throws ServiceException */ - public function createWithMasterPass(UserModel $itemData, string $userPass, string $masterPass): int + public function createWithMasterPass(UserModel $user, string $userPass, string $masterPass): int { - $response = $this->userPassService->create( + $response = $this->userMasterPassService->create( $masterPass, - $itemData->getLogin(), + $user->getLogin(), $userPass ); - $itemData->setMPass($response->getCryptMasterPass()); - $itemData->setMKey($response->getCryptSecuredKey()); - $itemData->setLastUpdateMPass(time()); - $itemData->setPass($userPass); - - return $this->create($itemData); + return $this->create( + $user->mutate( + [ + 'mPass' => $response->getCryptMasterPass(), + 'mKey' => $response->getCryptSecuredKey(), + 'lastUpdateMPass' => time(), + 'pass' => $userPass + ] + ) + ); } /** @@ -257,12 +237,10 @@ final class UserService extends Service implements UserServiceInterface * @throws DuplicatedItemException * @throws ServiceException */ - public function update(UserModel $userData): void + public function update(UserModel $user): void { - $update = $this->userRepository->update($userData); - - if ($update === 0) { - throw new ServiceException(__u('Error while updating the user')); + if ($this->userRepository->update($user) === 0) { + throw ServiceException::error(__u('Error while updating the user')); } } @@ -275,30 +253,31 @@ final class UserService extends Service implements UserServiceInterface */ public function updatePass(int $userId, string $pass): void { - $passRequest = new UpdatePassRequest(Hash::hashKey($pass)); - $passRequest->setIsChangePass(false); - $passRequest->setIsChangedPass(true); - - $updatePassById = $this->userRepository->updatePassById( - $userId, - $passRequest + $user = new UserModel( + [ + 'id' => $userId, + 'pass' => Hash::hashKey($pass), + 'isChangePass' => false, + 'isChangedPass' => true + ] ); - if ($updatePassById === 0) { - throw new ServiceException(__u('Error while updating the password')); + if ($this->userRepository->updatePassById($user) === 0) { + throw ServiceException::error(__u('Error while updating the password')); } } /** + * @param int $userId + * @param UserPreferences $userPreferences + * @return int * @throws ConstraintException * @throws QueryException + * @throws JsonException */ - public function updatePreferencesById(int $userId, UserPreferences $userPreferencesData): int + public function updatePreferencesById(int $userId, UserPreferences $userPreferences): int { - return $this->userRepository->updatePreferencesById( - $userId, - $userPreferencesData - ); + return $this->userRepository->updatePreferencesById($userId, $userPreferences); } /** @@ -307,26 +286,26 @@ final class UserService extends Service implements UserServiceInterface */ public function updateOnLogin(UserLoginRequest $userLoginRequest): int { - $userData = new UserModel(); - $userData->setLogin($userLoginRequest->getLogin()); - $userData->setName($userLoginRequest->getName()); - $userData->setEmail($userLoginRequest->getEmail()); - $userData->setIsLdap($userLoginRequest->getisLdap()); - $userData->setPass(Hash::hashKey($userLoginRequest->getPassword())); + $userData = [ + 'login' => $userLoginRequest->getLogin(), + 'name' => $userLoginRequest->getName(), + 'email' => $userLoginRequest->getEmail(), + 'isLdap' => $userLoginRequest->getisLdap(), + 'pass' => Hash::hashKey($userLoginRequest->getPassword()) - return $this->userRepository->updateOnLogin($userData); + ]; + + return $this->userRepository->updateOnLogin(new UserModel($userData)); } /** * Get all items from the service's repository * - * @return UserModel[] - * @throws ConstraintException - * @throws QueryException + * @return array */ public function getAll(): array { - return $this->userRepository->getAll()->getDataAsArray(); + return $this->userRepository->getAll()->getDataAsArray(UserModel::class); } /** @@ -337,20 +316,17 @@ final class UserService extends Service implements UserServiceInterface */ public function getUserEmailForGroup(int $groupId): array { - return $this->userRepository->getUserEmailForGroup($groupId)->getDataAsArray(); + return $this->userRepository->getUserEmailForGroup($groupId)->getDataAsArray(UserModel::class); } /** * Obtener el email de los usuarios de un grupo * - * @throws ConstraintException - * @throws QueryException - * - * @TODO create unit test + * @return array */ public function getUserEmailForAll(): array { - return $this->userRepository->getUserEmail()->getDataAsArray(); + return $this->userRepository->getUserEmail()->getDataAsArray(UserModel::class); } @@ -359,13 +335,11 @@ final class UserService extends Service implements UserServiceInterface * * @param int[] $ids * - * @throws ConstraintException - * @throws QueryException - * @TODO create unit test + * @return array */ public function getUserEmailById(array $ids): array { - return $this->userRepository->getUserEmailById($ids)->getDataAsArray(); + return $this->userRepository->getUserEmailById($ids)->getDataAsArray(UserModel::class); } /** diff --git a/lib/SP/Providers/Auth/Database/DatabaseAuth.php b/lib/SP/Providers/Auth/Database/DatabaseAuth.php index 28fafb3b..f7336485 100644 --- a/lib/SP/Providers/Auth/Database/DatabaseAuth.php +++ b/lib/SP/Providers/Auth/Database/DatabaseAuth.php @@ -29,7 +29,7 @@ use SP\Core\Crypt\Hash; use SP\Domain\Auth\Dtos\UserLoginDto; use SP\Domain\User\Dtos\UserDataDto; use SP\Domain\User\Ports\UserPassService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use function SP\processException; @@ -39,8 +39,8 @@ use function SP\processException; final readonly class DatabaseAuth implements DatabaseAuthService { public function __construct( - private UserServiceInterface $userService, - private UserPassService $userPassService + private UserService $userService, + private UserPassService $userPassService ) { } diff --git a/tests/SPT/Domain/Account/Services/AccountSearchTest.php b/tests/SPT/Domain/Account/Services/AccountSearchTest.php index e53e0cc9..9419a9d1 100644 --- a/tests/SPT/Domain/Account/Services/AccountSearchTest.php +++ b/tests/SPT/Domain/Account/Services/AccountSearchTest.php @@ -40,7 +40,7 @@ use SP\Domain\Core\Exceptions\SPException; use SP\Domain\User\Models\User; use SP\Domain\User\Models\UserGroup; use SP\Domain\User\Ports\UserGroupService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Infrastructure\Database\QueryResult; use SPT\Domain\Account\Services\Builders\AccountSearchTokenizerDataTrait; use SPT\UnitaryTestCase; @@ -236,7 +236,7 @@ class AccountSearchTest extends UnitaryTestCase { parent::setUp(); - $userService = $this->createMock(UserServiceInterface::class); + $userService = $this->createMock(UserService::class); $userService ->method('getByLogin') ->willReturn( diff --git a/tests/SPT/Domain/Api/Services/ApiTest.php b/tests/SPT/Domain/Api/Services/ApiTest.php index 13a659ff..58ca83a7 100644 --- a/tests/SPT/Domain/Api/Services/ApiTest.php +++ b/tests/SPT/Domain/Api/Services/ApiTest.php @@ -48,7 +48,7 @@ use SP\Domain\Core\Exceptions\SPException; use SP\Domain\Security\Dtos\TrackRequest; use SP\Domain\Security\Ports\TrackService; use SP\Domain\User\Ports\UserProfileService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Modules\Api\Controllers\Help\AccountHelp; use SPT\Generators\UserDataGenerator; @@ -68,8 +68,8 @@ class ApiTest extends UnitaryTestCase private TrackService|MockObject $trackService; private ApiRequestService|MockObject $apiRequest; - private AuthTokenService|MockObject $authTokenService; - private UserServiceInterface|MockObject $userService; + private AuthTokenService|MockObject $authTokenService; + private UserService|MockObject $userService; private MockObject|UserProfileService $userProfileService; private Api $apiService; private TrackRequest $trackRequest; @@ -303,7 +303,7 @@ class ApiTest extends UnitaryTestCase $this->trackService = $this->createMock(TrackService::class); $this->apiRequest = $this->createMock(ApiRequestService::class); $this->authTokenService = $this->createMock(AuthTokenService::class); - $this->userService = $this->createMock(UserServiceInterface::class); + $this->userService = $this->createMock(UserService::class); $this->userProfileService = $this->createMock(UserProfileService::class); $this->trackRequest = new TrackRequest(time(), __CLASS__, self::$faker->ipv4()); diff --git a/tests/SPT/Domain/Crypt/Services/TemporaryMasterPassTest.php b/tests/SPT/Domain/Crypt/Services/TemporaryMasterPassTest.php index 55f20617..960f7fb0 100644 --- a/tests/SPT/Domain/Crypt/Services/TemporaryMasterPassTest.php +++ b/tests/SPT/Domain/Crypt/Services/TemporaryMasterPassTest.php @@ -40,7 +40,7 @@ use SP\Domain\Core\Exceptions\CryptException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Crypt\Services\TemporaryMasterPass; use SP\Domain\Notification\Ports\MailService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SPT\UnitaryTestCase; @@ -53,8 +53,8 @@ class TemporaryMasterPassTest extends UnitaryTestCase { private ConfigService|MockObject $configService; - private UserServiceInterface|MockObject $userService; - private MailService|MockObject $mailService; + private UserService|MockObject $userService; + private MailService|MockObject $mailService; private CryptInterface|MockObject $crypt; private TemporaryMasterPass $temporaryMasterPass; @@ -394,7 +394,7 @@ class TemporaryMasterPassTest extends UnitaryTestCase parent::setUp(); $this->configService = $this->createMock(ConfigService::class); - $this->userService = $this->createMock(UserServiceInterface::class); + $this->userService = $this->createMock(UserService::class); $this->mailService = $this->createMock(MailService::class); $this->crypt = $this->createMock(CryptInterface::class); diff --git a/tests/SPT/Domain/Import/Services/LdapImportTest.php b/tests/SPT/Domain/Import/Services/LdapImportTest.php index 4b159eac..8ef76d69 100644 --- a/tests/SPT/Domain/Import/Services/LdapImportTest.php +++ b/tests/SPT/Domain/Import/Services/LdapImportTest.php @@ -36,7 +36,7 @@ use SP\Domain\Import\Services\LdapImport; use SP\Domain\User\Models\User; use SP\Domain\User\Models\UserGroup; use SP\Domain\User\Ports\UserGroupService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Providers\Auth\Ldap\LdapException; use SP\Providers\Auth\Ldap\LdapParams; use SP\Providers\Auth\Ldap\LdapResults; @@ -51,9 +51,9 @@ use SPT\UnitaryTestCase; class LdapImportTest extends UnitaryTestCase { - private LdapImport $ldapImport; - private UserServiceInterface|MockObject $userService; - private UserGroupService|MockObject $userGroupService; + private LdapImport $ldapImport; + private UserService|MockObject $userService; + private UserGroupService|MockObject $userGroupService; private LdapActionsService|MockObject $ldapActionsService; private LdapConnectionInterface|MockObject $ldapConnection; @@ -574,7 +574,7 @@ class LdapImportTest extends UnitaryTestCase { parent::setUp(); - $this->userService = $this->createMock(UserServiceInterface::class); + $this->userService = $this->createMock(UserService::class); $this->userGroupService = $this->createMock(UserGroupService::class); $this->ldapActionsService = $this->createMock(LdapActionsService::class); $this->ldapConnection = $this->createMock(LdapConnectionInterface::class); diff --git a/tests/SPT/Domain/Install/Services/InstallerTest.php b/tests/SPT/Domain/Install/Services/InstallerTest.php index d63c467b..6393d1e1 100644 --- a/tests/SPT/Domain/Install/Services/InstallerTest.php +++ b/tests/SPT/Domain/Install/Services/InstallerTest.php @@ -38,8 +38,8 @@ use SP\Domain\Install\Services\DatabaseSetupInterface; use SP\Domain\Install\Services\InstallerService; use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserProfileService; -use SP\Domain\User\Ports\UserServiceInterface; -use SP\Domain\User\Services\UserService; +use SP\Domain\User\Ports\UserService; +use SP\Domain\User\Services\User; use SP\Infrastructure\Database\DatabaseConnectionData; use SP\Util\VersionUtil; use SPT\UnitaryTestCase; @@ -56,7 +56,7 @@ class InstallerTest extends UnitaryTestCase */ private $databaseSetup; /** - * @var MockObject|UserService + * @var MockObject|User */ private $userService; /** @@ -439,7 +439,7 @@ class InstallerTest extends UnitaryTestCase protected function setUp(): void { $this->databaseSetup = $this->createMock(DatabaseSetupInterface::class); - $this->userService = $this->createMock(UserServiceInterface::class); + $this->userService = $this->createMock(UserService::class); $this->request = $this->createStub(RequestInterface::class); $this->configService = $this->createMock(ConfigService::class); $this->userGroupService = $this->createMock(UserGroupService::class); diff --git a/tests/SPT/Domain/User/Services/UserTest.php b/tests/SPT/Domain/User/Services/UserTest.php new file mode 100644 index 00000000..315a5e37 --- /dev/null +++ b/tests/SPT/Domain/User/Services/UserTest.php @@ -0,0 +1,679 @@ +. + */ + +namespace SPT\Domain\User\Services; + +use JsonException; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\MockObject\MockObject; +use SP\Core\Crypt\Hash; +use SP\DataModel\ItemSearchData; +use SP\Domain\Common\Services\ServiceException; +use SP\Domain\Core\Exceptions\ConstraintException; +use SP\Domain\Core\Exceptions\QueryException; +use SP\Domain\Core\Exceptions\SPException; +use SP\Domain\User\Dtos\UserLoginRequest; +use SP\Domain\User\Dtos\UserMasterPassDto; +use SP\Domain\User\Models\User as UserModel; +use SP\Domain\User\Ports\UserMasterPassService; +use SP\Domain\User\Ports\UserRepository; +use SP\Domain\User\Services\User; +use SP\Domain\User\Services\UserMasterPassStatus; +use SP\Infrastructure\Common\Repositories\DuplicatedItemException; +use SP\Infrastructure\Common\Repositories\NoSuchItemException; +use SP\Infrastructure\Database\QueryResult; +use SP\Infrastructure\File\FileException; +use SPT\Generators\UserDataGenerator; +use SPT\UnitaryTestCase; + +/** + * Class UserTest + */ +#[Group('unitary')] +class UserTest extends UnitaryTestCase +{ + + private MockObject|UserRepository $userRepository; + private MockObject|UserMasterPassService $userMasterPassService; + private User $user; + + /** + * @throws ConstraintException + * @throws QueryException + */ + public function testSearch() + { + $user = UserDataGenerator::factory()->buildUserData(); + + $itemSearchData = new ItemSearchData( + self::$faker->text(), + self::$faker->randomNumber(2), + self::$faker->randomNumber(2) + ); + + $queryResult = new QueryResult([$user]); + + $this->userRepository + ->expects($this->once()) + ->method('search') + ->with($itemSearchData) + ->willReturn($queryResult); + + $this->assertEquals($queryResult, $this->user->search($itemSearchData)); + } + + /** + * @throws SPException + */ + public function testGetById() + { + $user = UserDataGenerator::factory()->buildUserData(); + + $queryResult = new QueryResult([$user]); + + $this->userRepository + ->expects($this->once()) + ->method('getById') + ->with(100) + ->willReturn($queryResult); + + $this->assertEquals($user, $this->user->getById(100)); + } + + /** + * @throws SPException + */ + public function testGetByIdWithNoItems() + { + $this->userRepository + ->expects($this->once()) + ->method('getById') + ->with(100) + ->willReturn(new QueryResult()); + + $this->expectException(NoSuchItemException::class); + $this->expectExceptionMessage('User does not exist'); + + $this->user->getById(100); + } + + + /** + * @throws ConstraintException + * @throws JsonException + * @throws QueryException + */ + public function testUpdatePreferencesById() + { + $userPreferences = UserDataGenerator::factory()->buildUserPreferencesData(); + + $this->userRepository + ->expects($this->once()) + ->method('updatePreferencesById') + ->with(100, $userPreferences) + ->willReturn(10); + + $out = $this->user->updatePreferencesById(100, $userPreferences); + + $this->assertEquals(10, $out); + } + + public function testGetAll() + { + $user = UserDataGenerator::factory()->buildUserData(); + + $queryResult = new QueryResult([$user]); + + $this->userRepository + ->expects($this->once()) + ->method('getAll') + ->willReturn($queryResult); + + $this->assertEquals([$user], $this->user->getAll()); + } + + /** + * @throws ConstraintException + * @throws ServiceException + * @throws QueryException + */ + public function testDeleteByIdBatch() + { + $this->userRepository + ->expects($this->once()) + ->method('deleteByIdBatch') + ->with([100, 200, 300]) + ->willReturn(new QueryResult(null, 3)); + + $this->assertEquals(3, $this->user->deleteByIdBatch([100, 200, 300])); + } + + /** + * @throws ConstraintException + * @throws ServiceException + * @throws QueryException + */ + public function testDeleteByIdBatchWithException() + { + $this->userRepository + ->expects($this->once()) + ->method('deleteByIdBatch') + ->with([100, 200, 300]) + ->willReturn(new QueryResult()); + + $this->expectException(ServiceException::class); + $this->expectExceptionMessage('Error while deleting the users'); + + $this->user->deleteByIdBatch([100, 200, 300]); + } + + /** + * @throws ServiceException + * @throws SPException + */ + public function testCreateWithMasterPass() + { + $user = UserDataGenerator::factory()->buildUserData(); + + $userMasterPassDto = new UserMasterPassDto( + UserMasterPassStatus::Ok, + 'a_master_pass', + 'a_crypt_master_pass', + 'a_secured_key' + ); + + $this->userMasterPassService + ->expects($this->once()) + ->method('create') + ->with('a_master_pass', $user->getLogin(), 'a_password') + ->willReturn($userMasterPassDto); + + $this->userRepository + ->expects($this->once()) + ->method('create') + ->with( + self::callback(static function (UserModel $targetUser) use ($user) { + $targetUserProps = $targetUser->toArray(null, ['mPass', 'mKey', 'pass', 'lastUpdateMPass']); + $userProps = $user->toArray(null, ['mPass', 'mKey', 'pass', 'lastUpdateMPass']); + + return $targetUser->getMPass() === 'a_crypt_master_pass' + && $targetUser->getMKey() === 'a_secured_key' + && Hash::checkHashKey('a_password', $targetUser->getPass()) + && $targetUserProps === $userProps; + }) + ) + ->willReturn(new QueryResult(null, 0, 10)); + + $this->user->createWithMasterPass($user, 'a_password', 'a_master_pass'); + } + + public function testGetUserEmailForAll() + { + $user = UserDataGenerator::factory()->buildUserData(); + + $this->userRepository + ->expects($this->once()) + ->method('getUserEmail') + ->willReturn(new QueryResult([$user])); + + $this->assertEquals([$user], $this->user->getUserEmailForAll()); + } + + /** + * @throws ConstraintException + * @throws ServiceException + * @throws QueryException + */ + public function testUpdatePass() + { + $this->userRepository + ->expects($this->once()) + ->method('updatePassById') + ->with( + self::callback(static function (UserModel $user) { + return $user->getId() === 100 + && Hash::checkHashKey('a_password', $user->getPass()) + && $user->isChangedPass() === true + && $user->isChangePass() === false; + }) + ) + ->willReturn(1); + + $this->user->updatePass(100, 'a_password'); + } + + /** + * @throws ConstraintException + * @throws ServiceException + * @throws QueryException + */ + public function testUpdatePassWithException() + { + $this->userRepository + ->expects($this->once()) + ->method('updatePassById') + ->willReturn(0); + + $this->expectException(ServiceException::class); + $this->expectExceptionMessage('Error while updating the password'); + + $this->user->updatePass(100, 'a_password'); + } + + /** + * @throws ConstraintException + * @throws QueryException + */ + public function testGetUserEmailForGroup() + { + $user = UserDataGenerator::factory()->buildUserData(); + + $this->userRepository + ->expects($this->once()) + ->method('getUserEmailForGroup') + ->with(100) + ->willReturn(new QueryResult([$user])); + + $this->assertEquals([$user], $this->user->getUserEmailForGroup(100)); + } + + /** + * @throws FileException + * @throws SPException + */ + public function testCreateOnLoginWithLdap() + { + $userLoginRequest = new UserLoginRequest( + self::$faker->userName(), + self::$faker->password(), + self::$faker->name(), + self::$faker->email(), + true + ); + + $configData = $this->config->getConfigData(); + $configData->setLdapDefaultGroup(100); + $configData->setLdapDefaultProfile(200); + + $this->config->save($configData); + + $this->userRepository + ->expects($this->once()) + ->method('create') + ->with( + self::callback(static function (UserModel $user) use ($userLoginRequest) { + return $user->getLogin() === $userLoginRequest->getLogin() + && $user->getName() === $userLoginRequest->getName() + && Hash::checkHashKey($userLoginRequest->getPassword(), $user->getPass()) + && $user->getEmail() === $userLoginRequest->getEmail() + && $user->isLdap() === $userLoginRequest->getisLdap() + && $user->getUserGroupId() === 100 + && $user->getUserProfileId() === 200; + }) + ); + + $this->user->createOnLogin($userLoginRequest); + } + + /** + * @throws FileException + * @throws SPException + */ + public function testCreateOnLoginWithNoLdap() + { + $userLoginRequest = new UserLoginRequest( + self::$faker->userName(), + self::$faker->password(), + self::$faker->name(), + self::$faker->email(), + false + ); + + $configData = $this->config->getConfigData(); + $configData->setSsoDefaultGroup(101); + $configData->setSsoDefaultProfile(201); + + $this->config->save($configData); + + $this->userRepository + ->expects($this->once()) + ->method('create') + ->with( + self::callback(static function (UserModel $user) use ($userLoginRequest) { + return $user->getLogin() === $userLoginRequest->getLogin() + && $user->getName() === $userLoginRequest->getName() + && Hash::checkHashKey($userLoginRequest->getPassword(), $user->getPass()) + && $user->getEmail() === $userLoginRequest->getEmail() + && $user->isLdap() === $userLoginRequest->getisLdap() + && $user->getUserGroupId() === 101 + && $user->getUserProfileId() === 201; + }) + ); + + $this->user->createOnLogin($userLoginRequest); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testGetByLogin() + { + $user = UserDataGenerator::factory()->buildUserData(); + + $this->userRepository + ->expects($this->once()) + ->method('getByLogin') + ->with('a_login') + ->willReturn(new QueryResult([$user])); + + $this->assertEquals($user, $this->user->getByLogin('a_login')); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testGetByLoginWithException() + { + $user = UserDataGenerator::factory()->buildUserData(); + + $this->userRepository + ->expects($this->once()) + ->method('getByLogin') + ->with('a_login') + ->willReturn(new QueryResult()); + + $this->expectException(NoSuchItemException::class); + $this->expectExceptionMessage('User not found'); + + $this->user->getByLogin('a_login'); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testUpdateLastLoginById() + { + $this->userRepository + ->expects($this->once()) + ->method('updateLastLoginById') + ->with(100) + ->willReturn(10); + + $this->user->updateLastLoginById(100); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testUpdateLastLoginByIdWithException() + { + $this->userRepository + ->expects($this->once()) + ->method('updateLastLoginById') + ->with(100) + ->willReturn(0); + + $this->expectException(NoSuchItemException::class); + $this->expectExceptionMessage('User not found'); + + $this->user->updateLastLoginById(100); + } + + /** + * @throws ConstraintException + * @throws QueryException + */ + public function testCheckExistsByLogin() + { + $this->userRepository + ->expects($this->once()) + ->method('checkExistsByLogin') + ->with('a_login') + ->willReturn(true); + + $this->assertTrue($this->user->checkExistsByLogin('a_login')); + } + + /** + * @throws ConstraintException + * @throws QueryException + */ + public function testCheckExistsByLoginWithFalse() + { + $this->userRepository + ->expects($this->once()) + ->method('checkExistsByLogin') + ->with('a_login') + ->willReturn(false); + + $this->assertFalse($this->user->checkExistsByLogin('a_login')); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testDelete() + { + $this->userRepository + ->expects($this->once()) + ->method('delete') + ->with(100) + ->willReturn(new QueryResult(null, 1)); + + $this->user->delete(100); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testDeleteWithNoResult() + { + $this->userRepository + ->expects($this->once()) + ->method('delete') + ->with(100) + ->willReturn(new QueryResult()); + + $this->expectException(NoSuchItemException::class); + $this->expectExceptionMessage('User not found'); + + $this->user->delete(100); + } + + /** + * @throws SPException + */ + public function testCreate() + { + $user = UserDataGenerator::factory()->buildUserData(); + + $this->userRepository + ->expects($this->once()) + ->method('create') + ->with( + self::callback(static function (UserModel $input) use ($user) { + return $input->toArray(null, ['pass']) === $user->toArray(null, ['pass']) + && Hash::checkHashKey($user->getPass(), $input->getPass()); + }) + ) + ->willReturn(new QueryResult(null, 0, 10)); + + $this->assertEquals(10, $this->user->create($user)); + } + + /** + * @throws ConstraintException + * @throws QueryException + */ + public function testUpdateOnLoginWithLdap() + { + $userLoginRequest = new UserLoginRequest( + self::$faker->userName(), + self::$faker->password(), + self::$faker->name(), + self::$faker->email(), + true + ); + + $this->userRepository + ->expects($this->once()) + ->method('updateOnLogin') + ->with( + self::callback(static function (UserModel $user) use ($userLoginRequest) { + return $user->getLogin() === $userLoginRequest->getLogin() + && $user->getName() === $userLoginRequest->getName() + && Hash::checkHashKey($userLoginRequest->getPassword(), $user->getPass()) + && $user->getEmail() === $userLoginRequest->getEmail() + && $user->isLdap() === $userLoginRequest->getisLdap(); + }) + ); + + $this->user->updateOnLogin($userLoginRequest); + } + + /** + * @throws ConstraintException + * @throws QueryException + */ + public function testUpdateOnLoginWithNoLdap() + { + $userLoginRequest = new UserLoginRequest( + self::$faker->userName(), + self::$faker->password(), + self::$faker->name(), + self::$faker->email(), + false + ); + + $this->userRepository + ->expects($this->once()) + ->method('updateOnLogin') + ->with( + self::callback(static function (UserModel $user) use ($userLoginRequest) { + return $user->getLogin() === $userLoginRequest->getLogin() + && $user->getName() === $userLoginRequest->getName() + && Hash::checkHashKey($userLoginRequest->getPassword(), $user->getPass()) + && $user->getEmail() === $userLoginRequest->getEmail() + && $user->isLdap() === $userLoginRequest->getisLdap(); + }) + ); + + $this->user->updateOnLogin($userLoginRequest); + } + + public function testGetUserEmailById() + { + $user = UserDataGenerator::factory()->buildUserData(); + + $this->userRepository + ->expects($this->once()) + ->method('getUserEmailById') + ->with([100, 200]) + ->willReturn(new QueryResult([$user])); + + $out = $this->user->getUserEmailById([100, 200]); + + $this->assertEquals([$user], $out); + } + + /** + * @throws ServiceException + * @throws ConstraintException + * @throws DuplicatedItemException + * @throws QueryException + */ + public function testUpdate() + { + $user = UserDataGenerator::factory()->buildUserData(); + + $this->userRepository + ->expects($this->once()) + ->method('update') + ->with($user) + ->willReturn(10); + + $this->user->update($user); + } + + /** + * @throws ServiceException + * @throws ConstraintException + * @throws DuplicatedItemException + * @throws QueryException + */ + public function testUpdateWithException() + { + $user = UserDataGenerator::factory()->buildUserData(); + + $this->userRepository + ->expects($this->once()) + ->method('update') + ->with($user) + ->willReturn(0); + + $this->expectException(ServiceException::class); + $this->expectExceptionMessage('Error while updating the user'); + + $this->user->update($user); + } + + /** + * @throws ConstraintException + * @throws QueryException + */ + public function testGetUsageForUser() + { + $this->userRepository + ->expects($this->once()) + ->method('getUsageForUser') + ->with(100) + ->willReturn(new QueryResult([1])); + + $out = $this->user->getUsageForUser(100); + + $this->assertEquals([1], $out); + } + + protected function setUp(): void + { + parent::setUp(); + + $this->userRepository = $this->createMock(UserRepository::class); + $this->userMasterPassService = $this->createMock(UserMasterPassService::class); + + $this->user = new User($this->application, $this->userRepository, $this->userMasterPassService); + } +} diff --git a/tests/SPT/Providers/Auth/Database/DatabaseAuthTest.php b/tests/SPT/Providers/Auth/Database/DatabaseAuthTest.php index 5433a28b..e6e5082b 100644 --- a/tests/SPT/Providers/Auth/Database/DatabaseAuthTest.php +++ b/tests/SPT/Providers/Auth/Database/DatabaseAuthTest.php @@ -29,7 +29,7 @@ use PHPUnit\Framework\MockObject\MockObject; use SP\Core\Crypt\Hash; use SP\Domain\Auth\Dtos\UserLoginDto; use SP\Domain\User\Ports\UserPassService; -use SP\Domain\User\Ports\UserServiceInterface; +use SP\Domain\User\Ports\UserService; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Providers\Auth\Database\DatabaseAuth; use SPT\Generators\UserDataGenerator; @@ -43,8 +43,8 @@ use SPT\UnitaryTestCase; class DatabaseAuthTest extends UnitaryTestCase { - private UserServiceInterface|MockObject $userService; - private MockObject|UserPassService $userPassService; + private UserService|MockObject $userService; + private MockObject|UserPassService $userPassService; private DatabaseAuth $databaseAuth; public function testAuthenticate() @@ -252,7 +252,7 @@ class DatabaseAuthTest extends UnitaryTestCase { parent::setUp(); - $this->userService = $this->createMock(UserServiceInterface::class); + $this->userService = $this->createMock(UserService::class); $this->userPassService = $this->createMock(UserPassService::class); $this->databaseAuth = new DatabaseAuth($this->userService, $this->userPassService);