diff --git a/CHANGELOG.md b/CHANGELOG.md index cd9ba114fc7..734b62088c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ ownCloud admins and users. * Change - Replace auto-uploads with automatic uploads: [#4252](https://github.com/owncloud/android/issues/4252) * Enhancement - Unit tests for repository classes - Part 2: [#4233](https://github.com/owncloud/android/issues/4233) +* Enhancement - Unit tests for repository classes - Part 3: [#4234](https://github.com/owncloud/android/issues/4234) * Enhancement - Add status message when (un)setting av. offline from preview: [#4382](https://github.com/owncloud/android/issues/4382) * Enhancement - Quota improvements from GraphAPI: [#4411](https://github.com/owncloud/android/issues/4411) * Enhancement - Upgraded AGP version to 8.7.2: [#4478](https://github.com/owncloud/android/issues/4478) @@ -60,6 +61,15 @@ ownCloud admins and users. https://github.com/owncloud/android/issues/4233 https://github.com/owncloud/android/pull/4389 +* Enhancement - Unit tests for repository classes - Part 3: [#4234](https://github.com/owncloud/android/issues/4234) + + Unit tests for OCFolderBackupRepository, OCOAuthRepository, + OCServerInfoRepository, OCShareeRepository, OCShareRepository classes have been + completed. + + https://github.com/owncloud/android/issues/4234 + https://github.com/owncloud/android/pull/4523 + * Enhancement - Add status message when (un)setting av. offline from preview: [#4382](https://github.com/owncloud/android/issues/4382) A message has been added in all previews when the (un)setting av. offline diff --git a/changelog/unreleased/4523 b/changelog/unreleased/4523 new file mode 100644 index 00000000000..9dadb4b2bd4 --- /dev/null +++ b/changelog/unreleased/4523 @@ -0,0 +1,7 @@ +Enhancement: Unit tests for repository classes - Part 3 + +Unit tests for OCFolderBackupRepository, OCOAuthRepository, OCServerInfoRepository, +OCShareeRepository, OCShareRepository classes have been completed. + +https://github.com/owncloud/android/issues/4234 +https://github.com/owncloud/android/pull/4523 diff --git a/owncloudApp/src/main/java/com/owncloud/android/dependecyinjection/RepositoryModule.kt b/owncloudApp/src/main/java/com/owncloud/android/dependecyinjection/RepositoryModule.kt index 1272ad5e08a..4ca974f24c2 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/dependecyinjection/RepositoryModule.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/dependecyinjection/RepositoryModule.kt @@ -26,7 +26,7 @@ import com.owncloud.android.data.appregistry.repository.OCAppRegistryRepository import com.owncloud.android.data.authentication.repository.OCAuthenticationRepository import com.owncloud.android.data.capabilities.repository.OCCapabilityRepository import com.owncloud.android.data.files.repository.OCFileRepository -import com.owncloud.android.data.folderbackup.OCFolderBackupRepository +import com.owncloud.android.data.folderbackup.repository.OCFolderBackupRepository import com.owncloud.android.data.oauth.repository.OCOAuthRepository import com.owncloud.android.data.server.repository.OCServerInfoRepository import com.owncloud.android.data.sharing.sharees.repository.OCShareeRepository diff --git a/owncloudData/src/main/java/com/owncloud/android/data/folderbackup/OCFolderBackupRepository.kt b/owncloudData/src/main/java/com/owncloud/android/data/folderbackup/repository/OCFolderBackupRepository.kt similarity index 97% rename from owncloudData/src/main/java/com/owncloud/android/data/folderbackup/OCFolderBackupRepository.kt rename to owncloudData/src/main/java/com/owncloud/android/data/folderbackup/repository/OCFolderBackupRepository.kt index 338686c5499..3e859628c04 100644 --- a/owncloudData/src/main/java/com/owncloud/android/data/folderbackup/OCFolderBackupRepository.kt +++ b/owncloudData/src/main/java/com/owncloud/android/data/folderbackup/repository/OCFolderBackupRepository.kt @@ -16,7 +16,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package com.owncloud.android.data.folderbackup +package com.owncloud.android.data.folderbackup.repository import com.owncloud.android.data.folderbackup.datasources.LocalFolderBackupDataSource import com.owncloud.android.domain.automaticuploads.FolderBackupRepository diff --git a/owncloudData/src/test/java/com/owncloud/android/data/folderbackup/repository/OCFolderBackupRepositoryTest.kt b/owncloudData/src/test/java/com/owncloud/android/data/folderbackup/repository/OCFolderBackupRepositoryTest.kt new file mode 100644 index 00000000000..65b1a9fbd61 --- /dev/null +++ b/owncloudData/src/test/java/com/owncloud/android/data/folderbackup/repository/OCFolderBackupRepositoryTest.kt @@ -0,0 +1,115 @@ +/** + * ownCloud Android client application + * + * @author Jorge Aguado Recio + * + * Copyright (C) 2024 ownCloud GmbH. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.owncloud.android.data.folderbackup.repository + +import com.owncloud.android.data.folderbackup.datasources.LocalFolderBackupDataSource +import com.owncloud.android.testutil.OC_AUTOMATIC_UPLOADS_CONFIGURATION +import com.owncloud.android.testutil.OC_BACKUP +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.test.runTest +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +class OCFolderBackupRepositoryTest { + + private val localFolderBackupDataSource = mockk(relaxUnitFun = true) + private val ocFolderBackupRepository = OCFolderBackupRepository(localFolderBackupDataSource) + + @Test + fun `getAutomaticUploadsConfiguration returns an AutomaticUploadsConfiguration`() { + every { + localFolderBackupDataSource.getAutomaticUploadsConfiguration() + } returns OC_AUTOMATIC_UPLOADS_CONFIGURATION + + val automaticUploadsConfiguration = ocFolderBackupRepository.getAutomaticUploadsConfiguration() + assertEquals(OC_AUTOMATIC_UPLOADS_CONFIGURATION, automaticUploadsConfiguration) + + verify(exactly = 1) { + localFolderBackupDataSource.getAutomaticUploadsConfiguration() + } + } + + @Test + fun `getAutomaticUploadsConfiguration returns null when local datasource returns a null configuration`() { + every { + localFolderBackupDataSource.getAutomaticUploadsConfiguration() + } returns null + + val automaticUploadsConfiguration = ocFolderBackupRepository.getAutomaticUploadsConfiguration() + assertNull(automaticUploadsConfiguration) + + verify(exactly = 1) { + localFolderBackupDataSource.getAutomaticUploadsConfiguration() + } + } + + @Test + fun `getFolderBackupConfigurationByNameAsFlow returns a Flow with a FolderBackUpConfiguration`() = runTest { + every { + localFolderBackupDataSource.getFolderBackupConfigurationByNameAsFlow(OC_BACKUP.name) + } returns flowOf(OC_BACKUP) + + val folderBackUpConfiguration = ocFolderBackupRepository.getFolderBackupConfigurationByNameAsFlow(OC_BACKUP.name).first() + assertEquals(OC_BACKUP, folderBackUpConfiguration) + + verify(exactly = 1) { + localFolderBackupDataSource.getFolderBackupConfigurationByNameAsFlow(OC_BACKUP.name) + } + } + + @Test + fun `getFolderBackupConfigurationByNameAsFlow returns a Flow with null when local datasource returns a Flow with null `() = runTest { + every { + localFolderBackupDataSource.getFolderBackupConfigurationByNameAsFlow(OC_BACKUP.name) + } returns flowOf(null) + + val folderBackUpConfiguration = ocFolderBackupRepository.getFolderBackupConfigurationByNameAsFlow(OC_BACKUP.name).first() + assertNull(folderBackUpConfiguration) + + verify(exactly = 1) { + localFolderBackupDataSource.getFolderBackupConfigurationByNameAsFlow(OC_BACKUP.name) + } + } + + @Test + fun `saveFolderBackupConfiguration saves a folder backup configuration correctly`() { + ocFolderBackupRepository.saveFolderBackupConfiguration(OC_BACKUP) + + verify(exactly = 1) { + localFolderBackupDataSource.saveFolderBackupConfiguration(OC_BACKUP) + } + } + + @Test + fun `resetFolderBackupConfigurationByName resets a folder backup configuration by name correctly`() { + ocFolderBackupRepository.resetFolderBackupConfigurationByName(OC_BACKUP.name) + + verify(exactly = 1) { + localFolderBackupDataSource.resetFolderBackupConfigurationByName(OC_BACKUP.name) + } + } + +} diff --git a/owncloudData/src/test/java/com/owncloud/android/data/oauth/OAuthRepositoryTest.kt b/owncloudData/src/test/java/com/owncloud/android/data/oauth/OAuthRepositoryTest.kt deleted file mode 100644 index 1d83f8cf27c..00000000000 --- a/owncloudData/src/test/java/com/owncloud/android/data/oauth/OAuthRepositoryTest.kt +++ /dev/null @@ -1,113 +0,0 @@ -/** - * ownCloud Android client application - * - * @author Abel García de Prada - * @author Juan Carlos Garrote Gascón - * - * Copyright (C) 2023 ownCloud GmbH. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.owncloud.android.data.oauth - -import com.owncloud.android.data.oauth.datasources.RemoteOAuthDataSource -import com.owncloud.android.data.oauth.repository.OCOAuthRepository -import com.owncloud.android.domain.authentication.oauth.OAuthRepository -import com.owncloud.android.testutil.OC_SECURE_SERVER_INFO_BASIC_AUTH -import com.owncloud.android.testutil.oauth.OC_CLIENT_REGISTRATION -import com.owncloud.android.testutil.oauth.OC_CLIENT_REGISTRATION_REQUEST -import com.owncloud.android.testutil.oauth.OC_OIDC_SERVER_CONFIGURATION -import com.owncloud.android.testutil.oauth.OC_TOKEN_REQUEST_ACCESS -import com.owncloud.android.testutil.oauth.OC_TOKEN_RESPONSE -import io.mockk.every -import io.mockk.mockk -import io.mockk.verify -import org.junit.Assert.assertEquals -import org.junit.Test - -class OAuthRepositoryTest { - - private val remoteOAuthDataSource = mockk(relaxed = true) - private val oAuthRepository: OAuthRepository = OCOAuthRepository(remoteOAuthDataSource) - - private val issuerOIDCFromWebFinger = "server.url/oidc/issuer" - - @Test - fun `perform oidc discovery - issuer from webfinger - ok`() { - every { remoteOAuthDataSource.performOIDCDiscovery(issuerOIDCFromWebFinger) } returns OC_OIDC_SERVER_CONFIGURATION - - val oidcServerConfiguration = oAuthRepository.performOIDCDiscovery(issuerOIDCFromWebFinger) - - verify(exactly = 1) { - remoteOAuthDataSource.performOIDCDiscovery(issuerOIDCFromWebFinger) - } - assertEquals(OC_OIDC_SERVER_CONFIGURATION, oidcServerConfiguration) - } - - @Test - fun `perform oidc discovery - issuer from base url - ok`() { - every { remoteOAuthDataSource.performOIDCDiscovery(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl) } returns OC_OIDC_SERVER_CONFIGURATION - - val oidcServerConfiguration = oAuthRepository.performOIDCDiscovery(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl) - - verify(exactly = 1) { - remoteOAuthDataSource.performOIDCDiscovery(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl) - } - assertEquals(OC_OIDC_SERVER_CONFIGURATION, oidcServerConfiguration) - - } - - @Test(expected = Exception::class) - fun `perform oidc discovery - ko`() { - every { remoteOAuthDataSource.performOIDCDiscovery(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl) } throws Exception() - - oAuthRepository.performOIDCDiscovery(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl) - } - - @Test - fun `perform token request - ok`() { - every { remoteOAuthDataSource.performTokenRequest(OC_TOKEN_REQUEST_ACCESS) } returns OC_TOKEN_RESPONSE - - oAuthRepository.performTokenRequest(OC_TOKEN_REQUEST_ACCESS) - - verify(exactly = 1) { - remoteOAuthDataSource.performTokenRequest(OC_TOKEN_REQUEST_ACCESS) - } - } - - @Test(expected = Exception::class) - fun `perform token request - ko`() { - every { remoteOAuthDataSource.performTokenRequest(OC_TOKEN_REQUEST_ACCESS) } throws Exception() - - oAuthRepository.performTokenRequest(OC_TOKEN_REQUEST_ACCESS) - } - - @Test - fun `register client - ok`() { - every { remoteOAuthDataSource.registerClient(OC_CLIENT_REGISTRATION_REQUEST) } returns OC_CLIENT_REGISTRATION - - oAuthRepository.registerClient(OC_CLIENT_REGISTRATION_REQUEST) - - verify(exactly = 1) { - remoteOAuthDataSource.registerClient(OC_CLIENT_REGISTRATION_REQUEST) - } - } - - @Test(expected = Exception::class) - fun `register client - ko`() { - every { remoteOAuthDataSource.registerClient(OC_CLIENT_REGISTRATION_REQUEST) } throws Exception() - - remoteOAuthDataSource.registerClient(OC_CLIENT_REGISTRATION_REQUEST) - } -} diff --git a/owncloudData/src/test/java/com/owncloud/android/data/oauth/repository/OCOAuthRepositoryTest.kt b/owncloudData/src/test/java/com/owncloud/android/data/oauth/repository/OCOAuthRepositoryTest.kt new file mode 100644 index 00000000000..e6afc9377fc --- /dev/null +++ b/owncloudData/src/test/java/com/owncloud/android/data/oauth/repository/OCOAuthRepositoryTest.kt @@ -0,0 +1,84 @@ +/** + * ownCloud Android client application + * + * @author Abel García de Prada + * @author Juan Carlos Garrote Gascón + * @author Jorge Aguado Recio + * + * Copyright (C) 2024 ownCloud GmbH. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.owncloud.android.data.oauth.repository + +import com.owncloud.android.data.oauth.datasources.RemoteOAuthDataSource +import com.owncloud.android.testutil.OC_SECURE_SERVER_INFO_OIDC_AUTH +import com.owncloud.android.testutil.oauth.OC_CLIENT_REGISTRATION +import com.owncloud.android.testutil.oauth.OC_CLIENT_REGISTRATION_REQUEST +import com.owncloud.android.testutil.oauth.OC_OIDC_SERVER_CONFIGURATION +import com.owncloud.android.testutil.oauth.OC_TOKEN_REQUEST_ACCESS +import com.owncloud.android.testutil.oauth.OC_TOKEN_RESPONSE +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import org.junit.Assert.assertEquals +import org.junit.Test + +class OCOAuthRepositoryTest { + + private val remoteOAuthDataSource = mockk() + private val oAuthRepository = OCOAuthRepository(remoteOAuthDataSource) + + @Test + fun `performOIDCDiscovery returns an OIDCServerConfiguration`() { + every { + remoteOAuthDataSource.performOIDCDiscovery(OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl) + } returns OC_OIDC_SERVER_CONFIGURATION + + val oidcServerConfiguration = oAuthRepository.performOIDCDiscovery(OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl) + assertEquals(OC_OIDC_SERVER_CONFIGURATION, oidcServerConfiguration) + + verify(exactly = 1) { + remoteOAuthDataSource.performOIDCDiscovery(OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl) + } + } + + @Test + fun `performTokenRequest returns a TokenResponse`() { + every { + remoteOAuthDataSource.performTokenRequest(OC_TOKEN_REQUEST_ACCESS) + } returns OC_TOKEN_RESPONSE + + val tokenResponse = oAuthRepository.performTokenRequest(OC_TOKEN_REQUEST_ACCESS) + assertEquals(OC_TOKEN_RESPONSE, tokenResponse) + + verify(exactly = 1) { + remoteOAuthDataSource.performTokenRequest(OC_TOKEN_REQUEST_ACCESS) + } + } + + @Test + fun `registerClient returns a ClientRegistrationInfo`() { + every { + remoteOAuthDataSource.registerClient(OC_CLIENT_REGISTRATION_REQUEST) + } returns OC_CLIENT_REGISTRATION + + val clientRegistrationInfo = oAuthRepository.registerClient(OC_CLIENT_REGISTRATION_REQUEST) + assertEquals(OC_CLIENT_REGISTRATION, clientRegistrationInfo) + + verify(exactly = 1) { + remoteOAuthDataSource.registerClient(OC_CLIENT_REGISTRATION_REQUEST) + } + } +} diff --git a/owncloudData/src/test/java/com/owncloud/android/data/server/repository/OCServerInfoRepositoryTest.kt b/owncloudData/src/test/java/com/owncloud/android/data/server/repository/OCServerInfoRepositoryTest.kt index c28ada8c960..8026b0c14b6 100644 --- a/owncloudData/src/test/java/com/owncloud/android/data/server/repository/OCServerInfoRepositoryTest.kt +++ b/owncloudData/src/test/java/com/owncloud/android/data/server/repository/OCServerInfoRepositoryTest.kt @@ -3,8 +3,9 @@ * * @author Abel García de Prada * @author Juan Carlos Garrote Gascón + * @author Jorge Aguado Recio * - * Copyright (C) 2023 ownCloud GmbH. + * Copyright (C) 2024 ownCloud GmbH. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, @@ -24,8 +25,13 @@ package com.owncloud.android.data.server.repository import com.owncloud.android.data.oauth.datasources.RemoteOAuthDataSource import com.owncloud.android.data.server.datasources.RemoteServerInfoDataSource import com.owncloud.android.data.webfinger.datasources.RemoteWebFingerDataSource -import com.owncloud.android.domain.exceptions.NoConnectionWithServerException +import com.owncloud.android.domain.webfinger.model.WebFingerRel import com.owncloud.android.testutil.OC_SECURE_SERVER_INFO_BASIC_AUTH +import com.owncloud.android.testutil.OC_SECURE_SERVER_INFO_BEARER_AUTH +import com.owncloud.android.testutil.OC_SECURE_SERVER_INFO_OIDC_AUTH +import com.owncloud.android.testutil.OC_SECURE_SERVER_INFO_OIDC_AUTH_WEBFINGER_INSTANCE +import com.owncloud.android.testutil.OC_WEBFINGER_INSTANCE_URL +import com.owncloud.android.testutil.oauth.OC_OIDC_SERVER_CONFIGURATION import io.mockk.every import io.mockk.mockk import io.mockk.verify @@ -34,25 +40,206 @@ import org.junit.Test class OCServerInfoRepositoryTest { - private val remoteServerInfoDataSource = mockk(relaxed = true) - private val webFingerDataSource = mockk(relaxed = true) - private val oidcRemoteOAuthDataSource = mockk(relaxed = true) - private val ocServerInfoRepository: OCServerInfoRepository = OCServerInfoRepository(remoteServerInfoDataSource, webFingerDataSource, oidcRemoteOAuthDataSource) + private val remoteServerInfoDataSource = mockk() + private val remoteWebFingerDataSource = mockk() + private val remoteOAuthDataSource = mockk() + private val ocServerInfoRepository = OCServerInfoRepository(remoteServerInfoDataSource, remoteWebFingerDataSource, remoteOAuthDataSource) @Test - fun getServerInfoSuccess() { - every { remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl, false) } returns OC_SECURE_SERVER_INFO_BASIC_AUTH + fun `getServerInfo returns a BasicServer when creatingAccount parameter is false`() { + every { + remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl, false) + } returns OC_SECURE_SERVER_INFO_BASIC_AUTH - val currentValue = ocServerInfoRepository.getServerInfo(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl, false, false) - assertEquals(OC_SECURE_SERVER_INFO_BASIC_AUTH, currentValue) + val basicServer = ocServerInfoRepository.getServerInfo( + path = OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl, + creatingAccount = false, + enforceOIDC = false + ) + assertEquals(OC_SECURE_SERVER_INFO_BASIC_AUTH, basicServer) - verify { remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl, false) } + verify(exactly = 1) { + remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl, false) + } } - @Test(expected = NoConnectionWithServerException::class) - fun getServerInfoNoConnection() { - every { remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl, false) } throws NoConnectionWithServerException() + @Test + fun `getServerInfo returns a BasicServer when creatingAccount parameter is true and webfinger datasource throws an exception`() { + every { + remoteWebFingerDataSource.getInstancesFromWebFinger( + lookupServer = OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl, + rel = WebFingerRel.OIDC_ISSUER_DISCOVERY, + resource = OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl + ) + } throws Exception() + + every { + remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl, false) + } returns OC_SECURE_SERVER_INFO_BASIC_AUTH + + val basicServer = ocServerInfoRepository.getServerInfo( + path = OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl, + creatingAccount = true, + enforceOIDC = false + ) + assertEquals(OC_SECURE_SERVER_INFO_BASIC_AUTH, basicServer) + + verify(exactly = 1) { + remoteWebFingerDataSource.getInstancesFromWebFinger( + lookupServer = OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl, + rel = WebFingerRel.OIDC_ISSUER_DISCOVERY, + resource = OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl + ) + remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl, false) + } + } + + @Test + fun `getServerInfo returns an OAuth2Server when creatingAccount parameter is false`() { + every { + remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl, false) + } returns OC_SECURE_SERVER_INFO_BEARER_AUTH + + every { + remoteOAuthDataSource.performOIDCDiscovery(OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl) + } throws Exception() + + val oAuthServer = ocServerInfoRepository.getServerInfo( + path = OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl, + creatingAccount = false, + enforceOIDC = false + ) + assertEquals(OC_SECURE_SERVER_INFO_BEARER_AUTH, oAuthServer) + + verify(exactly = 1) { + remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl, false) + remoteOAuthDataSource.performOIDCDiscovery(OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl) + } + } + + @Test + fun `getServerInfo returns an OAuth2Server when creatingAccount parameter is true and webfinger datasource throws an exception`() { + every { + remoteWebFingerDataSource.getInstancesFromWebFinger( + lookupServer = OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl, + rel = WebFingerRel.OIDC_ISSUER_DISCOVERY, + resource = OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl + ) + } throws Exception() + + every { + remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl, false) + } returns OC_SECURE_SERVER_INFO_BEARER_AUTH + + every { + remoteOAuthDataSource.performOIDCDiscovery(OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl) + } throws Exception() + + val oAuthServer = ocServerInfoRepository.getServerInfo( + path = OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl, + creatingAccount = true, + enforceOIDC = false + ) + assertEquals(OC_SECURE_SERVER_INFO_BEARER_AUTH, oAuthServer) + + verify(exactly = 1) { + remoteWebFingerDataSource.getInstancesFromWebFinger( + lookupServer = OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl, + rel = WebFingerRel.OIDC_ISSUER_DISCOVERY, + resource = OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl + ) + remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl, false) + remoteOAuthDataSource.performOIDCDiscovery(OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl) + } + } + + @Test + fun `getServerInfo returns an OIDCServer when creatingAccount parameter is false`() { + every { + remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl, false) + } returns OC_SECURE_SERVER_INFO_OIDC_AUTH + + every { + remoteOAuthDataSource.performOIDCDiscovery(OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl) + } returns OC_OIDC_SERVER_CONFIGURATION + + val oIDCServer = ocServerInfoRepository.getServerInfo( + path = OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl, + creatingAccount = false, + enforceOIDC = false + ) + assertEquals(OC_SECURE_SERVER_INFO_OIDC_AUTH, oIDCServer) + + verify(exactly = 1) { + remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl, false) + remoteOAuthDataSource.performOIDCDiscovery(OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl) + } + } + + @Test + fun `getServerInfo returns an OIDCServer when creatingAccount parameter is true and webfinger datasource throws an exception`() { + every { + remoteWebFingerDataSource.getInstancesFromWebFinger( + lookupServer = OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl, + rel = WebFingerRel.OIDC_ISSUER_DISCOVERY, + resource = OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl + ) + } throws Exception() + + every { + remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl, false) + } returns OC_SECURE_SERVER_INFO_OIDC_AUTH + + every { + remoteOAuthDataSource.performOIDCDiscovery(OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl) + } returns OC_OIDC_SERVER_CONFIGURATION + + val oIDCServer = ocServerInfoRepository.getServerInfo( + path = OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl, + creatingAccount = true, + enforceOIDC = false + ) + assertEquals(OC_SECURE_SERVER_INFO_OIDC_AUTH, oIDCServer) + + verify(exactly = 1) { + remoteWebFingerDataSource.getInstancesFromWebFinger( + lookupServer = OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl, + rel = WebFingerRel.OIDC_ISSUER_DISCOVERY, + resource = OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl + ) + remoteServerInfoDataSource.getServerInfo(OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl, false) + remoteOAuthDataSource.performOIDCDiscovery(OC_SECURE_SERVER_INFO_OIDC_AUTH.baseUrl) + } + } + + @Test + fun `getServerInfo returns an OIDCServer when creatingAccount is true and webfinger datasource returns an OIDC issuer`() { + every { + remoteWebFingerDataSource.getInstancesFromWebFinger( + lookupServer = OC_SECURE_SERVER_INFO_OIDC_AUTH_WEBFINGER_INSTANCE.baseUrl, + rel = WebFingerRel.OIDC_ISSUER_DISCOVERY, + resource = OC_SECURE_SERVER_INFO_OIDC_AUTH_WEBFINGER_INSTANCE.baseUrl + ) + } returns listOf(OC_WEBFINGER_INSTANCE_URL) + + every { + remoteOAuthDataSource.performOIDCDiscovery(OC_WEBFINGER_INSTANCE_URL) + } returns OC_OIDC_SERVER_CONFIGURATION + + val oIDCServerWebfinger = ocServerInfoRepository.getServerInfo( + path = OC_SECURE_SERVER_INFO_OIDC_AUTH_WEBFINGER_INSTANCE.baseUrl, + creatingAccount = true, + enforceOIDC = false + ) + assertEquals(OC_SECURE_SERVER_INFO_OIDC_AUTH_WEBFINGER_INSTANCE, oIDCServerWebfinger) - ocServerInfoRepository.getServerInfo(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl, false, false) + verify(exactly = 1) { + remoteWebFingerDataSource.getInstancesFromWebFinger( + lookupServer = OC_SECURE_SERVER_INFO_OIDC_AUTH_WEBFINGER_INSTANCE.baseUrl, + rel = WebFingerRel.OIDC_ISSUER_DISCOVERY, + resource = OC_SECURE_SERVER_INFO_OIDC_AUTH_WEBFINGER_INSTANCE.baseUrl + ) + remoteOAuthDataSource.performOIDCDiscovery(OC_WEBFINGER_INSTANCE_URL) + } } } diff --git a/owncloudData/src/test/java/com/owncloud/android/data/sharing/sharees/repository/OCShareeRepositoryTest.kt b/owncloudData/src/test/java/com/owncloud/android/data/sharing/sharees/repository/OCShareeRepositoryTest.kt index 1164879488e..c8c005e717c 100644 --- a/owncloudData/src/test/java/com/owncloud/android/data/sharing/sharees/repository/OCShareeRepositoryTest.kt +++ b/owncloudData/src/test/java/com/owncloud/android/data/sharing/sharees/repository/OCShareeRepositoryTest.kt @@ -2,7 +2,9 @@ * ownCloud Android client application * * @author David González Verdugo - * Copyright (C) 2020 ownCloud GmbH. + * @author Jorge Aguado Recio + * + * Copyright (C) 2024 ownCloud GmbH. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, @@ -20,37 +22,34 @@ package com.owncloud.android.data.sharing.sharees.repository import com.owncloud.android.data.sharing.sharees.datasources.RemoteShareeDataSource -import com.owncloud.android.domain.exceptions.NoConnectionWithServerException import com.owncloud.android.testutil.OC_ACCOUNT_NAME +import com.owncloud.android.testutil.OC_SHAREE import io.mockk.every import io.mockk.mockk import io.mockk.verify +import org.junit.Assert.assertEquals import org.junit.Test class OCShareeRepositoryTest { - private val remoteShareeDataSource = mockk(relaxed = true) - private val oCShareeRepository: OCShareeRepository = OCShareeRepository((remoteShareeDataSource)) + private val remoteShareeDataSource = mockk() + private val ocShareeRepository = OCShareeRepository(remoteShareeDataSource) @Test - fun readShareesFromNetworkOk() { - every { remoteShareeDataSource.getSharees(any(), any(), any(), any()) } returns arrayListOf() - - oCShareeRepository.getSharees("user", 1, 5, OC_ACCOUNT_NAME) - - verify(exactly = 1) { - remoteShareeDataSource.getSharees("user", 1, 5, OC_ACCOUNT_NAME) - } - } + fun `getSharees returns a list of OCSharees`() { + val searchString = "user" + val requestedPage = 1 + val resultsPerPage = 30 - @Test(expected = NoConnectionWithServerException::class) - fun readShareesFromNetworkNoConnection() { - every { remoteShareeDataSource.getSharees(any(), any(), any(), any()) } throws NoConnectionWithServerException() + every { + remoteShareeDataSource.getSharees(searchString, requestedPage, resultsPerPage, OC_ACCOUNT_NAME) + } returns listOf(OC_SHAREE) - oCShareeRepository.getSharees("user", 1, 5, OC_ACCOUNT_NAME) + val listOfSharees = ocShareeRepository.getSharees(searchString, requestedPage, resultsPerPage, OC_ACCOUNT_NAME) + assertEquals(listOf(OC_SHAREE), listOfSharees) verify(exactly = 1) { - remoteShareeDataSource.getSharees("user", 1, 5, OC_ACCOUNT_NAME) + remoteShareeDataSource.getSharees(searchString, requestedPage, resultsPerPage, OC_ACCOUNT_NAME) } } } diff --git a/owncloudData/src/test/java/com/owncloud/android/data/sharing/shares/repository/OCShareRepositoryTest.kt b/owncloudData/src/test/java/com/owncloud/android/data/sharing/shares/repository/OCShareRepositoryTest.kt index e171ce76b55..50fdb95c897 100644 --- a/owncloudData/src/test/java/com/owncloud/android/data/sharing/shares/repository/OCShareRepositoryTest.kt +++ b/owncloudData/src/test/java/com/owncloud/android/data/sharing/shares/repository/OCShareRepositoryTest.kt @@ -20,496 +20,294 @@ package com.owncloud.android.data.sharing.shares.repository import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import com.owncloud.android.data.sharing.shares.datasources.LocalShareDataSource import com.owncloud.android.data.sharing.shares.datasources.RemoteShareDataSource -import com.owncloud.android.domain.exceptions.FileNotFoundException -import com.owncloud.android.domain.exceptions.NoConnectionWithServerException import com.owncloud.android.domain.sharing.shares.model.OCShare import com.owncloud.android.domain.sharing.shares.model.ShareType +import com.owncloud.android.lib.resources.shares.RemoteShare import com.owncloud.android.testutil.OC_ACCOUNT_NAME import com.owncloud.android.testutil.OC_PRIVATE_SHARE import com.owncloud.android.testutil.OC_PUBLIC_SHARE import com.owncloud.android.testutil.OC_SHARE +import com.owncloud.android.testutil.OC_SHAREE +import com.owncloud.android.testutil.livedata.getLastEmittedValue import io.mockk.every import io.mockk.mockk import io.mockk.verify -import org.junit.Assert +import junit.framework.TestCase.assertEquals import org.junit.Rule import org.junit.Test class OCShareRepositoryTest { + @Rule @JvmField val instantExecutorRule = InstantTaskExecutorRule() - private val localShareDataSource = mockk(relaxed = true) - private val remoteShareDataSource = mockk(relaxed = true) - private val ocShareRepository: OCShareRepository = - OCShareRepository(localShareDataSource, remoteShareDataSource) - - private val shares = arrayListOf( - OC_PUBLIC_SHARE, - OC_PRIVATE_SHARE, - OC_PRIVATE_SHARE, - OC_PUBLIC_SHARE - ) - - private val filePath = "/Images" - private val accountName = "admin@server" - - @Test - fun refreshSharesFromNetworkOk() { - every { remoteShareDataSource.getShares(any(), any(), any(), any()) } returns shares - - ocShareRepository.refreshSharesFromNetwork(filePath, accountName) - - verify(exactly = 1) { - remoteShareDataSource.getShares( - remoteFilePath = filePath, - reshares = true, - subfiles = false, - accountName = accountName - ) - } + private val localShareDataSource = mockk(relaxUnitFun = true) + private val remoteShareDataSource = mockk(relaxUnitFun = true) + private val ocShareRepository = OCShareRepository(localShareDataSource, remoteShareDataSource) - verify(exactly = 1) { - localShareDataSource.replaceShares(shares) - } - } + private val listOfShares = listOf(OC_PRIVATE_SHARE, OC_PUBLIC_SHARE) + private val filePath = OC_SHARE.path + private val password = "password" + private val permissions = OC_SHARE.permissions + private val expiration = RemoteShare.INIT_EXPIRATION_DATE_IN_MILLIS @Test - fun refreshSharesFromNetworkEmptyShares() { - every { remoteShareDataSource.getShares(any(), any(), any(), any()) } returns listOf() - - ocShareRepository.refreshSharesFromNetwork(filePath, accountName) - - verify(exactly = 1) { - remoteShareDataSource.getShares( + fun `insertPrivateShare inserts a private OCShare correctly`() { + every { + remoteShareDataSource.insert( remoteFilePath = filePath, - reshares = true, - subfiles = false, - accountName = accountName + shareType = OC_PRIVATE_SHARE.shareType, + shareWith = OC_SHAREE.shareWith, + permissions = permissions, + name = "", + password = "", + expirationDate = expiration, + accountName = OC_ACCOUNT_NAME ) - } - - verify(exactly = 1) { - localShareDataSource.deleteSharesForFile(filePath, accountName) - } - } + } returns OC_PRIVATE_SHARE - @Test(expected = NoConnectionWithServerException::class) - fun refreshSharesFromNetworkNoConnection() { - every { remoteShareDataSource.getShares(any(), any(), any(), any()) } throws NoConnectionWithServerException() + // The result of this method is not used, so it can be anything + every { + localShareDataSource.insert(OC_PRIVATE_SHARE) + } returns 1 - ocShareRepository.refreshSharesFromNetwork(filePath, accountName) + ocShareRepository.insertPrivateShare(filePath, OC_PRIVATE_SHARE.shareType, OC_SHAREE.shareWith, permissions, OC_ACCOUNT_NAME) verify(exactly = 1) { - remoteShareDataSource.getShares( + remoteShareDataSource.insert( remoteFilePath = filePath, - reshares = true, - subfiles = false, - accountName = accountName + shareType = OC_PRIVATE_SHARE.shareType, + shareWith = OC_SHAREE.shareWith, + permissions = permissions, + name = "", + password = "", + expirationDate = expiration, + accountName = OC_ACCOUNT_NAME ) + localShareDataSource.insert(OC_PRIVATE_SHARE) } - - verify(exactly = 1) { - localShareDataSource.replaceShares(shares) - } - } - - @Test - fun getSharesAsLiveDataOk() { - val sharesLiveData = MutableLiveData>() - - every { - localShareDataSource.getSharesAsLiveData(any(), any(), any()) - } returns sharesLiveData - - val sharesEmitted = mutableListOf>() - ocShareRepository.getSharesAsLiveData(filePath, accountName).observeForever { - sharesEmitted.add(it!!) - } - - val sharesToEmit = listOf(shares) - sharesToEmit.forEach { - sharesLiveData.postValue(it) - } - - Assert.assertEquals(sharesToEmit, sharesEmitted) - } - - @Test(expected = Exception::class) - fun getSharesAsLiveDataException() { - val sharesLiveData = MutableLiveData>() - - every { - localShareDataSource.getSharesAsLiveData(any(), any(), any()) - } throws Exception() - - val sharesEmitted = mutableListOf>() - ocShareRepository.getSharesAsLiveData(filePath, accountName) - - val sharesToEmit = listOf(shares) - sharesToEmit.forEach { - sharesLiveData.postValue(it) - } - - Assert.assertEquals(sharesToEmit, sharesEmitted) } @Test - fun getShareAsLiveDataOk() { - val shareLiveData = MutableLiveData() - + fun `updatePrivateShare updates a private OCShare correctly`() { every { - localShareDataSource.getShareAsLiveData(any()) - } returns shareLiveData - - val sharesEmitted = mutableListOf() - ocShareRepository.getShareAsLiveData(OC_SHARE.remoteId).observeForever { - sharesEmitted.add(it!!) - } - - val sharesToEmit = listOf(shares.first()) - - sharesToEmit.forEach { - shareLiveData.postValue(it) - } - - Assert.assertEquals(sharesToEmit, sharesEmitted) - } - - @Test(expected = Exception::class) - fun getShareAsLiveDataException() { - val shareLiveData = MutableLiveData() + remoteShareDataSource.updateShare( + remoteId = OC_PRIVATE_SHARE.remoteId, + name = "", + password = "", + expirationDateInMillis = expiration, + permissions = permissions, + accountName = OC_ACCOUNT_NAME + ) + } returns OC_PRIVATE_SHARE + // The result of this method is not used, so it can be anything every { - localShareDataSource.getShareAsLiveData(any()) - } throws Exception() - - val sharesEmitted = mutableListOf() - ocShareRepository.getShareAsLiveData(OC_SHARE.remoteId) + localShareDataSource.update(OC_PRIVATE_SHARE) + } returns 1 - val sharesToEmit = listOf(shares.first()) + ocShareRepository.updatePrivateShare(OC_PRIVATE_SHARE.remoteId, permissions, OC_ACCOUNT_NAME) - sharesToEmit.forEach { - shareLiveData.postValue(it) + verify(exactly = 1) { + remoteShareDataSource.updateShare( + remoteId = OC_PRIVATE_SHARE.remoteId, + name = "", + password = "", + expirationDateInMillis = expiration, + permissions = permissions, + accountName = OC_ACCOUNT_NAME + ) + localShareDataSource.update(OC_PRIVATE_SHARE) } - - Assert.assertEquals(sharesToEmit, sharesEmitted) } @Test - fun insertPublicShareOk() { + fun `insertPublicShare inserts a public OCShare correctly`() { every { remoteShareDataSource.insert( - any(), - any(), - any(), - any(), - any(), - any(), - any(), - any() - ) - } returns shares.first() - - ocShareRepository.insertPublicShare( - filePath, - -1, - "Docs link", - "password", - 2000, - accountName - ) - - verify(exactly = 1) { - remoteShareDataSource.insert( - filePath, - ShareType.PUBLIC_LINK, - "", - -1, - "Docs link", - "password", - 2000, - accountName + remoteFilePath = filePath, + shareType = OC_PUBLIC_SHARE.shareType, + shareWith = "", + permissions = permissions, + name = OC_PUBLIC_SHARE.name!!, + password = password, + expirationDate = expiration, + accountName = OC_ACCOUNT_NAME ) - } - - verify(exactly = 1) { localShareDataSource.insert(shares.first()) } - } + } returns OC_PUBLIC_SHARE - @Test(expected = FileNotFoundException::class) - fun insertPublicShareFileNotFoundException() { + // The result of this method is not used, so it can be anything every { - remoteShareDataSource.insert( - any(), - any(), - any(), - any(), - any(), - any(), - any(), - any() - ) - } throws FileNotFoundException() + localShareDataSource.insert(OC_PUBLIC_SHARE) + } returns 1 - ocShareRepository.insertPublicShare( - filePath, - -1, - "Docs link", - "password", - 2000, - accountName - ) + ocShareRepository.insertPublicShare(filePath, permissions, OC_PUBLIC_SHARE.name!!, password, expiration, OC_ACCOUNT_NAME) verify(exactly = 1) { remoteShareDataSource.insert( - filePath, - ShareType.PUBLIC_LINK, - "", - -1, - "Docs link", - "password", - 2000, - accountName + remoteFilePath = filePath, + shareType = OC_PUBLIC_SHARE.shareType, + shareWith = "", + permissions = permissions, + name = OC_PUBLIC_SHARE.name!!, + password = password, + expirationDate = expiration, + accountName = OC_ACCOUNT_NAME ) - } - - verify(exactly = 0) { - localShareDataSource.insert(shares.first()) + localShareDataSource.insert(OC_PUBLIC_SHARE) } } @Test - fun updatePublicShareOk() { + fun `updatePublicShare updates a public OCShare correctly`() { every { remoteShareDataSource.updateShare( - any(), - any(), - any(), - any(), - any(), - any() - ) - } returns shares.first() - - ocShareRepository.updatePublicShare( - OC_SHARE.remoteId, - "Docs link", - "password", - 2000, - -1, - accountName - ) - - verify(exactly = 1) { - remoteShareDataSource.updateShare( - OC_SHARE.remoteId, - "Docs link", - "password", - 2000, - -1, - accountName + remoteId = OC_PUBLIC_SHARE.remoteId, + name = OC_PUBLIC_SHARE.name!!, + password = password, + expirationDateInMillis = expiration, + permissions = permissions, + accountName = OC_ACCOUNT_NAME ) - } + } returns OC_PUBLIC_SHARE - verify(exactly = 1) { localShareDataSource.update(shares.first()) } - } - - @Test(expected = FileNotFoundException::class) - fun updatePublicShareFileNotFoundException() { + // The result of this method is not used, so it can be anything every { - remoteShareDataSource.updateShare( - any(), - any(), - any(), - any(), - any(), - any() - ) - } throws FileNotFoundException() + localShareDataSource.update(OC_PUBLIC_SHARE) + } returns 1 - ocShareRepository.updatePublicShare( - OC_SHARE.remoteId, - "Docs link", - "password", - 2000, - -1, - accountName - ) + ocShareRepository.updatePublicShare(OC_PUBLIC_SHARE.remoteId, OC_PUBLIC_SHARE.name!!, password, expiration, permissions, OC_ACCOUNT_NAME) verify(exactly = 1) { remoteShareDataSource.updateShare( - OC_SHARE.remoteId, - "Docs link", - "password", - 2000, - -1, - accountName + remoteId = OC_PUBLIC_SHARE.remoteId, + name = OC_PUBLIC_SHARE.name!!, + password = password, + expirationDateInMillis = expiration, + permissions = permissions, + accountName = OC_ACCOUNT_NAME ) - } - - verify(exactly = 0) { - localShareDataSource.update(shares.first()) + localShareDataSource.update(OC_PUBLIC_SHARE) } } @Test - fun insertPrivateShareOk() { + fun `getSharesAsLiveData returns a LiveData with a list of OCShares`() { + val sharesLiveDataList: LiveData> = MutableLiveData(listOf(OC_SHARE)) + every { - remoteShareDataSource.insert( - any(), - any(), - any(), - any(), - any(), - any(), - any(), - any() + localShareDataSource.getSharesAsLiveData( + filePath = filePath, + accountName = OC_ACCOUNT_NAME, + shareTypes = listOf(ShareType.PUBLIC_LINK, ShareType.USER, ShareType.GROUP, ShareType.FEDERATED) ) - } returns shares[2] + } returns sharesLiveDataList - ocShareRepository.insertPrivateShare( - filePath, - ShareType.GROUP, - "whoever", - -1, - accountName - ) + val sharesResult = ocShareRepository.getSharesAsLiveData(filePath, OC_ACCOUNT_NAME).getLastEmittedValue()!! + assertEquals(1, sharesResult.size) + assertEquals(OC_SHARE, sharesResult.first()) verify(exactly = 1) { - remoteShareDataSource.insert( - filePath, - ShareType.GROUP, - "whoever", - -1, - accountName = accountName + localShareDataSource.getSharesAsLiveData( + filePath = filePath, + accountName = OC_ACCOUNT_NAME, + shareTypes = listOf(ShareType.PUBLIC_LINK, ShareType.USER, ShareType.GROUP, ShareType.FEDERATED) ) } - - verify(exactly = 1) { localShareDataSource.insert(shares[2]) } } - @Test(expected = FileNotFoundException::class) - fun insertPrivateShareFileNotFoundException() { + @Test + fun `getShareAsLiveData returns a LiveData with an OCShare`() { + val shareAsLiveData: LiveData = MutableLiveData(OC_SHARE) + every { - remoteShareDataSource.insert( - any(), - any(), - any(), - any(), - any(), - any(), - any(), - any() - ) - } throws FileNotFoundException() + localShareDataSource.getShareAsLiveData(OC_SHARE.remoteId) + } returns shareAsLiveData - ocShareRepository.insertPrivateShare( - filePath, - ShareType.GROUP, - "whoever", - -1, - accountName - ) + val shareResult = ocShareRepository.getShareAsLiveData(OC_SHARE.remoteId).getLastEmittedValue()!! + assertEquals(OC_SHARE, shareResult) verify(exactly = 1) { - remoteShareDataSource.insert( - filePath, - ShareType.GROUP, - "whoever", - -1, - accountName = accountName - ) - } - - verify(exactly = 0) { - localShareDataSource.insert(shares[2]) + localShareDataSource.getShareAsLiveData(OC_SHARE.remoteId) } } @Test - fun updatePrivateShareOk() { + fun `refreshSharesFromNetwork refreshes shares correctly when the list of shares received is not empty`() { every { - remoteShareDataSource.updateShare( - any(), - any(), - any(), - any(), - any(), - any() + remoteShareDataSource.getShares( + remoteFilePath = filePath, + reshares = true, + subfiles = false, + accountName = OC_ACCOUNT_NAME ) - } returns shares[2] + } returns listOfShares + + // The result of this method is not used, so it can be anything + every { + localShareDataSource.replaceShares(listOfShares) + } returns listOf(1, 1) - ocShareRepository.updatePrivateShare( - OC_SHARE.remoteId, - -1, - accountName - ) + ocShareRepository.refreshSharesFromNetwork(filePath, OC_ACCOUNT_NAME) verify(exactly = 1) { - remoteShareDataSource.updateShare( - OC_SHARE.remoteId, - permissions = -1, - accountName = accountName + remoteShareDataSource.getShares( + remoteFilePath = filePath, + reshares = true, + subfiles = false, + accountName = OC_ACCOUNT_NAME ) + localShareDataSource.replaceShares(listOfShares) } - - verify(exactly = 1) { localShareDataSource.update(shares[2]) } } - @Test(expected = FileNotFoundException::class) - fun updatePrivateShareFileNotFoundException() { + @Test + fun `refreshSharesFromNetwork deletes local shares and refreshes shares correctly when the list of shares received is empty`() { every { - remoteShareDataSource.updateShare( - any(), - any(), - any(), - any(), - any(), - any() + remoteShareDataSource.getShares( + remoteFilePath = filePath, + reshares = true, + subfiles = false, + accountName = OC_ACCOUNT_NAME ) - } throws FileNotFoundException() + } returns emptyList() + + every { + localShareDataSource.replaceShares(emptyList()) + } returns emptyList() - ocShareRepository.updatePrivateShare( - OC_SHARE.remoteId, - -1, - accountName - ) + ocShareRepository.refreshSharesFromNetwork(filePath, OC_ACCOUNT_NAME) verify(exactly = 1) { - remoteShareDataSource.updateShare( - OC_SHARE.remoteId, - permissions = -1, - accountName = accountName + remoteShareDataSource.getShares( + remoteFilePath = filePath, + reshares = true, + subfiles = false, + accountName = OC_ACCOUNT_NAME ) + localShareDataSource.deleteSharesForFile(filePath, OC_ACCOUNT_NAME) + localShareDataSource.replaceShares(emptyList()) } - - verify(exactly = 0) { localShareDataSource.update(shares[2]) } } @Test - fun removeShare() { - val shareId = "fjCZxtidwFrzoCl" - ocShareRepository.deleteShare(shareId, OC_ACCOUNT_NAME) - - verify(exactly = 1) { remoteShareDataSource.deleteShare(shareId, OC_ACCOUNT_NAME) } - verify(exactly = 1) { localShareDataSource.deleteShare(shareId) } - } - - @Test(expected = FileNotFoundException::class) - fun removeShareFileNotFoundException() { - val shareId = "fjCZxtidwFrzoCl" - + fun `deleteShare deletes a share correctly`() { + // The result of this method is not used, so it can be anything every { - remoteShareDataSource.deleteShare(shareId, OC_ACCOUNT_NAME) - } throws FileNotFoundException() + localShareDataSource.deleteShare(OC_SHARE.remoteId) + } returns 1 - ocShareRepository.deleteShare(shareId, OC_ACCOUNT_NAME) + ocShareRepository.deleteShare(OC_SHARE.remoteId, OC_ACCOUNT_NAME) - verify(exactly = 1) { remoteShareDataSource.deleteShare(shareId, OC_ACCOUNT_NAME) } - verify(exactly = 0) { localShareDataSource.deleteShare(shareId) } + verify(exactly = 1) { + remoteShareDataSource.deleteShare(OC_SHARE.remoteId, OC_ACCOUNT_NAME) + localShareDataSource.deleteShare(OC_SHARE.remoteId) + } } + } diff --git a/owncloudTestUtil/src/main/java/com/owncloud/android/testutil/OCFolderBackUpConfiguration.kt b/owncloudTestUtil/src/main/java/com/owncloud/android/testutil/OCFolderBackUpConfiguration.kt index 8dd9c97e92b..9c6c0f1778d 100644 --- a/owncloudTestUtil/src/main/java/com/owncloud/android/testutil/OCFolderBackUpConfiguration.kt +++ b/owncloudTestUtil/src/main/java/com/owncloud/android/testutil/OCFolderBackUpConfiguration.kt @@ -23,6 +23,7 @@ package com.owncloud.android.testutil import com.owncloud.android.domain.automaticuploads.model.FolderBackUpConfiguration import com.owncloud.android.domain.automaticuploads.model.UploadBehavior import com.owncloud.android.data.folderbackup.db.FolderBackUpEntity +import com.owncloud.android.domain.automaticuploads.model.AutomaticUploadsConfiguration val OC_BACKUP = FolderBackUpConfiguration( accountName = "", @@ -47,3 +48,8 @@ val OC_BACKUP_ENTITY = FolderBackUpEntity( name = "", spaceId = null, ) + +val OC_AUTOMATIC_UPLOADS_CONFIGURATION = AutomaticUploadsConfiguration( + pictureUploadsConfiguration = OC_BACKUP, + videoUploadsConfiguration = OC_BACKUP +) diff --git a/owncloudTestUtil/src/main/java/com/owncloud/android/testutil/OCServerInfo.kt b/owncloudTestUtil/src/main/java/com/owncloud/android/testutil/OCServerInfo.kt index e08dc40ded5..45a85006fb6 100644 --- a/owncloudTestUtil/src/main/java/com/owncloud/android/testutil/OCServerInfo.kt +++ b/owncloudTestUtil/src/main/java/com/owncloud/android/testutil/OCServerInfo.kt @@ -3,8 +3,9 @@ * * @author Abel García de Prada * @author Juan Carlos Garrote Gascón + * @author Jorge Aguado Recio * - * Copyright (C) 2023 ownCloud GmbH. + * Copyright (C) 2024 ownCloud GmbH. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, @@ -22,6 +23,7 @@ package com.owncloud.android.testutil import com.owncloud.android.domain.server.model.ServerInfo +import com.owncloud.android.testutil.oauth.OC_OIDC_SERVER_CONFIGURATION const val OC_SECURE_BASE_URL = "https://demo.owncloud.com" const val OC_INSECURE_BASE_URL = "http://demo.owncloud.com" @@ -46,9 +48,21 @@ val OC_INSECURE_SERVER_INFO_BEARER_AUTH = ServerInfo.OAuth2Server( ownCloudVersion = "10.3.2.1", ) +val OC_SECURE_SERVER_INFO_OIDC_AUTH = ServerInfo.OIDCServer( + baseUrl = OC_SECURE_BASE_URL, + ownCloudVersion = "10.3.2.1", + oidcServerConfiguration = OC_OIDC_SERVER_CONFIGURATION +) + const val OC_WEBFINGER_INSTANCE_URL = "WEBFINGER_INSTANCE" val OC_SECURE_SERVER_INFO_BEARER_AUTH_WEBFINGER_INSTANCE = ServerInfo.OAuth2Server( baseUrl = OC_WEBFINGER_INSTANCE_URL, ownCloudVersion = "10.3.2.1", ) + +val OC_SECURE_SERVER_INFO_OIDC_AUTH_WEBFINGER_INSTANCE = ServerInfo.OIDCServer( + baseUrl = OC_SECURE_BASE_URL, + ownCloudVersion = "10.12", + oidcServerConfiguration = OC_OIDC_SERVER_CONFIGURATION +) diff --git a/owncloudTestUtil/src/main/java/com/owncloud/android/testutil/OCSharee.kt b/owncloudTestUtil/src/main/java/com/owncloud/android/testutil/OCSharee.kt new file mode 100644 index 00000000000..c6d97074d6d --- /dev/null +++ b/owncloudTestUtil/src/main/java/com/owncloud/android/testutil/OCSharee.kt @@ -0,0 +1,32 @@ +/** + * ownCloud Android client application + * + * @author Jorge Aguado Recio + * + * Copyright (C) 2024 ownCloud GmbH. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.owncloud.android.testutil + +import com.owncloud.android.domain.sharing.sharees.model.OCSharee +import com.owncloud.android.domain.sharing.shares.model.ShareType + +val OC_SHAREE = OCSharee( + label = "", + shareType = ShareType.USER, + shareWith = "WhoEver", + additionalInfo = "", + isExactMatch = true +)