Skip to content

Commit

Permalink
Merge pull request #45 from niscy-eudiw/feature/test/DocumentDetailsI…
Browse files Browse the repository at this point in the history
…nteractor

Unit tests for Document Details Interactor.
  • Loading branch information
stzouvaras authored Mar 8, 2024
2 parents 1a1d31d + 0c39193 commit 8476ab0
Show file tree
Hide file tree
Showing 14 changed files with 1,062 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import junit.framework.TestCase.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
import org.mockito.Spy
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

Expand Down Expand Up @@ -72,131 +72,131 @@ class TestSecurityController {
)
MockitoAnnotations.openMocks(this)

Mockito.`when`(configLogic.appBuildType).thenReturn(AppBuildType.RELEASE)
Mockito.`when`(androidPackageController.getSignatures()).thenReturn(listOf(signature))
whenever(configLogic.appBuildType).thenReturn(AppBuildType.RELEASE)
whenever(androidPackageController.getSignatures()).thenReturn(listOf(signature))
}

@Test
fun testIsRunningOnEmulatorWithBlockEmulatorConfigTrue() {
Mockito.`when`(configSecurityLogic.blockEmulator).thenReturn(true)
whenever(configSecurityLogic.blockEmulator).thenReturn(true)
assertTrue(securityController.isRunningOnEmulator())
}

@Test
fun testIsRunningOnEmulatorWithBlockEmulatorConfigFalse() {
Mockito.`when`(configSecurityLogic.blockEmulator).thenReturn(false)
whenever(configSecurityLogic.blockEmulator).thenReturn(false)
assertFalse(securityController.isRunningOnEmulator())
}

@Test
fun testIsDeviceRootedWithBlockRootAccessConfigTrue() {
Mockito.`when`(configSecurityLogic.blockRootAccess).thenReturn(true)
Mockito.`when`(rootController.isRooted()).thenReturn(true)
whenever(configSecurityLogic.blockRootAccess).thenReturn(true)
whenever(rootController.isRooted()).thenReturn(true)
assertTrue(securityController.isDeviceRooted())
}

@Test
fun testIsDeviceRootedWithBlockRootAccessConfigFalse() {
Mockito.`when`(configSecurityLogic.blockRootAccess).thenReturn(false)
Mockito.`when`(rootController.isRooted()).thenReturn(true)
whenever(configSecurityLogic.blockRootAccess).thenReturn(false)
whenever(rootController.isRooted()).thenReturn(true)
assertFalse(securityController.isDeviceRooted())
}

@Test
fun testIsSignatureValidWithSignaturePackageNotNullAndValid() {
Mockito.`when`(configSecurityLogic.packageSignature).thenReturn(signature)
whenever(configSecurityLogic.packageSignature).thenReturn(signature)
assertTrue(securityController.isSignatureValid())
}

@Test
fun testIsSignatureValidWithSignaturePackageNotNullAndNotValid() {
Mockito.`when`(configSecurityLogic.packageSignature).thenReturn("")
whenever(configSecurityLogic.packageSignature).thenReturn("")
assertFalse(securityController.isSignatureValid())
}

@Test
fun testIsSignatureValidWithSignaturePackageConfigIsNull() {
Mockito.`when`(configSecurityLogic.packageSignature).thenReturn(null)
whenever(configSecurityLogic.packageSignature).thenReturn(null)
assertTrue(securityController.isSignatureValid())
}

@Test
fun testIsPackageInstallerValidWithConfigPackageInstallersNotEmptyAndValidInstaller() {
Mockito.`when`(configSecurityLogic.packageInstallers).thenReturn(installers)
Mockito.`when`(androidPackageController.getInstaller(installers))
whenever(configSecurityLogic.packageInstallers).thenReturn(installers)
whenever(androidPackageController.getInstaller(installers))
.thenReturn(AndroidInstaller.TRUSTED)
assertTrue(securityController.isPackageInstallerValid())
}

@Test
fun testIsPackageInstallerValidWithConfigPackageInstallersNotEmptyAndInValidInstaller() {
Mockito.`when`(configSecurityLogic.packageInstallers).thenReturn(installers)
Mockito.`when`(androidPackageController.getInstaller(installers))
whenever(configSecurityLogic.packageInstallers).thenReturn(installers)
whenever(androidPackageController.getInstaller(installers))
.thenReturn(AndroidInstaller.UNKNOWN)
assertFalse(securityController.isPackageInstallerValid())
}

@Test
fun testIsPackageInstallerValidWithConfigPackageInstallersEmpty() {
Mockito.`when`(configSecurityLogic.packageInstallers).thenReturn(emptyList())
whenever(configSecurityLogic.packageInstallers).thenReturn(emptyList())
assertTrue(securityController.isPackageInstallerValid())
}

