Skip to content

Commit 985e156

Browse files
committed
adding tests around the user name availability checks
1 parent 1062bfe commit 985e156

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import im.vector.app.test.fakes.FakeHomeServerConnectionConfigFactory
3333
import im.vector.app.test.fakes.FakeHomeServerHistoryService
3434
import im.vector.app.test.fakes.FakeLoginWizard
3535
import im.vector.app.test.fakes.FakeRegistrationActionHandler
36+
import im.vector.app.test.fakes.FakeRegistrationWizard
3637
import im.vector.app.test.fakes.FakeSession
3738
import im.vector.app.test.fakes.FakeStartAuthenticationFlowUseCase
3839
import im.vector.app.test.fakes.FakeStringProvider
@@ -41,6 +42,7 @@ import im.vector.app.test.fakes.FakeUriFilenameResolver
4142
import im.vector.app.test.fakes.FakeVectorFeatures
4243
import im.vector.app.test.fakes.FakeVectorOverrides
4344
import im.vector.app.test.fakes.toTestString
45+
import im.vector.app.test.fixtures.a401ServerError
4446
import im.vector.app.test.fixtures.aBuildMeta
4547
import im.vector.app.test.fixtures.aHomeServerCapabilities
4648
import im.vector.app.test.test
@@ -50,11 +52,13 @@ import org.junit.Rule
5052
import org.junit.Test
5153
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
5254
import org.matrix.android.sdk.api.auth.registration.Stage
55+
import org.matrix.android.sdk.api.failure.Failure
5356
import org.matrix.android.sdk.api.session.Session
5457
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities
5558

5659
private const val A_DISPLAY_NAME = "a display name"
5760
private const val A_PICTURE_FILENAME = "a-picture.png"
61+
private val A_SERVER_ERROR = a401ServerError()
5862
private val AN_ERROR = RuntimeException("an error!")
5963
private val A_LOADABLE_REGISTER_ACTION = RegisterAction.StartRegistration
6064
private val A_NON_LOADABLE_REGISTER_ACTION = RegisterAction.CheckIfEmailHasBeenValidated(delayMillis = -1L)
@@ -64,7 +68,7 @@ private val ANY_CONTINUING_REGISTRATION_RESULT = RegistrationActionHandler.Resul
6468
private val A_DIRECT_LOGIN = OnboardingAction.AuthenticateAction.LoginDirect("@a-user:id.org", "a-password", "a-device-name")
6569
private const val A_HOMESERVER_URL = "https://edited-homeserver.org"
6670
private val A_HOMESERVER_CONFIG = HomeServerConnectionConfig(FakeUri().instance)
67-
private val SELECTED_HOMESERVER_STATE = SelectedHomeserverState(preferredLoginMode = LoginMode.Password)
71+
private val SELECTED_HOMESERVER_STATE = SelectedHomeserverState(preferredLoginMode = LoginMode.Password, userFacingUrl = A_HOMESERVER_URL)
6872
private val SELECTED_HOMESERVER_STATE_SUPPORTED_LOGOUT_DEVICES = SelectedHomeserverState(isLogoutDevicesSupported = true)
6973
private const val AN_EMAIL = "[email protected]"
7074
private const val A_PASSWORD = "a-password"
@@ -323,6 +327,93 @@ class OnboardingViewModelTest {
323327
.finish()
324328
}
325329

