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

Commit 69045348 authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

refac:3870: Add unit tests.

parent 2bc6d088
Loading
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ package foundation.e.apps.data.playstore

import com.aurora.gplayapi.data.models.AuthData
import com.aurora.gplayapi.helpers.AuthHelper
import foundation.e.apps.data.login.exceptions.GPlayException
import foundation.e.apps.data.playstore.utils.AC2DMUtil
import foundation.e.apps.data.playstore.utils.GPlayHttpClient
import okhttp3.RequestBody.Companion.toRequestBody
@@ -61,7 +62,7 @@ class GoogleLoginDataSource @Inject constructor(
         * https://gitlab.com/AuroraOSS/gplayapi/-/blob/master/src/main/java/com/aurora/gplayapi/data/models/PlayResponse.kt
         */
        if (error != "No Error") {
            throw Exception(error)
            throw GPlayException(false, error)
        }
        return aasToken
    }
+9 −19
Original line number Diff line number Diff line
@@ -47,9 +47,7 @@ import foundation.e.apps.data.application.utils.toApplication
import foundation.e.apps.data.enums.Source
import foundation.e.apps.data.enums.User
import foundation.e.apps.data.handleNetworkResult
import foundation.e.apps.data.playstore.TokenDispenserDataSource
import foundation.e.apps.data.playstore.utils.GPlayHttpClient
import foundation.e.apps.data.playstore.GoogleLoginDataSource
import foundation.e.apps.data.playstore.utils.GplayHttpRequestException
import foundation.e.apps.data.preference.AppLoungeDataStore
import foundation.e.apps.utils.SystemInfoProvider
@@ -63,7 +61,7 @@ import java.util.Properties
import javax.inject.Inject
import com.aurora.gplayapi.data.models.App as GplayApp

@Suppress("TooManyFunctions")
@Suppress("TooManyFunctions", "LongParameterList")
class PlayStoreRepository @Inject constructor(
    @ApplicationContext private val context: Context,
    private val gPlayHttpClient: GPlayHttpClient,
@@ -75,21 +73,20 @@ class PlayStoreRepository @Inject constructor(
    private val nativeDeviceProperty: Properties,
    private val json: Json,
) : StoreRepository {


    suspend fun updateToken(): AuthData {
        val userType = appLoungeDataStore.getUserType()
        val userType = appLoungeDataStore.getUser()

        val rawAuthToken = when (userType) {
            User.GOOGLE -> fetchGoogleLoginToken()
            User.ANONYMOUS -> tokenDispenserDataSource.fetchGToken(nativeDeviceProperty)
            else -> throw Exception("User type not ANONYMOUS or GOOGLE") // TODO 20251208 <- better handle this case ?
            else -> error("User type not ANONYMOUS or GOOGLE")
        }

        val authData = formatAuthData(rawAuthToken)

        // Force locale to be the one configured on the device. But didn't we expect to have the one of the Google Account here ?
        authData.locale =  context.resources.configuration.locales[0] //nativeDeviceProperty.getProperty("Locales").split(",")[0]
        // Force locale to be the one configured on the device.
        // But didn't we expect to have the one of the Google Account here ?
        authData.locale = context.resources.configuration.locales[0]

        appLoungeDataStore.saveAuthData(authData)

@@ -106,12 +103,6 @@ class PlayStoreRepository @Inject constructor(
         */
        if (aasToken.isBlank()) {
            aasToken = googleLoginDataSource.getAasToken(email, appLoungeDataStore.oauthToken.first())

            if (aasToken.isBlank()) {
                // TODO 20251208 : Unit Test this case, improve exception
                throw Exception("Fetched AAS Token is blank")
            }

            appLoungeDataStore.saveAasToken(aasToken)
        }

@@ -127,7 +118,6 @@ class PlayStoreRepository @Inject constructor(
        return json.decodeFromString<AuthData>(localAuthDataJson)
    }


    override suspend fun getHomeScreenData(list: MutableList<Home>): List<Home> {
        val homeScreenData = mutableMapOf<String, List<Application>>()
        val homeElements = createTopChartElements()
+14 −11
Original line number Diff line number Diff line
@@ -3,7 +3,9 @@ package foundation.e.apps.data.playstore
import com.aurora.gplayapi.data.models.AuthData
import com.aurora.gplayapi.helpers.AuthHelper
import foundation.e.apps.data.login.Auth
import foundation.e.apps.data.login.exceptions.GPlayException
import foundation.e.apps.data.playstore.utils.GPlayHttpClient
import foundation.e.apps.data.playstore.utils.GPlayHttpClient.Companion.STATUS_CODE_OK
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
@@ -29,16 +31,17 @@ class TokenDispenserDataSource @Inject constructor (
    suspend fun fetchGToken(nativeDeviceProperty: Properties): AuthData {
        return withContext(Dispatchers.IO) {
            val response = gPlayHttpClient.postAuth(
                tokenUrl, json.encodeToString(nativeDeviceProperty).toByteArray()
                tokenUrl,
                json.encodeToString(nativeDeviceProperty).toByteArray()
            )
            if (response.code != 200 || !response.isSuccessful) {
                throw Exception(
            if (response.code != STATUS_CODE_OK || !response.isSuccessful) {
                throw GPlayException(
                    false,
                    "Error fetching Anonymous credentials\n" +
                        "Network code: ${response.code}\n" +
                        "Success: ${response.isSuccessful}" +
                        response.errorString.run {
                                if (isNotBlank()) "\nError message: $this"
                                else ""
                            if (isNotBlank()) "\nError message: $this" else ""
                        }
                )
            } else {
+1 −1
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ class GPlayHttpClient @Inject constructor(
        private const val HTTP_METHOD_POST = "POST"
        private const val HTTP_METHOD_GET = "GET"
        private const val SEARCH_SUGGEST = "searchSuggest"
        private const val STATUS_CODE_OK = 200
        const val STATUS_CODE_OK = 200
        const val STATUS_CODE_UNAUTHORIZED = 401
        const val STATUS_CODE_TOO_MANY_REQUESTS = 429
        private const val URL_SUBSTRING_PURCHASE = "purchase"
+2 −2
Original line number Diff line number Diff line
@@ -71,7 +71,7 @@ class AppLoungeDataStore @Inject constructor(
    private val TOCSTATUS = booleanPreferencesKey("tocStatus")
    private val TOSVERSION = stringPreferencesKey("tosversion")

    val authData = context.dataStore.data.map { it[AUTHDATA] ?: "" }
    val rawAuthData = context.dataStore.data.map { it[AUTHDATA] ?: "" }
    val emailData = context.dataStore.data.map { it[EMAIL] ?: "" }
    val oauthToken = context.dataStore.data.map { it[OAUTHTOKEN] ?: "" }
    val aasToken = context.dataStore.data.map { it[AASTOKEN] ?: "" }
@@ -90,7 +90,7 @@ class AppLoungeDataStore @Inject constructor(
    }

    fun getAuthData(): AuthData {
        val authData = authData.getSync()
        val authData = rawAuthData.getSync()
        return if (authData.isEmpty()) {
            AuthData("", "")
        } else {
Loading