Skip to content

Commit

Permalink
Merge 159c845 into e56727c
Browse files Browse the repository at this point in the history
  • Loading branch information
BartNijland91 authored Apr 25, 2022
2 parents e56727c + 159c845 commit d4026aa
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ class GetDashboardItemsUseCaseImpl(
): DashboardItem.CardsItem {
// Check if we have a credential
val activeCredential = credentialUtil.getActiveCredential(
greenCardType = greenCard.greenCardEntity.type,
entities = greenCard.credentialEntities
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package nl.rijksoverheid.ctr.holder.dashboard.util

import nl.rijksoverheid.ctr.persistence.HolderCachedAppConfigUseCase
import nl.rijksoverheid.ctr.persistence.database.entities.CredentialEntity
import nl.rijksoverheid.ctr.persistence.database.entities.GreenCardType
import nl.rijksoverheid.ctr.shared.MobileCoreWrapper
import nl.rijksoverheid.ctr.shared.ext.getStringOrNull
import org.json.JSONArray
Expand All @@ -19,7 +20,7 @@ import java.time.OffsetDateTime
import java.time.ZoneOffset

interface CredentialUtil {
fun getActiveCredential(entities: List<CredentialEntity>): CredentialEntity?
fun getActiveCredential(greenCardType: GreenCardType, entities: List<CredentialEntity>): CredentialEntity?
fun isExpiring(credentialRenewalDays: Long, credential: CredentialEntity): Boolean
fun getTestTypeForEuropeanCredentials(entities: List<CredentialEntity>): String
fun getVaccinationDosesForEuropeanCredentials(
Expand All @@ -40,17 +41,28 @@ class CredentialUtilImpl(

private val holderConfig = cachedAppConfigUseCase.getCachedAppConfig()

override fun getActiveCredential(entities: List<CredentialEntity>): CredentialEntity? {
override fun getActiveCredential(greenCardType: GreenCardType, entities: List<CredentialEntity>): CredentialEntity? {

// All credentials that fall into the expiration window
val credentialsInWindow = entities.filter {
it.validFrom.isBefore(
OffsetDateTime.now(clock)
) && it.expirationTime.isAfter(
OffsetDateTime.now(
clock
)
)
when (greenCardType) {
is GreenCardType.Domestic -> {
it.validFrom.isBefore(
OffsetDateTime.now(clock)
) && it.expirationTime.isAfter(
OffsetDateTime.now(
clock
)
)
}
is GreenCardType.Eu -> {
it.expirationTime.isAfter(
OffsetDateTime.now(
clock
)
)
}
}
}

// Return the credential with the longest expiration time if it exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class GreenCardRefreshUtilImpl(
return allGreenCards.filter {
it.greenCardEntity.type == selectedType
}.all {
credentialUtil.getActiveCredential(it.credentialEntities) == null
credentialUtil.getActiveCredential(it.greenCardEntity.type, it.credentialEntities) == null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class GreenCardUtilImpl(
}

override fun hasNoActiveCredentials(greenCard: GreenCard): Boolean {
return credentialUtil.getActiveCredential(greenCard.credentialEntities) == null
return credentialUtil.getActiveCredential(greenCard.greenCardEntity.type, greenCard.credentialEntities) == null
}

override fun isDomesticTestGreenCard(greenCard: GreenCard): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import nl.rijksoverheid.ctr.appconfig.api.model.HolderConfig
import nl.rijksoverheid.ctr.holder.dashboard.util.CredentialUtilImpl
import nl.rijksoverheid.ctr.persistence.HolderCachedAppConfigUseCase
import nl.rijksoverheid.ctr.persistence.database.entities.CredentialEntity
import nl.rijksoverheid.ctr.persistence.database.entities.GreenCardType
import nl.rijksoverheid.ctr.shared.MobileCoreWrapper
import org.json.JSONObject
import org.junit.Assert.*
Expand Down Expand Up @@ -37,7 +38,7 @@ class CredentialUtilImplTest : AutoCloseKoinTest() {
private val mobileCoreWrapper: MobileCoreWrapper = mockk(relaxed = true)

@Test
fun `getActiveCredential returns active credential with highest expiration time`() {
fun `domestic getActiveCredential returns active credential with highest expiration time`() {
val clock = Clock.fixed(Instant.ofEpochSecond(50), ZoneId.of("UTC"))
val credentialUtil = CredentialUtilImpl(clock, mobileCoreWrapper, mockk(), mockk(relaxed = true))

Expand All @@ -60,14 +61,15 @@ class CredentialUtilImplTest : AutoCloseKoinTest() {
)

val activeCredential = credentialUtil.getActiveCredential(
greenCardType = GreenCardType.Domestic,
entities = listOf(credential1, credential2)
)

assertEquals(credential2, activeCredential)
}

@Test
fun `getActiveCredential returns no active credential if not in window`() {
fun `domestic getActiveCredential returns no active credential if not in window`() {
val clock = Clock.fixed(Instant.ofEpochSecond(50), ZoneId.of("UTC"))
val credentialUtil = CredentialUtilImpl(clock, mobileCoreWrapper, mockk(), mockk(relaxed = true))

Expand All @@ -90,12 +92,35 @@ class CredentialUtilImplTest : AutoCloseKoinTest() {
)

val activeCredential = credentialUtil.getActiveCredential(
greenCardType = GreenCardType.Domestic,
entities = listOf(credential1, credential2)
)

assertNull(null, activeCredential)
}

@Test
fun `international getActiveCredential ignores validFrom`() {
val clock = Clock.fixed(Instant.parse("2022-01-02T09:00:00.00Z"), ZoneId.of("UTC"))
val credentialUtil = CredentialUtilImpl(clock, mobileCoreWrapper, mockk(), mockk(relaxed = true))

val credential = CredentialEntity(
id = 0,
greenCardId = 0,
data = "".toByteArray(),
credentialVersion = 1,
validFrom = OffsetDateTime.ofInstant(Instant.parse("2022-01-01T09:00:00.00Z"), ZoneOffset.UTC),
expirationTime = OffsetDateTime.ofInstant(Instant.parse("2022-01-03T09:00:00.00Z"), ZoneOffset.UTC)
)

val activeCredential = credentialUtil.getActiveCredential(
greenCardType = GreenCardType.Eu,
entities = listOf(credential)
)

assertEquals(credential, activeCredential)
}

@Test
fun `given a credential expiring before the renewal date, when isExpiring, then it returns true`() {
val clock = Clock.fixed(Instant.parse("2021-01-01T00:00:00.00Z"), ZoneId.of("UTC"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class GreenCardUtilImplTest {

@Test
fun `hasNoActiveCredentials returns true when there are no active credentials for this green card`() {
every { credentialUtil.getActiveCredential(any()) } answers { null }
every { credentialUtil.getActiveCredential(any(), any()) } answers { null }

val clock = Clock.fixed(Instant.ofEpochSecond(0), ZoneId.of("UTC"))
val greenCardUtil = GreenCardUtilImpl(holderDatabase, clock, credentialUtil)
Expand All @@ -257,7 +257,7 @@ class GreenCardUtilImplTest {

@Test
fun `hasNoActiveCredentials returns false when there are active credentials for this green card`() {
every { credentialUtil.getActiveCredential(any()) } answers {
every { credentialUtil.getActiveCredential(any(), any()) } answers {
CredentialEntity(
id = 1,
greenCardId = 1L,
Expand Down

0 comments on commit d4026aa

Please sign in to comment.