diff --git a/app/src/test/kotlin/fr/free/nrw/commons/auth/WikiAccountAuthenticatorServiceUnitTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/auth/WikiAccountAuthenticatorServiceUnitTest.kt new file mode 100644 index 000000000..1b2a07a06 --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/auth/WikiAccountAuthenticatorServiceUnitTest.kt @@ -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) + } + +} \ No newline at end of file diff --git a/app/src/test/kotlin/fr/free/nrw/commons/auth/WikiAccountAuthenticatorUnitTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/auth/WikiAccountAuthenticatorUnitTest.kt new file mode 100644 index 000000000..44cb9b000 --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/auth/WikiAccountAuthenticatorUnitTest.kt @@ -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) + } + +} \ No newline at end of file