Skip to content

Commit

Permalink
Rename usecases and add remaining data layer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abelgardep committed Feb 11, 2020
1 parent fd98291 commit 057c885
Show file tree
Hide file tree
Showing 23 changed files with 278 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -874,8 +874,6 @@ private void checkOcServer() {
} else {
Timber.e("Server check tried with OperationService unbound!");
}
//UseCaseHelper useCaseHelper = new UseCaseHelper();
//Timber.d("Server info: " + useCaseHelper.getServerInfo(uri).toString());

} else {
mServerStatusText = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ protected RemoteOperationResult doInBackground(Object... params) {
if (params != null && params.length == 3) {
String username = (String) params[1];
String password = (String) params[2];
Timber.d("Server info : " + mUseCaseHelper.getServerInfo((String)params[0]));
/// validate credentials accessing the root folder
credentials = OwnCloudCredentialsFactory.newBasicCredentials(
username,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import com.owncloud.android.domain.authentication.usecases.LoginAsyncUseCase
import com.owncloud.android.domain.capabilities.usecases.GetCapabilitiesAsLiveDataUseCase
import com.owncloud.android.domain.capabilities.usecases.GetStoredCapabilitiesUseCase
import com.owncloud.android.domain.capabilities.usecases.RefreshCapabilitiesFromServerAsyncUseCase
import com.owncloud.android.domain.server.usecases.CheckPathExistenceUseCase
import com.owncloud.android.domain.server.usecases.GetServerInfoUseCase
import com.owncloud.android.domain.server.usecases.CheckPathExistenceAsyncUseCase
import com.owncloud.android.domain.server.usecases.GetServerInfoAsyncUseCase
import com.owncloud.android.domain.sharing.sharees.GetShareesAsyncUseCase
import com.owncloud.android.domain.sharing.shares.usecases.CreatePrivateShareAsyncUseCase
import com.owncloud.android.domain.sharing.shares.usecases.CreatePublicShareAsyncUseCase
Expand All @@ -34,7 +34,7 @@ import com.owncloud.android.domain.sharing.shares.usecases.EditPublicShareAsyncU
import com.owncloud.android.domain.sharing.shares.usecases.GetShareAsLiveDataUseCase
import com.owncloud.android.domain.sharing.shares.usecases.GetSharesAsLiveDataUseCase
import com.owncloud.android.domain.sharing.shares.usecases.RefreshSharesFromServerAsyncUseCase
import com.owncloud.android.domain.user.usecases.GetUserInfoUseCase
import com.owncloud.android.domain.user.usecases.GetUserInfoAsyncUseCase
import org.koin.dsl.module

val useCaseModule = module {
Expand All @@ -58,9 +58,9 @@ val useCaseModule = module {
factory { DeleteShareAsyncUseCase(get()) }

// User
factory { GetUserInfoUseCase(get()) }
factory { GetUserInfoAsyncUseCase(get()) }

// Server
factory { CheckPathExistenceUseCase(get()) }
factory { GetServerInfoUseCase(get()) }
factory { CheckPathExistenceAsyncUseCase(get()) }
factory { GetServerInfoAsyncUseCase(get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* a value of {@link AuthenticationMethod}.
*/
@Deprecated
// TODO: Remove this operation. Get AuthenticationMethods from GetServerInfoUseCase
// TODO: Remove this operation. Get AuthenticationMethods from GetServerInfoAsyncUseCase
public class DetectAuthenticationMethodOperation extends RemoteOperation<AuthenticationMethod> {

/**
Expand All @@ -68,15 +68,18 @@ protected RemoteOperationResult<AuthenticationMethod> run(OwnCloudClient client)
// Step 1: check whether the root folder exists, following redirections
RemoteOperationResult resultFromExistenceCheck = operation.execute(client);
String redirectedLocation = resultFromExistenceCheck.getRedirectedLocation();
Timber.d("Redirected location:" + redirectedLocation);
while (redirectedLocation != null && redirectedLocation.length() > 0) {
client.setBaseUri(Uri.parse(resultFromExistenceCheck.getRedirectedLocation()));
resultFromExistenceCheck = operation.execute(client);
redirectedLocation = resultFromExistenceCheck.getRedirectedLocation();
Timber.d("Redirected location:" + redirectedLocation);
}

// Step 2: look for authentication methods
if (resultFromExistenceCheck.getHttpCode() == HttpConstants.HTTP_UNAUTHORIZED) {
String authenticateHeaders = resultFromExistenceCheck.getAuthenticateHeaders();
Timber.d("Authentication Header:" + authenticateHeaders);
if (authenticateHeaders.contains("basic")) {
authenticationMethod = AuthenticationMethod.BASIC_HTTP_AUTH;
} else if (authenticateHeaders.contains("bearer")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* Checks the existence of a configured ownCloud server in the URL, gets its version
* and finds out what authentication method is needed to access files in it.
*
* TODO: Remove this operation. Call {@link com.owncloud.android.domain.server.usecases.GetServerInfoUseCase} instead.
* TODO: Remove this operation. Call {@link com.owncloud.android.domain.server.usecases.GetServerInfoAsyncUseCase} instead.
*/
@Deprecated
public class GetServerInfoOperation extends RemoteOperation<GetServerInfoOperation.ServerInfo> {
Expand Down Expand Up @@ -75,6 +75,7 @@ protected RemoteOperationResult<ServerInfo> run(OwnCloudClient client) {
if (remoteStatusResult.isSuccess()) {
// second: get authentication method required by the server
mResultData.mVersion = remoteStatusResult.getData();
Timber.d("Result code : " + remoteStatusResult.getCode());
mResultData.mIsSslConn = (remoteStatusResult.getCode() == ResultCode.OK_SSL);
mResultData.mBaseUrl = normalizeProtocolPrefix(mUrl, mResultData.mIsSslConn);
final RemoteOperationResult<AuthenticationMethod> detectAuthResult = detectAuthorizationMethod(client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ package com.owncloud.android.operations.common
import com.owncloud.android.domain.UseCaseResult
import com.owncloud.android.domain.authentication.usecases.LoginAsyncUseCase
import com.owncloud.android.domain.server.model.ServerInfo
import com.owncloud.android.domain.server.usecases.CheckPathExistenceUseCase
import com.owncloud.android.domain.server.usecases.GetServerInfoUseCase
import com.owncloud.android.domain.server.usecases.CheckPathExistenceAsyncUseCase
import com.owncloud.android.domain.server.usecases.GetServerInfoAsyncUseCase
import com.owncloud.android.domain.user.model.UserInfo
import com.owncloud.android.domain.user.usecases.GetUserInfoUseCase
import com.owncloud.android.domain.user.usecases.GetUserInfoAsyncUseCase
import org.koin.core.KoinComponent
import org.koin.core.inject

Expand All @@ -33,18 +33,18 @@ import org.koin.core.inject
* TODO: Remove this and call directly to usecases from ViewModel.
*/
class UseCaseHelper : KoinComponent {
private val getUserInfoUseCase: GetUserInfoUseCase by inject()
private val checkPathExistenceUseCase: CheckPathExistenceUseCase by inject()
private val getServerInfoUseCase: GetServerInfoUseCase by inject()
private val mGetUserInfoAsyncUseCase: GetUserInfoAsyncUseCase by inject()
private val mCheckPathExistenceAsyncUseCase: CheckPathExistenceAsyncUseCase by inject()
private val mGetServerInfoAsyncUseCase: GetServerInfoAsyncUseCase by inject()
private val loginAsyncUseCase: LoginAsyncUseCase by inject()

fun getUserInfo(): UseCaseResult<UserInfo> = getUserInfoUseCase.execute(Unit)
fun getUserInfo(): UseCaseResult<UserInfo> = mGetUserInfoAsyncUseCase.execute(Unit)

fun checkPathExistence(remotePath: String): UseCaseResult<Any> =
checkPathExistenceUseCase.execute(CheckPathExistenceUseCase.Params(remotePath, false))
mCheckPathExistenceAsyncUseCase.execute(CheckPathExistenceAsyncUseCase.Params(remotePath, false))

fun getServerInfo(serverUrl: String): UseCaseResult<ServerInfo> =
getServerInfoUseCase.execute(GetServerInfoUseCase.Params(serverPath = serverUrl))
mGetServerInfoAsyncUseCase.execute(GetServerInfoAsyncUseCase.Params(serverPath = serverUrl))

fun login(serverUrl: String, username: String, password: String) =
loginAsyncUseCase.execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ package com.owncloud.android.presentation.viewmodels.authentication
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.owncloud.android.domain.authentication.usecases.LoginAsyncUseCase
import com.owncloud.android.domain.user.usecases.GetUserInfoUseCase
import com.owncloud.android.domain.user.usecases.GetUserInfoAsyncUseCase
import com.owncloud.android.providers.CoroutinesDispatcherProvider
import kotlinx.coroutines.launch
import timber.log.Timber

class OCAuthenticationViewModel(
private val loginAsyncUseCase: LoginAsyncUseCase,
private val getUserInfoUseCase: GetUserInfoUseCase,
private val getUserInfoAsyncUseCase: GetUserInfoAsyncUseCase,
private val coroutinesDispatcherProvider: CoroutinesDispatcherProvider
) : ViewModel() {
fun login(
Expand All @@ -52,7 +52,7 @@ class OCAuthenticationViewModel(

fun getUserInfo() {
viewModelScope.launch(coroutinesDispatcherProvider.io) {
val useCaseResult = getUserInfoUseCase.execute(Unit)
val useCaseResult = getUserInfoAsyncUseCase.execute(Unit)
Timber.d(useCaseResult.toString())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@

package com.owncloud.android.data.server.datasources.implementation

import android.net.Uri
import com.owncloud.android.data.executeRemoteOperation
import com.owncloud.android.data.server.datasources.RemoteAnonymousDatasource
import com.owncloud.android.data.server.network.OCAnonymousServerService
import com.owncloud.android.domain.server.model.AuthenticationMethod
import com.owncloud.android.lib.common.http.HttpConstants
import com.owncloud.android.lib.common.operations.RemoteOperationResult
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
/**
* ownCloud Android client application
*
* @author Abel García de Prada
* Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
*/

package com.owncloud.android.data.server.datasources

import com.owncloud.android.data.server.datasources.implementation.OCRemoteAnonymousDataSource
import com.owncloud.android.data.server.network.OCAnonymousServerService
import com.owncloud.android.domain.server.model.AuthenticationMethod
import com.owncloud.android.lib.common.http.HttpConstants.HTTP_UNAUTHORIZED
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK_NO_SSL
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK_SSL
import com.owncloud.android.lib.resources.status.OwnCloudVersion
import com.owncloud.android.testutil.OC_ServerInfo
import com.owncloud.android.utils.createRemoteOperationResultMock
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Test

Expand All @@ -11,10 +42,156 @@ class OCRemoteAnonymousDatasourceTest {

private val ocAnonymousService: OCAnonymousServerService = mockk()

private val OC_OWNCLOUD_VERSION = OwnCloudVersion("10.3.2")
private val basicAuthHeader = "basic realm=\"owncloud\", charset=\"utf-8\""
private val bearerHeader = "bearer realm=\"owncloud\""
private val redirectedLocation = "http://demo.owncloud.demo.com"

@Before
fun init() {
ocRemoteAnonymousDatasource = OCRemoteAnonymousDataSource(ocAnonymousService)
}

//TODO: Test getAuthenticationMethod and getRemoteStatus
@Test
fun getAuthenticationMethodFollowRedirections() {
val checkPathExistenceResultFollowRedirectionMocked: RemoteOperationResult<Boolean> =
createRemoteOperationResultMock(
data = true,
isSuccess = true,
redirectedLocation = OC_ServerInfo.baseUrl
)
val checkPathExistenceResultMocked: RemoteOperationResult<Boolean> =
createRemoteOperationResultMock(
data = true,
isSuccess = true,
resultCode = OK_SSL,
authenticationHeader = basicAuthHeader,
httpCode = HTTP_UNAUTHORIZED
)

every {
ocAnonymousService.checkPathExistence(redirectedLocation, false)
} returns checkPathExistenceResultFollowRedirectionMocked

every {
ocAnonymousService.checkPathExistence(OC_ServerInfo.baseUrl, false)
} returns checkPathExistenceResultMocked

val authenticationMethod = ocRemoteAnonymousDatasource.getAuthenticationMethod(redirectedLocation)

assertNotNull(authenticationMethod)
assertEquals(AuthenticationMethod.BASIC_HTTP_AUTH, authenticationMethod)

verify { ocAnonymousService.checkPathExistence(OC_ServerInfo.baseUrl, false) }
}

@Test
fun getAuthenticationMethodBasic() {
val checkPathExistenceResultMocked: RemoteOperationResult<Boolean> =
createRemoteOperationResultMock(
data = true,
isSuccess = true,
resultCode = OK_SSL,
authenticationHeader = basicAuthHeader,
httpCode = HTTP_UNAUTHORIZED,
redirectedLocation = null
)

every {
ocAnonymousService.checkPathExistence(OC_ServerInfo.baseUrl, false)
} returns checkPathExistenceResultMocked

val authenticationMethod = ocRemoteAnonymousDatasource.getAuthenticationMethod(OC_ServerInfo.baseUrl)

assertNotNull(authenticationMethod)
assertEquals(AuthenticationMethod.BASIC_HTTP_AUTH, authenticationMethod)

verify { ocAnonymousService.checkPathExistence(OC_ServerInfo.baseUrl, false) }
}

@Test
fun getAuthenticationMethodBearer() {
val checkPathExistenceResultMocked: RemoteOperationResult<Boolean> =
createRemoteOperationResultMock(
data = true,
isSuccess = true,
resultCode = OK_SSL,
authenticationHeader = bearerHeader,
httpCode = HTTP_UNAUTHORIZED,
redirectedLocation = null
)

every {
ocAnonymousService.checkPathExistence(OC_ServerInfo.baseUrl, false)
} returns checkPathExistenceResultMocked

val authenticationMethod = ocRemoteAnonymousDatasource.getAuthenticationMethod(OC_ServerInfo.baseUrl)

assertNotNull(authenticationMethod)
assertEquals(AuthenticationMethod.BEARER_TOKEN, authenticationMethod)

verify { ocAnonymousService.checkPathExistence(OC_ServerInfo.baseUrl, false) }
}

@Test(expected = Exception::class)
fun getAuthenticationMethodException() {
every {
ocAnonymousService.checkPathExistence(OC_ServerInfo.baseUrl, false)
} throws Exception()

val authenticationMethod = ocRemoteAnonymousDatasource.getAuthenticationMethod(OC_ServerInfo.baseUrl)

assertNotNull(authenticationMethod)
assertEquals(AuthenticationMethod.BASIC_HTTP_AUTH, authenticationMethod)

verify { ocAnonymousService.checkPathExistence(OC_ServerInfo.baseUrl, false) }
}

@Test
fun getRemoteStatusIsSecureConnection() {
val remoteStatusResultMocked: RemoteOperationResult<OwnCloudVersion> =
createRemoteOperationResultMock(data = OC_OWNCLOUD_VERSION, isSuccess = true, resultCode = OK_SSL)

every {
ocAnonymousService.getRemoteStatus(OC_ServerInfo.baseUrl)
} returns remoteStatusResultMocked

val remoteStatus = ocRemoteAnonymousDatasource.getRemoteStatus(OC_ServerInfo.baseUrl)

assertNotNull(remoteStatus)
assertEquals(Pair(OC_OWNCLOUD_VERSION, true), remoteStatus)

verify { ocAnonymousService.getRemoteStatus(OC_ServerInfo.baseUrl) }
}

@Test
fun getRemoteStatusIsNotSecureConnection() {
val remoteStatusResultMocked: RemoteOperationResult<OwnCloudVersion> =
createRemoteOperationResultMock(data = OC_OWNCLOUD_VERSION, isSuccess = true, resultCode = OK_NO_SSL)

every {
ocAnonymousService.getRemoteStatus(OC_ServerInfo.baseUrl)
} returns remoteStatusResultMocked

val remoteStatus = ocRemoteAnonymousDatasource.getRemoteStatus(OC_ServerInfo.baseUrl)

assertNotNull(remoteStatus)
assertEquals(Pair(OC_OWNCLOUD_VERSION, false), remoteStatus)

verify { ocAnonymousService.getRemoteStatus(OC_ServerInfo.baseUrl) }
}

@Test(expected = Exception::class)
fun getRemoteStatusException() {
every {
ocAnonymousService.getRemoteStatus(OC_ServerInfo.baseUrl)
} throws Exception()

val remoteStatus = ocRemoteAnonymousDatasource.getRemoteStatus(OC_ServerInfo.baseUrl)

assertNotNull(remoteStatus)
assertEquals(Pair(OC_OWNCLOUD_VERSION, true), remoteStatus)

verify { ocAnonymousService.getRemoteStatus(OC_ServerInfo.baseUrl) }
}
}
Loading

0 comments on commit 057c885

Please sign in to comment.