From 25cee5ed69bec3e67e25c53095c54671ec9bcea8 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Fri, 27 Nov 2026 14:09:48 -0500 Subject: [PATCH] fix(android): new passwords follow the web's rules and wording (DRY B5) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Settings › Password said "New passwords do not match." where the web says "Passwords do not match.", and neither it nor an admin resetting someone's password checked the 8-character minimum before sending: the reset button lit up after one character. auth/PasswordRules mirrors web/src/lib/auth/password.ts: the minimum, the "At least 8 characters." hint under the field, the too-short and mismatch wording. Co-Authored-By: Claude Opus 5.5 --- .../minstrel/admin/ui/AdminUsersScreen.kt | 4 +- .../minstrel/auth/PasswordRules.kt | 22 ++++++++++ .../minstrel/settings/ui/PasswordCard.kt | 4 ++ .../minstrel/settings/ui/PasswordViewModel.kt | 4 +- .../settings/ui/PasswordViewModelTest.kt | 42 +++++++++++++++++++ 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 android/app/src/main/java/com/fabledsword/minstrel/auth/PasswordRules.kt create mode 100644 android/app/src/test/java/com/fabledsword/minstrel/settings/ui/PasswordViewModelTest.kt diff --git a/android/app/src/main/java/com/fabledsword/minstrel/admin/ui/AdminUsersScreen.kt b/android/app/src/main/java/com/fabledsword/minstrel/admin/ui/AdminUsersScreen.kt index 74e7e345..6bbb9741 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/admin/ui/AdminUsersScreen.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/admin/ui/AdminUsersScreen.kt @@ -31,6 +31,7 @@ import androidx.compose.material3.TextButton import android.content.ClipData import androidx.compose.ui.platform.ClipEntry import androidx.compose.ui.platform.LocalClipboard +import com.fabledsword.minstrel.auth.PasswordRules import com.fabledsword.minstrel.models.Invite import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect @@ -471,6 +472,7 @@ private fun ResetPasswordDialog( value = newPassword, onValueChange = { newPassword = it }, label = { Text("New password") }, + supportingText = { Text(PasswordRules.HINT) }, singleLine = true, visualTransformation = PasswordVisualTransformation(), ) @@ -479,7 +481,7 @@ private fun ResetPasswordDialog( confirmButton = { TextButton( onClick = { onConfirm(newPassword) }, - enabled = newPassword.isNotEmpty(), + enabled = PasswordRules.isLongEnough(newPassword), ) { Text("Reset") } }, dismissButton = { TextButton(onClick = onDismiss) { Text("Cancel") } }, diff --git a/android/app/src/main/java/com/fabledsword/minstrel/auth/PasswordRules.kt b/android/app/src/main/java/com/fabledsword/minstrel/auth/PasswordRules.kt new file mode 100644 index 00000000..cce594a3 --- /dev/null +++ b/android/app/src/main/java/com/fabledsword/minstrel/auth/PasswordRules.kt @@ -0,0 +1,22 @@ +package com.fabledsword.minstrel.auth + +/** + * The rules every new-password field shares (Settings › Password, an admin + * resetting someone's password), worded as the web app words them + * (web/src/lib/auth/password.ts). The server enforces the same minimum and + * answers password_too_short; checking here only saves the round trip. + */ +object PasswordRules { + const val MIN_LENGTH = 8 + + /** The hint under a new-password field. */ + const val HINT = "At least $MIN_LENGTH characters." + + /** Said when a new password is shorter than [MIN_LENGTH]. */ + const val TOO_SHORT = "Password must be at least $MIN_LENGTH characters." + + /** Said when a password and its confirmation differ. */ + const val DIFFER = "Passwords do not match." + + fun isLongEnough(password: String): Boolean = password.length >= MIN_LENGTH +} diff --git a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/PasswordCard.kt b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/PasswordCard.kt index 7f1234a5..03cd431a 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/PasswordCard.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/PasswordCard.kt @@ -19,6 +19,7 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle +import com.fabledsword.minstrel.auth.PasswordRules import com.fabledsword.minstrel.theme.Muted import com.fabledsword.minstrel.theme.Section import com.fabledsword.minstrel.theme.SwitchRow @@ -36,6 +37,7 @@ fun PasswordCard(viewModel: PasswordViewModel = hiltViewModel()) { value = state.next, label = "New password", onChange = viewModel::setNext, + hint = PasswordRules.HINT, ) PasswordField( value = state.confirm, @@ -73,11 +75,13 @@ private fun PasswordField( value: String, label: String, onChange: (String) -> Unit, + hint: String? = null, ) { OutlinedTextField( value = value, onValueChange = onChange, label = { Text(label) }, + supportingText = hint?.let { { Text(it) } }, modifier = Modifier.fillMaxWidth(), singleLine = true, visualTransformation = PasswordVisualTransformation(), diff --git a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/PasswordViewModel.kt b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/PasswordViewModel.kt index 6b76a435..ee9bf071 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/PasswordViewModel.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/PasswordViewModel.kt @@ -3,6 +3,7 @@ package com.fabledsword.minstrel.settings.ui import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.fabledsword.minstrel.api.ErrorCopy +import com.fabledsword.minstrel.auth.PasswordRules import com.fabledsword.minstrel.settings.data.MeRepository import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow @@ -46,7 +47,8 @@ class PasswordViewModel @Inject constructor( val validationError: String? = when { s.isChanging -> "" // sentinel: silent no-op s.current.isEmpty() || s.next.isEmpty() -> "All fields required." - s.next != s.confirm -> "New passwords do not match." + !PasswordRules.isLongEnough(s.next) -> PasswordRules.TOO_SHORT + s.next != s.confirm -> PasswordRules.DIFFER else -> null } if (validationError != null) { diff --git a/android/app/src/test/java/com/fabledsword/minstrel/settings/ui/PasswordViewModelTest.kt b/android/app/src/test/java/com/fabledsword/minstrel/settings/ui/PasswordViewModelTest.kt new file mode 100644 index 00000000..f169f638 --- /dev/null +++ b/android/app/src/test/java/com/fabledsword/minstrel/settings/ui/PasswordViewModelTest.kt @@ -0,0 +1,42 @@ +package com.fabledsword.minstrel.settings.ui + +import com.fabledsword.minstrel.auth.PasswordRules +import com.fabledsword.minstrel.settings.data.MeRepository +import com.fabledsword.minstrel.testutil.MainDispatcherExtension +import io.mockk.coVerify +import io.mockk.mockk +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import kotlin.test.assertEquals + +/** + * A new password is checked as the web app checks it, with the same words, + * before anything is sent: at least 8 characters, and the same twice. + */ +@ExtendWith(MainDispatcherExtension::class) +class PasswordViewModelTest { + private val repository: MeRepository = mockk(relaxed = true) + + private fun attempt(next: String, confirm: String): PasswordViewModel = + PasswordViewModel(repository).apply { + setCurrent("old-password") + setNext(next) + setConfirm(confirm) + change() + } + + @Test + fun `a password shorter than 8 characters is refused without asking the server`() { + val viewModel = attempt(next = "short", confirm = "short") + assertEquals("Password must be at least 8 characters.", viewModel.state.value.message) + coVerify(exactly = 0) { repository.changePassword(any(), any(), any()) } + } + + @Test + fun `a confirmation that differs says so as the web does`() { + val viewModel = attempt(next = "long-enough-1", confirm = "long-enough-2") + assertEquals(PasswordRules.DIFFER, viewModel.state.value.message) + assertEquals("Passwords do not match.", PasswordRules.DIFFER) + coVerify(exactly = 0) { repository.changePassword(any(), any(), any()) } + } +}