@Test
fun testIsDebugModeEnabledWithConfigBlockDebugModeEnabledAndFlagDebuggable() {
Mockito.`when`(configSecurityLogic.blockDebugMode).thenReturn(true)
Mockito.`when`(androidPackageController.isDebugModeEnabled()).thenReturn(true)
whenever(configSecurityLogic.blockDebugMode).thenReturn(true)
whenever(androidPackageController.isDebugModeEnabled()).thenReturn(true)
assertTrue(securityController.isDebugModeEnabled())
}

@Test
fun testIsDebugModeEnabledWithConfigBlockDebugModeEnabledAndFlagNotDebuggable() {
Mockito.`when`(configSecurityLogic.blockDebugMode).thenReturn(true)
Mockito.`when`(androidPackageController.isDebugModeEnabled()).thenReturn(false)
whenever(configSecurityLogic.blockDebugMode).thenReturn(true)
whenever(androidPackageController.isDebugModeEnabled()).thenReturn(false)
assertFalse(securityController.isDebugModeEnabled())
}

@Test
fun testBlockScreenCaptureWithConfigBlockScreenCaptureEnabled() {
Mockito.`when`(configSecurityLogic.blockScreenCapture).thenReturn(true)
whenever(configSecurityLogic.blockScreenCapture).thenReturn(true)
assertTrue(securityController.blockScreenCapture())
}

@Test
fun testBlockScreenCaptureWithConfigBlockScreenCaptureDisabled() {
Mockito.`when`(configSecurityLogic.blockScreenCapture).thenReturn(false)
whenever(configSecurityLogic.blockScreenCapture).thenReturn(false)
assertFalse(securityController.blockScreenCapture())
}

@Test
fun testIsHookDetectedWithConfigBlockHooksEnabledAndReleaseBuildAndIsStackTracedHookDetectedAndIsMemoryHookedDetected() {
Mockito.`when`(configSecurityLogic.blockHooks).thenReturn(true)
Mockito.`when`(antiHookController.isMemoryHooked()).thenReturn(true)
Mockito.`when`(antiHookController.isStacktraceHooked()).thenReturn(true)
whenever(configSecurityLogic.blockHooks).thenReturn(true)
whenever(antiHookController.isMemoryHooked()).thenReturn(true)
whenever(antiHookController.isStacktraceHooked()).thenReturn(true)
assertTrue(securityController.isHookDetected())
}

@Test
fun testIsHookDetectedWithConfigBlockHooksEnabledAndReleaseBuildAndIsStackTracedHookDetectedAndIsMemoryHookedNotDetected() {
Mockito.`when`(configSecurityLogic.blockHooks).thenReturn(true)
Mockito.`when`(antiHookController.isMemoryHooked()).thenReturn(false)
Mockito.`when`(antiHookController.isStacktraceHooked()).thenReturn(true)
whenever(configSecurityLogic.blockHooks).thenReturn(true)
whenever(antiHookController.isMemoryHooked()).thenReturn(false)
whenever(antiHookController.isStacktraceHooked()).thenReturn(true)
assertTrue(securityController.isHookDetected())
}

@Test
fun testIsHookDetectedWithConfigBlockHooksEnabledAndReleaseBuildAndIsStackTracedHookNotDetectedAndIsMemoryHookedNotDetected() {
Mockito.`when`(configSecurityLogic.blockHooks).thenReturn(true)
Mockito.`when`(antiHookController.isMemoryHooked()).thenReturn(false)
Mockito.`when`(antiHookController.isStacktraceHooked()).thenReturn(false)
whenever(configSecurityLogic.blockHooks).thenReturn(true)
whenever(antiHookController.isMemoryHooked()).thenReturn(false)
whenever(antiHookController.isStacktraceHooked()).thenReturn(false)
assertFalse(securityController.isHookDetected())
}

