diff --git a/src/main/scala/it/pagopa/interop/authorizationprocess/api/impl/ClientApiServiceImpl.scala b/src/main/scala/it/pagopa/interop/authorizationprocess/api/impl/ClientApiServiceImpl.scala index fff3f3b7..36ec9543 100644 --- a/src/main/scala/it/pagopa/interop/authorizationprocess/api/impl/ClientApiServiceImpl.scala +++ b/src/main/scala/it/pagopa/interop/authorizationprocess/api/impl/ClientApiServiceImpl.scala @@ -473,8 +473,8 @@ final case class ClientApiServiceImpl( } - private def checkAuthorizationForRoles(roles: String, userIds: String, userId: UUID): Future[List[UUID]] = { - if (roles.contains(SECURITY_ROLE)) Future.successful(userId :: Nil) + private def checkAuthorizationForRoles(roles: String, userIds: String, requesterUserId: UUID): Future[List[UUID]] = { + if (roles.contains(SECURITY_ROLE)) Future.successful(requesterUserId :: Nil) else parseArrayParameters(userIds).traverse(_.toFutureUUID) } diff --git a/src/main/scala/it/pagopa/interop/authorizationprocess/api/impl/UserApiServiceImpl.scala b/src/main/scala/it/pagopa/interop/authorizationprocess/api/impl/UserApiServiceImpl.scala index c85a9eb6..72186806 100644 --- a/src/main/scala/it/pagopa/interop/authorizationprocess/api/impl/UserApiServiceImpl.scala +++ b/src/main/scala/it/pagopa/interop/authorizationprocess/api/impl/UserApiServiceImpl.scala @@ -13,11 +13,9 @@ import it.pagopa.interop.commons.jwt.{ADMIN_ROLE, SECURITY_ROLE, SUPPORT_ROLE, a import it.pagopa.interop.commons.logging.{CanLogContextFields, ContextFieldsToLog} import it.pagopa.interop.selfcare.v2.client.model.{Problem => _} import it.pagopa.interop.commons.utils.TypeConversions._ -import it.pagopa.interop.commons.utils.AkkaUtils._ import it.pagopa.interop.authorizationprocess.common.Adapters._ import it.pagopa.interop.commons.cqrs.service.ReadModelService import it.pagopa.interop.authorizationprocess.common.AuthorizationUtils._ -import it.pagopa.interop.authorizationprocess.error.AuthorizationProcessErrors._ import scala.concurrent.{ExecutionContext, Future} @@ -39,19 +37,12 @@ final case class UserApiServiceImpl( logger.info(operationLabel) val result: Future[Keys] = for { - clientUuid <- clientId.toFutureUUID - requestedUserUuid <- getUidFutureUUID(contexts) - selfcareId <- getSelfcareIdFutureUUID(contexts) - client <- authorizationManagementService.getClient(clientUuid) - _ <- assertIsClientConsumer(client).toFuture - userUuid <- userId.toFutureUUID - users <- selfcareV2ClientService - .getInstitutionProductUsers(selfcareId, requestedUserUuid, userUuid.some, Seq.empty) - .map(_.map(_.toApi)) - usersApi <- users.traverse(_.toFuture) - user <- usersApi.headOption.toFuture(UserNotFound(selfcareId, userUuid)) - clientKeys <- authorizationManagementService.getClientKeys(client.id) - apiKeys = clientKeys.filter(user.id.some == _.userId).map(_.toApi) + clientUuid <- clientId.toFutureUUID + client <- authorizationManagementService.getClient(clientUuid) + _ <- assertIsClientConsumer(client).toFuture + userUuid <- userId.toFutureUUID + clientKeys <- authorizationManagementService.getClientKeys(client.id) + apiKeys = clientKeys.filter(userUuid.some == _.userId).map(_.toApi) keys <- apiKeys.traverse(_.toFuture) } yield Keys(keys = keys) diff --git a/src/test/scala/it/pagopa/interop/authorizationprocess/UserOperationSpec.scala b/src/test/scala/it/pagopa/interop/authorizationprocess/UserOperationSpec.scala index 9f270ac4..a6df17bc 100644 --- a/src/test/scala/it/pagopa/interop/authorizationprocess/UserOperationSpec.scala +++ b/src/test/scala/it/pagopa/interop/authorizationprocess/UserOperationSpec.scala @@ -277,23 +277,12 @@ class UserOperationSpec extends AnyWordSpecLike with MockFactory with SpecUtilsW "User retrieve keys" should { "succeed" in { - val results: Seq[UserResource] = Seq(userResource) - (mockAuthorizationManagementService .getClient(_: UUID)(_: ExecutionContext, _: ReadModelService)) .expects(persistentClient.id, *, *) .once() .returns(Future.successful(persistentClient.copy(users = Set(userId)))) - (mockSelfcareV2ClientService - .getInstitutionProductUsers(_: UUID, _: UUID, _: Option[UUID], _: Seq[String])( - _: Seq[(String, String)], - _: ExecutionContext - )) - .expects(selfcareId, personId, userId.some, Seq.empty, *, *) - .once() - .returns(Future.successful(results)) - (mockAuthorizationManagementService .getClientKeys(_: UUID)(_: ExecutionContext, _: ReadModelService)) .expects(persistentClient.id, *, *) @@ -332,73 +321,5 @@ class UserOperationSpec extends AnyWordSpecLike with MockFactory with SpecUtilsW } } - "fail if Institution not found" in { - - (mockAuthorizationManagementService - .getClient(_: UUID)(_: ExecutionContext, _: ReadModelService)) - .expects(persistentClient.id, *, *) - .once() - .returns(Future.successful(persistentClient.copy(users = Set(userId)))) - - (mockSelfcareV2ClientService - .getInstitutionProductUsers(_: UUID, _: UUID, _: Option[UUID], _: Seq[String])( - _: Seq[(String, String)], - _: ExecutionContext - )) - .expects(selfcareId, personId, userId.some, Seq.empty, *, *) - .once() - .returns(Future.failed(InstitutionNotFound(selfcareId))) - - Get() ~> serviceUser.getClientUserKeys(persistentClient.id.toString, userId.toString) ~> check { - status shouldEqual StatusCodes.InternalServerError - responseAs[Problem].errors.head.code shouldEqual "007-9991" - } - } - "fail if User not found" in { - - (mockAuthorizationManagementService - .getClient(_: UUID)(_: ExecutionContext, _: ReadModelService)) - .expects(persistentClient.id, *, *) - .once() - .returns(Future.successful(persistentClient.copy(users = Set(userId)))) - - (mockSelfcareV2ClientService - .getInstitutionProductUsers(_: UUID, _: UUID, _: Option[UUID], _: Seq[String])( - _: Seq[(String, String)], - _: ExecutionContext - )) - .expects(selfcareId, personId, userId.some, Seq.empty, *, *) - .once() - .returns(Future.successful(Seq.empty)) - - Get() ~> serviceUser.getClientUserKeys(persistentClient.id.toString, userId.toString) ~> check { - status shouldEqual StatusCodes.InternalServerError - responseAs[Problem].errors.head.code shouldEqual "007-9991" - } - } - "fail if User has empty fields" in { - - val results: Seq[UserResource] = Seq(emptyUserResource) - - (mockAuthorizationManagementService - .getClient(_: UUID)(_: ExecutionContext, _: ReadModelService)) - .expects(persistentClient.id, *, *) - .once() - .returns(Future.successful(persistentClient.copy(users = Set(userId)))) - - (mockSelfcareV2ClientService - .getInstitutionProductUsers(_: UUID, _: UUID, _: Option[UUID], _: Seq[String])( - _: Seq[(String, String)], - _: ExecutionContext - )) - .expects(selfcareId, personId, userId.some, Seq.empty, *, *) - .once() - .returns(Future.successful(results)) - - Get() ~> serviceUser.getClientUserKeys(persistentClient.id.toString, userId.toString) ~> check { - status shouldEqual StatusCodes.InternalServerError - responseAs[Problem].errors.head.code shouldEqual "007-9991" - } - } } }