Skip to content

Commit

Permalink
PIN-3515 Resolved PR issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nttdata-rtorsoli committed Oct 25, 2023
1 parent 5d69d15 commit d826e9b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import it.pagopa.interop.authorizationprocess.common.Adapters._
import it.pagopa.interop.authorizationprocess.common.AuthorizationUtils._
import it.pagopa.interop.authorizationprocess.error.AuthorizationProcessErrors._
import it.pagopa.interop.authorizationprocess.model._
import it.pagopa.interop.authorizationprocess.service.model.{UserResource => CommonUserResource}
import it.pagopa.interop.authorizationprocess.service._
import it.pagopa.interop.authorizationprocess.service.SelfcareV2ClientService
import it.pagopa.interop.catalogmanagement.model.{CatalogDescriptor, Published, Deprecated => DeprecatedState}
Expand Down Expand Up @@ -165,9 +164,9 @@ final case class ClientApiServiceImpl(
client <- authorizationManagementService
.getClient(clientUuid)
.ensureOr(client => OrganizationNotAllowedOnClient(clientId, client.consumerId))(_.consumerId == requesterOrgId)
userApi <- getSecurityUser(selfcareId, requesterUserId, userUUID)
_ <- getSecurityUser(selfcareId, requesterUserId, userUUID)
updatedClient <- client.users
.find(_ === userApi.id)
.find(_ === userUUID)
.fold(authorizationManagementService.addUser(clientUuid, userUUID)(contexts))(_ =>
Future.failed(UserAlreadyAssigned(client.id, userUUID))
)
Expand All @@ -186,18 +185,11 @@ final case class ClientApiServiceImpl(
logger.info(operationLabel)

val result: Future[Unit] = for {
requesterUserId <- getUidFutureUUID(contexts)
selfcareId <- getSelfcareIdFutureUUID(contexts)
clientUUID <- clientId.toFutureUUID
client <- authorizationManagementService.getClient(clientUUID)
_ <- assertIsClientConsumer(client).toFuture
userUUID <- userId.toFutureUUID
users <- selfcareV2ClientService
.getInstitutionProductUsers(selfcareId, requesterUserId, userUUID.some, Seq.empty)
.map(_.map(_.toApi))
usersApi <- users.traverse(_.toFuture)
_ <- usersApi.headOption.toFuture(UserNotFound(selfcareId, userUUID))
_ <- authorizationManagementService.removeUser(clientUUID, userUUID)(contexts)
clientUUID <- clientId.toFutureUUID
client <- authorizationManagementService.getClient(clientUUID)
_ <- assertIsClientConsumer(client).toFuture
userUUID <- userId.toFutureUUID
_ <- authorizationManagementService.removeUser(clientUUID, userUUID)(contexts)
} yield ()

onComplete(result) {
Expand Down Expand Up @@ -433,13 +425,12 @@ final case class ClientApiServiceImpl(
requesterUserId: UUID,
userId: UUID,
roles: Seq[String] = Seq(SECURITY_ROLE, ADMIN_ROLE)
)(implicit contexts: Seq[(String, String)]): Future[CommonUserResource] = for {
users <- selfcareV2ClientService
)(implicit contexts: Seq[(String, String)]): Future[Unit] = for {
users <- selfcareV2ClientService
.getInstitutionProductUsers(selfcareId, requesterUserId, userId.some, roles)
.map(_.map(_.toApi))
usersApi <- users.traverse(_.toFuture)
user <- usersApi.headOption.toFuture(SecurityUserNotFound(requesterUserId, userId))
} yield user
_ <- users.headOption.toFuture(SecurityUserNotFound(requesterUserId, userId))
} yield ()

override def getClients(
name: Option[String],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,23 +207,12 @@ class UserOperationSpec extends AnyWordSpecLike with MockFactory with SpecUtilsW
"User removal" should {
"succeed" in {

val results: Seq[UserResource] = Seq(userResource)

(mockAuthorizationManagementService
.getClient(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(*, *, *)
.once()
.returns(Future.successful(persistentClient))

(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
.removeUser(_: UUID, _: UUID)(_: Seq[(String, String)]))
.expects(persistentClient.id, userId, *)
Expand All @@ -243,83 +232,6 @@ class UserOperationSpec extends AnyWordSpecLike with MockFactory with SpecUtilsW
responseAs[Problem].errors.head.code shouldEqual "007-9989"
}
}

"fail if Institution not found" in {

val results: Seq[UserResource] = Seq(userResource)

(mockAuthorizationManagementService
.getClient(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(*, *, *)
.once()
.returns(Future.successful(persistentClient))

(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
.removeUser(_: UUID, _: UUID)(_: Seq[(String, String)]))
.expects(persistentClient.id, userId, *)
.once()
.returns(Future.failed(InstitutionNotFound(selfcareId)))

Delete() ~> service.removeUser(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(*, *, *)
.once()
.returns(Future.successful(persistentClient))

(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))

Delete() ~> service.removeUser(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(*, *, *)
.once()
.returns(Future.successful(persistentClient))

(mockSelfcareV2ClientService
.getInstitutionProductUsers(_: UUID, _: UUID, _: Option[UUID], _: Seq[String])(
_: Seq[(String, String)],
_: ExecutionContext
))
.expects(selfcareId, personId, userId.some, Seq.empty, *, *)
.once()
.returns(Future.successful(results))

Delete() ~> service.removeUser(persistentClient.id.toString, userId.toString) ~> check {
status shouldEqual StatusCodes.InternalServerError
responseAs[Problem].errors.head.code shouldEqual "007-9991"
}
}
}

"User retrieve" should {
Expand Down

0 comments on commit d826e9b

Please sign in to comment.