330+
@Test
331+
fun `given available username, when a register username is entered, then emits available registration state`() = runTest {
332+
viewModelWith(initialRegistrationState(A_HOMESERVER_URL))
333+
val onlyUsername = "a-username"
334+
givenUserNameIsAvailable(onlyUsername)
335+
val test = viewModel.test()
336+
337+
viewModel.handle(OnboardingAction.UserNameEnteredAction.Registration(onlyUsername))
338+
339+
test
340+
.assertStatesChanges(
341+
initialState,
342+
{ copy(registrationState = availableRegistrationState(onlyUsername, A_HOMESERVER_URL)) }
343+
)
344+
.assertNoEvents()
345+
.finish()
346+
}
347+
348+
@Test
349+
fun `given unavailable username, when a register username is entered, then emits availability error`() = runTest {
350+
viewModelWith(initialRegistrationState(A_HOMESERVER_URL))
351+
val onlyUsername = "a-username"
352+
givenUserNameIsUnavailable(onlyUsername, A_SERVER_ERROR)
353+
val test = viewModel.test()
354+
355+
viewModel.handle(OnboardingAction.UserNameEnteredAction.Registration(onlyUsername))
356+
357+
test
358+
.assertState(initialState)
359+
.assertEvents(OnboardingViewEvents.Failure(A_SERVER_ERROR))
360+
.finish()
361+
}
362+
363+
@Test
364+
fun `given available full matrix id, when a register username is entered, then changes homeserver and emits available registration state`() = runTest {
365+
viewModelWith(initialRegistrationState("ignored-url"))
366+
givenCanSuccessfullyUpdateHomeserver(A_HOMESERVER_URL, SELECTED_HOMESERVER_STATE)
367+
val userName = "a-user"
368+
val fullMatrixId = "@$userName:${A_HOMESERVER_URL.removePrefix("https://")}"
369+
givenUserNameIsAvailable(userName)
370+
val test = viewModel.test()
371+
372+
viewModel.handle(OnboardingAction.UserNameEnteredAction.Registration(fullMatrixId))
373+
374+
test
375+
.assertStatesChanges(
376+
initialState,
377+
{ copy(isLoading = true) },
378+
{ copy(selectedHomeserver = SELECTED_HOMESERVER_STATE) },
379+
{ copy(registrationState = availableRegistrationState(userName, A_HOMESERVER_URL)) },
380+
{ copy(isLoading = false) },
381+
)
382+
.assertEvents(OnboardingViewEvents.OnHomeserverEdited)
383+
.finish()
384+
}
385+
386+
@Test
387+
fun `given unavailable full matrix id, when a register username is entered, then emits availability error`() = runTest {
388+
viewModelWith(initialRegistrationState("ignored-url"))
389+
givenCanSuccessfullyUpdateHomeserver(A_HOMESERVER_URL, SELECTED_HOMESERVER_STATE)
390+
val userName = "a-user"
391+
val fullMatrixId = "@$userName:${A_HOMESERVER_URL.removePrefix("https://")}"
392+
givenUserNameIsUnavailable(userName, A_SERVER_ERROR)
393+
val test = viewModel.test()
394+
395+
viewModel.handle(OnboardingAction.UserNameEnteredAction.Registration(fullMatrixId))
396+
397+
test
398+
.assertStatesChanges(
399+
initialState,
400+
{ copy(isLoading = true) },
401+
{ copy(selectedHomeserver = SELECTED_HOMESERVER_STATE) },
402+
{ copy(isLoading = false) },
403+
)
404+
.assertEvents(OnboardingViewEvents.OnHomeserverEdited, OnboardingViewEvents.Failure(A_SERVER_ERROR))
405+
.finish()
406+
}
407+
408+
private fun availableRegistrationState(userName: String, homeServerUrl: String) = RegistrationState(
409+
isUserNameAvailable = true,
410+
selectedMatrixId = "@$userName:${homeServerUrl.removePrefix("https://")}"
411+
)
412+
413+
private fun initialRegistrationState(homeServerUrl: String) = initialState.copy(
414+
onboardingFlow = OnboardingFlow.SignUp, selectedHomeserver = SelectedHomeserverState(userFacingUrl = homeServerUrl)
415+
)
416+
326417
@Test
327418
fun `given in the sign up flow, when editing homeserver errors, then does not update the selected homeserver state and emits error`() = runTest {
328419
viewModelWith(initialState.copy(onboardingFlow = OnboardingFlow.SignUp))
@@ -639,6 +730,14 @@ class OnboardingViewModelTest {
639730
givenRegistrationResultFor(RegisterAction.StartRegistration, RegistrationActionHandler.Result.Error(error))
640731
fakeHomeServerHistoryService.expectUrlToBeAdded(A_HOMESERVER_CONFIG.homeServerUri.toString())
641732
}
733+
734+
private fun givenUserNameIsAvailable(userName: String) {
735+
fakeAuthenticationService.givenRegistrationWizard(FakeRegistrationWizard().also { it.givenUserNameIsAvailable(userName) })
736+
}
737+
738+
private fun givenUserNameIsUnavailable(userName: String, failure: Failure.ServerError) {
739+
fakeAuthenticationService.givenRegistrationWizard(FakeRegistrationWizard().also { it.givenUserNameIsUnavailable(userName, failure) })
740+
}
642741
}
643742

644743
private fun HomeServerCapabilities.toPersonalisationState() = PersonalizationState(

vector/src/test/java/im/vector/app/test/fakes/FakeRegistrationWizard.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import io.mockk.coEvery
2020
import io.mockk.coVerify
2121
import io.mockk.mockk
2222
import org.matrix.android.sdk.api.auth.registration.RegisterThreePid
23+
import org.matrix.android.sdk.api.auth.registration.RegistrationAvailability
2324
import org.matrix.android.sdk.api.auth.registration.RegistrationResult
2425
import org.matrix.android.sdk.api.auth.registration.RegistrationWizard
26+
import org.matrix.android.sdk.api.failure.Failure
2527
import org.matrix.android.sdk.api.session.Session
2628

2729
class FakeRegistrationWizard : RegistrationWizard by mockk(relaxed = false) {
@@ -43,6 +45,14 @@ class FakeRegistrationWizard : RegistrationWizard by mockk(relaxed = false) {
4345
}
4446
}
4547

48+
fun givenUserNameIsAvailable(userName: String) {
49+
coEvery { registrationAvailable(userName) } returns RegistrationAvailability.Available
50+
}
51+
52+
fun givenUserNameIsUnavailable(userName: String, failure: Failure.ServerError) {
53+
coEvery { registrationAvailable(userName) } returns RegistrationAvailability.NotAvailable(failure)
54+
}
55+
4656
fun verifyCheckedEmailedVerification(times: Int) {
4757
coVerify(exactly = times) { checkIfEmailHasBeenValidated(any()) }
4858
}

0 commit comments

Comments
 (0)