SignupViewModel: minor refactor
`createUser` and `createExternalUser` are now parametrized and they accept non-null values.
This commit is contained in:
parent
f3f2efa010
commit
3b79c7a3f6
|
@ -164,12 +164,21 @@ internal class SignupViewModel @Inject constructor(
|
|||
* previously set [AccountType].
|
||||
* @see currentAccountType public property
|
||||
*/
|
||||
fun startCreateUserWorkflow() = viewModelScope.launch {
|
||||
fun startCreateUserWorkflow() {
|
||||
_userCreationState.tryEmit(State.Idle)
|
||||
|
||||
val password by lazy { requireNotNull(_password) { "Password is not set (initialized)." } }
|
||||
|
||||
when (currentAccountType) {
|
||||
AccountType.Username,
|
||||
AccountType.Internal -> createUser()
|
||||
AccountType.External -> createExternalUser()
|
||||
AccountType.Internal -> {
|
||||
val username = requireNotNull(username) { "Username is not set." }
|
||||
createUser(username, password)
|
||||
}
|
||||
AccountType.External -> {
|
||||
val email = requireNotNull(externalEmail) { "External email is not set." }
|
||||
createExternalUser(email, password)
|
||||
}
|
||||
}.exhaustive
|
||||
}
|
||||
|
||||
|
@ -209,13 +218,8 @@ internal class SignupViewModel @Inject constructor(
|
|||
// endregion
|
||||
|
||||
// region private functions
|
||||
private suspend fun createUser() {
|
||||
val username = username
|
||||
val encryptedPassword = _password
|
||||
|
||||
private fun createUser(username: String, encryptedPassword: EncryptedString) {
|
||||
flow {
|
||||
requireNotNull(username) { "Username is not set." }
|
||||
requireNotNull(encryptedPassword) { "Password is not set (initialized)." }
|
||||
emit(State.Processing)
|
||||
|
||||
val verification = _recoveryMethod?.let {
|
||||
|
@ -236,7 +240,7 @@ internal class SignupViewModel @Inject constructor(
|
|||
)
|
||||
emit(State.Success(result.id, username, encryptedPassword))
|
||||
}.catchWhen(Throwable::userAlreadyExists) {
|
||||
val userId = performLogin.invoke(username!!, encryptedPassword!!).userId
|
||||
val userId = performLogin.invoke(username, encryptedPassword).userId
|
||||
emit(State.Success(userId.id, username, encryptedPassword))
|
||||
}.catch { error ->
|
||||
emit(State.Error.Message(error.message))
|
||||
|
@ -245,13 +249,8 @@ internal class SignupViewModel @Inject constructor(
|
|||
}.launchIn(viewModelScope)
|
||||
}
|
||||
|
||||
private suspend fun createExternalUser() {
|
||||
val externalEmail = externalEmail
|
||||
val encryptedPassword = _password
|
||||
|
||||
private fun createExternalUser(externalEmail: String, encryptedPassword: EncryptedString) {
|
||||
flow {
|
||||
requireNotNull(externalEmail) { "External email is not set." }
|
||||
requireNotNull(encryptedPassword) { "Password is not set (initialized)." }
|
||||
emit(State.Processing)
|
||||
val userId = performCreateExternalEmailUser(
|
||||
email = externalEmail,
|
||||
|
@ -260,7 +259,7 @@ internal class SignupViewModel @Inject constructor(
|
|||
)
|
||||
emit(State.Success(userId.id, externalEmail, encryptedPassword))
|
||||
}.catchWhen(Throwable::userAlreadyExists) {
|
||||
val userId = performLogin.invoke(externalEmail!!, encryptedPassword!!).userId
|
||||
val userId = performLogin.invoke(externalEmail, encryptedPassword).userId
|
||||
emit(State.Success(userId.id, externalEmail, encryptedPassword))
|
||||
}.catch { error ->
|
||||
emit(State.Error.Message(error.message))
|
||||
|
|
|
@ -46,6 +46,7 @@ import me.proton.core.user.domain.entity.User
|
|||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFails
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class SignupViewModelTest : ArchTest, CoroutinesTest {
|
||||
|
@ -140,13 +141,9 @@ class SignupViewModelTest : ArchTest, CoroutinesTest {
|
|||
@Test
|
||||
fun `create Internal user no username no password set`() = coroutinesTest {
|
||||
viewModel.userCreationState.test {
|
||||
// WHEN
|
||||
viewModel.startCreateUserWorkflow()
|
||||
// THEN
|
||||
val throwable = assertFails { viewModel.startCreateUserWorkflow() }
|
||||
assertTrue(awaitItem() is SignupViewModel.State.Idle)
|
||||
val errorItem = awaitItem()
|
||||
assertTrue(errorItem is SignupViewModel.State.Error.Message)
|
||||
assertEquals("Username is not set.", errorItem.message)
|
||||
assertEquals("Username is not set.", throwable.message)
|
||||
|
||||
coVerify(exactly = 0) {
|
||||
performCreateUser(
|
||||
|
@ -167,13 +164,9 @@ class SignupViewModelTest : ArchTest, CoroutinesTest {
|
|||
viewModel.setPassword(testPassword)
|
||||
|
||||
viewModel.userCreationState.test {
|
||||
// WHEN
|
||||
viewModel.startCreateUserWorkflow()
|
||||
// THEN
|
||||
val throwable = assertFails { viewModel.startCreateUserWorkflow() }
|
||||
assertTrue(awaitItem() is SignupViewModel.State.Idle)
|
||||
val errorItem = awaitItem()
|
||||
assertTrue(errorItem is SignupViewModel.State.Error.Message)
|
||||
assertEquals("Username is not set.", errorItem.message)
|
||||
assertEquals("Username is not set.", throwable.message)
|
||||
|
||||
coVerify(exactly = 0) {
|
||||
performCreateUser(
|
||||
|
@ -193,13 +186,9 @@ class SignupViewModelTest : ArchTest, CoroutinesTest {
|
|||
// GIVEN
|
||||
viewModel.username = testUsername
|
||||
viewModel.userCreationState.test {
|
||||
// WHEN
|
||||
viewModel.startCreateUserWorkflow()
|
||||
// THEN
|
||||
val throwable = assertFails { viewModel.startCreateUserWorkflow() }
|
||||
assertTrue(awaitItem() is SignupViewModel.State.Idle)
|
||||
val errorItem = awaitItem()
|
||||
assertTrue(errorItem is SignupViewModel.State.Error.Message)
|
||||
assertEquals("Password is not set (initialized).", errorItem.message)
|
||||
assertEquals("Password is not set (initialized).", throwable.message)
|
||||
|
||||
coVerify(exactly = 0) {
|
||||
performCreateUser(
|
||||
|
@ -355,13 +344,9 @@ class SignupViewModelTest : ArchTest, CoroutinesTest {
|
|||
// GIVEN
|
||||
viewModel.currentAccountType = AccountType.External
|
||||
viewModel.userCreationState.test {
|
||||
// WHEN
|
||||
viewModel.startCreateUserWorkflow()
|
||||
// THEN
|
||||
val throwable = assertFails { viewModel.startCreateUserWorkflow() }
|
||||
assertTrue(awaitItem() is SignupViewModel.State.Idle)
|
||||
val errorItem = awaitItem()
|
||||
assertTrue(errorItem is SignupViewModel.State.Error.Message)
|
||||
assertEquals("External email is not set.", errorItem.message)
|
||||
assertEquals("External email is not set.", throwable.message)
|
||||
|
||||
coVerify(exactly = 0) {
|
||||
performCreateExternalUser(
|
||||
|
@ -379,13 +364,9 @@ class SignupViewModelTest : ArchTest, CoroutinesTest {
|
|||
viewModel.currentAccountType = AccountType.External
|
||||
viewModel.setPassword(testPassword)
|
||||
viewModel.userCreationState.test {
|
||||
// WHEN
|
||||
viewModel.startCreateUserWorkflow()
|
||||
// THEN
|
||||
val throwable = assertFails { viewModel.startCreateUserWorkflow() }
|
||||
assertTrue(awaitItem() is SignupViewModel.State.Idle)
|
||||
val errorItem = awaitItem()
|
||||
assertTrue(errorItem is SignupViewModel.State.Error.Message)
|
||||
assertEquals("External email is not set.", errorItem.message)
|
||||
assertEquals("External email is not set.", throwable.message)
|
||||
|
||||
coVerify(exactly = 0) {
|
||||
performCreateExternalUser(
|
||||
|
@ -403,13 +384,9 @@ class SignupViewModelTest : ArchTest, CoroutinesTest {
|
|||
viewModel.currentAccountType = AccountType.External
|
||||
viewModel.externalEmail = testEmail
|
||||
viewModel.userCreationState.test {
|
||||
// WHEN
|
||||
viewModel.startCreateUserWorkflow()
|
||||
// THEN
|
||||
val throwable = assertFails { viewModel.startCreateUserWorkflow() }
|
||||
assertTrue(awaitItem() is SignupViewModel.State.Idle)
|
||||
val errorItem = awaitItem()
|
||||
assertTrue(errorItem is SignupViewModel.State.Error.Message)
|
||||
assertEquals("Password is not set (initialized).", errorItem.message)
|
||||
assertEquals("Password is not set (initialized).", throwable.message)
|
||||
|
||||
coVerify(exactly = 0) {
|
||||
performCreateExternalUser(
|
||||
|
|
Loading…
Reference in New Issue