diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/ChangePasswordTest.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/ChangePasswordTest.kt index 981385f3c7..2574700d49 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/ChangePasswordTest.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/ChangePasswordTest.kt @@ -17,9 +17,11 @@ package im.vector.matrix.android.account import im.vector.matrix.android.InstrumentedTest +import im.vector.matrix.android.api.failure.isInvalidPassword import im.vector.matrix.android.common.CommonTestHelper import im.vector.matrix.android.common.SessionTestParams import im.vector.matrix.android.common.TestConstants +import org.amshove.kluent.shouldBeTrue import org.junit.FixMethodOrder import org.junit.Test import org.junit.runner.RunWith @@ -46,12 +48,12 @@ class ChangePasswordTest : InstrumentedTest { } // Try to login with the previous password, it will fail - commonTestHelper.logAccountBadPassword(session.myUserId, TestConstants.PASSWORD) + val throwable = commonTestHelper.logAccountWithError(session.myUserId, TestConstants.PASSWORD) + throwable.isInvalidPassword().shouldBeTrue() // Try to login with the new password, should work val session2 = commonTestHelper.logIntoAccount(session.myUserId, NEW_PASSWORD, SessionTestParams(withInitialSync = false)) - commonTestHelper.signOutAndClose(session) commonTestHelper.signOutAndClose(session2) } diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/DeactivateAccountTest.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/DeactivateAccountTest.kt new file mode 100644 index 0000000000..17ff984bc8 --- /dev/null +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/DeactivateAccountTest.kt @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2020 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 im.vector.matrix.android.account + +import im.vector.matrix.android.InstrumentedTest +import im.vector.matrix.android.api.auth.data.LoginFlowResult +import im.vector.matrix.android.api.auth.registration.RegistrationResult +import im.vector.matrix.android.api.failure.Failure +import im.vector.matrix.android.api.failure.MatrixError +import im.vector.matrix.android.common.CommonTestHelper +import im.vector.matrix.android.common.SessionTestParams +import im.vector.matrix.android.common.TestConstants +import im.vector.matrix.android.common.TestMatrixCallback +import org.junit.Assert.assertTrue +import org.junit.FixMethodOrder +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.runners.MethodSorters + +@RunWith(JUnit4::class) +@FixMethodOrder(MethodSorters.JVM) +class DeactivateAccountTest : InstrumentedTest { + + private val commonTestHelper = CommonTestHelper(context()) + + @Test + fun deactivateAccountTest() { + val session = commonTestHelper.createAccount(TestConstants.USER_ALICE, SessionTestParams(withInitialSync = false)) + + // Deactivate the account + commonTestHelper.doSync { + session.deactivateAccount(TestConstants.PASSWORD, false, it) + } + + // Try to login on the previous account, it will fail (M_USER_DEACTIVATED) + val throwable = commonTestHelper.logAccountWithError(session.myUserId, TestConstants.PASSWORD) + + // Test the error + assertTrue(throwable is Failure.ServerError + && throwable.error.code == MatrixError.M_USER_DEACTIVATED + && throwable.error.message == "This account has been deactivated") + + // Try to create an account with the deactivate account user id, it will fail (M_USER_IN_USE) + val hs = commonTestHelper.createHomeServerConfig() + + commonTestHelper.doSync { + commonTestHelper.matrix.authenticationService.getLoginFlow(hs, it) + } + + var accountCreationError: Throwable? = null + commonTestHelper.waitWithLatch { + commonTestHelper.matrix.authenticationService + .getRegistrationWizard() + .createAccount(session.myUserId.substringAfter("@").substringBefore(":"), + TestConstants.PASSWORD, + null, + object : TestMatrixCallback(it, false) { + override fun onFailure(failure: Throwable) { + accountCreationError = failure + super.onFailure(failure) + } + }) + } + + // Test the error + accountCreationError.let { + assertTrue(it is Failure.ServerError + && it.error.code == MatrixError.M_USER_IN_USE) + } + + // No need to close the session, it has been deactivated + } +} diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt index f5027ec7b1..5bc8653f3d 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt @@ -26,7 +26,6 @@ import im.vector.matrix.android.api.MatrixConfiguration import im.vector.matrix.android.api.auth.data.HomeServerConnectionConfig import im.vector.matrix.android.api.auth.data.LoginFlowResult import im.vector.matrix.android.api.auth.registration.RegistrationResult -import im.vector.matrix.android.api.failure.isInvalidPassword import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.events.model.EventType import im.vector.matrix.android.api.session.events.model.LocalEcho @@ -42,7 +41,6 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking -import org.amshove.kluent.shouldBeTrue import org.junit.Assert.assertEquals import org.junit.Assert.assertNotNull import org.junit.Assert.assertTrue @@ -263,13 +261,13 @@ class CommonTestHelper(context: Context) { } /** - * Log into the account using a wrong password + * Log into the account and expect an error * - * @param userName the account username - * @param badPassword an incorrect password + * @param userName the account username + * @param password the password */ - fun logAccountBadPassword(userName: String, - badPassword: String) { + fun logAccountWithError(userName: String, + password: String): Throwable { val hs = createHomeServerConfig() doSync { @@ -281,7 +279,7 @@ class CommonTestHelper(context: Context) { waitWithLatch { latch -> matrix.authenticationService .getLoginWizard() - .login(userName, badPassword, "myDevice", object : TestMatrixCallback(latch, onlySuccessful = false) { + .login(userName, password, "myDevice", object : TestMatrixCallback(latch, onlySuccessful = false) { override fun onFailure(failure: Throwable) { requestFailure = failure super.onFailure(failure) @@ -289,7 +287,8 @@ class CommonTestHelper(context: Context) { }) } - requestFailure!!.isInvalidPassword().shouldBeTrue() + assertNotNull(requestFailure) + return requestFailure!! } /** diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/account/DeactivateAccountTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/account/DeactivateAccountTask.kt index 7052057779..f5b105cfee 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/account/DeactivateAccountTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/account/DeactivateAccountTask.kt @@ -16,9 +16,6 @@ package im.vector.matrix.android.internal.session.account -import im.vector.matrix.android.api.failure.Failure -import im.vector.matrix.android.internal.auth.registration.RegistrationFlowResponse -import im.vector.matrix.android.internal.di.MoshiProvider import im.vector.matrix.android.internal.di.UserId import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.session.cleanup.CleanupSession @@ -42,36 +39,9 @@ internal class DefaultDeactivateAccountTask @Inject constructor( override suspend fun execute(params: DeactivateAccountTask.Params) { val deactivateAccountParams = DeactivateAccountParams.create(userId, params.password, params.eraseAllData) - try { - executeRequest(eventBus) { - apiCall = accountAPI.deactivate(deactivateAccountParams) - } - } catch (throwable: Throwable) { - if (throwable is Failure.OtherServerError - && throwable.httpCode == 401 - /* Avoid infinite loop */ - && deactivateAccountParams.auth?.session == null) { - try { - MoshiProvider.providesMoshi() - .adapter(RegistrationFlowResponse::class.java) - .fromJson(throwable.errorBody) - } catch (e: Exception) { - null - }?.let { - // Retry with authentication - try { - executeRequest(eventBus) { - apiCall = accountAPI.deactivate( - deactivateAccountParams.copy(auth = deactivateAccountParams.auth?.copy(session = it.session)) - ) - } - return - } catch (failure: Throwable) { - throw failure - } - } - } - throw throwable + + executeRequest(eventBus) { + apiCall = accountAPI.deactivate(deactivateAccountParams) } cleanupSession.handle()