From 921253fca0b8cd121e26f298b9bbedd5a3fb4d16 Mon Sep 17 00:00:00 2001 From: Sergejs Luhmirins Date: Tue, 23 Apr 2024 10:07:23 +0300 Subject: [PATCH 1/2] MS-91 Add methods to delete all licenses to the repository --- .../infra/license/LicenseRepository.kt | 2 +- .../infra/license/LicenseRepositoryImpl.kt | 1 + .../license/local/LicenseLocalDataSource.kt | 2 ++ .../local/LicenseLocalDataSourceImpl.kt | 9 +++++++++ .../local/LicenseLocalDataSourceImplTest.kt | 7 +++++++ .../repository/LicenseRepositoryImplTest.kt | 18 ++++++++++++++++-- 6 files changed, 36 insertions(+), 3 deletions(-) diff --git a/infra/license/src/main/java/com/simprints/infra/license/LicenseRepository.kt b/infra/license/src/main/java/com/simprints/infra/license/LicenseRepository.kt index 6e12b34874..c3afea1902 100644 --- a/infra/license/src/main/java/com/simprints/infra/license/LicenseRepository.kt +++ b/infra/license/src/main/java/com/simprints/infra/license/LicenseRepository.kt @@ -13,5 +13,5 @@ interface LicenseRepository { suspend fun getCachedLicense(licenseVendor: Vendor): License? suspend fun deleteCachedLicense(licenseVendor: Vendor) - + suspend fun deleteCachedLicenses() } diff --git a/infra/license/src/main/java/com/simprints/infra/license/LicenseRepositoryImpl.kt b/infra/license/src/main/java/com/simprints/infra/license/LicenseRepositoryImpl.kt index 9577420d56..6bbc9a4628 100644 --- a/infra/license/src/main/java/com/simprints/infra/license/LicenseRepositoryImpl.kt +++ b/infra/license/src/main/java/com/simprints/infra/license/LicenseRepositoryImpl.kt @@ -74,4 +74,5 @@ internal class LicenseRepositoryImpl @Inject constructor( override suspend fun deleteCachedLicense(licenseVendor: Vendor) = licenseLocalDataSource.deleteCachedLicense(licenseVendor) + override suspend fun deleteCachedLicenses() = licenseLocalDataSource.deleteCachedLicenses() } diff --git a/infra/license/src/main/java/com/simprints/infra/license/local/LicenseLocalDataSource.kt b/infra/license/src/main/java/com/simprints/infra/license/local/LicenseLocalDataSource.kt index 574f1e2bb4..d7e05746a6 100644 --- a/infra/license/src/main/java/com/simprints/infra/license/local/LicenseLocalDataSource.kt +++ b/infra/license/src/main/java/com/simprints/infra/license/local/LicenseLocalDataSource.kt @@ -10,6 +10,8 @@ internal interface LicenseLocalDataSource { suspend fun deleteCachedLicense(vendor: Vendor) + suspend fun deleteCachedLicenses() + companion object { const val LICENSES_FOLDER = "licenses" } diff --git a/infra/license/src/main/java/com/simprints/infra/license/local/LicenseLocalDataSourceImpl.kt b/infra/license/src/main/java/com/simprints/infra/license/local/LicenseLocalDataSourceImpl.kt index b2ab908fef..56b2e68def 100644 --- a/infra/license/src/main/java/com/simprints/infra/license/local/LicenseLocalDataSourceImpl.kt +++ b/infra/license/src/main/java/com/simprints/infra/license/local/LicenseLocalDataSourceImpl.kt @@ -92,6 +92,15 @@ internal class LicenseLocalDataSourceImpl @Inject constructor( } } + override suspend fun deleteCachedLicenses(): Unit = withContext(dispatcherIo) { + try { + val deleted = File(licenseDirectoryPath).deleteRecursively() + Simber.d("Deleted all licenses successfully = $deleted") + } catch (t: Throwable) { + Simber.e(t) + } + } + private fun getFileFromStorage(vendor: Vendor): String? = try { val file = File("$licenseDirectoryPath/$vendor") val encryptedFile = keyHelper.getEncryptedFileBuilder(file, context) diff --git a/infra/license/src/test/java/com/simprints/infra/license/local/LicenseLocalDataSourceImplTest.kt b/infra/license/src/test/java/com/simprints/infra/license/local/LicenseLocalDataSourceImplTest.kt index 5c39baf217..86930bd022 100644 --- a/infra/license/src/test/java/com/simprints/infra/license/local/LicenseLocalDataSourceImplTest.kt +++ b/infra/license/src/test/java/com/simprints/infra/license/local/LicenseLocalDataSourceImplTest.kt @@ -80,6 +80,13 @@ class LicenseLocalDataSourceImplTest { assertThat(File("${filesDirPath}/${LicenseLocalDataSource.LICENSES_FOLDER}/$licenseVendor").exists()).isFalse() } + @Test + fun `check delete all deletes the dir`() = runTest { + localSource.deleteCachedLicenses() + + assertThat(File("${filesDirPath}/${LicenseLocalDataSource.LICENSES_FOLDER}/$licenseVendor").exists()).isFalse() + } + @Test fun `check getting the file renames old Roc license file to RANK_ONE_FACE `() = runTest { // Create the license folder and the old ROC.lic file diff --git a/infra/license/src/test/java/com/simprints/infra/license/repository/LicenseRepositoryImplTest.kt b/infra/license/src/test/java/com/simprints/infra/license/repository/LicenseRepositoryImplTest.kt index e65def185a..0f224bbecd 100644 --- a/infra/license/src/test/java/com/simprints/infra/license/repository/LicenseRepositoryImplTest.kt +++ b/infra/license/src/test/java/com/simprints/infra/license/repository/LicenseRepositoryImplTest.kt @@ -9,6 +9,7 @@ import com.simprints.infra.license.remote.ApiLicenseResult import com.simprints.infra.license.remote.License import com.simprints.infra.license.remote.LicenseRemoteDataSource import io.mockk.coEvery +import io.mockk.coVerify import io.mockk.mockk import kotlinx.coroutines.flow.toCollection import kotlinx.coroutines.test.runTest @@ -148,7 +149,7 @@ class LicenseRepositoryImplTest { } @Test - fun ` test getCachedLicense success`() = runTest { + fun `test getCachedLicense success`() = runTest { // Given coEvery { licenseLocalDataSource.getLicense(RANK_ONE_FACE) } returns license // When @@ -157,15 +158,28 @@ class LicenseRepositoryImplTest { assertThat(cachedLicense).isEqualTo(license) } - fun ` test getCachedLicense failure`() = runTest { + @Test + fun `test getCachedLicense failure`() = runTest { // Given coEvery { licenseLocalDataSource.getLicense(RANK_ONE_FACE) } returns null // When val license= licenseRepositoryImpl.getCachedLicense(RANK_ONE_FACE) // Then assertThat(license).isNull() + } + + @Test + fun `deletes cached licence`() = runTest { + licenseRepositoryImpl.deleteCachedLicense(RANK_ONE_FACE) + coVerify { licenseLocalDataSource.deleteCachedLicense(RANK_ONE_FACE) } + } + + @Test + fun `deletes all cached licence`() = runTest { + licenseRepositoryImpl.deleteCachedLicenses() + coVerify { licenseLocalDataSource.deleteCachedLicenses() } } companion object { From 3118406678e7029b45627c4a981cb38028b99cca Mon Sep 17 00:00:00 2001 From: Sergejs Luhmirins Date: Tue, 23 Apr 2024 10:08:19 +0300 Subject: [PATCH 2/2] MS-91 Call license deletion on logout --- infra/auth-logic/build.gradle.kts | 1 + .../infra/authlogic/authenticator/SignerManager.kt | 3 +++ .../infra/authlogic/authenticator/SignerManagerTest.kt | 6 ++++++ 3 files changed, 10 insertions(+) diff --git a/infra/auth-logic/build.gradle.kts b/infra/auth-logic/build.gradle.kts index 968a12aafa..df3f7c3650 100644 --- a/infra/auth-logic/build.gradle.kts +++ b/infra/auth-logic/build.gradle.kts @@ -16,6 +16,7 @@ dependencies { implementation(project(":infra:enrolment-records-store")) implementation(project(":infra:images")) implementation(project(":infra:recent-user-activity")) + implementation(project(":infra:license")) implementation(project(":fingerprint:infra:scanner")) diff --git a/infra/auth-logic/src/main/java/com/simprints/infra/authlogic/authenticator/SignerManager.kt b/infra/auth-logic/src/main/java/com/simprints/infra/authlogic/authenticator/SignerManager.kt index cd1e1c6bb2..f431e0b5c9 100644 --- a/infra/auth-logic/src/main/java/com/simprints/infra/authlogic/authenticator/SignerManager.kt +++ b/infra/auth-logic/src/main/java/com/simprints/infra/authlogic/authenticator/SignerManager.kt @@ -8,6 +8,7 @@ import com.simprints.infra.config.store.ConfigRepository import com.simprints.infra.enrolment.records.store.EnrolmentRecordRepository import com.simprints.infra.events.EventRepository import com.simprints.infra.images.ImageRepository +import com.simprints.infra.license.LicenseRepository import com.simprints.infra.logging.LoggingConstants import com.simprints.infra.logging.Simber import com.simprints.infra.network.SimNetwork @@ -24,6 +25,7 @@ internal class SignerManager @Inject constructor( private val imageRepository: ImageRepository, private val eventRepository: EventRepository, private val enrolmentRecordRepository: EnrolmentRecordRepository, + private val licenseRepository: LicenseRepository, private val scannerManager: ScannerManager, @DispatcherIO private val dispatcher: CoroutineDispatcher, ) { @@ -61,6 +63,7 @@ internal class SignerManager @Inject constructor( eventRepository.deleteAll() enrolmentRecordRepository.deleteAll() scannerManager.deleteFirmwareFiles() + licenseRepository.deleteCachedLicenses() Simber.tag(LoggingConstants.CrashReportTag.LOGOUT.name).i("Signed out") } diff --git a/infra/auth-logic/src/test/java/com/simprints/infra/authlogic/authenticator/SignerManagerTest.kt b/infra/auth-logic/src/test/java/com/simprints/infra/authlogic/authenticator/SignerManagerTest.kt index 5a20e26f9a..0e00389546 100644 --- a/infra/auth-logic/src/test/java/com/simprints/infra/authlogic/authenticator/SignerManagerTest.kt +++ b/infra/auth-logic/src/test/java/com/simprints/infra/authlogic/authenticator/SignerManagerTest.kt @@ -12,6 +12,7 @@ import com.simprints.infra.enrolment.records.store.EnrolmentRecordRepository import com.simprints.infra.events.EventRepository import com.simprints.infra.events.sampledata.SampleDefaults.DEFAULT_PROJECT_ID import com.simprints.infra.images.ImageRepository +import com.simprints.infra.license.LicenseRepository import com.simprints.infra.network.SimNetwork import com.simprints.infra.recent.user.activity.RecentUserActivityManager import com.simprints.testtools.common.syntax.assertThrows @@ -47,6 +48,9 @@ internal class SignerManagerTest { @MockK lateinit var mockImageRepository: ImageRepository + @MockK + lateinit var mockLicenseRepository: LicenseRepository + @MockK lateinit var mockEnrolmentRecordRepository: EnrolmentRecordRepository @@ -74,6 +78,7 @@ internal class SignerManagerTest { mockImageRepository, mockEventRepository, mockEnrolmentRecordRepository, + mockLicenseRepository, scannerManager, UnconfinedTestDispatcher(), ) @@ -186,6 +191,7 @@ internal class SignerManagerTest { coVerify { mockEventRepository.deleteAll() } coVerify { mockEnrolmentRecordRepository.deleteAll() } coVerify { scannerManager.deleteFirmwareFiles() } + coVerify { mockLicenseRepository.deleteCachedLicenses() } } @Test