ganfra
3 months ago
committed by
GitHub
33 changed files with 292 additions and 142 deletions
@ -1,40 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (c) 2024 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 io.element.android.features.lockscreen.impl.unlock.signout |
|
||||||
|
|
||||||
import com.squareup.anvil.annotations.ContributesBinding |
|
||||||
import io.element.android.libraries.di.AppScope |
|
||||||
import io.element.android.libraries.matrix.api.MatrixClientProvider |
|
||||||
import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService |
|
||||||
import javax.inject.Inject |
|
||||||
|
|
||||||
@ContributesBinding(AppScope::class) |
|
||||||
class DefaultSignOut @Inject constructor( |
|
||||||
private val authenticationService: MatrixAuthenticationService, |
|
||||||
private val matrixClientProvider: MatrixClientProvider, |
|
||||||
) : SignOut { |
|
||||||
override suspend fun invoke(): String? { |
|
||||||
val currentSession = authenticationService.getLatestSessionId() |
|
||||||
return if (currentSession != null) { |
|
||||||
matrixClientProvider.getOrRestore(currentSession) |
|
||||||
.getOrThrow() |
|
||||||
.logout(ignoreSdkError = true) |
|
||||||
} else { |
|
||||||
error("No session to sign out") |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,61 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (c) 2024 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 io.element.android.features.lockscreen.impl.unlock |
|
||||||
|
|
||||||
import com.google.common.truth.Truth.assertThat |
|
||||||
import io.element.android.features.lockscreen.impl.unlock.signout.DefaultSignOut |
|
||||||
import io.element.android.libraries.matrix.test.FakeMatrixClient |
|
||||||
import io.element.android.libraries.matrix.test.FakeMatrixClientProvider |
|
||||||
import io.element.android.libraries.matrix.test.auth.FakeMatrixAuthenticationService |
|
||||||
import io.element.android.tests.testutils.lambda.assert |
|
||||||
import io.element.android.tests.testutils.lambda.lambdaRecorder |
|
||||||
import kotlinx.coroutines.test.runTest |
|
||||||
import org.junit.Test |
|
||||||
|
|
||||||
class DefaultSignOutTest { |
|
||||||
private val matrixClient = FakeMatrixClient() |
|
||||||
private val authenticationService = FakeMatrixAuthenticationService() |
|
||||||
private val matrixClientProvider = FakeMatrixClientProvider(getClient = { Result.success(matrixClient) }) |
|
||||||
private val sut = DefaultSignOut(authenticationService, matrixClientProvider) |
|
||||||
|
|
||||||
@Test |
|
||||||
fun `when no active session then it throws`() = runTest { |
|
||||||
authenticationService.getLatestSessionIdLambda = { null } |
|
||||||
val result = runCatching { sut.invoke() } |
|
||||||
assertThat(result.isFailure).isTrue() |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
fun `with one active session and successful logout on client`() = runTest { |
|
||||||
val logoutLambda = lambdaRecorder<Boolean, String?> { _: Boolean -> null } |
|
||||||
authenticationService.getLatestSessionIdLambda = { matrixClient.sessionId } |
|
||||||
matrixClient.logoutLambda = logoutLambda |
|
||||||
val result = runCatching { sut.invoke() } |
|
||||||
assertThat(result.isSuccess).isTrue() |
|
||||||
assert(logoutLambda).isCalledOnce() |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
fun `with one active session and and failed logout on client`() = runTest { |
|
||||||
val logoutLambda = lambdaRecorder<Boolean, String?> { _: Boolean -> error("Failed to logout") } |
|
||||||
authenticationService.getLatestSessionIdLambda = { matrixClient.sessionId } |
|
||||||
matrixClient.logoutLambda = logoutLambda |
|
||||||
val result = runCatching { sut.invoke() } |
|
||||||
assertThat(result.isFailure).isTrue() |
|
||||||
assert(logoutLambda).isCalledOnce() |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,33 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2024 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 |
||||||
|
* |
||||||
|
* https://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 io.element.android.features.logout.api |
||||||
|
|
||||||
|
/** |
||||||
|
* Used to trigger a log out of the current user from any part of the app. |
||||||
|
*/ |
||||||
|
interface LogoutUseCase { |
||||||
|
/** |
||||||
|
* Log out the current user and then perform any needed cleanup tasks. |
||||||
|
* @param ignoreSdkError if true, the SDK error will be ignored and the user will be logged out anyway. |
||||||
|
* @return the session id of the logged out user. |
||||||
|
*/ |
||||||
|
suspend fun logout(ignoreSdkError: Boolean): String |
||||||
|
|
||||||
|
interface Factory { |
||||||
|
fun create(sessionId: String): LogoutUseCase |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,43 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2024 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 |
||||||
|
* |
||||||
|
* https://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 io.element.android.features.logout.impl |
||||||
|
|
||||||
|
import com.squareup.anvil.annotations.ContributesBinding |
||||||
|
import dagger.assisted.Assisted |
||||||
|
import dagger.assisted.AssistedFactory |
||||||
|
import dagger.assisted.AssistedInject |
||||||
|
import io.element.android.features.logout.api.LogoutUseCase |
||||||
|
import io.element.android.libraries.di.AppScope |
||||||
|
import io.element.android.libraries.matrix.api.MatrixClientProvider |
||||||
|
import io.element.android.libraries.matrix.api.core.SessionId |
||||||
|
|
||||||
|
class DefaultLogoutUseCase @AssistedInject constructor( |
||||||
|
@Assisted private val sessionId: String, |
||||||
|
private val matrixClientProvider: MatrixClientProvider, |
||||||
|
) : LogoutUseCase { |
||||||
|
@ContributesBinding(AppScope::class) |
||||||
|
@AssistedFactory |
||||||
|
interface Factory : LogoutUseCase.Factory { |
||||||
|
override fun create(sessionId: String): DefaultLogoutUseCase |
||||||
|
} |
||||||
|
|
||||||
|
override suspend fun logout(ignoreSdkError: Boolean): String { |
||||||
|
val matrixClient = matrixClientProvider.getOrRestore(SessionId(sessionId)).getOrThrow() |
||||||
|
matrixClient.logout(ignoreSdkError = ignoreSdkError) |
||||||
|
return sessionId |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,36 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2024 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 |
||||||
|
* |
||||||
|
* https://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 io.element.android.features.logout.impl |
||||||
|
|
||||||
|
import com.squareup.anvil.annotations.ContributesTo |
||||||
|
import dagger.Module |
||||||
|
import dagger.Provides |
||||||
|
import io.element.android.features.logout.api.LogoutUseCase |
||||||
|
import io.element.android.libraries.di.SessionScope |
||||||
|
import io.element.android.libraries.matrix.api.user.CurrentSessionIdHolder |
||||||
|
|
||||||
|
@Module |
||||||
|
@ContributesTo(SessionScope::class) |
||||||
|
object SessionLogoutModule { |
||||||
|
@Provides |
||||||
|
fun provideLogoutUseCase( |
||||||
|
currentSessionIdHolder: CurrentSessionIdHolder, |
||||||
|
factory: DefaultLogoutUseCase.Factory, |
||||||
|
): LogoutUseCase { |
||||||
|
return factory.create(currentSessionIdHolder.current.value) |
||||||
|
} |
||||||
|
} |
@ -1,3 +1,3 @@ |
|||||||
version https://git-lfs.github.com/spec/v1 |
version https://git-lfs.github.com/spec/v1 |
||||||
oid sha256:db1631ff53075b31c674cde4bf25cc2876cda8053ffcb593bf6c497c5c41d0c4 |
oid sha256:fb9971d6aa7f0734f9a30f83b25a03519f97789618219f0c79efa87d4430ca0f |
||||||
size 49825 |
size 58918 |
||||||
|
@ -1,3 +1,3 @@ |
|||||||
version https://git-lfs.github.com/spec/v1 |
version https://git-lfs.github.com/spec/v1 |
||||||
oid sha256:db1631ff53075b31c674cde4bf25cc2876cda8053ffcb593bf6c497c5c41d0c4 |
oid sha256:fb9971d6aa7f0734f9a30f83b25a03519f97789618219f0c79efa87d4430ca0f |
||||||
size 49825 |
size 58918 |
||||||
|
@ -1,3 +1,3 @@ |
|||||||
version https://git-lfs.github.com/spec/v1 |
version https://git-lfs.github.com/spec/v1 |
||||||
oid sha256:d7f5ea92f8413f6b6dc92d074038727036912ef1828210d0c4d6e36fb0f7e5d3 |
oid sha256:f44fc0dc3cd92839a7b176b086be75b082a238b9c8cd197f7273b33e7f01c591 |
||||||
size 48352 |
size 57495 |
||||||
|
@ -1,3 +1,3 @@ |
|||||||
version https://git-lfs.github.com/spec/v1 |
version https://git-lfs.github.com/spec/v1 |
||||||
oid sha256:ead9e1fe98bc9ffd33d3768e6983964768edbf3bb4c75cd78a82da5ec0eae03b |
oid sha256:93a6d6723bc89b9660a440848461c1568004d312599d7d2d2b94299d6aa47c0a |
||||||
size 48275 |
size 57037 |
||||||
|
@ -1,3 +1,3 @@ |
|||||||
version https://git-lfs.github.com/spec/v1 |
version https://git-lfs.github.com/spec/v1 |
||||||
oid sha256:ead9e1fe98bc9ffd33d3768e6983964768edbf3bb4c75cd78a82da5ec0eae03b |
oid sha256:93a6d6723bc89b9660a440848461c1568004d312599d7d2d2b94299d6aa47c0a |
||||||
size 48275 |
size 57037 |
||||||
|
@ -1,3 +1,3 @@ |
|||||||
version https://git-lfs.github.com/spec/v1 |
version https://git-lfs.github.com/spec/v1 |
||||||
oid sha256:ee30083ec3b87ba84a34bdee7b10d50939f31c596c3358bdca9b914ba001d198 |
oid sha256:bd7e58a29f370733af1e35531f428d5d2a732c70b9156d66ba590c9c5203f5d6 |
||||||
size 46843 |
size 55689 |
||||||
|
Loading…
Reference in new issue