Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinovega committed Sep 6, 2023
1 parent 06a8a59 commit 0d343c4
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 5 deletions.
2 changes: 1 addition & 1 deletion daikoku/app/controllers/ApiController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3954,7 +3954,7 @@ class ApiController(
_ <- EitherT.liftF[Future, AppError, Boolean](env.dataStore.apiRepo.forTenant(ctx.tenant).save(updatedApi))
_ <- EitherT.liftF[Future, AppError, Boolean](env.dataStore.usagePlanRepo.forTenant(ctx.tenant).save(updatedPlan))

} yield Ok(updatedApi.asJson))
} yield Created(updatedApi.asJson))
.leftMap(_.render())
.merge
}
Expand Down
6 changes: 2 additions & 4 deletions daikoku/app/domain/apiEntities.scala
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,8 @@ sealed trait UsagePlan {
.collect { case Some(name) => name }
//TODO: check conflict with extisting name in case of creation
tenant.display match {
case TenantDisplay.Environment => customName match {
case Some(customName) if tenant.environments.contains(customName) => EitherT.pure[Future, AppError](())
case _ => EitherT.leftT[Future, Unit](AppError.EntityConflict("Plan custom name"))
}
case TenantDisplay.Environment =>
EitherT.cond[Future](customName.exists(name => tenant.environments.contains(name) && !existingNames.contains(name)), (), AppError.EntityConflict("Plan custom name"))
case TenantDisplay.Default => EitherT.pure[Future, AppError](())
}
}
Expand Down
122 changes: 122 additions & 0 deletions daikoku/test/daikoku/EnvironmentDisplayMode.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package daikoku

import cats.implicits.catsSyntaxOptionId
import fr.maif.otoroshi.daikoku.domain.UsagePlan.FreeWithoutQuotas
import fr.maif.otoroshi.daikoku.domain.{BillingDuration, BillingTimeUnit, Currency, IntegrationProcess, OtoroshiTarget, TenantDisplay, UsagePlan, UsagePlanId}
import fr.maif.otoroshi.daikoku.tests.utils.{DaikokuSpecHelper, OneServerPerSuiteWithMyComponents}
import fr.maif.otoroshi.daikoku.utils.IdGenerator
import org.scalatest.concurrent.IntegrationPatience
import org.scalatestplus.play.PlaySpec
import play.api.libs.json.{JsArray, Json}

class EnvironmentDisplayMode()
extends PlaySpec
with OneServerPerSuiteWithMyComponents
with DaikokuSpecHelper
with IntegrationPatience {

def createPlan(maybeName: Option[String]): UsagePlan = FreeWithoutQuotas(
id = UsagePlanId(IdGenerator.token),
tenant = tenant.id,
billingDuration = BillingDuration(1, BillingTimeUnit.Month),
currency = Currency("EUR"),
customName = maybeName,
customDescription = None,
otoroshiTarget = None,
allowMultipleKeys = Some(false),
subscriptionProcess = Seq.empty,
integrationProcess = IntegrationProcess.ApiKey,
autoRotation = Some(false)
)

"a usage plan" must {
"have a custom name as an avalaible environment" in {

val api = generateApi("0", tenant.id, teamOwnerId, Seq.empty)
.api
.copy(possibleUsagePlans = Seq.empty)

val t = tenant.copy(display = TenantDisplay.Environment, environments = Set("dev", "prod"))

setupEnvBlocking(
tenants = Seq(t),
teams = Seq(teamOwner),
users = Seq(userAdmin),
usagePlans = Seq(),
apis = Seq(api)
)


val devPlan = createPlan("dev".some)
val devPlan2 = createPlan("dev".some)
val preprodplan = createPlan("preprod".some)
val nonePlan = createPlan(None)

val session = loginWithBlocking(userAdmin, tenant)

// can create plan with avalaible env
val respDev = httpJsonCallBlocking(s"/api/teams/${teamOwner.id.value}/apis/${api.id.value}/${api.currentVersion.value}/plan",
method = "POST",
body = devPlan.asJson.some
)(tenant, session)
respDev.status mustBe 201

// can create plan with avalaible env
val respDev2 = httpJsonCallBlocking(s"/api/teams/${teamOwner.id.value}/apis/${api.id.value}/${api.currentVersion.value}/plan",
method = "POST",
body = devPlan2.asJson.some
)(tenant, session)
respDev2.status mustBe 409

// can't create plan with unknown env name
val respPreprod = httpJsonCallBlocking(s"/api/teams/${teamOwner.id.value}/apis/${api.id.value}/${api.currentVersion.value}/plan",
method = "POST",
body = preprodplan.asJson.some
)(tenant, session)
respPreprod.status mustBe 409

// can't create plan with no custom name
val respNone = httpJsonCallBlocking(s"/api/teams/${teamOwner.id.value}/apis/${api.id.value}/${api.currentVersion.value}/plan",
method = "POST",
body = nonePlan.asJson.some
)(tenant, session)
respNone.status mustBe 409

}

"be deleted if associated env is deleted" in {


val devPlan = createPlan("dev".some)
val prodPlan = createPlan("prod".some)

val api = generateApi("0", tenant.id, teamOwnerId, Seq.empty)
.api
.copy(possibleUsagePlans = Seq(devPlan.id, prodPlan.id))

val _tenant = tenant.copy(display = TenantDisplay.Environment, environments = Set("dev", "prod"))
setupEnvBlocking(
tenants = Seq(_tenant),
teams = Seq(teamOwner, defaultAdminTeam),
users = Seq(tenantAdmin),
usagePlans = Seq(devPlan, prodPlan),
apis = Seq(api)
)

val session = loginWithBlocking(tenantAdmin, tenant)

// can create plan with avalaible env
val respUpdateTenant = httpJsonCallBlocking(s"/api/tenants/${tenant.id.value}",
method = "PUT",
body = _tenant.copy(environments = Set("prod")).asJson.some
)(_tenant, session)
respUpdateTenant.status mustBe 200

val respVerif = httpJsonCallBlocking(s"/api/me/visible-apis/${api.id.value}")(tenant, session)
respVerif.status mustBe 200
(respVerif.json \ "possibleUsagePlans").as[JsArray].value.size mustBe 1
val uniqPlan = (respVerif.json \ "possibleUsagePlans").as[JsArray].value.head.as[String]
uniqPlan mustBe prodPlan.id.value
}
}
}

0 comments on commit 0d343c4

Please sign in to comment.