Add WikiAccountAuthenticator Unit Tests (#4827)

This commit is contained in:
Madhur Gupta 2022-02-18 11:49:11 +05:30 committed by GitHub
parent 9205f37605
commit 52bdb56fb2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 148 additions and 0 deletions

View file

@ -0,0 +1,34 @@
package fr.free.nrw.commons.auth
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.mockito.MockitoAnnotations
import java.lang.reflect.Field
class WikiAccountAuthenticatorServiceUnitTest {
private lateinit var service: WikiAccountAuthenticatorService
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
service = WikiAccountAuthenticatorService()
service.onBind(null)
}
@Test
fun checkNotNull() {
Assert.assertNotNull(service)
}
@Test
fun testOnBindCaseNull() {
val field: Field =
WikiAccountAuthenticatorService::class.java.getDeclaredField("authenticator")
field.isAccessible = true
field.set(service, null)
Assert.assertEquals(service.onBind(null), null)
}
}

View file

@ -0,0 +1,114 @@
package fr.free.nrw.commons.auth
import android.accounts.Account
import android.accounts.AccountAuthenticatorResponse
import android.accounts.AccountManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import fr.free.nrw.commons.BuildConfig
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.MockitoAnnotations
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [21], application = TestCommonsApplication::class)
@LooperMode(LooperMode.Mode.PAUSED)
class WikiAccountAuthenticatorUnitTest {
private lateinit var context: Context
private lateinit var authenticator: WikiAccountAuthenticator
@Mock
private lateinit var response: AccountAuthenticatorResponse
@Mock
private lateinit var account: Account
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
context = RuntimeEnvironment.application.applicationContext
authenticator = WikiAccountAuthenticator(context)
}
@Test
fun checkNotNull() {
Assert.assertNotNull(authenticator)
}
@Test
fun testEditProperties() {
val bundle: Bundle = authenticator.editProperties(response, "test")
Assert.assertEquals(bundle.getString("test"), "editProperties")
}
@Test
fun testAddAccountCaseNotSupportedType() {
val bundle: Bundle = authenticator.addAccount(response, "test", null, null, null)
Assert.assertEquals(bundle.getString("test"), "addAccount")
}
@Test
fun testAddAccountCaseSupportedType() {
val bundle: Bundle =
authenticator.addAccount(response, BuildConfig.ACCOUNT_TYPE, null, null, null)
val intent: Intent? = bundle.getParcelable(AccountManager.KEY_INTENT)
Assert.assertEquals(
intent?.extras!![AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE],
response
)
}
@Test
fun testConfirmCredentials() {
val bundle: Bundle = authenticator.confirmCredentials(response, account, null)
Assert.assertEquals(bundle.getString("test"), "confirmCredentials")
}
@Test
fun testGetAuthToken() {
val bundle: Bundle = authenticator.getAuthToken(response, account, "", null)
Assert.assertEquals(bundle.getString("test"), "getAuthToken")
}
@Test
fun testGetAuthTokenLabelCaseNull() {
Assert.assertEquals(authenticator.getAuthTokenLabel(""), null)
}
@Test
fun testGetAuthTokenLabelCaseNonNull() {
Assert.assertEquals(
authenticator.getAuthTokenLabel(BuildConfig.ACCOUNT_TYPE),
AccountUtil.AUTH_TOKEN_TYPE
)
}
@Test
fun testUpdateCredentials() {
val bundle: Bundle? = authenticator.updateCredentials(response, account, null, null)
Assert.assertEquals(bundle?.getString("test"), "updateCredentials")
}
@Test
fun testHasFeatures() {
val bundle: Bundle? = authenticator.hasFeatures(response, account, arrayOf(""))
Assert.assertEquals(bundle?.getBoolean(AccountManager.KEY_BOOLEAN_RESULT), false)
}
@Test
fun testGetAccountRemovalAllowed() {
val bundle: Bundle? = authenticator.getAccountRemovalAllowed(response, account)
Assert.assertEquals(bundle?.getBoolean(AccountManager.KEY_BOOLEAN_RESULT), true)
}
}