mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Add more tests for Auth (#4436)
This commit is contained in:
parent
4fa18e5e27
commit
d400790d9d
3 changed files with 531 additions and 0 deletions
|
|
@ -0,0 +1,347 @@
|
|||
package fr.free.nrw.commons.auth
|
||||
|
||||
import android.accounts.Account
|
||||
import android.app.ProgressDialog
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.view.KeyEvent
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import android.widget.Button
|
||||
import fr.free.nrw.commons.R
|
||||
import fr.free.nrw.commons.TestAppAdapter
|
||||
import fr.free.nrw.commons.TestCommonsApplication
|
||||
import fr.free.nrw.commons.kvstore.JsonKvStore
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito.`when`
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.powermock.reflect.Whitebox
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.fakes.RoboMenuItem
|
||||
import org.wikipedia.AppAdapter
|
||||
import org.wikipedia.login.LoginResult
|
||||
import java.lang.reflect.Method
|
||||
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [21], application = TestCommonsApplication::class)
|
||||
class LoginActivityUnitTests {
|
||||
|
||||
private lateinit var menuItem: MenuItem
|
||||
private lateinit var context: Context
|
||||
|
||||
@Mock
|
||||
private lateinit var activity: LoginActivity
|
||||
|
||||
@Mock
|
||||
private lateinit var progressDialog: ProgressDialog
|
||||
|
||||
@Mock
|
||||
private lateinit var view: View
|
||||
|
||||
@Mock
|
||||
private lateinit var params: ViewGroup.LayoutParams
|
||||
|
||||
@Mock
|
||||
private lateinit var keyEvent: KeyEvent
|
||||
|
||||
@Mock
|
||||
private lateinit var loginButton: Button
|
||||
|
||||
@Mock
|
||||
private lateinit var bundle: Bundle
|
||||
|
||||
@Mock
|
||||
private lateinit var applicationKvStore: JsonKvStore
|
||||
|
||||
@Mock
|
||||
private lateinit var sessionManager: SessionManager
|
||||
|
||||
@Mock
|
||||
private lateinit var account: Account
|
||||
|
||||
@Mock
|
||||
private lateinit var loginResult: LoginResult
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
AppAdapter.set(TestAppAdapter())
|
||||
activity = Robolectric.buildActivity(LoginActivity::class.java).create().get()
|
||||
context = RuntimeEnvironment.application.applicationContext
|
||||
menuItem = RoboMenuItem(null)
|
||||
Whitebox.setInternalState(activity, "progressDialog", progressDialog)
|
||||
Whitebox.setInternalState(activity, "applicationKvStore", applicationKvStore)
|
||||
Whitebox.setInternalState(activity, "sessionManager", sessionManager)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun checkActivityNotNull() {
|
||||
Assert.assertNotNull(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnEditorActionCaseDefault() {
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"onEditorAction",
|
||||
Int::class.java,
|
||||
KeyEvent::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, 0, keyEvent)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnEditorActionCaseLoginEnabledFirstCase() {
|
||||
Whitebox.setInternalState(activity, "loginButton", loginButton)
|
||||
`when`(loginButton.isEnabled).thenReturn(true)
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"onEditorAction",
|
||||
Int::class.java,
|
||||
KeyEvent::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, EditorInfo.IME_ACTION_DONE, keyEvent)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnEditorActionCaseLoginEnabledSecondCase() {
|
||||
Whitebox.setInternalState(activity, "loginButton", loginButton)
|
||||
`when`(loginButton.isEnabled).thenReturn(true)
|
||||
`when`(keyEvent.keyCode).thenReturn(KeyEvent.KEYCODE_ENTER)
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"onEditorAction",
|
||||
Int::class.java,
|
||||
KeyEvent::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, 0, keyEvent)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testSkipLogin() {
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"skipLogin"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testForgotPassword() {
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"forgotPassword"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnPrivacyPolicyClicked() {
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"onPrivacyPolicyClicked"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testSignUp() {
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"signUp"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnPostCreate() {
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"onPostCreate",
|
||||
Bundle::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, bundle)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnDestroy() {
|
||||
`when`(progressDialog.isShowing).thenReturn(true)
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"onDestroy"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnDestroyWithException() {
|
||||
`when`(progressDialog.isShowing).thenThrow(NullPointerException())
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"onDestroy"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnLoginSuccessCaseDefault() {
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"onLoginSuccess",
|
||||
LoginResult::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, loginResult)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnLoginSuccess() {
|
||||
`when`(progressDialog.isShowing).thenReturn(true)
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"onLoginSuccess",
|
||||
LoginResult::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, loginResult)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testShowPasswordResetPrompt() {
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"showPasswordResetPrompt"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testHideProgress() {
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"hideProgress"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnResume() {
|
||||
`when`(applicationKvStore.getBoolean("firstrun", true)).thenReturn(true)
|
||||
`when`(applicationKvStore.getBoolean("login_skipped", false)).thenReturn(true)
|
||||
`when`(sessionManager.currentAccount).thenReturn(account)
|
||||
`when`(sessionManager.isUserLoggedIn).thenReturn(true)
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"onResume"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnStart() {
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"onStart"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnStop() {
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"onStop"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnPostResume() {
|
||||
val method: Method = LoginActivity::class.java.getDeclaredMethod(
|
||||
"onPostResume"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testShowMessageAndCancelDialog() {
|
||||
activity.showMessageAndCancelDialog("")
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testStartMainActivity() {
|
||||
activity.startMainActivity()
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testShowMessageAndCancelDialogRes() {
|
||||
activity.showMessageAndCancelDialog(R.color.secondaryDarkColor)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testAskUserForTwoFactorAuth() {
|
||||
activity.askUserForTwoFactorAuth()
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testShowSuccessAndDismissDialog() {
|
||||
activity.showSuccessAndDismissDialog()
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnOptionsItemSelectedCaseDefault() {
|
||||
activity.onOptionsItemSelected(menuItem)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testGetMenuInflater() {
|
||||
activity.menuInflater
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testPerformLogin() {
|
||||
activity.performLogin()
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testSetContentView() {
|
||||
activity.setContentView(view, params)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,161 @@
|
|||
package fr.free.nrw.commons.auth
|
||||
|
||||
import android.accounts.Account
|
||||
import android.accounts.AccountManager
|
||||
import android.content.Context
|
||||
import fr.free.nrw.commons.TestCommonsApplication
|
||||
import fr.free.nrw.commons.kvstore.JsonKvStore
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mock
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.powermock.api.mockito.PowerMockito.`when`
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.LooperMode
|
||||
import org.wikipedia.login.LoginResult
|
||||
import java.lang.reflect.Method
|
||||
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [21], application = TestCommonsApplication::class)
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class SessionManagerUnitTests {
|
||||
|
||||
private lateinit var sessionManager: SessionManager
|
||||
private lateinit var accountManager: AccountManager
|
||||
|
||||
@Mock
|
||||
private lateinit var account: Account
|
||||
|
||||
@Mock
|
||||
private lateinit var defaultKvStore: JsonKvStore
|
||||
|
||||
@Mock
|
||||
private lateinit var loginResult: LoginResult
|
||||
|
||||
@Mock
|
||||
private lateinit var context: Context
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
accountManager = AccountManager.get(RuntimeEnvironment.application)
|
||||
shadowOf(accountManager).addAccount(account)
|
||||
sessionManager =
|
||||
SessionManager(RuntimeEnvironment.application.applicationContext, defaultKvStore)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun checkNotNull() {
|
||||
Assert.assertNotNull(sessionManager)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testRemoveAccountCaseNull() {
|
||||
val method: Method = SessionManager::class.java.getDeclaredMethod(
|
||||
"removeAccount"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(sessionManager)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testUpdateAccount() {
|
||||
`when`(loginResult.userName).thenReturn("username")
|
||||
`when`(loginResult.password).thenReturn("password")
|
||||
val method: Method = SessionManager::class.java.getDeclaredMethod(
|
||||
"updateAccount",
|
||||
LoginResult::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(sessionManager, loginResult)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testLogout() {
|
||||
sessionManager.logout()
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testDoesAccountExist() {
|
||||
sessionManager.doesAccountExist()
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testIsUserLoggedIn() {
|
||||
Assert.assertEquals(sessionManager.isUserLoggedIn, false)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testGetPreference() {
|
||||
Assert.assertEquals(sessionManager.getPreference("key"), false)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testForceLoginCaseNull() {
|
||||
sessionManager.forceLogin(null)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testForceLogin() {
|
||||
sessionManager.forceLogin(context)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testCreateAccount() {
|
||||
val method: Method = SessionManager::class.java.getDeclaredMethod(
|
||||
"createAccount",
|
||||
String::class.java,
|
||||
String::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
Assert.assertEquals(method.invoke(sessionManager, "username", "password"), true)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testSetUserLoggedIn() {
|
||||
val method: Method = SessionManager::class.java.getDeclaredMethod(
|
||||
"setUserLoggedIn",
|
||||
Boolean::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(sessionManager, true)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testGetUserName() {
|
||||
val method: Method = SessionManager::class.java.getDeclaredMethod(
|
||||
"getUserName"
|
||||
)
|
||||
method.isAccessible = true
|
||||
Assert.assertEquals(method.invoke(sessionManager), null)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testGetPassword() {
|
||||
val method: Method = SessionManager::class.java.getDeclaredMethod(
|
||||
"getPassword"
|
||||
)
|
||||
method.isAccessible = true
|
||||
Assert.assertEquals(method.invoke(sessionManager), null)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,10 +1,15 @@
|
|||
package fr.free.nrw.commons.auth
|
||||
|
||||
import android.webkit.WebView
|
||||
import fr.free.nrw.commons.TestCommonsApplication
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito.`when`
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.powermock.reflect.Whitebox
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
|
@ -16,8 +21,12 @@ class SignupActivityTest {
|
|||
|
||||
private lateinit var activity: SignupActivity
|
||||
|
||||
@Mock
|
||||
private lateinit var webView: WebView
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
activity = Robolectric.buildActivity(SignupActivity::class.java).create().get()
|
||||
}
|
||||
|
||||
|
|
@ -26,4 +35,18 @@ class SignupActivityTest {
|
|||
fun checkActivityNotNull() {
|
||||
Assert.assertNotNull(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnBackPressed() {
|
||||
activity.onBackPressed()
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnBackPressedCaseCanGoBack() {
|
||||
Whitebox.setInternalState(activity, "webView", webView)
|
||||
`when`(webView.canGoBack()).thenReturn(true)
|
||||
activity.onBackPressed()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue