From 5ba12cf8c6ebbc154a8f3b428d2bc53026423a31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sat, 23 Feb 2019 14:39:22 +0100 Subject: [PATCH] Fill login credentials with default values upon fakelog selection (#255) Resolves #245 --- .../modules/login/form/LoginFormFragment.kt | 26 ++++++++----- .../modules/login/form/LoginFormPresenter.kt | 7 +++- .../ui/modules/login/form/LoginFormView.kt | 8 ++++ app/src/main/res/values/api_endpoints.xml | 2 - .../login/form/LoginFormPresenterTest.kt | 39 +++++++++++++++---- 5 files changed, 61 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormFragment.kt index f5c7d1de..673e2166 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormFragment.kt @@ -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() diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt index e529501d..a0717649 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt @@ -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) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormView.kt index 56d476a7..69672780 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormView.kt @@ -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) diff --git a/app/src/main/res/values/api_endpoints.xml b/app/src/main/res/values/api_endpoints.xml index b6fd6a3b..ff8cd23c 100644 --- a/app/src/main/res/values/api_endpoints.xml +++ b/app/src/main/res/values/api_endpoints.xml @@ -7,7 +7,6 @@ EduNet Miasta Tarnowa ResMan Rzeszów Fakelog - Fakelog lokalnie https://vulcan.net.pl @@ -16,6 +15,5 @@ https://umt.tarnow.pl https://resman.pl https://fakelog.cf - http://fakelog.localhost:3000 diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt index b9c39590..e2d910a9 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt @@ -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())) .`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())) .`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>(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)