-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from niscy-eudiw/test/common_feature_interact…
…ors_unit_tests Test: Common Feature interactors unit tests
- Loading branch information
Showing
5 changed files
with
396 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
...on-feature/src/test/java/eu/europa/ec/commonfeature/interactor/TestBiometricInteractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* | ||
* Copyright (c) 2023 European Commission | ||
* | ||
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European | ||
* Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work | ||
* except in compliance with the Licence. | ||
* | ||
* You may obtain a copy of the Licence at: | ||
* https://joinup.ec.europa.eu/software/page/eupl | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF | ||
* ANY KIND, either express or implied. See the Licence for the specific language | ||
* governing permissions and limitations under the Licence. | ||
*/ | ||
|
||
package eu.europa.ec.commonfeature.interactor | ||
|
||
import eu.europa.ec.authenticationlogic.controller.authentication.BiometricAuthenticationController | ||
import eu.europa.ec.authenticationlogic.controller.authentication.BiometricsAuthenticate | ||
import eu.europa.ec.authenticationlogic.controller.authentication.BiometricsAvailability | ||
import eu.europa.ec.authenticationlogic.controller.storage.BiometryStorageController | ||
import eu.europa.ec.testlogic.base.TestApplication | ||
import eu.europa.ec.testlogic.base.getMockedContext | ||
import eu.europa.ec.testlogic.extension.runFlowTest | ||
import eu.europa.ec.testlogic.extension.runTest | ||
import eu.europa.ec.testlogic.extension.toFlow | ||
import eu.europa.ec.testlogic.rule.CoroutineTestRule | ||
import junit.framework.TestCase.assertEquals | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.Mockito.mock | ||
import org.mockito.MockitoAnnotations | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.whenever | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.annotation.Config | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
@Config(application = TestApplication::class) | ||
class TestBiometricInteractor { | ||
|
||
@get:Rule | ||
val coroutineRule = CoroutineTestRule() | ||
|
||
@Mock | ||
private lateinit var biometryStorageController: BiometryStorageController | ||
|
||
@Mock | ||
private lateinit var biometricAuthenticationController: BiometricAuthenticationController | ||
|
||
@Mock | ||
private lateinit var quickPinInteractor: QuickPinInteractor | ||
|
||
private lateinit var interactor: BiometricInteractor | ||
|
||
private lateinit var closeable: AutoCloseable | ||
|
||
@Before | ||
fun before() { | ||
closeable = MockitoAnnotations.openMocks(this) | ||
|
||
interactor = BiometricInteractorImpl( | ||
biometryStorageController = biometryStorageController, | ||
biometricAuthenticationController = biometricAuthenticationController, | ||
quickPinInteractor = quickPinInteractor | ||
) | ||
} | ||
|
||
@After | ||
fun after() { | ||
closeable.close() | ||
} | ||
|
||
//region isPinValid | ||
|
||
// Case: isPinValid behaviour | ||
// When isCurrentPinValid returns QuickPinInteractorPinValidPartialState.Success, | ||
// the expected result of isPinValid is Success | ||
@Test | ||
fun `Given isCurrentPinValid returns state Success, When isPinValid is called, Then assert the result is the expected`() = | ||
coroutineRule.runTest { | ||
// Given | ||
whenever(quickPinInteractor.isCurrentPinValid(mockedPin)).thenReturn( | ||
QuickPinInteractorPinValidPartialState.Success.toFlow() | ||
) | ||
|
||
// When | ||
interactor.isPinValid(mockedPin).runFlowTest { | ||
// Then | ||
val expectedResult = QuickPinInteractorPinValidPartialState.Success | ||
assertEquals(expectedResult, awaitItem()) | ||
} | ||
} | ||
//endregion | ||
|
||
//region launchBiometricSystemScreen | ||
|
||
// Case: launchBiometricSystemScreen behaviour | ||
@Test | ||
fun `When launchBiometricSystemScreen is called, Then verify function is executed on the controller`() { | ||
// When | ||
interactor.launchBiometricSystemScreen() | ||
|
||
// Then | ||
verify(biometricAuthenticationController).launchBiometricSystemScreen() | ||
} | ||
//endregion | ||
|
||
///region getBiometricUserSelection | ||
|
||
// Case: getBiometricUserSelection behaviour | ||
// When getUseBiometricsAuth returns true, the expected result of getBiometricUserSelection | ||
// should be true | ||
@Test | ||
fun `When getBiometricUserSelection is called, Then assert the correct value is returned`() { | ||
// Given | ||
whenever(biometryStorageController.getUseBiometricsAuth()).thenReturn(true) | ||
|
||
// When | ||
val result = interactor.getBiometricUserSelection() | ||
|
||
// Then | ||
assertEquals(true, result) | ||
verify(biometryStorageController).getUseBiometricsAuth() | ||
} | ||
//endregion | ||
|
||
//region storeBiometricsUsageDecision | ||
|
||
// Case: storeBiometricsUsageDecision behaviour | ||
@Test | ||
fun `When storeBiometricsUsageDecision is called, Then verify setUseBiometricsAuth is executed`() { | ||
// Given | ||
val shouldUseBiometrics = true | ||
|
||
// When | ||
interactor.storeBiometricsUsageDecision(shouldUseBiometrics = shouldUseBiometrics) | ||
|
||
// Then | ||
verify(biometryStorageController).setUseBiometricsAuth(shouldUseBiometrics) | ||
} | ||
//endregion | ||
|
||
//region getBiometricsAvailability | ||
|
||
// Case: getBiometricsAvailability behaviour | ||
@Test | ||
fun `When getBiometricsAvailability is called, Then verify deviceSupportsBiometrics is executed`() { | ||
// Given | ||
val mockListener: (BiometricsAvailability) -> Unit = mock() | ||
|
||
// When | ||
interactor.getBiometricsAvailability(mockListener) | ||
|
||
// Then | ||
verify(biometricAuthenticationController).deviceSupportsBiometrics(mockListener) | ||
} | ||
//endregion | ||
|
||
//region authenticateWithBiometrics | ||
|
||
// Case: authenticateWithBiometrics behaviour | ||
// Defining a mock BiometricsAuthenticate function callback to verify that authenticate function | ||
// on the biometricAuthenticationController is executed when authenticateWithBiometrics is called | ||
@Test | ||
fun `When authenticateWithBiometrics is called, Then verify authenticate is executed with correct parameters`() { | ||
// Given | ||
val mockListener: (BiometricsAuthenticate) -> Unit = mock() | ||
val context = getMockedContext() | ||
|
||
// When | ||
interactor.authenticateWithBiometrics( | ||
context = context, | ||
listener = mockListener | ||
) | ||
|
||
// Then | ||
verify(biometricAuthenticationController).authenticate(context, mockListener) | ||
} | ||
//endregion | ||
|
||
//region Mocked objects | ||
private val mockedPin = "1234" | ||
//endregion | ||
} |
102 changes: 102 additions & 0 deletions
102
...src/test/java/eu/europa/ec/commonfeature/interactor/TestDeviceAuthenticationInteractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright (c) 2023 European Commission | ||
* | ||
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European | ||
* Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work | ||
* except in compliance with the Licence. | ||
* | ||
* You may obtain a copy of the Licence at: | ||
* https://joinup.ec.europa.eu/software/page/eupl | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF | ||
* ANY KIND, either express or implied. See the Licence for the specific language | ||
* governing permissions and limitations under the Licence. | ||
*/ | ||
|
||
package eu.europa.ec.commonfeature.interactor | ||
|
||
import eu.europa.ec.authenticationlogic.controller.authentication.BiometricsAvailability | ||
import eu.europa.ec.authenticationlogic.controller.authentication.DeviceAuthenticationController | ||
import eu.europa.ec.authenticationlogic.controller.authentication.DeviceAuthenticationResult | ||
import eu.europa.ec.authenticationlogic.model.BiometricCrypto | ||
import eu.europa.ec.testlogic.base.TestApplication | ||
import eu.europa.ec.testlogic.base.getMockedContext | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.Mockito.mock | ||
import org.mockito.Mockito.verify | ||
import org.mockito.MockitoAnnotations | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.annotation.Config | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
@Config(application = TestApplication::class) | ||
class TestDeviceAuthenticationInteractor { | ||
|
||
@Mock | ||
lateinit var deviceAuthenticationController: DeviceAuthenticationController | ||
|
||
@Mock | ||
private lateinit var resultHandler: DeviceAuthenticationResult | ||
|
||
private lateinit var interactor: DeviceAuthenticationInteractor | ||
|
||
private lateinit var closeable: AutoCloseable | ||
|
||
private lateinit var biometricCrypto: BiometricCrypto | ||
|
||
@Before | ||
fun before() { | ||
closeable = MockitoAnnotations.openMocks(this) | ||
interactor = DeviceAuthenticationInteractorImpl( | ||
deviceAuthenticationController = deviceAuthenticationController | ||
) | ||
|
||
biometricCrypto = BiometricCrypto(cryptoObject = null) | ||
} | ||
|
||
@After | ||
fun after() { | ||
closeable.close() | ||
} | ||
|
||
// Case: getBiometricsAvailability behaviour | ||
@Test | ||
fun `Given a BiometricsAvailability listener, When getBiometricsAvailability is called, Then deviceSupportsBiometrics should be triggered`() { | ||
// Given | ||
val mockListener: (BiometricsAvailability) -> Unit = mock() | ||
|
||
// When | ||
interactor.getBiometricsAvailability( | ||
listener = mockListener | ||
) | ||
|
||
// Then | ||
verify(deviceAuthenticationController).deviceSupportsBiometrics(mockListener) | ||
} | ||
|
||
// Case: authenticateWithBiometrics behaviour | ||
@Test | ||
fun `When authenticateWithBiometrics is called, Then authenticate function should be executed`() { | ||
// Given | ||
val context = getMockedContext() | ||
|
||
// When | ||
interactor.authenticateWithBiometrics( | ||
context = context, | ||
crypto = biometricCrypto, | ||
resultHandler = resultHandler | ||
) | ||
|
||
// Then | ||
verify(deviceAuthenticationController).authenticate( | ||
context = context, | ||
biometryCrypto = biometricCrypto, | ||
result = resultHandler | ||
) | ||
} | ||
} |
Oops, something went wrong.