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

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

Merge branch '333-add_unit_test_429' into 'main'

added unit test for 429

See merge request !425
parents f352a435 ea39de9b
Loading
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -56,8 +56,8 @@ class GPlayHttpClient @Inject constructor(
        private const val HTTP_TIMEOUT_IN_SECOND = 10L
        private const val SEARCH_SUGGEST = "searchSuggest"
        private const val STATUS_CODE_OK = 200
        private const val STATUS_CODE_UNAUTHORIZED = 401
        private const val STATUS_CODE_TOO_MANY_REQUESTS = 429
        const val STATUS_CODE_UNAUTHORIZED = 401
        const val STATUS_CODE_TOO_MANY_REQUESTS = 429
        private const val URL_SUBSTRING_PURCHASE = "purchase"
        const val STATUS_CODE_TIMEOUT = 408
    }
+23 −3
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.any
import org.mockito.kotlin.eq

class PrivacyScoreRepositoryImplTest {

@@ -50,7 +51,7 @@ class PrivacyScoreRepositoryImplTest {
            _id = "113",
            status = Status.UNAVAILABLE,
            name = "Demo Three",
            package_name = "a.b.c",
            package_name = "com.test.fakePackage",
            latest_version_code = 123,
            is_pwa = true,
            permsFromExodus = listOf(),
@@ -67,7 +68,7 @@ class PrivacyScoreRepositoryImplTest {
            _id = "113",
            status = Status.UNAVAILABLE,
            name = "Demo Three",
            package_name = "a.b.c",
            package_name = "com.test.fakePackage",
            latest_version_code = 123,
            is_pwa = true,
            perms = listOf(),
@@ -83,7 +84,7 @@ class PrivacyScoreRepositoryImplTest {
            _id = "113",
            status = Status.UNAVAILABLE,
            name = "Demo Three",
            package_name = "a.b.c",
            package_name = "com.test.fakePackage",
            latest_version_code = 123,
            is_pwa = true,
            permsFromExodus = listOf(),
@@ -93,4 +94,23 @@ class PrivacyScoreRepositoryImplTest {
        val privacyScore = privacyScoreRepository.calculatePrivacyScore(application)
        Assert.assertEquals("failed to retrieve valid privacy score", 9, privacyScore)
    }

    @Test
    fun `test calculate privacyScore for the app are available in privacyScoreZero applist`() {
        val application = Application(
            _id = "113",
            status = Status.UNAVAILABLE,
            name = "Demo Three",
            package_name = "com.test.fakePackage.privacyZero",
            latest_version_code = 123,
            is_pwa = true,
            permsFromExodus = listOf(),
            perms = listOf(),
            trackers = CommonUtilsModule.LIST_OF_NULL
        )
        Mockito.`when`(blockedAppRepository.isPrivacyScoreZero(eq("com.test.fakePackage.privacyZero"))).thenReturn(true)

        val privacyScore = privacyScoreRepository.calculatePrivacyScore(application)
        Assert.assertEquals("privacy score zero", 0, privacyScore)
    }
}
+59 −11
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ package foundation.e.apps.gplay
import com.aurora.gplayapi.data.models.PlayResponse
import foundation.e.apps.data.playstore.utils.GPlayHttpClient
import foundation.e.apps.data.login.AuthObject
import foundation.e.apps.data.playstore.utils.GplayHttpRequestException
import foundation.e.apps.util.FakeCall
import foundation.e.apps.util.MainCoroutineRule
import foundation.e.apps.utils.SystemInfoProvider
@@ -71,62 +72,109 @@ class GplyHttpClientTest {

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

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

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

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

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

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

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

    private fun initMocks() {
    @Test
    fun testPostAuthFailedWhenStatus429() = runTest {
        initMocksForStatus429()
        try {
            gPlayHttpClient.postAuth(FakeCall.FAKE_URL, "".toByteArray())
        } catch (e: Exception) {
            assert429(e)
        }
    }

    @Test
    fun testGetWithMapParamsFailedWhenStatus429() = runTest {
        initMocksForStatus429()
        try {
            gPlayHttpClient.get(FakeCall.FAKE_URL, mapOf(), mapOf())
        } catch (e: Exception) {
            assert429(e)
        }
    }

    @Test
    fun testGetWithStringParamsFailedWhenStatus429() = runTest {
        initMocksForStatus429()
        try {
            gPlayHttpClient.get(FakeCall.FAKE_URL, mapOf(), "")
        } catch (e: Exception) {
            assert429(e)
        }
    }

    private fun initMocksForStatus401() {
        call.willThrow401 = true
        mockkObject(SystemInfoProvider)
        every { SystemInfoProvider.getAppBuildInfo() } returns ""
        Mockito.`when`(okHttpClient.newCall(any())).thenReturn(call)
    }
    private suspend fun assertResponse(response: PlayResponse) {

    private suspend fun assert429(e: Exception) {
        assertTrue(
            "Status429",
            e is GplayHttpRequestException && e.status == GPlayHttpClient.STATUS_CODE_TOO_MANY_REQUESTS
        )
        val event = EventBus.events.first()
        assertTrue(event is AppEvent.TooManyRequests)
    }

    private fun initMocksForStatus429() {
        call.willThrow429 = true
        mockkObject(SystemInfoProvider)
        every { SystemInfoProvider.getAppBuildInfo() } returns ""
        Mockito.`when`(okHttpClient.newCall(any())).thenReturn(call)
    }

    private suspend fun assertResponse(response: PlayResponse, statusValue: Int = 401) {
        assertFalse(response.isSuccessful)
        assertTrue(response.code == 401)
        assertTrue(response.code == statusValue)
        val event = EventBus.events.first()
        assertTrue(event is AppEvent.InvalidAuthEvent)
        assertTrue(event.data is String)
+14 −9
Original line number Diff line number Diff line
@@ -29,9 +29,10 @@ import okio.Timeout
class FakeCall : Call {

    var willThrow401 = false
    var willThrow429 = false

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

    private val fakeRequest = Request.Builder().url(FAKE_URL).build()
@@ -48,16 +49,20 @@ class FakeCall : Call {
    }

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

        if (willThrow401) {
            builder.code(401)
        } else if (willThrow429) {
            builder.code(429)
        }
        return Response.Builder().build()

        return builder.build()
    }

    override fun isCanceled(): Boolean {