adding tests around the login error parsing

This commit is contained in:
Adam Brown 2022-06-09 16:08:17 +01:00
parent f89b9305e8
commit b25fd4a540
3 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,102 @@
/*
* 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 im.vector.app.features.onboarding.ftueauth
import im.vector.app.R
import im.vector.app.test.fakes.FakeErrorFormatter
import im.vector.app.test.fakes.FakeStringProvider
import im.vector.app.test.fakes.toTestString
import im.vector.app.test.fixtures.aHomeserverUnavailableError
import im.vector.app.test.fixtures.aLoginEmailUnknownError
import im.vector.app.test.fixtures.anInvalidPasswordError
import im.vector.app.test.fixtures.anInvalidUserNameError
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
private const val A_VALID_PASSWORD = "11111111"
private const val A_FORMATTED_ERROR_MESSAGE = "error message"
class LoginErrorParserTest {
private val fakeErrorFormatter = FakeErrorFormatter()
private val fakeStringProvider = FakeStringProvider()
private val loginErrorParser = LoginErrorParser(fakeErrorFormatter, fakeStringProvider.instance)
@Test
fun `given a generic error, when parsing, then has null username and password errors`() {
val cause = RuntimeException()
val result = loginErrorParser.parse(throwable = cause, password = A_VALID_PASSWORD)
result shouldBeEqualTo LoginErrorParser.LoginErrorResult(cause, usernameOrIdError = null, passwordError = null)
}
@Test
fun `given an invalid username error, when parsing, then has username error`() {
val cause = anInvalidUserNameError()
fakeErrorFormatter.given(cause, formatsTo = A_FORMATTED_ERROR_MESSAGE)
val result = loginErrorParser.parse(throwable = cause, password = A_VALID_PASSWORD)
result shouldBeEqualTo LoginErrorParser.LoginErrorResult(
cause,
usernameOrIdError = A_FORMATTED_ERROR_MESSAGE,
passwordError = null
)
}
@Test
fun `given a homeserver unavailable error, when parsing, then has username error`() {
val cause = aHomeserverUnavailableError()
val result = loginErrorParser.parse(throwable = cause, password = A_VALID_PASSWORD)
result shouldBeEqualTo LoginErrorParser.LoginErrorResult(
cause,
usernameOrIdError = R.string.login_error_homeserver_not_found.toTestString(),
passwordError = null
)
}
@Test
fun `given a login email unknown error, when parsing, then has username error`() {
val cause = aLoginEmailUnknownError()
val result = loginErrorParser.parse(throwable = cause, password = A_VALID_PASSWORD)
result shouldBeEqualTo LoginErrorParser.LoginErrorResult(
cause,
usernameOrIdError = R.string.login_login_with_email_error.toTestString(),
passwordError = null
)
}
@Test
fun `given a password with surrounding spaces and an invalid password error, when parsing, then has password error`() {
val cause = anInvalidPasswordError()
val result = loginErrorParser.parse(throwable = cause, password = " $A_VALID_PASSWORD ")
result shouldBeEqualTo LoginErrorParser.LoginErrorResult(
cause,
usernameOrIdError = null,
passwordError = R.string.auth_invalid_login_param_space_in_password.toTestString()
)
}
}

View File

@ -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 im.vector.app.test.fakes
import im.vector.app.core.error.ErrorFormatter
import io.mockk.every
import io.mockk.mockk
class FakeErrorFormatter : ErrorFormatter by mockk() {
fun given(cause: Throwable, formatsTo: String) {
every { toHumanReadable(cause) } returns formatsTo
}
}

View File

@ -25,4 +25,16 @@ fun a401ServerError() = Failure.ServerError(
MatrixError(MatrixError.M_UNAUTHORIZED, ""), HttpsURLConnection.HTTP_UNAUTHORIZED
)
fun anInvalidUserNameError() = Failure.ServerError(
MatrixError(MatrixError.M_INVALID_USERNAME, ""), HttpsURLConnection.HTTP_BAD_REQUEST
)
fun anInvalidPasswordError() = Failure.ServerError(
MatrixError(MatrixError.M_FORBIDDEN, "Invalid password"), HttpsURLConnection.HTTP_FORBIDDEN
)
fun aLoginEmailUnknownError() = Failure.ServerError(
MatrixError(MatrixError.M_FORBIDDEN, ""), HttpsURLConnection.HTTP_FORBIDDEN
)
fun aHomeserverUnavailableError() = Failure.NetworkConnection(UnknownHostException())