Skip to content

Commit

Permalink
Merge pull request #194 from niscy-eudiw/test/common_feature_interact…
Browse files Browse the repository at this point in the history
…ors_unit_tests

Test: Common Feature interactors unit tests
  • Loading branch information
stzouvaras authored Oct 1, 2024
2 parents 643d5e6 + e88f4b2 commit 5398529
Show file tree
Hide file tree
Showing 5 changed files with 396 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class DeviceAuthenticationControllerImpl(
private val resourceProvider: ResourceProvider,
private val biometricAuthenticationController: BiometricAuthenticationController
) : DeviceAuthenticationController {

override fun deviceSupportsBiometrics(listener: (BiometricsAvailability) -> Unit) {
biometricAuthenticationController.deviceSupportsBiometrics(listener)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ interface DeviceAuthenticationInteractor {
)
}

class DeviceAuthenticationInteractorImpl(private val deviceAuthenticationController: DeviceAuthenticationController) :
DeviceAuthenticationInteractor {
class DeviceAuthenticationInteractorImpl(
private val deviceAuthenticationController: DeviceAuthenticationController,
) : DeviceAuthenticationInteractor {

override fun getBiometricsAvailability(listener: (BiometricsAvailability) -> Unit) {
deviceAuthenticationController.deviceSupportsBiometrics(listener)
}
Expand Down
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
}
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
)
}
}
Loading

0 comments on commit 5398529

Please sign in to comment.