1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2024-09-20 06:19:09 -05:00

Add early validation for special mail domains (#1000) (#1176)

This commit is contained in:
Kamil Studziński 2021-03-03 22:37:58 +01:00 committed by GitHub
parent 76039e5eb9
commit 06a27199ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 10 deletions

View File

@ -150,6 +150,13 @@ class LoginFormFragment : BaseFragment<FragmentLoginFormBinding>(R.layout.fragme
}
}
override fun setErrorEmailInvalid(domain: String) {
with(binding.loginFormUsernameLayout) {
requestFocus()
error = getString(R.string.login_invalid_custom_email,domain)
}
}
override fun clearUsernameError() {
binding.loginFormUsernameLayout.error = null
}

View File

@ -11,6 +11,7 @@ import io.github.wulkanowy.utils.flowWithResource
import io.github.wulkanowy.utils.ifNullOrBlank
import kotlinx.coroutines.flow.onEach
import timber.log.Timber
import java.net.URL
import javax.inject.Inject
class LoginFormPresenter @Inject constructor(
@ -87,7 +88,14 @@ class LoginFormPresenter @Inject constructor(
if (!validateCredentials(email, password, host)) return
flowWithResource { studentRepository.getStudentsScrapper(email, password, host, symbol) }.onEach {
flowWithResource {
studentRepository.getStudentsScrapper(
email,
password,
host,
symbol
)
}.onEach {
when (it.status) {
Status.LOADING -> view?.run {
Timber.i("Login started")
@ -150,11 +158,18 @@ class LoginFormPresenter @Inject constructor(
view?.setErrorLoginRequired()
isCorrect = false
}
if ("@" !in login && "email" in host) {
view?.setErrorEmailRequired()
isCorrect = false
}
if ("@" in login && "login" !in host && "email" !in host) {
val emailHost = login.substringAfter("@")
val emailDomain = URL(host).host
if (emailHost != emailDomain) {
view?.setErrorEmailInvalid(domain = emailDomain)
isCorrect = false
}
}
}
if (password.isEmpty()) {

View File

@ -39,6 +39,8 @@ interface LoginFormView : BaseView {
fun setErrorPassIncorrect()
fun setErrorEmailInvalid(domain: String)
fun clearUsernameError()
fun clearPassError()

View File

@ -50,6 +50,7 @@
<string name="login_expired_token">Token expired</string>
<string name="login_invalid_email">Invalid email</string>
<string name="login_invalid_login">Use the assigned login instead of email</string>
<string name="login_invalid_custom_email">Use the assigned login or email in @%1$s</string>
<string name="login_invalid_symbol">Invalid symbol</string>
<string name="login_incorrect_symbol">Student not found. Validate the symbol and the chosen variation of the UONET+ register</string>
<string name="login_field_required">This field is required</string>

View File

@ -65,7 +65,7 @@ class LoginFormPresenterTest {
fun emptyNicknameLoginTest() {
every { loginFormView.formUsernameValue } returns ""
every { loginFormView.formPassValue } returns "test123"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?standard"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?email"
presenter.onSignInClick()
verify { loginFormView.setErrorUsernameRequired() }
@ -73,11 +73,21 @@ class LoginFormPresenterTest {
verify(exactly = 0) { loginFormView.setErrorPassInvalid(false) }
}
@Test
fun invalidEmailLoginTest() {
every { loginFormView.formUsernameValue } returns "@"
every { loginFormView.formPassValue } returns "123456"
every { loginFormView.formHostValue } returns "https://fakelog.cf/"
presenter.onSignInClick()
verify { loginFormView.setErrorEmailInvalid("fakelog.cf") }
}
@Test
fun emptyPassLoginTest() {
every { loginFormView.formUsernameValue } returns "@"
every { loginFormView.formPassValue } returns ""
every { loginFormView.formHostValue } returns "https://fakelog.cf/?standard"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?email"
presenter.onSignInClick()
verify(exactly = 0) { loginFormView.setErrorUsernameRequired() }
@ -89,7 +99,7 @@ class LoginFormPresenterTest {
fun invalidPassLoginTest() {
every { loginFormView.formUsernameValue } returns "@"
every { loginFormView.formPassValue } returns "123"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?standard"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?email"
presenter.onSignInClick()
verify(exactly = 0) { loginFormView.setErrorUsernameRequired() }
@ -102,7 +112,7 @@ class LoginFormPresenterTest {
val studentTest = Student(
email = "test@",
password = "123",
scrapperBaseUrl = "https://fakelog.cf/",
scrapperBaseUrl = "https://fakelog.cf/?email",
loginType = "AUTO",
studentName = "",
schoolSymbol = "",
@ -128,7 +138,7 @@ class LoginFormPresenterTest {
every { loginFormView.formUsernameValue } returns "@"
every { loginFormView.formPassValue } returns "123456"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?standard"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?email"
every { loginFormView.formHostSymbol } returns "Default"
presenter.onSignInClick()
@ -144,7 +154,7 @@ class LoginFormPresenterTest {
coEvery { repository.getStudentsScrapper(any(), any(), any(), any()) } returns listOf()
every { loginFormView.formUsernameValue } returns "@"
every { loginFormView.formPassValue } returns "123456"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?standard"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?email"
every { loginFormView.formHostSymbol } returns "Default"
presenter.onSignInClick()
@ -160,7 +170,7 @@ class LoginFormPresenterTest {
coEvery { repository.getStudentsScrapper(any(), any(), any(), any()) } returns listOf()
every { loginFormView.formUsernameValue } returns "@"
every { loginFormView.formPassValue } returns "123456"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?standard"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?email"
every { loginFormView.formHostSymbol } returns "Default"
presenter.onSignInClick()
presenter.onSignInClick()
@ -178,7 +188,7 @@ class LoginFormPresenterTest {
coEvery { repository.getStudentsScrapper(any(), any(), any(), any()) } throws testException
every { loginFormView.formUsernameValue } returns "@"
every { loginFormView.formPassValue } returns "123456"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?standard"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?email"
every { loginFormView.formHostSymbol } returns "Default"
every { loginFormView.showProgress(any()) } just Runs
every { loginFormView.showProgress(any()) } just Runs