Skip to content

Commit

Permalink
Merge pull request #230 from pagopa/PIN-4485
Browse files Browse the repository at this point in the history
PIN-4485 BKE- The update of the label of a document should not be allowed if the descriptor is not draft
  • Loading branch information
nttdata-rtorsoli authored Feb 2, 2024
2 parents ab4f25d + 342d060 commit 9848a80
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ final case class ProcessApiServiceImpl(
documentUuid <- documentId.toFutureUUID
catalogItem <- catalogManagementService.getEServiceById(eServiceUuid)
_ <- assertRequesterAllowed(catalogItem.producerId)(organizationId)
descriptor <- assertDescriptorExists(catalogItem, descriptorUuid)
_ <- isDraftDescriptor(descriptor)
result <- catalogManagementService
.updateEServiceDocument(eServiceId, descriptorUuid.toString, documentUuid.toString, clientSeed)
.map(_.toApi)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ object ResponseHandlers extends AkkaResponses {
)(result: Try[T])(implicit contexts: Seq[(String, String)], logger: LoggerTakingImplicit[ContextFieldsToLog]): Route =
result match {
case Success(s) => success(s)
case Failure(ex: NotValidDescriptor) => badRequest(ex, logMessage)
case Failure(ex: OperationForbidden.type) => forbidden(ex, logMessage)
case Failure(ex: EServiceNotFound) => notFound(ex, logMessage)
case Failure(ex: EServiceDescriptorNotFound) => notFound(ex, logMessage)
case Failure(ex: DescriptorDocumentNotFound) => notFound(ex, logMessage)
case Failure(ex) => internalServerError(ex, logMessage)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2748,7 +2748,7 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
}
}
"Document update" should {
"succeed" in {
"succeed on draft descriptor" in {
val requesterId = UUID.randomUUID()
val descriptorId = UUID.randomUUID()
val documentId = UUID.randomUUID()
Expand All @@ -2757,7 +2757,11 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> requesterId.toString)

val descriptor =
SpecData.catalogDescriptor.copy(id = descriptorId, docs = Seq(SpecData.catalogDocument.copy(id = documentId)))
SpecData.catalogDescriptor.copy(
id = descriptorId,
state = Draft,
docs = Seq(SpecData.catalogDocument.copy(id = documentId))
)

val eService = SpecData.catalogItem.copy(descriptors = Seq(descriptor), producerId = requesterId)

Expand Down Expand Up @@ -2792,6 +2796,36 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
status shouldEqual StatusCodes.OK
}
}
"fail if descriptor is not draft" in {
val requesterId = UUID.randomUUID()
val descriptorId = UUID.randomUUID()
val documentId = UUID.randomUUID()

implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> requesterId.toString)

val descriptor =
SpecData.catalogDescriptor.copy(id = descriptorId, docs = Seq(SpecData.catalogDocument.copy(id = documentId)))

val eService = SpecData.catalogItem.copy(descriptors = Seq(descriptor), producerId = requesterId)

val seed = UpdateEServiceDescriptorDocumentSeed(prettyName = "prettyNameUpdated")

(mockCatalogManagementService
.getEServiceById(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(SpecData.catalogItem.id, *, *)
.once()
.returns(Future.successful(eService))

Post() ~> service.updateEServiceDocumentById(
SpecData.catalogItem.id.toString,
descriptorId.toString,
documentId.toString,
seed
) ~> check {
status shouldEqual StatusCodes.BadRequest
}
}
"fail if EService does not exist" in {

val requesterId = UUID.randomUUID()
Expand All @@ -2815,6 +2849,31 @@ class CatalogProcessSpec extends SpecHelper with AnyWordSpecLike with ScalatestR
status shouldEqual StatusCodes.NotFound
}
}
"fail if descriptor does not exist" in {

val requesterId = UUID.randomUUID()
val eService = SpecData.catalogItem.copy(descriptors = Seq.empty, producerId = requesterId)

implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> requesterId.toString)

val seed = UpdateEServiceDescriptorDocumentSeed(prettyName = "prettyNameUpdated")

(mockCatalogManagementService
.getEServiceById(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(SpecData.catalogItem.id, *, *)
.once()
.returns(Future.successful(eService))

Post() ~> service.updateEServiceDocumentById(
SpecData.catalogItem.id.toString,
UUID.randomUUID().toString,
UUID.randomUUID().toString,
seed
) ~> check {
status shouldEqual StatusCodes.NotFound
}
}
"fail if requester is not the Producer" in {
implicit val context: Seq[(String, String)] =
Seq("bearer" -> bearerToken, USER_ROLES -> "admin", ORGANIZATION_ID_CLAIM -> UUID.randomUUID().toString)
Expand Down

0 comments on commit 9848a80

Please sign in to comment.