Skip to content

Commit

Permalink
PIN-4804: check purpose version state when adding client purpose (#242)
Browse files Browse the repository at this point in the history
Co-authored-by: nttdata-rtorsoli <[email protected]>
  • Loading branch information
nttdata-rtorsoli and nttdata-rtorsoli authored Apr 23, 2024
1 parent cbf5e8b commit 60d1082
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ import it.pagopa.interop.commons.utils.TypeConversions.{EitherOps, OptionOps, St
import it.pagopa.interop.commons.utils.service.OffsetDateTimeSupplier
import it.pagopa.interop.purposemanagement.model.purpose.{
Archived,
Rejected,
Draft,
PersistentPurposeVersion,
PersistentPurposeVersionState,
Active => ActiveState
Active => ActiveState,
WaitingForApproval
}

import java.util.UUID
Expand Down Expand Up @@ -330,7 +333,7 @@ final case class ClientApiServiceImpl(
Set[PersistentAgreementState](Active, Suspended)

val invalidPurposeStates: Set[PersistentPurposeVersionState] =
Set(Archived)
Set(Archived, Rejected, Draft, WaitingForApproval)

def descriptorToComponentState(
descriptor: CatalogDescriptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,4 @@ object AuthorizationProcessErrors {
"0024",
s"The number of the keys ${size.toString} for the client ${clientId.toString} exceed maximun allowed"
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import it.pagopa.interop.authorizationprocess.util.SpecUtilsWithImplicit
import it.pagopa.interop.authorizationprocess.error.AuthorizationProcessErrors._
import it.pagopa.interop.commons.cqrs.service.ReadModelService
import it.pagopa.interop.catalogmanagement.model.Published
import it.pagopa.interop.purposemanagement.model.purpose.{Draft, Active => ActivePurpose, Archived}
import it.pagopa.interop.purposemanagement.model.purpose.{
Active => ActivePurpose,
WaitingForApproval,
Archived,
Draft,
Suspended => SuspendedPurpose
}
import it.pagopa.interop.agreementmanagement.model.agreement.{Pending, Active, Suspended}
import org.scalamock.scalatest.MockFactory
import org.scalatest.matchers.should.Matchers._
Expand All @@ -31,7 +37,7 @@ class PurposeOperationSpec extends AnyWordSpecLike with MockFactory with SpecUti
)(ExecutionContext.global, mockReadModel)

"Purpose add to Client" should {
"succeed" in {
"succeed if Purpose has Active versions" in {

val purposeSeed = AuthorizationManagementDependency.PurposeSeed(states =
AuthorizationManagementDependency.ClientStatesChainSeed(
Expand Down Expand Up @@ -191,7 +197,7 @@ class PurposeOperationSpec extends AnyWordSpecLike with MockFactory with SpecUti
}
}

"succeed even if Purpose has only draft versions" in {
"succeed even if Purpose has Suspended versions" in {

(mockAuthorizationManagementService
.getClient(_: UUID)(_: ExecutionContext, _: ReadModelService))
Expand All @@ -206,7 +212,56 @@ class PurposeOperationSpec extends AnyWordSpecLike with MockFactory with SpecUti
.returns(
Future.successful(
purpose
.copy(consumerId = consumerId, versions = Seq(purposeVersion.copy(state = Draft)))
.copy(consumerId = consumerId, versions = Seq(purposeVersion.copy(state = SuspendedPurpose)))
)
)

(mockCatalogManagementService
.getEServiceById(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(eService.id, *, *)
.once()
.returns(Future.successful(eService.copy(descriptors = Seq(activeDescriptor.copy(state = Published)))))

(mockAgreementManagementService
.getAgreements(_: UUID, _: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(eService.id, consumer.id, *, *)
.once()
.returns(Future.successful(Seq(agreement.copy(state = Active))))

(mockAuthorizationManagementService
.addClientPurpose(_: UUID, _: AuthorizationManagementDependency.PurposeSeed)(_: Seq[(String, String)]))
.expects(persistentClient.id, *, *)
.once()
.returns(Future.successful(clientPurpose))

Post() ~> service.addClientPurpose(persistentClient.id.toString, PurposeAdditionDetails(purpose.id)) ~> check {
status shouldEqual StatusCodes.NoContent
}
}

"succeed even if Purpose has Waiting for Approval versions" in {

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

(mockPurposeManagementService
.getPurposeById(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(purpose.id, *, *)
.once()
.returns(
Future.successful(
purpose
.copy(
consumerId = consumerId,
versions = Seq(
purposeVersion
.copy(id = UUID.randomUUID(), state = ActivePurpose, createdAt = timestamp.minusDays(5)),
purposeVersion.copy(state = WaitingForApproval)
)
)
)
)

Expand Down Expand Up @@ -270,4 +325,41 @@ class PurposeOperationSpec extends AnyWordSpecLike with MockFactory with SpecUti
}
}
}

"fail if Purpose has only draft versions" in {

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

(mockPurposeManagementService
.getPurposeById(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(purpose.id, *, *)
.once()
.returns(
Future.successful(
purpose
.copy(consumerId = consumerId, versions = Seq(purposeVersion.copy(state = Draft)))
)
)

(mockCatalogManagementService
.getEServiceById(_: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(eService.id, *, *)
.once()
.returns(Future.successful(eService.copy(descriptors = Seq(activeDescriptor.copy(state = Published)))))

(mockAgreementManagementService
.getAgreements(_: UUID, _: UUID)(_: ExecutionContext, _: ReadModelService))
.expects(eService.id, consumer.id, *, *)
.once()
.returns(Future.successful(Seq(agreement.copy(state = Active))))

Post() ~> service.addClientPurpose(persistentClient.id.toString, PurposeAdditionDetails(purpose.id)) ~> check {
status shouldEqual StatusCodes.BadRequest
responseAs[Problem].errors.head.code shouldEqual "007-0015"
}
}
}

0 comments on commit 60d1082

Please sign in to comment.