Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes crash when entering non ascii username during account creation #6736

Merged
merged 3 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/6735.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes crash when entering non ascii characters during account creation
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ fun Throwable.isUsernameInUse() = this is Failure.ServerError &&
error.code == MatrixError.M_USER_IN_USE

fun Throwable.isInvalidUsername() = this is Failure.ServerError &&
error.code == MatrixError.M_INVALID_USERNAME
(error.code == MatrixError.M_INVALID_USERNAME || usernameContainsNonAsciiCharacters())

private fun Failure.ServerError.usernameContainsNonAsciiCharacters() = error.code == MatrixError.M_UNKNOWN &&
error.message == "Query parameter \'username\' must be ascii"

fun Throwable.isInvalidPassword() = this is Failure.ServerError &&
error.code == MatrixError.M_FORBIDDEN &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,25 +193,32 @@ class OnboardingViewModel @AssistedInject constructor(
}

private suspend fun checkUserNameAvailability(userName: String) {
when (val result = registrationWizard.registrationAvailable(userName)) {
RegistrationAvailability.Available -> {
setState {
copy(
registrationState = RegistrationState(
isUserNameAvailable = true,
selectedMatrixId = when {
userName.isMatrixId() -> userName
else -> "@$userName:${selectedHomeserver.userFacingUrl.toReducedUrl()}"
},
)
)
}
}
runCatching { registrationWizard.registrationAvailable(userName) }.fold(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding the runCatching is the fix

onSuccess = { result ->
when (result) {
RegistrationAvailability.Available -> {
setState {
copy(
registrationState = RegistrationState(
isUserNameAvailable = true,
selectedMatrixId = when {
userName.isMatrixId() -> userName
else -> "@$userName:${selectedHomeserver.userFacingUrl.toReducedUrl()}"
},
)
)
}
}

is RegistrationAvailability.NotAvailable -> {
_viewEvents.post(OnboardingViewEvents.Failure(result.failure))
}
}
is RegistrationAvailability.NotAvailable -> {
_viewEvents.post(OnboardingViewEvents.Failure(result.failure))
}
}
},
onFailure = {
_viewEvents.post(OnboardingViewEvents.Failure(it))
}
)
}

private fun withAction(action: OnboardingAction, block: (OnboardingAction) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,20 @@ class OnboardingViewModelTest {
.finish()
}

@Test
fun `given available username throws, when a register username is entered, then emits error`() = runTest {
viewModelWith(initialRegistrationState(A_HOMESERVER_URL))
fakeAuthenticationService.givenRegistrationWizard(FakeRegistrationWizard().also { it.givenUserNameIsAvailableThrows(A_USERNAME, AN_ERROR) })
val test = viewModel.test()

viewModel.handle(OnboardingAction.UserNameEnteredAction.Registration(A_USERNAME))

test
.assertStates(initialState)
.assertEvents(OnboardingViewEvents.Failure(AN_ERROR))
.finish()
}

@Test
fun `given available username, when a register username is entered, then emits available registration state`() = runTest {
viewModelWith(initialRegistrationState(A_HOMESERVER_URL))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class FakeRegistrationWizard : RegistrationWizard by mockk(relaxed = false) {
coEvery { registrationAvailable(userName) } returns RegistrationAvailability.Available
}

fun givenUserNameIsAvailableThrows(userName: String, cause: Throwable) {
coEvery { registrationAvailable(userName) } throws cause
}

fun givenUserNameIsUnavailable(userName: String, failure: Failure.ServerError) {
coEvery { registrationAvailable(userName) } returns RegistrationAvailability.NotAvailable(failure)
}
Expand Down