Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 41733e93 authored by Hasib Prince's avatar Hasib Prince
Browse files

Merge branch '1540-unit_test_token_401' into 'main'

1540 unit test token 401

See merge request !369
parents 0d3efbaf 9aab8495
Loading
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

package foundation.e.apps.data.gplay.utils

import androidx.annotation.VisibleForTesting
import com.aurora.gplayapi.data.models.PlayResponse
import com.aurora.gplayapi.network.IHttpClient
import foundation.e.apps.data.login.AuthObject
@@ -61,7 +62,8 @@ class GPlayHttpClient @Inject constructor(
        const val STATUS_CODE_TIMEOUT = 408
    }

    private val okHttpClient = OkHttpClient().newBuilder()
    @VisibleForTesting
    var okHttpClient = OkHttpClient().newBuilder()
        .retryOnConnectionFailure(false)
        .callTimeout(HTTP_TIMEOUT_IN_SECOND, TimeUnit.SECONDS)
        .followRedirects(true)
+4 −2
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.apps
package foundation.e.apps.fused

import android.content.Context
import android.text.format.Formatter
@@ -25,6 +25,8 @@ import com.aurora.gplayapi.data.models.App
import com.aurora.gplayapi.data.models.AuthData
import com.aurora.gplayapi.data.models.Category
import com.aurora.gplayapi.data.models.SearchBundle
import foundation.e.apps.FakePreferenceModule
import foundation.e.apps.R
import foundation.e.apps.data.cleanapk.data.categories.Categories
import foundation.e.apps.data.cleanapk.data.search.Search
import foundation.e.apps.data.cleanapk.repositories.CleanApkRepository
@@ -651,7 +653,7 @@ class FusedApiImplTest {

        Mockito.`when`(
            gPlayAPIRepository.getCategories(CategoryType.APPLICATION)
        ).thenThrow(RuntimeException())
        ).thenThrow()

        val categoryListResponse =
            fusedAPIImpl.getCategoriesList(CategoryType.APPLICATION)
+1 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.apps
package foundation.e.apps.fused

import foundation.e.apps.data.fused.FusedAPIRepository
import foundation.e.apps.data.fused.FusedApiImpl
+135 −0
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2023
 * Apps  Quickly and easily install Android apps onto your device!
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.apps.gplay

import com.aurora.gplayapi.data.models.PlayResponse
import foundation.e.apps.data.gplay.utils.GPlayHttpClient
import foundation.e.apps.data.login.AuthObject
import foundation.e.apps.util.FakeCall
import foundation.e.apps.util.MainCoroutineRule
import foundation.e.apps.utils.SystemInfoProvider
import foundation.e.apps.utils.eventBus.AppEvent
import foundation.e.apps.utils.eventBus.EventBus
import io.mockk.every
import io.mockk.mockkObject
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.RequestBody.Companion.toRequestBody
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.any
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue

@OptIn(ExperimentalCoroutinesApi::class)
class GplyHttpClientTest {

    @Mock
    private lateinit var cache: Cache

    @Mock
    private lateinit var okHttpClient: OkHttpClient

    private lateinit var call: FakeCall

    private lateinit var gPlayHttpClient: GPlayHttpClient

    @OptIn(ExperimentalCoroutinesApi::class)
    @get:Rule
    val coroutineTestRule = MainCoroutineRule()

    @Before
    fun setup() {
        MockitoAnnotations.openMocks(this)
        gPlayHttpClient = GPlayHttpClient(cache)
        gPlayHttpClient.okHttpClient = this.okHttpClient
        call = FakeCall()
    }

    @Test
    fun testPostWithMapFailedWhenStatus401() = runTest {
        initMocks()
        val response = gPlayHttpClient.post("http://abc.abc", mapOf(), mapOf())
        assertResponse(response)
    }

    @Test
    fun testPostWithRequestBodyFailedWhenStatus401() = runTest {
        initMocks()
        val response = gPlayHttpClient.post("http://abc.abc", mapOf(), "".toRequestBody())
        assertResponse(response)
    }

    @Test
    fun testPostWithByteArrayFailedWhenStatus401() = runTest {
        initMocks()
        val response = gPlayHttpClient.post("http://abc.abc", mapOf(), "".toByteArray())
        assertResponse(response)
    }

    @Test
    fun testGetWithoutParamsFailedWhenStatus401() = runTest {
        initMocks()
        val response = gPlayHttpClient.get(FakeCall.FAKE_URL, mapOf())
        assertResponse(response)
    }

    @Test
    fun testGetWithStringParamsFailedWhenStatus401() = runTest {
        initMocks()
        val response = gPlayHttpClient.get(FakeCall.FAKE_URL, mapOf(), "")
        assertResponse(response)
    }

    @Test
    fun testGetWithMapParamsFailedWhenStatus401() = runTest {
        initMocks()
        val response = gPlayHttpClient.get(FakeCall.FAKE_URL, mapOf(), mapOf())
        assertResponse(response)
    }

    @Test
    fun testPostAuthFailedWhenStatus401() = runTest {
        initMocks()
        val response = gPlayHttpClient.postAuth("http://abc.abc", "".toByteArray())
        assertResponse(response)
    }

    private fun initMocks() {
        call.willThrow401 = true
        mockkObject(SystemInfoProvider)
        every { SystemInfoProvider.getAppBuildInfo() } returns ""
        Mockito.`when`(okHttpClient.newCall(any())).thenReturn(call)
    }
    private suspend fun assertResponse(response: PlayResponse) {
        assertFalse(response.isSuccessful)
        assertTrue(response.code == 401)
        val event = EventBus.events.first()
        assertTrue(event is AppEvent.InvalidAuthEvent)
        assertTrue(event.data is String)
        assertTrue(event.data == AuthObject.GPlayAuth::class.java.simpleName)
    }
}
+70 −0
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2023
 * Apps  Quickly and easily install Android apps onto your device!
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.apps.login

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.aurora.gplayapi.data.models.AuthData
import foundation.e.apps.data.ResultSupreme
import foundation.e.apps.data.enums.User
import foundation.e.apps.data.login.AuthObject
import foundation.e.apps.data.login.LoginSourceRepository
import foundation.e.apps.data.login.LoginViewModel
import okhttp3.Cache
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.Mock
import org.mockito.MockitoAnnotations

class LoginViewModelTest {

    @Mock
    private lateinit var loginSourceRepository: LoginSourceRepository
    @Mock
    private lateinit var cache: Cache

    private lateinit var loginViewModel: LoginViewModel

    @Suppress("unused")
    @get:Rule
    val instantTaskExecutorRule: InstantTaskExecutorRule = InstantTaskExecutorRule()

    @Before
    fun setup() {
        MockitoAnnotations.openMocks(this)
        loginViewModel = LoginViewModel(loginSourceRepository, cache)
    }

    @Test
    fun testMarkInvalidAuthObject() {
        val authObjectList = mutableListOf<AuthObject>(
            AuthObject.GPlayAuth(
                ResultSupreme.Success(AuthData("aa@aa.com", "feri4234")), User.GOOGLE
            )
        )
        loginViewModel.authObjects.value = authObjectList

        loginViewModel.markInvalidAuthObject(AuthObject.GPlayAuth::class.java.simpleName)
        val currentAuthObjectList = loginViewModel.authObjects.value as List<AuthObject>
        val invalidGplayAuth = currentAuthObjectList.find { it is AuthObject.GPlayAuth }

        assert(invalidGplayAuth != null)
        assert((invalidGplayAuth as AuthObject.GPlayAuth).result.isUnknownError())
    }
}
 No newline at end of file
Loading