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

Commit aa651fe0 authored by Hasib Prince's avatar Hasib Prince
Browse files

added unit tests for unauthorized response

parent a2f15f0c
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ class GPlayHttpClient @Inject constructor(
        const val STATUS_CODE_TIMEOUT = 408
    }

    private val okHttpClient = OkHttpClient().newBuilder()
    var okHttpClient = OkHttpClient().newBuilder()
        .retryOnConnectionFailure(false)
        .callTimeout(HTTP_TIMEOUT_IN_SECOND, TimeUnit.SECONDS)
        .followRedirects(true)
+1 −1
Original line number Diff line number Diff line
@@ -682,7 +682,7 @@ class FusedApiImplTest {

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

        val categoryListResponse =
            fusedAPIImpl.getCategoriesList(CategoryType.APPLICATION)
+132 −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

import com.aurora.gplayapi.data.models.PlayResponse
import foundation.e.apps.data.gplay.utils.GPlayHttpClient
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 testPostMapFailWhenStatus401() = runTest {
        initMocks()
        val response = gPlayHttpClient.post("http://abc.abc", mapOf(), mapOf())
        assertResponse(response)
    }

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

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

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

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

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

    @Test
    fun testPostAuthFailWhenStatus401() = 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)
        assertTrue(EventBus.events.first() is AppEvent.InvalidAuthEvent)
    }

}
+9 −0
Original line number Diff line number Diff line
package foundation.e.apps

import foundation.e.apps.data.handleNetworkResult

class NetworkHandlerTest {

    fun testHandleNetworkResultWhenStatus401() {
    }
}
 No newline at end of file
+79 −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.util

import okhttp3.Call
import okhttp3.Callback
import okhttp3.Protocol
import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody
import okhttp3.ResponseBody.Companion.toResponseBody
import okio.Timeout

class FakeCall : Call {

    var willThrow401 = false

    companion object {
        const val FAKE_URL = "https://abc.abc"
    }

    private val fakeRequest = Request.Builder().url(FAKE_URL).build()
    override fun cancel() {
        TODO("Not yet implemented")
    }

    override fun clone(): Call {
        TODO("Not yet implemented")
    }

    override fun enqueue(responseCallback: Callback) {
        TODO("Not yet implemented")
    }

    override fun execute(): Response {
        if (willThrow401) {
            return Response.Builder()
                .request(fakeRequest)
                .protocol(Protocol.HTTP_2)
                .message("")
                .code(401)
                .body("".toResponseBody())
                .build()
        }
        return Response.Builder().build()
    }

    override fun isCanceled(): Boolean {
        TODO("Not yet implemented")
    }

    override fun isExecuted(): Boolean {
        TODO("Not yet implemented")
    }

    override fun request(): Request {
        TODO("Not yet implemented")
    }

    override fun timeout(): Timeout {
        TODO("Not yet implemented")
    }
}
 No newline at end of file