From 40dee006dd2ca130496f3a13470c02d5a47facf3 Mon Sep 17 00:00:00 2001 From: ericdecanini Date: Thu, 3 Mar 2022 17:51:50 +0100 Subject: [PATCH] Refactors SessionCreator with added tests --- matrix-sdk-android/build.gradle | 2 +- .../android/sdk/internal/auth/AuthModule.kt | 3 + .../sdk/internal/auth/SessionCreator.kt | 56 ++++--------- .../sdk/internal/auth/SessionParamsCreator.kt | 83 +++++++++++++++++++ .../auth/DefaultSessionCreatorTest.kt | 61 ++++++++++++++ .../auth/DefaultSessionParamsCreatorTest.kt | 62 ++++++++++++++ .../DefaultSessionParamsCreatorTestBase.kt | 72 ++++++++++++++++ .../auth/db/SessionParamsMapperTest.kt | 10 +-- .../auth/db/migration/MigrateAuthTo005Test.kt | 2 +- .../android/sdk/test/fakes/api/FakeSession.kt | 25 ++++++ .../test/fakes/internal/FakeSessionManager.kt | 45 ++++++++++ .../auth/FakeIsValidClientServerApiTask.kt | 41 +++++++++ .../internal/auth/FakePendingSessionStore.kt | 35 ++++++++ .../internal/auth/FakeSessionParamsCreator.kt | 47 +++++++++++ .../internal/auth/FakeSessionParamsStore.kt | 36 ++++++++ .../db/migration/Fake005MigrationRealm.kt | 2 +- .../FakeCredentialsJsonAdapter.kt | 6 +- ...keHomeServerConnectionConfigJsonAdapter.kt | 6 +- .../FakeSessionParamsMapperMoshi.kt | 10 +-- .../fixtures/DiscoveryInformationFixture.kt | 30 +++++++ .../fixtures/WellKnownBaseConfigFixture.kt | 27 ++++++ 21 files changed, 600 insertions(+), 61 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/SessionParamsCreator.kt create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/DefaultSessionCreatorTest.kt create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/DefaultSessionParamsCreatorTest.kt create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/DefaultSessionParamsCreatorTestBase.kt create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/api/FakeSession.kt create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/FakeSessionManager.kt create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakeIsValidClientServerApiTask.kt create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakePendingSessionStore.kt create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakeSessionParamsCreator.kt create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakeSessionParamsStore.kt rename matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/{ => internal}/auth/db/migration/Fake005MigrationRealm.kt (97%) rename matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/{ => internal}/auth/db/sessionparams/FakeCredentialsJsonAdapter.kt (81%) rename matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/{ => internal}/auth/db/sessionparams/FakeHomeServerConnectionConfigJsonAdapter.kt (82%) rename matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/{ => internal}/auth/db/sessionparams/FakeSessionParamsMapperMoshi.kt (82%) create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/DiscoveryInformationFixture.kt create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/WellKnownBaseConfigFixture.kt diff --git a/matrix-sdk-android/build.gradle b/matrix-sdk-android/build.gradle index 3e301eebb9..32a8b23f30 100644 --- a/matrix-sdk-android/build.gradle +++ b/matrix-sdk-android/build.gradle @@ -175,7 +175,7 @@ dependencies { // Note: version sticks to 1.9.2 due to https://github.com/mockk/mockk/issues/281 testImplementation libs.mockk.mockk testImplementation libs.tests.kluent - implementation libs.jetbrains.coroutinesAndroid + testImplementation libs.jetbrains.coroutinesTest // Plant Timber tree for test testImplementation 'net.lachlanmckee:timber-junit-rule:1.0.1' // Transitively required for mocking realm as monarchy doesn't expose Rx diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthModule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthModule.kt index 298e116199..b5c072370e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthModule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthModule.kt @@ -81,6 +81,9 @@ internal abstract class AuthModule { @Binds abstract fun bindSessionCreator(creator: DefaultSessionCreator): SessionCreator + @Binds + abstract fun bindSessionParamsCreator(creator: DefaultSessionParamsCreator): SessionParamsCreator + @Binds abstract fun bindDirectLoginTask(task: DefaultDirectLoginTask): DirectLoginTask diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/SessionCreator.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/SessionCreator.kt index cc00c963ea..5e6d19d21a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/SessionCreator.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/SessionCreator.kt @@ -16,69 +16,41 @@ package org.matrix.android.sdk.internal.auth -import android.net.Uri import org.matrix.android.sdk.api.auth.data.Credentials import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig -import org.matrix.android.sdk.api.auth.data.SessionParams -import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.internal.SessionManager -import timber.log.Timber +import org.matrix.android.sdk.internal.auth.login.LoginType import javax.inject.Inject internal interface SessionCreator { - suspend fun createSession(credentials: Credentials, homeServerConnectionConfig: HomeServerConnectionConfig): Session + + suspend fun createSession( + credentials: Credentials, + homeServerConnectionConfig: HomeServerConnectionConfig, + loginType: LoginType, + ): Session } internal class DefaultSessionCreator @Inject constructor( private val sessionParamsStore: SessionParamsStore, private val sessionManager: SessionManager, private val pendingSessionStore: PendingSessionStore, - private val isValidClientServerApiTask: IsValidClientServerApiTask + private val sessionParamsCreator: SessionParamsCreator, ) : SessionCreator { /** * Credentials can affect the homeServerConnectionConfig, override homeserver url and/or * identity server url if provided in the credentials */ - override suspend fun createSession(credentials: Credentials, homeServerConnectionConfig: HomeServerConnectionConfig): Session { + override suspend fun createSession( + credentials: Credentials, + homeServerConnectionConfig: HomeServerConnectionConfig, + loginType: LoginType, + ): Session { // We can cleanup the pending session params pendingSessionStore.delete() - - val overriddenUrl = credentials.discoveryInformation?.homeServer?.baseURL - // remove trailing "/" - ?.trim { it == '/' } - ?.takeIf { it.isNotBlank() } - // It can be the same value, so in this case, do not check again the validity - ?.takeIf { it != homeServerConnectionConfig.homeServerUriBase.toString() } - ?.also { Timber.d("Overriding homeserver url to $it (will check if valid)") } - ?.let { Uri.parse(it) } - ?.takeIf { - // Validate the URL, if the configuration is wrong server side, do not override - tryOrNull { - isValidClientServerApiTask.execute( - IsValidClientServerApiTask.Params( - homeServerConnectionConfig.copy(homeServerUriBase = it) - ) - ) - .also { Timber.d("Overriding homeserver url: $it") } - } ?: true // In case of other error (no network, etc.), consider it is valid... - } - - val sessionParams = SessionParams( - credentials = credentials, - homeServerConnectionConfig = homeServerConnectionConfig.copy( - homeServerUriBase = overriddenUrl ?: homeServerConnectionConfig.homeServerUriBase, - identityServerUri = credentials.discoveryInformation?.identityServer?.baseURL - // remove trailing "/" - ?.trim { it == '/' } - ?.takeIf { it.isNotBlank() } - ?.also { Timber.d("Overriding identity server url to $it") } - ?.let { Uri.parse(it) } - ?: homeServerConnectionConfig.identityServerUri - ), - isTokenValid = true) - + val sessionParams = sessionParamsCreator.create(credentials, homeServerConnectionConfig, loginType) sessionParamsStore.save(sessionParams) return sessionManager.getOrCreateSession(sessionParams) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/SessionParamsCreator.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/SessionParamsCreator.kt new file mode 100644 index 0000000000..318389486e --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/SessionParamsCreator.kt @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.internal.auth + +import android.net.Uri +import org.matrix.android.sdk.api.auth.data.Credentials +import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig +import org.matrix.android.sdk.api.auth.data.SessionParams +import org.matrix.android.sdk.api.extensions.tryOrNull +import org.matrix.android.sdk.internal.auth.login.LoginType +import timber.log.Timber +import javax.inject.Inject + +internal interface SessionParamsCreator { + + suspend fun create( + credentials: Credentials, + homeServerConnectionConfig: HomeServerConnectionConfig, + loginType: LoginType, + ): SessionParams +} + +internal class DefaultSessionParamsCreator @Inject constructor( + private val isValidClientServerApiTask: IsValidClientServerApiTask +) : SessionParamsCreator { + + override suspend fun create( + credentials: Credentials, + homeServerConnectionConfig: HomeServerConnectionConfig, + loginType: LoginType, + ) = SessionParams( + credentials = credentials, + homeServerConnectionConfig = homeServerConnectionConfig.overrideWithCredentials(credentials), + isTokenValid = true, + loginType = loginType, + ) + + private suspend fun HomeServerConnectionConfig.overrideWithCredentials(credentials: Credentials) = copy( + homeServerUriBase = credentials.getHomeServerUri(this) ?: homeServerUriBase, + identityServerUri = credentials.getIdentityServerUri() ?: identityServerUri + ) + + private suspend fun Credentials.getHomeServerUri(homeServerConnectionConfig: HomeServerConnectionConfig) = + discoveryInformation?.homeServer?.baseURL + ?.trim { it == '/' } + ?.takeIf { it.isNotBlank() } + // It can be the same value, so in this case, do not check again the validity + ?.takeIf { it != homeServerConnectionConfig.homeServerUriBase.toString() } + ?.also { Timber.d("Overriding homeserver url to $it (will check if valid)") } + ?.let { Uri.parse(it) } + ?.takeIf { validateUri(it, homeServerConnectionConfig) } + + private suspend fun validateUri(uri: Uri, homeServerConnectionConfig: HomeServerConnectionConfig) = + // Validate the URL, if the configuration is wrong server side, do not override + tryOrNull { + performClientServerApiValidation(uri, homeServerConnectionConfig) + } ?: true // In case of other error (no network, etc.), consider it is valid... + + private suspend fun performClientServerApiValidation(uri: Uri, homeServerConnectionConfig: HomeServerConnectionConfig) = + isValidClientServerApiTask.execute( + IsValidClientServerApiTask.Params(homeServerConnectionConfig.copy(homeServerUriBase = uri)) + ).also { Timber.d("Overriding homeserver url: $it") } + + private fun Credentials.getIdentityServerUri() = discoveryInformation?.identityServer?.baseURL + ?.trim { it == '/' } + ?.takeIf { it.isNotBlank() } + ?.also { Timber.d("Overriding identity server url to $it") } + ?.let { Uri.parse(it) } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/DefaultSessionCreatorTest.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/DefaultSessionCreatorTest.kt new file mode 100644 index 0000000000..c9eb2bc2e0 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/DefaultSessionCreatorTest.kt @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.internal.auth + +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runBlockingTest +import org.junit.Test +import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig +import org.matrix.android.sdk.internal.auth.login.LoginType +import org.matrix.android.sdk.test.fakes.internal.FakeSessionManager +import org.matrix.android.sdk.test.fakes.internal.auth.FakePendingSessionStore +import org.matrix.android.sdk.test.fakes.internal.auth.FakeSessionParamsCreator +import org.matrix.android.sdk.test.fakes.internal.auth.FakeSessionParamsStore +import org.matrix.android.sdk.test.fixtures.CredentialsFixture.aCredentials +import org.matrix.android.sdk.test.fixtures.SessionParamsFixture.aSessionParams + +@ExperimentalCoroutinesApi +class DefaultSessionCreatorTest { + + private val fakeSessionParamsStore = FakeSessionParamsStore() + private val fakeSessionManager = FakeSessionManager() + private val fakePendingSessionStore = FakePendingSessionStore() + private val fakeSessionParamsCreator = FakeSessionParamsCreator() + + private val sessionCreator = DefaultSessionCreator( + fakeSessionParamsStore.instance, + fakeSessionManager.instance, + fakePendingSessionStore.instance, + fakeSessionParamsCreator.instance, + ) + + @Test + fun `when createSession, then session created`() = runBlockingTest { + val output = sessionCreator.createSession(credentials, homeServerConnectionConfig, LoginType.UNKNOWN) + + fakePendingSessionStore.verifyPendingSessionDataCleared() + fakeSessionParamsCreator.verifyCreatedWithParameters(credentials, homeServerConnectionConfig, LoginType.UNKNOWN) + fakeSessionParamsStore.verifyParamsSaved(sessionParams) + fakeSessionManager.assertSessionCreatedWithParams(output, sessionParams) + } + + companion object { + private val sessionParams = aSessionParams() + private val credentials = aCredentials() + private val homeServerConnectionConfig = HomeServerConnectionConfig.Builder().build() + } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/DefaultSessionParamsCreatorTest.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/DefaultSessionParamsCreatorTest.kt new file mode 100644 index 0000000000..85723d6e08 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/DefaultSessionParamsCreatorTest.kt @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.internal.auth + +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runBlockingTest +import org.junit.Test +import org.matrix.android.sdk.internal.auth.login.LoginType +import org.matrix.android.sdk.test.fakes.internal.auth.FakeIsValidClientServerApiTask + +@ExperimentalCoroutinesApi +class DefaultSessionParamsCreatorTest : DefaultSessionParamsCreatorTestBase() { + + private val fakeIsValidClientServerApiTask = FakeIsValidClientServerApiTask() + + private val sessionParamsCreator = DefaultSessionParamsCreator(fakeIsValidClientServerApiTask.instance) + + @Test + fun `when create, then SessionParams created`() = runBlockingTest { + val output = sessionParamsCreator.create(credentials, homeServerConnectionConfig, LoginType.UNKNOWN) + + assertExpectedSessionParams(output) + } + + @Test + fun `given credentials contains homeServerUri, when create, then SessionParams created with validated credentials uri`() = runBlockingTest { + val output = sessionParamsCreator.create(credentialsWithHomeServer, homeServerConnectionConfig, LoginType.UNKNOWN) + + fakeIsValidClientServerApiTask.verifyExecutionWithConfig(homeServerConnectionConfig.copy(homeServerUriBase = discoveryWithHomeServer.getHomeServerUri())) + assertExpectedSessionParamsWithHomeServer(output) + } + + @Test + fun `given credentials homeServerUri is equal to homeServerConnectionConfig, when create, then do not validate`() = runBlockingTest { + val homeServerConnectionConfigWithCredentialsUri = homeServerConnectionConfig.copy(homeServerUriBase = discoveryWithHomeServer.getHomeServerUri()) + val output = sessionParamsCreator.create(credentialsWithHomeServer, homeServerConnectionConfigWithCredentialsUri , LoginType.UNKNOWN) + + fakeIsValidClientServerApiTask.verifyNoExecution() + assertExpectedSessionParamsWithHomeServer(output) + } + + @Test + fun `given credentials contains identityServerUri, when create, then SessionParams created with credentials uri`() = runBlockingTest { + val output = sessionParamsCreator.create(credentialsWithIdentityServer, homeServerConnectionConfig, LoginType.UNKNOWN) + + assertExpectedSessionParamsWithIdentityServer(output) + } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/DefaultSessionParamsCreatorTestBase.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/DefaultSessionParamsCreatorTestBase.kt new file mode 100644 index 0000000000..8265c1cc27 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/DefaultSessionParamsCreatorTestBase.kt @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.internal.auth + +import android.net.Uri +import org.amshove.kluent.shouldBeEqualTo +import org.matrix.android.sdk.api.auth.data.DiscoveryInformation +import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig +import org.matrix.android.sdk.api.auth.data.SessionParams +import org.matrix.android.sdk.internal.auth.login.LoginType +import org.matrix.android.sdk.test.fixtures.CredentialsFixture +import org.matrix.android.sdk.test.fixtures.DiscoveryInformationFixture +import org.matrix.android.sdk.test.fixtures.WellKnownBaseConfigFixture + +abstract class DefaultSessionParamsCreatorTestBase { + + protected val discoveryWithHomeServer = DiscoveryInformationFixture.aDiscoveryInformation(homeServer = WellKnownBaseConfigFixture.aWellKnownBaseConfig("http://homeserver_url/")) + private val discoveryWithIdentityServer = DiscoveryInformationFixture.aDiscoveryInformation(identityServer = WellKnownBaseConfigFixture.aWellKnownBaseConfig("http://identity_server_url/")) + protected val credentials = CredentialsFixture.aCredentials() + protected val credentialsWithHomeServer = CredentialsFixture.aCredentials(discoveryInformation = discoveryWithHomeServer) + protected val credentialsWithIdentityServer = CredentialsFixture.aCredentials(discoveryInformation = discoveryWithIdentityServer) + protected val homeServerConnectionConfig = HomeServerConnectionConfig.Builder().build() + + protected fun assertExpectedSessionParams(sessionParams: SessionParams) { + sessionParams shouldBeEqualTo SessionParams( + credentials = credentials, + homeServerConnectionConfig = homeServerConnectionConfig, + isTokenValid = true, + loginType = LoginType.UNKNOWN, + ) + } + + protected fun assertExpectedSessionParamsWithHomeServer(sessionParams: SessionParams) { + sessionParams shouldBeEqualTo SessionParams( + credentials = credentialsWithHomeServer, + homeServerConnectionConfig = homeServerConnectionConfig.copy(homeServerUriBase = discoveryWithHomeServer.getHomeServerUri()), + isTokenValid = true, + loginType = LoginType.UNKNOWN, + ) + } + + protected fun assertExpectedSessionParamsWithIdentityServer(sessionParams: SessionParams) { + sessionParams shouldBeEqualTo SessionParams( + credentials = credentialsWithHomeServer, + homeServerConnectionConfig = homeServerConnectionConfig.copy(identityServerUri = discoveryWithIdentityServer.getIdentityServerUri()), + isTokenValid = true, + loginType = LoginType.UNKNOWN, + ) + } + + private fun DiscoveryInformation.getIdentityServerUri() = identityServer?.baseURL?.convertToUri()!! + + protected fun DiscoveryInformation.getHomeServerUri() = homeServer?.baseURL?.convertToUri()!! + + private fun String.convertToUri() = trim { it == '/' } + .takeIf { it.isNotBlank() } + .let { Uri.parse(it) } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/db/SessionParamsMapperTest.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/db/SessionParamsMapperTest.kt index 72b8305b8b..1ba3724d63 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/db/SessionParamsMapperTest.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/db/SessionParamsMapperTest.kt @@ -17,11 +17,11 @@ package org.matrix.android.sdk.internal.auth.db import org.junit.Test -import org.matrix.android.sdk.test.fakes.auth.db.sessionparams.FakeSessionParamsMapperMoshi -import org.matrix.android.sdk.test.fakes.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.nullSessionParams -import org.matrix.android.sdk.test.fakes.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.nullSessionParamsEntity -import org.matrix.android.sdk.test.fakes.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParams -import org.matrix.android.sdk.test.fakes.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParamsEntity +import org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams.FakeSessionParamsMapperMoshi +import org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.nullSessionParams +import org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.nullSessionParamsEntity +import org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParams +import org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParamsEntity class SessionParamsMapperTest { diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/db/migration/MigrateAuthTo005Test.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/db/migration/MigrateAuthTo005Test.kt index ab5b1f4c7a..6b2432b5f6 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/db/migration/MigrateAuthTo005Test.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/db/migration/MigrateAuthTo005Test.kt @@ -17,7 +17,7 @@ package org.matrix.android.sdk.internal.auth.db.migration import org.junit.Test -import org.matrix.android.sdk.test.fakes.auth.db.migration.Fake005MigrationRealm +import org.matrix.android.sdk.test.fakes.internal.auth.db.migration.Fake005MigrationRealm class MigrateAuthTo005Test { diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/api/FakeSession.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/api/FakeSession.kt new file mode 100644 index 0000000000..5aac29c328 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/api/FakeSession.kt @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.test.fakes.api + +import io.mockk.mockk +import org.matrix.android.sdk.api.session.Session + +class FakeSession { + + val instance: Session = mockk() +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/FakeSessionManager.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/FakeSessionManager.kt new file mode 100644 index 0000000000..b251349249 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/FakeSessionManager.kt @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.test.fakes.internal + +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import org.amshove.kluent.shouldBeEqualTo +import org.matrix.android.sdk.api.auth.data.SessionParams +import org.matrix.android.sdk.api.session.Session +import org.matrix.android.sdk.internal.SessionManager +import org.matrix.android.sdk.test.fakes.api.FakeSession + +internal class FakeSessionManager { + + val instance: SessionManager = mockk() + + init { + every { instance.getOrCreateSession(any()) } returns fakeSession.instance + } + + fun assertSessionCreatedWithParams(session: Session, sessionParams: SessionParams) { + verify { instance.getOrCreateSession(sessionParams) } + + session shouldBeEqualTo fakeSession.instance + } + + companion object { + private val fakeSession = FakeSession() + } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakeIsValidClientServerApiTask.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakeIsValidClientServerApiTask.kt new file mode 100644 index 0000000000..3eeda6db65 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakeIsValidClientServerApiTask.kt @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.test.fakes.internal.auth + +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.mockk +import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig +import org.matrix.android.sdk.internal.auth.IsValidClientServerApiTask +import org.matrix.android.sdk.internal.auth.IsValidClientServerApiTask.Params + +internal class FakeIsValidClientServerApiTask { + + init { + coEvery { instance.execute(any()) } returns true + } + + val instance: IsValidClientServerApiTask = mockk() + + fun verifyExecutionWithConfig(config: HomeServerConnectionConfig) { + coVerify { instance.execute(Params(config)) } + } + + fun verifyNoExecution() { + coVerify(inverse = true) { instance.execute(any()) } + } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakePendingSessionStore.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakePendingSessionStore.kt new file mode 100644 index 0000000000..be91daf910 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakePendingSessionStore.kt @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.test.fakes.internal.auth + +import io.mockk.coJustRun +import io.mockk.coVerify +import io.mockk.mockk +import org.matrix.android.sdk.internal.auth.PendingSessionStore + +internal class FakePendingSessionStore { + + val instance: PendingSessionStore = mockk() + + init { + coJustRun { instance.delete() } + } + + fun verifyPendingSessionDataCleared() { + coVerify { instance.delete() } + } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakeSessionParamsCreator.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakeSessionParamsCreator.kt new file mode 100644 index 0000000000..b0757d5bd5 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakeSessionParamsCreator.kt @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.test.fakes.internal.auth + +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.mockk +import org.matrix.android.sdk.api.auth.data.Credentials +import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig +import org.matrix.android.sdk.internal.auth.SessionParamsCreator +import org.matrix.android.sdk.internal.auth.login.LoginType +import org.matrix.android.sdk.test.fixtures.SessionParamsFixture.aSessionParams + +internal class FakeSessionParamsCreator { + + val instance: SessionParamsCreator = mockk() + + init { + coEvery { instance.create(any(), any(), any()) } returns sessionParams + } + + fun verifyCreatedWithParameters( + credentials: Credentials, + homeServerConnectionConfig: HomeServerConnectionConfig, + loginType: LoginType, + ) { + coVerify { instance.create(credentials, homeServerConnectionConfig, loginType) } + } + + companion object { + val sessionParams = aSessionParams() + } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakeSessionParamsStore.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakeSessionParamsStore.kt new file mode 100644 index 0000000000..4336f6ba7c --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/FakeSessionParamsStore.kt @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.test.fakes.internal.auth + +import io.mockk.coJustRun +import io.mockk.coVerify +import io.mockk.mockk +import org.matrix.android.sdk.api.auth.data.SessionParams +import org.matrix.android.sdk.internal.auth.SessionParamsStore + +internal class FakeSessionParamsStore { + + val instance: SessionParamsStore = mockk() + + init { + coJustRun { instance.save(any()) } + } + + fun verifyParamsSaved(sessionParams: SessionParams) { + coVerify { instance.save(sessionParams) } + } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/auth/db/migration/Fake005MigrationRealm.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/db/migration/Fake005MigrationRealm.kt similarity index 97% rename from matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/auth/db/migration/Fake005MigrationRealm.kt rename to matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/db/migration/Fake005MigrationRealm.kt index 3f317fadf0..e5e2b6682f 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/auth/db/migration/Fake005MigrationRealm.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/db/migration/Fake005MigrationRealm.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.matrix.android.sdk.test.fakes.auth.db.migration +package org.matrix.android.sdk.test.fakes.internal.auth.db.migration import io.mockk.every import io.mockk.mockk diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/auth/db/sessionparams/FakeCredentialsJsonAdapter.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/db/sessionparams/FakeCredentialsJsonAdapter.kt similarity index 81% rename from matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/auth/db/sessionparams/FakeCredentialsJsonAdapter.kt rename to matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/db/sessionparams/FakeCredentialsJsonAdapter.kt index 7c0d78f96d..eea3e4fab1 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/auth/db/sessionparams/FakeCredentialsJsonAdapter.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/db/sessionparams/FakeCredentialsJsonAdapter.kt @@ -14,14 +14,14 @@ * limitations under the License. */ -package org.matrix.android.sdk.test.fakes.auth.db.sessionparams +package org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams import com.squareup.moshi.JsonAdapter import io.mockk.every import io.mockk.mockk import org.matrix.android.sdk.api.auth.data.Credentials -import org.matrix.android.sdk.test.fakes.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParams -import org.matrix.android.sdk.test.fakes.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParamsEntity +import org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParams +import org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParamsEntity import org.matrix.android.sdk.test.fixtures.CredentialsFixture.aCredentials internal class FakeCredentialsJsonAdapter { diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/auth/db/sessionparams/FakeHomeServerConnectionConfigJsonAdapter.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/db/sessionparams/FakeHomeServerConnectionConfigJsonAdapter.kt similarity index 82% rename from matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/auth/db/sessionparams/FakeHomeServerConnectionConfigJsonAdapter.kt rename to matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/db/sessionparams/FakeHomeServerConnectionConfigJsonAdapter.kt index 6a9285e650..d066907cdf 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/auth/db/sessionparams/FakeHomeServerConnectionConfigJsonAdapter.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/db/sessionparams/FakeHomeServerConnectionConfigJsonAdapter.kt @@ -14,14 +14,14 @@ * limitations under the License. */ -package org.matrix.android.sdk.test.fakes.auth.db.sessionparams +package org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams import com.squareup.moshi.JsonAdapter import io.mockk.every import io.mockk.mockk import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig -import org.matrix.android.sdk.test.fakes.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParams -import org.matrix.android.sdk.test.fakes.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParamsEntity +import org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParams +import org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParamsEntity internal class FakeHomeServerConnectionConfigJsonAdapter { diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/auth/db/sessionparams/FakeSessionParamsMapperMoshi.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/db/sessionparams/FakeSessionParamsMapperMoshi.kt similarity index 82% rename from matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/auth/db/sessionparams/FakeSessionParamsMapperMoshi.kt rename to matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/db/sessionparams/FakeSessionParamsMapperMoshi.kt index 6327c90565..546f8dcee9 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/auth/db/sessionparams/FakeSessionParamsMapperMoshi.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/internal/auth/db/sessionparams/FakeSessionParamsMapperMoshi.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.matrix.android.sdk.test.fakes.auth.db.sessionparams +package org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams import com.squareup.moshi.Moshi import io.mockk.every @@ -27,10 +27,10 @@ import org.matrix.android.sdk.api.auth.data.SessionParams import org.matrix.android.sdk.api.auth.data.sessionId import org.matrix.android.sdk.internal.auth.db.SessionParamsEntity import org.matrix.android.sdk.internal.auth.login.LoginType -import org.matrix.android.sdk.test.fakes.auth.db.sessionparams.FakeCredentialsJsonAdapter.Companion.CREDENTIALS_JSON -import org.matrix.android.sdk.test.fakes.auth.db.sessionparams.FakeCredentialsJsonAdapter.Companion.credentials -import org.matrix.android.sdk.test.fakes.auth.db.sessionparams.FakeHomeServerConnectionConfigJsonAdapter.Companion.HOME_SERVER_CONNECTION_CONFIG_JSON -import org.matrix.android.sdk.test.fakes.auth.db.sessionparams.FakeHomeServerConnectionConfigJsonAdapter.Companion.homeServerConnectionConfig +import org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams.FakeCredentialsJsonAdapter.Companion.CREDENTIALS_JSON +import org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams.FakeCredentialsJsonAdapter.Companion.credentials +import org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams.FakeHomeServerConnectionConfigJsonAdapter.Companion.HOME_SERVER_CONNECTION_CONFIG_JSON +import org.matrix.android.sdk.test.fakes.internal.auth.db.sessionparams.FakeHomeServerConnectionConfigJsonAdapter.Companion.homeServerConnectionConfig import org.matrix.android.sdk.test.fixtures.SessionParamsEntityFixture.aSessionParamsEntity import org.matrix.android.sdk.test.fixtures.SessionParamsFixture.aSessionParams diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/DiscoveryInformationFixture.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/DiscoveryInformationFixture.kt new file mode 100644 index 0000000000..4a15fd383a --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/DiscoveryInformationFixture.kt @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.test.fixtures + +import org.matrix.android.sdk.api.auth.data.DiscoveryInformation +import org.matrix.android.sdk.api.auth.data.WellKnownBaseConfig + +object DiscoveryInformationFixture { + fun aDiscoveryInformation( + homeServer: WellKnownBaseConfig? = null, + identityServer: WellKnownBaseConfig? = null, + ) = DiscoveryInformation( + homeServer, + identityServer + ) +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/WellKnownBaseConfigFixture.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/WellKnownBaseConfigFixture.kt new file mode 100644 index 0000000000..96a146587c --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/WellKnownBaseConfigFixture.kt @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.test.fixtures + +import org.matrix.android.sdk.api.auth.data.WellKnownBaseConfig + +object WellKnownBaseConfigFixture { + fun aWellKnownBaseConfig( + baseUrl: String? = null, + ) = WellKnownBaseConfig( + baseUrl, + ) +}