Add better validation to login/email field (#741)

This commit is contained in:
Mikołaj Pich 2020-04-02 20:27:14 +02:00 committed by GitHub
parent c0adeaadfd
commit da357775ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 71 additions and 24 deletions

View File

@ -146,6 +146,20 @@ class LoginAdvancedFragment : BaseFragment(), LoginAdvancedView {
}
}
override fun setErrorLoginRequired() {
with(loginFormUsernameLayout) {
requestFocus()
error = getString(R.string.login_invalid_login)
}
}
override fun setErrorEmailRequired() {
with(loginFormUsernameLayout) {
requestFocus()
error = getString(R.string.login_invalid_email)
}
}
override fun setErrorPassRequired(focus: Boolean) {
with(loginFormPassLayout) {
if (focus) requestFocus()

View File

@ -173,6 +173,8 @@ class LoginAdvancedPresenter @Inject constructor(
val login = view?.formUsernameValue.orEmpty()
val password = view?.formPassValue.orEmpty()
val host = view?.formHostValue.orEmpty()
val pin = view?.formPinValue.orEmpty()
val symbol = view?.formSymbolValue.orEmpty()
val token = view?.formTokenValue.orEmpty()
@ -196,26 +198,20 @@ class LoginAdvancedPresenter @Inject constructor(
isCorrect = false
}
}
Sdk.Mode.SCRAPPER -> {
Sdk.Mode.HYBRID, Sdk.Mode.SCRAPPER -> {
if (login.isEmpty()) {
view?.setErrorUsernameRequired()
isCorrect = false
}
} else {
if ("@" in login && "standard" !in host) {
view?.setErrorLoginRequired()
isCorrect = false
}
if (password.isEmpty()) {
view?.setErrorPassRequired(focus = isCorrect)
isCorrect = false
}
if (password.length < 6 && password.isNotEmpty()) {
view?.setErrorPassInvalid(focus = isCorrect)
isCorrect = false
}
}
Sdk.Mode.HYBRID -> {
if (login.isEmpty()) {
view?.setErrorUsernameRequired()
isCorrect = false
if ("@" !in login && "standard" in host) {
view?.setErrorEmailRequired()
isCorrect = false
}
}
if (password.isEmpty()) {

View File

@ -41,6 +41,10 @@ interface LoginAdvancedView : BaseView {
fun setErrorUsernameRequired()
fun setErrorLoginRequired()
fun setErrorEmailRequired()
fun setErrorPassRequired(focus: Boolean)
fun setErrorPassInvalid(focus: Boolean)

View File

@ -121,6 +121,20 @@ class LoginFormFragment : BaseFragment(), LoginFormView {
}
}
override fun setErrorLoginRequired() {
with(loginFormUsernameLayout) {
requestFocus()
error = getString(R.string.login_invalid_login)
}
}
override fun setErrorEmailRequired() {
with(loginFormUsernameLayout) {
requestFocus()
error = getString(R.string.login_invalid_email)
}
}
override fun setErrorSymbolRequired(focus: Boolean) {
with(loginFormSymbolLayout) {
if (focus) requestFocus()

View File

@ -131,6 +131,16 @@ class LoginFormPresenter @Inject constructor(
if (login.isEmpty()) {
view?.setErrorUsernameRequired()
isCorrect = false
} else {
if ("@" in login && "standard" !in host) {
view?.setErrorLoginRequired()
isCorrect = false
}
if ("@" !in login && "standard" in host) {
view?.setErrorEmailRequired()
isCorrect = false
}
}
if (password.isEmpty()) {

View File

@ -31,6 +31,10 @@ interface LoginFormView : BaseView {
fun setErrorUsernameRequired()
fun setErrorLoginRequired()
fun setErrorEmailRequired()
fun setErrorSymbolRequired(focus: Boolean)
fun setErrorPassRequired(focus: Boolean)

View File

@ -48,6 +48,7 @@
<string name="login_invalid_token">Ungültige token</string>
<string name="login_expired_token">Token ist nicht mehr gültig</string>
<string name="login_invalid_email">Ungültige email</string>
<string name="login_invalid_login">Ungültige login</string>
<string name="login_invalid_symbol">Ungültige symbol</string>
<string name="login_incorrect_symbol">Student nicht gefunden. Überprüfen Sie das Symbol</string>
<string name="login_field_required">Dieses Datenfeld ist erforderlich</string>

View File

@ -49,6 +49,7 @@
<string name="login_invalid_token">Nieprawidłowy token</string>
<string name="login_expired_token">Token stracił ważność</string>
<string name="login_invalid_email">Niepoprawny adres email</string>
<string name="login_invalid_login">Niepoprawny login</string>
<string name="login_invalid_symbol">Niepoprawny symbol</string>
<string name="login_incorrect_symbol">Nie znaleziono ucznia. Sprawdź symbol</string>
<string name="login_field_required">To pole jest wymagane</string>

View File

@ -49,6 +49,7 @@
<string name="login_invalid_token">Недействительный token</string>
<string name="login_expired_token">Токен просрочен</string>
<string name="login_invalid_email">Неверный адрес электронной почты</string>
<string name="login_invalid_login">Неправильный логин</string>
<string name="login_invalid_symbol">Недействительный symbol</string>
<string name="login_incorrect_symbol">Не удалось найти ученика. Пожалуйста, проверьте \"symbol\"</string>
<string name="login_field_required">Это поле обязательно</string>

View File

@ -49,6 +49,7 @@
<string name="login_invalid_pin">Недійсний PIN</string>
<string name="login_invalid_token">Недійсний token</string>
<string name="login_invalid_email">Недійсна адреса електронної пошти</string>
<string name="login_invalid_login">Невірний логін</string>
<string name="login_expired_token">Токен протермінований</string>
<string name="login_invalid_symbol">Недійсний symbol</string>
<string name="login_incorrect_symbol">Не вдалося знайти учня. Будь ласка, перевірте \"symbol\"</string>

View File

@ -50,6 +50,7 @@
<string name="login_invalid_token">Invalid token</string>
<string name="login_expired_token">Token expired</string>
<string name="login_invalid_email">Invalid email</string>
<string name="login_invalid_login">Invalid login</string>
<string name="login_invalid_symbol">Invalid symbol</string>
<string name="login_incorrect_symbol">Student not found. Check the symbol</string>
<string name="login_field_required">This field is required</string>

View File

@ -52,7 +52,7 @@ class LoginFormPresenterTest {
fun emptyNicknameLoginTest() {
`when`(loginFormView.formUsernameValue).thenReturn("")
`when`(loginFormView.formPassValue).thenReturn("test123")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf/?standard")
presenter.onSignInClick()
verify(loginFormView).setErrorUsernameRequired()
@ -64,7 +64,7 @@ class LoginFormPresenterTest {
fun emptyPassLoginTest() {
`when`(loginFormView.formUsernameValue).thenReturn("@")
`when`(loginFormView.formPassValue).thenReturn("")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf/?standard")
presenter.onSignInClick()
verify(loginFormView, never()).setErrorUsernameRequired()
@ -76,7 +76,7 @@ class LoginFormPresenterTest {
fun invalidPassLoginTest() {
`when`(loginFormView.formUsernameValue).thenReturn("@")
`when`(loginFormView.formPassValue).thenReturn("123")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf/?standard")
presenter.onSignInClick()
verify(loginFormView, never()).setErrorUsernameRequired()
@ -86,12 +86,12 @@ class LoginFormPresenterTest {
@Test
fun loginTest() {
val studentTest = Student(email = "test@", password = "123", scrapperBaseUrl = "https://fakelog.cf", loginType = "AUTO", studentName = "", schoolSymbol = "", schoolName = "", studentId = 0, classId = 1, isCurrent = false, symbol = "", registrationDate = now(), className = "", mobileBaseUrl = "", privateKey = "", certificateKey = "", loginMode = "", userLoginId = 0, schoolShortName = "", isParent = false)
val studentTest = Student(email = "test@", password = "123", scrapperBaseUrl = "https://fakelog.cf/", loginType = "AUTO", studentName = "", schoolSymbol = "", schoolName = "", studentId = 0, classId = 1, isCurrent = false, symbol = "", registrationDate = now(), className = "", mobileBaseUrl = "", privateKey = "", certificateKey = "", loginMode = "", userLoginId = 0, schoolShortName = "", isParent = false)
doReturn(Single.just(listOf(studentTest))).`when`(repository).getStudentsScrapper(anyString(), anyString(), anyString(), anyString())
`when`(loginFormView.formUsernameValue).thenReturn("@")
`when`(loginFormView.formPassValue).thenReturn("123456")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf/?standard")
`when`(loginFormView.formSymbolValue).thenReturn("Default")
`when`(loginFormView.formHostSymbol).thenReturn("Default")
presenter.onSignInClick()
@ -109,7 +109,7 @@ class LoginFormPresenterTest {
.`when`(repository).getStudentsScrapper(anyString(), anyString(), anyString(), anyString())
`when`(loginFormView.formUsernameValue).thenReturn("@")
`when`(loginFormView.formPassValue).thenReturn("123456")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf/?standard")
`when`(loginFormView.formSymbolValue).thenReturn("Default")
`when`(loginFormView.formHostSymbol).thenReturn("Default")
presenter.onSignInClick()
@ -127,7 +127,7 @@ class LoginFormPresenterTest {
.`when`(repository).getStudentsScrapper(anyString(), anyString(), anyString(), anyString())
`when`(loginFormView.formUsernameValue).thenReturn("@")
`when`(loginFormView.formPassValue).thenReturn("123456")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf/?standard")
`when`(loginFormView.formSymbolValue).thenReturn("Default")
`when`(loginFormView.formHostSymbol).thenReturn("Default")
presenter.onSignInClick()
@ -146,7 +146,7 @@ class LoginFormPresenterTest {
doReturn(Single.error<List<Student>>(testException)).`when`(repository).getStudentsScrapper(anyString(), anyString(), anyString(), anyString())
`when`(loginFormView.formUsernameValue).thenReturn("@")
`when`(loginFormView.formPassValue).thenReturn("123456")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf/?standard")
`when`(loginFormView.formSymbolValue).thenReturn("Default")
`when`(loginFormView.formHostSymbol).thenReturn("Default")
presenter.onSignInClick()