Skip to content

Commit

Permalink
PIN-3515 Resolved missing PR after deep analysis from SkyNET
Browse files Browse the repository at this point in the history
  • Loading branch information
nttdata-rtorsoli committed Oct 25, 2023
1 parent e5db9e3 commit b3bec97
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, *, *)
Expand Down Expand Up @@ -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"
}
}
}
}

0 comments on commit b3bec97

Please sign in to comment.