Skip to content

Commit

Permalink
Add test cases and modify test conf.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-in-a-Jar committed Oct 13, 2023
1 parent 7273a5a commit 639bd2c
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/test/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ termsOfService {
enabled = false
version = 1
url = "app.terra.bio/#terms-of-service"
rollingAcceptanceWindowExpirationDatetime = "2019-01-01T00:00:00Z"
rollingAcceptanceWindowPreviousTosVersion = 0
}

petServiceAccount {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.broadinstitute.dsde.workbench.sam.util.SamRequestContext
import org.broadinstitute.dsde.workbench.sam.{Generator, TestSupport}
import org.scalatest.concurrent.ScalaFutures

import java.time.Instant
import scala.concurrent.ExecutionContext

/** Created by dvoet on 7/14/17.
Expand All @@ -49,7 +50,7 @@ class TestSamRoutes(
userService,
statusService,
managedGroupService,
TermsOfServiceConfig(true, false, "0", "app.terra.bio/#terms-of-service"),
TermsOfServiceConfig(true, false, "0", "app.terra.bio/#terms-of-service", Instant.now(), "0"),
policyEvaluatorService,
tosService,
LiquibaseConfig("", false),
Expand Down Expand Up @@ -91,7 +92,7 @@ class TestSamTosEnabledRoutes(
userService,
statusService,
managedGroupService,
TermsOfServiceConfig(true, false, "0", "app.terra.bio/#terms-of-service"),
TermsOfServiceConfig(true, false, "0", "app.terra.bio/#terms-of-service", Instant.now(), "0"),
policyEvaluatorService,
tosService,
LiquibaseConfig("", false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ class MockDirectoryDAO(val groups: mutable.Map[WorkbenchGroupIdentity, Workbench
userTos.get(userId)
}

override def getUserTos(userId: WorkbenchUserId, tosVersion: String, samRequestContext: SamRequestContext): IO[Option[SamUserTos]] =
loadUser(userId, samRequestContext).map {
case None => None
case Some(_) =>
userTos.get(userId)
}

override def createPetManagedIdentity(petManagedIdentity: PetManagedIdentity, samRequestContext: SamRequestContext): IO[PetManagedIdentity] = {
if (petManagedIdentitiesByUser.keySet.contains(petManagedIdentity.id)) {
IO.raiseError(new WorkbenchExceptionWithErrorReport(ErrorReport(StatusCodes.Conflict, s"pet managed identity ${petManagedIdentity.id} already exists")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class TosServiceSpec(_system: ActorSystem)
new TosService(dirDAO, TestSupport.tosConfig.copy(version = tosVersion))
when(dirDAO.getLatestUserTos(serviceAccountUser.id, samRequestContext))
.thenReturn(IO.pure(Some(SamUserTos(serviceAccountUser.id, tosVersion, TosTable.ACCEPT, Instant.now()))))

when(dirDAO.getUserTos(serviceAccountUser.id, "0", samRequestContext))
.thenReturn(IO.pure(Some(SamUserTos(serviceAccountUser.id, tosVersion, TosTable.ACCEPT, Instant.now()))))

val complianceStatus = tosService.getTosComplianceStatus(serviceAccountUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe true
}
Expand All @@ -100,13 +104,18 @@ class TosServiceSpec(_system: ActorSystem)
}

val tosVersion = "2"
val rollingAcceptanceWindowPreviousTosVersion = "0"
val withoutGracePeriod = "without the grace period enabled"
val withGracePeriod = " with the grace period enabled"
val withoutRollingAcceptanceWindow = "outside of the rolling acceptance window"
val withRollingAcceptanceWindow = " inside of the rolling acceptance window"
val cannotUseTheSystem = "says the user cannot use the system"
val canUseTheSystem = "says the user can use the system"
val tosServiceV2 = new TosService(dirDAO, TestSupport.tosConfig.copy(version = tosVersion))
val tosServiceV2GracePeriodEnabled =
new TosService(dirDAO, TestSupport.tosConfig.copy(version = tosVersion, isGracePeriodEnabled = true))
val tosServiceV2AcceptanceWindowEnabled = new TosService(dirDAO, TestSupport.tosConfig.copy(isTosEnabled=true, isGracePeriodEnabled=false, version = tosVersion, rollingAcceptanceWindowExpiration = Instant.now().plusSeconds(3600), rollingAcceptanceWindowPreviousTosVersion = rollingAcceptanceWindowPreviousTosVersion))


/** | Case | Grace Period Enabled | Accepted Version | Current Version | User accepted latest | Permits system usage |
* |:-----|:---------------------|:-----------------|:----------------|:---------------------|:---------------------|
Expand All @@ -122,13 +131,18 @@ class TosServiceSpec(_system: ActorSystem)
"says the user has not accepted the latest version" in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(None))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(None))

val complianceStatus = tosServiceV2.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.userHasAcceptedLatestTos shouldBe false
}
withoutGracePeriod - {
cannotUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(None))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(None))
// CASE 1
val complianceStatus = tosServiceV2.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe false
Expand All @@ -138,23 +152,51 @@ class TosServiceSpec(_system: ActorSystem)
cannotUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(None))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(None))
// CASE 4
val complianceStatus = tosServiceV2GracePeriodEnabled.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe false
}
}
withRollingAcceptanceWindow - {
cannotUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(None))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(None))
// CASE 1
val complianceStatus = tosServiceV2AcceptanceWindowEnabled.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe false
}
}
withoutRollingAcceptanceWindow - {
cannotUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(None))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(None))
// CASE 4
val complianceStatus = tosServiceV2.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe false
}
}
}
"when the user has accepted a non-current ToS version" - {
"says the user has not accepted the latest version" in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(None))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
val complianceStatus = tosServiceV2.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.userHasAcceptedLatestTos shouldBe false
}
withoutGracePeriod - {
cannotUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(None))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
// CASE 2
val complianceStatus = tosServiceV2.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe false
Expand All @@ -164,23 +206,62 @@ class TosServiceSpec(_system: ActorSystem)
canUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, tosVersion, TosTable.ACCEPT, Instant.now()))))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
// CASE 5
val complianceStatus = tosServiceV2GracePeriodEnabled.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe true
}
}
withRollingAcceptanceWindow - {
canUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
// CASE 1
val complianceStatus = tosServiceV2AcceptanceWindowEnabled.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe true
}
}
withRollingAcceptanceWindow - {
cannotUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
// CASE 1
val complianceStatus = tosServiceV2AcceptanceWindowEnabled.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe true
}
}
withoutRollingAcceptanceWindow - {
cannotUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
// CASE 4
val complianceStatus = tosServiceV2.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe false
}
}
}
"when the user has accepted the current ToS version" - {
"says the user has accepted the latest version" in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, tosVersion, TosTable.ACCEPT, Instant.now()))))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
val complianceStatus = tosServiceV2.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.userHasAcceptedLatestTos shouldBe true
}
withoutGracePeriod - {
canUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, tosVersion, TosTable.ACCEPT, Instant.now()))))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
// CASE 3
val complianceStatus = tosServiceV2.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe true
Expand All @@ -190,24 +271,52 @@ class TosServiceSpec(_system: ActorSystem)
canUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, tosVersion, TosTable.ACCEPT, Instant.now()))))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
// CASE 6
val complianceStatus = tosServiceV2GracePeriodEnabled.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe true
}
}
withRollingAcceptanceWindow - {
canUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, tosVersion, TosTable.ACCEPT, Instant.now()))))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
// CASE 1
val complianceStatus = tosServiceV2AcceptanceWindowEnabled.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe true
}
}
withoutRollingAcceptanceWindow - {
canUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, tosVersion, TosTable.ACCEPT, Instant.now()))))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
// CASE 4
val complianceStatus = tosServiceV2.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe true
}
}
}

"when the user has rejected the latest ToS version" - {
"says the user has rejected the latest version" in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, tosVersion, TosTable.REJECT, Instant.now()))))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
val complianceStatus = tosServiceV2.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.userHasAcceptedLatestTos shouldBe false
}
withoutGracePeriod - {
cannotUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, tosVersion, TosTable.REJECT, Instant.now()))))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
// CASE 1
val complianceStatus = tosServiceV2.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe false
Expand All @@ -217,16 +326,42 @@ class TosServiceSpec(_system: ActorSystem)
cannotUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, tosVersion, TosTable.REJECT, Instant.now()))))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
// CASE 4
val complianceStatus = tosServiceV2GracePeriodEnabled.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe false
}
}
withRollingAcceptanceWindow - {
canUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, tosVersion, TosTable.REJECT, Instant.now()))))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
// CASE 1
val complianceStatus = tosServiceV2AcceptanceWindowEnabled.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe true
}
}
withoutRollingAcceptanceWindow - {
cannotUseTheSystem in {
when(dirDAO.getLatestUserTos(defaultUser.id, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, tosVersion, TosTable.REJECT, Instant.now()))))
when(dirDAO.getUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(Option(SamUserTos(defaultUser.id, rollingAcceptanceWindowPreviousTosVersion, TosTable.ACCEPT, Instant.now()))))
// CASE 4
val complianceStatus = tosServiceV2.getTosComplianceStatus(defaultUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe false
}
}
}
"when a service account is using the api" - {
"let it use the api regardless of tos status" in {
when(dirDAO.getLatestUserTos(serviceAccountUser.id, samRequestContext))
.thenReturn(IO.pure(None))
when(dirDAO.getUserTos(serviceAccountUser.id, rollingAcceptanceWindowPreviousTosVersion, samRequestContext))
.thenReturn(IO.pure(None))
val complianceStatus = tosServiceV2.getTosComplianceStatus(serviceAccountUser, samRequestContext).unsafeRunSync()
complianceStatus.permitsSystemUsage shouldBe true
}
Expand Down

0 comments on commit 639bd2c

Please sign in to comment.