Skip to content

Commit

Permalink
Add data layer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abelgardep committed Feb 11, 2020
1 parent dd6f61b commit fd98291
Show file tree
Hide file tree
Showing 24 changed files with 434 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.server.CheckPathExistenceRemoteOperation;
import com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation;
import com.owncloud.android.operations.common.UseCaseHelper;
import timber.log.Timber;

import java.lang.ref.WeakReference;

Expand All @@ -43,11 +45,13 @@
public class AuthenticatorAsyncTask extends AsyncTask<Object, Void, RemoteOperationResult> {

private Context mContext;
private UseCaseHelper mUseCaseHelper;
private final WeakReference<OnAuthenticatorTaskListener> mListener;

AuthenticatorAsyncTask(Activity activity) {
mContext = activity.getApplicationContext();
mListener = new WeakReference<>((OnAuthenticatorTaskListener) activity);
mUseCaseHelper = new UseCaseHelper();
}

@Override
Expand All @@ -57,7 +61,7 @@ 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 @@ -61,7 +61,7 @@ val remoteDataSourceModule = module {
single<ShareeService> { OCShareeService(get()) }
single<UserService> { OCUserService(get()) }
single<ServerService> { OCServerService(get()) }
single<AnonymousService>{ OCAnonymousServerService(get())}
single<AnonymousService>{ OCAnonymousServerService()}

factory<RemoteCapabilitiesDataSource> {
OCRemoteCapabilitiesDataSource(
Expand All @@ -87,6 +87,6 @@ val remoteDataSourceModule = module {
)
}
factory<RemoteServerDataSource> { OCRemoteServerDataSource(get()) }
factory<RemoteAnonymousDatasource> { OCRemoteAnonymousDataSource() }
factory<RemoteAnonymousDatasource> { OCRemoteAnonymousDataSource(get()) }
factory<RemoteAuthenticationDataSource> { OCRemoteAuthenticationDataSource(androidContext()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ protected RemoteOperationResult<AuthenticationMethod> run(OwnCloudClient client)
// Step 2: look for authentication methods
if (resultFromExistenceCheck.getHttpCode() == HttpConstants.HTTP_UNAUTHORIZED) {
String authenticateHeaders = resultFromExistenceCheck.getAuthenticateHeaders();
Timber.d("MEthods " + 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 @@ -24,6 +24,8 @@ import com.owncloud.android.domain.authentication.AuthenticationRepository
class OCAuthenticationRepository(
private val remoteAuthenticationDataSource: RemoteAuthenticationDataSource
) : AuthenticationRepository {
override fun login(serverUrl: String, username: String, password: String) =
override fun login(serverUrl: String, username: String, password: String) {
remoteAuthenticationDataSource.login(serverPath = serverUrl, username = username, password = password)
//TODO: Add account creation stuff
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.owncloud.android.domain.server.model.AuthenticationMethod
import com.owncloud.android.lib.resources.status.OwnCloudVersion

interface RemoteAnonymousDatasource {
fun checkPathExistence(path: String, checkUserCredentials: Boolean): Boolean

fun getAuthenticationMethod(path: String): AuthenticationMethod

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,27 @@

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
import com.owncloud.android.lib.resources.server.AnonymousService
import com.owncloud.android.lib.resources.status.OwnCloudVersion

class OCRemoteAnonymousDataSource : RemoteAnonymousDatasource {
private lateinit var serverService: OCAnonymousServerService

override fun checkPathExistence(path: String, checkUserCredentials: Boolean): Boolean {
serverService = OCAnonymousServerService(path)
executeRemoteOperation {
serverService.checkPathExistence(path = path, isUserLogged = checkUserCredentials)
}.let { return it == Any() }
}
class OCRemoteAnonymousDataSource(
private val anonymousService: AnonymousService
) : RemoteAnonymousDatasource {

/* Basically, tries to access to the root folder without authorization and analyzes the response.*/
override fun getAuthenticationMethod(path: String): AuthenticationMethod {
// Step 1: check whether the root folder exists, following redirections
serverService = OCAnonymousServerService(path)
var checkPathExistenceResult = serverService.checkPathExistence("", isUserLogged = false)
var checkPathExistenceResult = anonymousService.checkPathExistence(path, isUserLogged = false)
var redirectionLocation = checkPathExistenceResult.redirectedLocation
while (!redirectionLocation.isNullOrEmpty()) {
serverService = OCAnonymousServerService(redirectionLocation)
//serverService.client.baseUri = Uri.parse(redirectionLocation)
checkPathExistenceResult = serverService.checkPathExistence("", isUserLogged = false)
checkPathExistenceResult = anonymousService.checkPathExistence(redirectionLocation, isUserLogged = false)
redirectionLocation = checkPathExistenceResult.redirectedLocation
}

Expand All @@ -64,8 +57,7 @@ class OCRemoteAnonymousDataSource : RemoteAnonymousDatasource {
}

override fun getRemoteStatus(path: String): Pair<OwnCloudVersion, Boolean> {
serverService = OCAnonymousServerService(path)
val remoteStatusResult = serverService.getRemoteStatus(path)
val remoteStatusResult = anonymousService.getRemoteStatus(path)

val ownCloudVersion = executeRemoteOperation {
remoteStatusResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ import com.owncloud.android.lib.resources.server.CheckPathExistenceRemoteOperati
import com.owncloud.android.lib.resources.server.GetStatusRemoteOperation
import com.owncloud.android.lib.resources.status.OwnCloudVersion

class OCAnonymousServerService(baseUrl: String) : AnonymousService {
var client: OwnCloudClient = OwnCloudClient(Uri.parse(baseUrl))

class OCAnonymousServerService() : AnonymousService {
override fun checkPathExistence(path: String, isUserLogged: Boolean): RemoteOperationResult<Boolean> =
CheckPathExistenceRemoteOperation(
remotePath = path,
isUserLogged = true
).execute(client)
).execute(createClientFromPath(path))

override fun getRemoteStatus(path: String): RemoteOperationResult<OwnCloudVersion> =
GetStatusRemoteOperation().execute(client)
GetStatusRemoteOperation().execute(createClientFromPath(path))

private fun createClientFromPath(path: String): OwnCloudClient {
return OwnCloudClient(Uri.parse(path))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import java.util.Locale
class OCAnonymousServerRepository(
private val remoteAnonymousDatasource: RemoteAnonymousDatasource
) : AnonymousServerRepository {
override fun checkPathExistence(path: String, userLogged: Boolean): Boolean =
remoteAnonymousDatasource.checkPathExistence(path, userLogged)

override fun getServerInfo(path: String): ServerInfo {
// First step: check the status of the server (including its version)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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.authentication.repository

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.owncloud.android.data.authentication.datasources.RemoteAuthenticationDataSource
import com.owncloud.android.domain.exceptions.NoConnectionWithServerException
import com.owncloud.android.testutil.OC_ServerInfo
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.Rule
import org.junit.Test

class OCAuthenticationRepositoryTest {
@Rule
@JvmField
val instantExecutorRule = InstantTaskExecutorRule()

private val remoteAuthenticationDataSource = mockk<RemoteAuthenticationDataSource>(relaxed = true)
private val ocAuthenticationRepository: OCAuthenticationRepository =
OCAuthenticationRepository(remoteAuthenticationDataSource)

@Test
fun loginOk() {
every { remoteAuthenticationDataSource.login(any(), any(), any()) } returns Unit

ocAuthenticationRepository.login(OC_ServerInfo.baseUrl, "username", "password")

verify(exactly = 1) {
remoteAuthenticationDataSource.login(OC_ServerInfo.baseUrl, "username", "password")
}
}

@Test(expected = NoConnectionWithServerException::class)
fun loginException() {
every {
remoteAuthenticationDataSource.login(any(), any(), any())
} throws NoConnectionWithServerException()

ocAuthenticationRepository.login(OC_ServerInfo.baseUrl, "test", "test")

verify(exactly = 1) {
remoteAuthenticationDataSource.login(any(), any(), any())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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 io.mockk.mockk
import org.junit.Before
import org.junit.Test

class OCRemoteAnonymousDatasourceTest {
private lateinit var ocRemoteAnonymousDatasource: OCRemoteAnonymousDataSource

private val ocAnonymousService: OCAnonymousServerService = mockk()

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

//TODO: Test getAuthenticationMethod and getRemoteStatus
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* 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.OCRemoteServerDataSource
import com.owncloud.android.data.server.network.OCServerService
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.testutil.OC_ServerInfo
import com.owncloud.android.utils.createRemoteOperationResultMock
import io.mockk.every
import io.mockk.mockk
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Test

class OCRemoteServerDataSourceTest {
private lateinit var ocRemoteServerDataSource: OCRemoteServerDataSource

private val ocServerService: OCServerService = mockk()

@Before
fun init() {
ocRemoteServerDataSource = OCRemoteServerDataSource(ocServerService)
}

@Test
fun checkPathExistenceTrue() {
val checkPathExistenceRemoteResult: RemoteOperationResult<Boolean> = createRemoteOperationResultMock(data = true, isSuccess = true)

every {
ocServerService.checkPathExistence(OC_ServerInfo.baseUrl, true)
} returns checkPathExistenceRemoteResult

val checkPathExistence = ocRemoteServerDataSource.checkPathExistence(OC_ServerInfo.baseUrl, true)

assertNotNull(checkPathExistence)
assertEquals(checkPathExistenceRemoteResult.data, checkPathExistence)
}

@Test
fun checkPathExistenceFalse() {
val checkPathExistenceRemoteResult: RemoteOperationResult<Boolean> = createRemoteOperationResultMock(data = false, isSuccess = true)

every {
ocServerService.checkPathExistence(OC_ServerInfo.baseUrl, true)
} returns checkPathExistenceRemoteResult

val checkPathExistence = ocRemoteServerDataSource.checkPathExistence(OC_ServerInfo.baseUrl, true)

assertNotNull(checkPathExistence)
assertEquals(checkPathExistenceRemoteResult.data, checkPathExistence)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* 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.repository

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.owncloud.android.data.server.datasources.RemoteServerDataSource
import com.owncloud.android.domain.exceptions.NoConnectionWithServerException
import com.owncloud.android.testutil.OC_ServerInfo
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.Rule
import org.junit.Test

class OCServerRepositoryTest {
@Rule
@JvmField
val instantExecutorRule = InstantTaskExecutorRule()

private val remoteServerDataSource = mockk<RemoteServerDataSource>(relaxed = true)
private val ocServerRepository: OCServerRepository = OCServerRepository(remoteServerDataSource)

@Test
fun checkPathExistenceExists() {
every { remoteServerDataSource.checkPathExistence(OC_ServerInfo.baseUrl, false) } returns true

ocServerRepository.checkPathExistence(OC_ServerInfo.baseUrl, false)

verify(exactly = 1) {
remoteServerDataSource.checkPathExistence(OC_ServerInfo.baseUrl, false)
}
}

@Test(expected = NoConnectionWithServerException::class)
fun checkPathExistenceExistsNoConnection() {
every { remoteServerDataSource.checkPathExistence(OC_ServerInfo.baseUrl, false) } throws NoConnectionWithServerException()

ocServerRepository.checkPathExistence(OC_ServerInfo.baseUrl, false)

verify(exactly = 1) {
remoteServerDataSource.checkPathExistence(OC_ServerInfo.baseUrl, false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test

class OCLocalDataSourceTest {
class OCLocalShareDataSourceTest {
private lateinit var ocLocalSharesDataSource: OCLocalShareDataSource
private val ocSharesDao = mockkClass(OCShareDao::class)
private val ocShareMapper = OCShareMapper()
Expand Down
Loading

0 comments on commit fd98291

Please sign in to comment.