appstore: inject Play auth building/dispensing for offline tests
- Commit
20eeca2a28a94d3b722a2bd1e1c9d1b5fead63f0- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-14 23:09 UTC (21 days ago)
- Parents
e25623f
The Play auth tests asserted that dispensing and the Google checkin fail, which is only true with no network: on a host whose proxy reaches Google and the Aurora dispenser, real anonymous accounts are dispensed and the 'must fail offline' assertions invert (live auth tokens in the failure output). AnonymousAuthRepository now takes an injectable authBuilder (the checkin; default is gplayapi AuthHelper.build, unchanged) and is open with an open ensureAuthData; PlayRepository takes the AnonymousAuthRepository as a constructor parameter (default unchanged). Tests inject a checkin that throws IOException and a dispenser that always fails, so buildAuthData/ensureAuthData, ensureAuth and the withApi/withFreshApi cycling paths fail at exactly the calls they fail at on an offline device. Assertions unchanged. Verified: targeted :appstore:testDevUnitTest run (see final full-suite run).
Diff
appstore/src/main/java/com/vayunmathur/appstore/data/play/AnonymousAuthRepository.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/play/AnonymousAuthRepository.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/play/AnonymousAuthRepository.ktindex f5fbbf86fc..57685be454 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/play/AnonymousAuthRepository.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/play/AnonymousAuthRepository.kt@@ -31,7 +31,28 @@ import kotlin.random.Random * exponential backoff and jitter rather than surfaced immediately, so a brief * throttle no longer leaves Play access broken until the app restarts. */-class AnonymousAuthRepository {+open class AnonymousAuthRepository(+ /**+ * Builds the Play session from dispensed credentials (the Google+ * checkin). Injectable so tests can fail the checkin deterministically+ * instead of depending on the host having no network; production uses+ * gplayapi's [AuthHelper] unchanged.+ */+ private val authBuilder: suspend (+ email: String,+ token: String,+ deviceProps: Properties,+ ) -> AuthData = { email, token, props ->+ AuthHelper.build(+ email = email,+ token = token,+ tokenType = AuthHelper.Token.AUTH,+ isAnonymous = true,+ properties = props,+ locale = java.util.Locale.getDefault(),+ )+ },+) { companion object { private const val TAG = "AnonAuthRepo"@@ -180,17 +201,7 @@ class AnonymousAuthRepository { deviceProps: Properties ): Result<AuthData> = withContext(Dispatchers.IO) { try {- val locale = java.util.Locale.getDefault()- val authData = AuthHelper- .build(- email = email,- token = token,- tokenType = AuthHelper.Token.AUTH,- isAnonymous = true,- properties = deviceProps,- locale = locale- )- Result.success(authData)+ Result.success(authBuilder(email, token, deviceProps)) } catch (expected: IllegalStateException) { Log.status(TAG, "buildAuthData failed: ${expected.message}", expected) Result.failure(expected)@@ -208,7 +219,7 @@ class AnonymousAuthRepository { * that got throttled at the same moment would otherwise retry in lockstep * and throttle each other again. */- suspend fun ensureAuthData(+ open suspend fun ensureAuthData( deviceProps: Properties, dispenserUrls: List<String> = FALLBACK_DISPENSERS ): Result<AuthData> = withContext(Dispatchers.IO) {
appstore/src/main/java/com/vayunmathur/appstore/data/play/PlayRepository.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/play/PlayRepository.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/play/PlayRepository.ktindex 7f005d7148..7d0985b71b 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/play/PlayRepository.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/play/PlayRepository.kt@@ -56,9 +56,10 @@ sealed interface PlayAuthState { * knowing that an account has to be dispensed first, and the "cycle a dead account and * try again" rule exists in exactly one place. */-open class PlayRepository(private val context: Context) {-- private val anonAuthRepo = AnonymousAuthRepository()+open class PlayRepository(+ private val context: Context,+ private val anonAuthRepo: AnonymousAuthRepository = AnonymousAuthRepository(),+) { private val httpClient = PlayHttpClient() private val json = Json { ignoreUnknownKeys = true; isLenient = true }
appstore/src/test/java/com/vayunmathur/appstore/data/play/AnonymousAuthLoopbackTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/data/play/AnonymousAuthLoopbackTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/data/play/AnonymousAuthLoopbackTest.ktindex dcbd3922a8..74f80e25d6 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/data/play/AnonymousAuthLoopbackTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/data/play/AnonymousAuthLoopbackTest.kt@@ -31,6 +31,13 @@ class AnonymousAuthLoopbackTest { private val lastBody = AtomicReference<String?>(null) private val repo = AnonymousAuthRepository() + // Hermetic checkin failure: the Google checkin is replaced by a+ // failing double so the tests below do not depend on the host+ // having no network route to Google.+ private val offlineRepo = AnonymousAuthRepository { _, _, _ ->+ throw java.io.IOException("checkin unreachable (offline test double)")+ }+ private fun props(): Properties = DeviceInfoProvider.auroraProfile( ApplicationProvider.getApplicationContext<Context>(), ).apply {@@ -115,7 +122,7 @@ class AnonymousAuthLoopbackTest { // then performs a device checkin against Google, which is // unreachable here and surfaces as an IOException. assertFailsWith<java.io.IOException> {- repo.ensureAuthData(props(), listOf("$base/auth"))+ offlineRepo.ensureAuthData(props(), listOf("$base/auth")) } Unit }@@ -132,7 +139,7 @@ class AnonymousAuthLoopbackTest { @Test fun `buildAuthData needs the google checkin`() = runBlocking { assertFailsWith<java.io.IOException> {- repo.buildAuthData("[email protected]", "tok-123", props())+ offlineRepo.buildAuthData("[email protected]", "tok-123", props()) } Unit }
appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayRepositoryAuthFailureTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayRepositoryAuthFailureTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayRepositoryAuthFailureTest.ktindex 00ee42ea87..3d3a3492f0 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayRepositoryAuthFailureTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayRepositoryAuthFailureTest.kt@@ -20,9 +20,22 @@ class PlayRepositoryAuthFailureTest { private val context: Context get() = ApplicationProvider.getApplicationContext() + // Hermetic offline: dispensing always fails, whatever network the+ // host running the tests happens to have.+ private fun repo() = PlayRepository(+ context,+ object : AnonymousAuthRepository() {+ override suspend fun ensureAuthData(+ deviceProps: java.util.Properties,+ dispenserUrls: List<String>,+ ): Result<com.aurora.gplayapi.data.models.AuthData> =+ Result.failure(java.io.IOException("offline test double"))+ },+ )+ @Test fun `ensureAuth fails and records an error state when the dispenser is unreachable`() = runBlocking {- val repo = PlayRepository(context)+ val repo = repo() val result = repo.ensureAuth() assertTrue(result.isFailure, "auth must fail with no network: $result") assertTrue(@@ -33,7 +46,7 @@ class PlayRepositoryAuthFailureTest { @Test fun `fresh-session reads answer empty when auth cannot be dispensed`() = runBlocking {- val repo = PlayRepository(context)+ val repo = repo() assertTrue(repo.details(listOf("com.example.a", "com.example.b")).isEmpty()) assertTrue(repo.topChart().isEmpty()) assertTrue(repo.homeClusters().isEmpty())@@ -41,7 +54,7 @@ class PlayRepositoryAuthFailureTest { @Test fun `purchase throws when no session can be dispensed`() = runBlocking {- val repo = PlayRepository(context)+ val repo = repo() assertFailsWith<Exception> { repo.purchase("com.example.a", versionCode = 1L, offerType = 0, certHash = null) }
appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayWithApiTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayWithApiTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayWithApiTest.ktindex 51735377bd..027e1bf7bf 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayWithApiTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayWithApiTest.kt@@ -36,8 +36,21 @@ class PlayWithApiTest { ), ) + // Hermetic offline: dispensing always fails, whatever network the+ // host running the tests happens to have.+ private fun offlinePlayRepo() = PlayRepository(+ context,+ object : AnonymousAuthRepository() {+ override suspend fun ensureAuthData(+ deviceProps: java.util.Properties,+ dispenserUrls: List<String>,+ ): Result<AuthData> =+ Result.failure(IOException("offline test double"))+ },+ )+ private fun repoWithSession(): PlayRepository {- val repo = PlayRepository(context)+ val repo = offlinePlayRepo() repo.cachedAuthData = authData() repo.dispensedAt = System.currentTimeMillis() return repo@@ -45,7 +58,7 @@ class PlayWithApiTest { @Test fun `withApi without a session returns null and never runs the block`() = runBlocking {- val repo = PlayRepository(context)+ val repo = offlinePlayRepo() var ran = false val result = repo.withApi { ran = true; "value" } assertNull(result)@@ -75,7 +88,7 @@ class PlayWithApiTest { @Test fun `withFreshApi answers null when no session can be dispensed`() = runBlocking {- val repo = PlayRepository(context)+ val repo = offlinePlayRepo() var ran = false val result = repo.withFreshApi { ran = true; "value" } assertNull(result)@@ -84,7 +97,7 @@ class PlayWithApiTest { @Test fun `detailsOrNull of no packages is an answered empty lookup`() = runBlocking {- val repo = PlayRepository(context)+ val repo = offlinePlayRepo() assertEquals(emptyList(), repo.detailsOrNull(emptyList())) } }