diff --git a/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt b/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt index 3e23c0f91436810bbf6071a63f276892041b5cac..a7e1f3dbb9d60f8bac2834be582fbf188aaceda6 100644 --- a/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt +++ b/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt @@ -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 } diff --git a/app/src/test/java/foundation/e/apps/exodus/PrivacyScoreRepositoryImplTest.kt b/app/src/test/java/foundation/e/apps/exodus/PrivacyScoreRepositoryImplTest.kt index 1602f92b0fcb3a8001bdc002dad316f9e7723fa1..818cf22dce83b6878ed535a83c6c5bde4cef22ec 100644 --- a/app/src/test/java/foundation/e/apps/exodus/PrivacyScoreRepositoryImplTest.kt +++ b/app/src/test/java/foundation/e/apps/exodus/PrivacyScoreRepositoryImplTest.kt @@ -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) + } } diff --git a/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt b/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt index 89c5e9f81a7742c022ad8e72d79383d475cb71ac..b0217740b32fef206355b8b586f771eba58e3647 100644 --- a/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt +++ b/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt @@ -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) diff --git a/app/src/test/java/foundation/e/apps/util/FakeCall.kt b/app/src/test/java/foundation/e/apps/util/FakeCall.kt index c05e9487a7b1cc2f3953a3c8dcf4629d579c33c9..aa9cd57e59138036e85b2eccf897e5fde6f5fc2f 100644 --- a/app/src/test/java/foundation/e/apps/util/FakeCall.kt +++ b/app/src/test/java/foundation/e/apps/util/FakeCall.kt @@ -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 { + val builder = Response.Builder() + .request(fakeRequest) + .protocol(Protocol.HTTP_2) + .message("") + .code(401) + .body("".toResponseBody()) + if (willThrow401) { - return Response.Builder() - .request(fakeRequest) - .protocol(Protocol.HTTP_2) - .message("") - .code(401) - .body("".toResponseBody()) - .build() + builder.code(401) + } else if (willThrow429) { + builder.code(429) } - return Response.Builder().build() + + return builder.build() } override fun isCanceled(): Boolean {