@Test
fun testIsHookDetectedWithConfigBlockHooksDisabledAndReleaseBuildAndIsStackTracedHookDetectedAndIsMemoryHookedDetected() {
Mockito.`when`(configSecurityLogic.blockHooks).thenReturn(false)
Mockito.`when`(antiHookController.isMemoryHooked()).thenReturn(true)
Mockito.`when`(antiHookController.isStacktraceHooked()).thenReturn(true)
whenever(configSecurityLogic.blockHooks).thenReturn(false)
whenever(antiHookController.isMemoryHooked()).thenReturn(true)
whenever(antiHookController.isStacktraceHooked()).thenReturn(true)
assertFalse(securityController.isHookDetected())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import eu.europa.ec.businesslogic.util.toDateFormatted
import eu.europa.ec.businesslogic.util.toList
import eu.europa.ec.commonfeature.model.DocumentUi
import eu.europa.ec.commonfeature.model.toDocumentTypeUi
import eu.europa.ec.commonfeature.model.toUiName
import eu.europa.ec.commonfeature.ui.document_details.model.DocumentDetailsUi
import eu.europa.ec.commonfeature.ui.document_details.model.DocumentJsonKeys
import eu.europa.ec.commonfeature.util.extractFullNameFromDocumentOrEmpty
Expand All @@ -35,7 +36,7 @@ import org.json.JSONObject

object DocumentDetailsTransformer {

fun transformToUiItems(
fun transformToUiItem(
document: Document,
resourceProvider: ResourceProvider,
docType: String,
Expand Down Expand Up @@ -68,13 +69,15 @@ object DocumentDetailsTransformer {
)
}

val documentTypeUi = document.docType.toDocumentTypeUi()

return DocumentUi(
documentId = document.id,
documentName = document.name,
documentType = document.docType.toDocumentTypeUi(),
documentName = documentTypeUi.toUiName(resourceProvider),
documentType = documentTypeUi,
documentExpirationDateFormatted = documentJson.getStringFromJsonOrEmpty(
key = DocumentJsonKeys.EXPIRY_DATE
).toDateFormatted().toString(),
).toDateFormatted() ?: "",
documentImage = documentJson.getStringFromJsonOrEmpty(
key = DocumentJsonKeys.PORTRAIT
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ fun extractFullNameFromDocumentOrEmpty(document: Document): String {
document = document,
key = DocumentJsonKeys.LAST_NAME
)
return "$firstName $lastName"
return if (firstName.isNotBlank() && lastName.isNotBlank()) {
"$firstName $lastName"
} else if (firstName.isNotBlank()) {
firstName
} else if (lastName.isNotBlank()) {
lastName
} else {
""
}
}

fun keyIsBase64(key: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import androidx.annotation.VisibleForTesting
import eu.europa.ec.commonfeature.model.DocumentOptionItemUi
import eu.europa.ec.commonfeature.model.DocumentTypeUi
import eu.europa.ec.commonfeature.model.DocumentUi
import eu.europa.ec.commonfeature.ui.document_details.model.DocumentDetailsUi
import eu.europa.ec.uilogic.component.AppIcons
import eu.europa.ec.uilogic.component.InfoTextWithNameAndImageData
import eu.europa.ec.uilogic.component.InfoTextWithNameAndValueData

@VisibleForTesting(otherwise = VisibleForTesting.NONE)
object TestsConstants {
Expand All @@ -45,6 +48,68 @@ object TestsConstants {
documentDetails = emptyList(),
)

val mockedBasicPidUi = mockedFullPidUi.copy(
documentDetails = listOf(
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "birth_city",
infoValues = arrayOf("KATRINEHOLM")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "gender",
infoValues = arrayOf("male")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "age_over_18",
infoValues = arrayOf("yes")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "age_birth_year",
infoValues = arrayOf("1985")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "expiry_date",
infoValues = arrayOf("30 Mar 2050")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "given_name",
infoValues = arrayOf("JAN")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "family_name",
infoValues = arrayOf("ANDERSSON")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "age_over_65",
infoValues = arrayOf("no")
)
),
),
userFullName = "JAN ANDERSSON"
)

val mockedFullMdlUi = DocumentUi(
documentId = mockedId2,
documentName = mockedDocUiNameMdl,
Expand All @@ -54,6 +119,74 @@ object TestsConstants {
documentDetails = emptyList(),
)

val mockedBasicMdlUi = mockedFullMdlUi.copy(
documentDetails = listOf(
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "driving_privileges",
infoValues = arrayOf(
"issue_date: 1 Jul 2010\n" +
"expiry_date: 30 Mar 2050\n" +
"vehicle_category_code: A\n" +
"issue_date: 19 May 2008\n" +
"expiry_date: 30 Mar 2050\n" +
"vehicle_category_code: B"
)
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "expiry_date",
infoValues = arrayOf("30 Mar 2050")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "sex",
infoValues = arrayOf("male")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "birth_place",
infoValues = arrayOf("SWEDEN")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "given_name",
infoValues = arrayOf("JAN")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "portrait",
infoValues = arrayOf("Shown above")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "family_name",
infoValues = arrayOf("ANDERSSON")
)
),
DocumentDetailsUi.SignatureItem(
itemData = InfoTextWithNameAndImageData(
title = "signature_usual_mark",
base64Image = "SE"
)
),
),
userFullName = "JAN ANDERSSON"
)

val mockedMdlUiWithNoUserNameAndNoUserImage: DocumentUi = mockedFullMdlUi

val mockedMdlUiWithNoExpirationDate: DocumentUi = mockedFullMdlUi.copy(
Expand Down
Loading

0 comments on commit 8476ab0

Please sign in to comment.