Fill login credentials with default values upon fakelog selection (#255)

Resolves #245
This commit is contained in:
Mikołaj Pich 2019-02-23 14:39:22 +01:00 committed by Rafał Borcz
parent 82d7cf94e8
commit 5ba12cf8c6
5 changed files with 61 additions and 21 deletions

View File

@ -31,6 +31,15 @@ class LoginFormFragment : BaseFragment(), LoginFormView {
fun newInstance() = LoginFormFragment()
}
override val formNameValue: String
get() = loginFormName.text.toString()
override val formPassValue: String
get() = loginFormPass.text.toString()
override val formHostValue: String?
get() = resources.getStringArray(R.array.endpoints_values)[loginFormHost.selectedItemPosition]
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_login_form, container, false)
}
@ -41,29 +50,26 @@ class LoginFormFragment : BaseFragment(), LoginFormView {
}
override fun initView() {
loginFormSignIn.setOnClickListener {
presenter.attemptLogin(
loginFormName.text.toString(),
loginFormPass.text.toString(),
resources.getStringArray(R.array.endpoints_values)[loginFormHost.selectedItemPosition]
)
}
loginFormName.setOnTextChangedListener { presenter.onNameTextChanged() }
loginFormPass.setOnTextChangedListener { presenter.onPassTextChanged() }
loginFormHost.setOnItemSelectedListener { presenter.onHostSelected() }
loginFormSignIn.setOnClickListener { presenter.attemptLogin() }
loginFormPass.setOnEditorActionListener { _, id, _ ->
if (id == IME_ACTION_DONE || id == IME_NULL) loginFormSignIn.callOnClick() else false
}
loginFormHost.setOnItemSelectedListener { presenter.onHostSelected() }
context?.let {
loginFormHost.adapter = ArrayAdapter.createFromResource(it, R.array.endpoints_keys, android.R.layout.simple_spinner_item)
.apply { setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) }
}
}
override fun setDefaultCredentials(name: String, pass: String) {
loginFormName.setText(name)
loginFormPass.setText(pass)
}
override fun setErrorNameRequired() {
loginFormNameLayout.run {
requestFocus()

View File

@ -35,6 +35,7 @@ class LoginFormPresenter @Inject constructor(
view?.apply {
clearPassError()
clearNameError()
if (formHostValue?.contains("fakelog") == true) setDefaultCredentials("jan@fakelog.cf", "jan123")
}
}
@ -46,7 +47,11 @@ class LoginFormPresenter @Inject constructor(
view?.clearNameError()
}
fun attemptLogin(email: String, password: String, endpoint: String) {
fun attemptLogin() {
val email = view?.formNameValue.orEmpty()
val password = view?.formPassValue.orEmpty()
val endpoint = view?.formHostValue.orEmpty()
if (!validateCredentials(email, password)) return
disposable.add(studentRepository.getStudents(email, password, endpoint)

View File

@ -7,6 +7,14 @@ interface LoginFormView : BaseView {
fun initView()
val formNameValue: String
val formPassValue: String
val formHostValue: String?
fun setDefaultCredentials(name: String, pass: String)
fun setErrorNameRequired()
fun setErrorPassRequired(focus: Boolean)

View File

@ -7,7 +7,6 @@
<item>EduNet Miasta Tarnowa</item>
<item>ResMan Rzeszów</item>
<item>Fakelog</item>
<item>Fakelog lokalnie</item>
</string-array>
<string-array name="endpoints_values">
<item>https://vulcan.net.pl</item>
@ -16,6 +15,5 @@
<item>https://umt.tarnow.pl</item>
<item>https://resman.pl</item>
<item>https://fakelog.cf</item>
<item>http://fakelog.localhost:3000</item>
</string-array>
</resources>

View File

@ -10,6 +10,7 @@ import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentMatchers.anyString
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.Mockito.clearInvocations
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.never
@ -49,7 +50,10 @@ class LoginFormPresenterTest {
@Test
fun emptyNicknameLoginTest() {
presenter.attemptLogin("", "test123", "https://fakelog.cf")
`when`(loginFormView.formNameValue).thenReturn("")
`when`(loginFormView.formPassValue).thenReturn("test123")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf")
presenter.attemptLogin()
verify(loginFormView).setErrorNameRequired()
verify(loginFormView, never()).setErrorPassRequired(false)
@ -58,7 +62,10 @@ class LoginFormPresenterTest {
@Test
fun emptyPassLoginTest() {
presenter.attemptLogin("@", "", "https://fakelog.cf")
`when`(loginFormView.formNameValue).thenReturn("@")
`when`(loginFormView.formPassValue).thenReturn("")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf")
presenter.attemptLogin()
verify(loginFormView, never()).setErrorNameRequired()
verify(loginFormView).setErrorPassRequired(true)
@ -67,7 +74,10 @@ class LoginFormPresenterTest {
@Test
fun invalidPassLoginTest() {
presenter.attemptLogin("@", "123", "https://fakelog.cf")
`when`(loginFormView.formNameValue).thenReturn("@")
`when`(loginFormView.formPassValue).thenReturn("123")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf")
presenter.attemptLogin()
verify(loginFormView, never()).setErrorNameRequired()
verify(loginFormView, never()).setErrorPassRequired(true)
@ -79,7 +89,11 @@ class LoginFormPresenterTest {
val studentTest = Student(email = "test@", password = "123", endpoint = "https://fakelog.cf", loginType = "AUTO", studentName = "", schoolSymbol = "", schoolName = "", studentId = 0, isCurrent = false, symbol = "", registrationDate = now())
doReturn(Single.just(listOf(studentTest)))
.`when`(repository).getStudents(anyString(), anyString(), anyString(), anyString())
presenter.attemptLogin("@", "123456", "https://fakelog.cf")
`when`(loginFormView.formNameValue).thenReturn("@")
`when`(loginFormView.formPassValue).thenReturn("123456")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf")
presenter.attemptLogin()
verify(loginFormView).hideSoftKeyboard()
verify(loginFormView).showProgress(true)
@ -92,7 +106,10 @@ class LoginFormPresenterTest {
fun loginEmptyTest() {
doReturn(Single.just(emptyList<Student>()))
.`when`(repository).getStudents(anyString(), anyString(), anyString(), anyString())
presenter.attemptLogin("@", "123456", "https://fakelog.cf")
`when`(loginFormView.formNameValue).thenReturn("@")
`when`(loginFormView.formPassValue).thenReturn("123456")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf")
presenter.attemptLogin()
verify(loginFormView).hideSoftKeyboard()
verify(loginFormView).showProgress(true)
@ -105,8 +122,11 @@ class LoginFormPresenterTest {
fun loginEmptyTwiceTest() {
doReturn(Single.just(emptyList<Student>()))
.`when`(repository).getStudents(anyString(), anyString(), anyString(), anyString())
presenter.attemptLogin("@", "123456", "https://fakelog.cf")
presenter.attemptLogin("@", "123456", "https://fakelog.cf")
`when`(loginFormView.formNameValue).thenReturn("@")
`when`(loginFormView.formPassValue).thenReturn("123456")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf")
presenter.attemptLogin()
presenter.attemptLogin()
verify(loginFormView, times(2)).hideSoftKeyboard()
verify(loginFormView, times(2)).showProgress(true)
@ -120,7 +140,10 @@ class LoginFormPresenterTest {
val testException = RuntimeException("test")
doReturn(Single.error<List<Student>>(testException))
.`when`(repository).getStudents(anyString(), anyString(), anyString(), anyString())
presenter.attemptLogin("@", "123456", "https://fakelog.cf")
`when`(loginFormView.formNameValue).thenReturn("@")
`when`(loginFormView.formPassValue).thenReturn("123456")
`when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf")
presenter.attemptLogin()
verify(loginFormView).hideSoftKeyboard()
verify(loginFormView).showProgress(true)