Skip to content

Commit

Permalink
fix purchase error when existing access token is invalidated in BE
Browse files Browse the repository at this point in the history
  • Loading branch information
lmac012 committed Feb 26, 2025
1 parent e1692af commit d5315e0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -768,10 +768,10 @@ class RealSubscriptionsManager @Inject constructor(
isSignedInV2() -> try {
refreshSubscriptionData()
} catch (e: HttpException) {
if (e.code() in listOf(400, 404)) {
// expected if this is a first ever purchase using this account - ignore
} else {
throw e
when (e.code()) {
400, 404 -> {} // expected if this is a first ever purchase using this account - ignore
401 -> signOut() // access token was rejected even though it's not expired - can happen if the account was removed from BE
else -> throw e
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.ResponseBody.Companion.toResponseBody
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
Expand Down Expand Up @@ -311,6 +312,26 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
assertNull(authRepository.getAccessToken())
}

@Test
fun whenPurchaseFlowIfUserIsSignedInAndSubscriptionFailsWith401ThenSignOutAndCreateNewAccount() = runTest {
givenUserIsSignedIn(accountExternalId = "5678")
givenSubscriptionFails(httpResponseCode = 401)
givenCreateAccountSucceeds()
val accountExternalId = authDataStore.externalId

subscriptionsManager.purchase(mock(), planId = "")

if (authApiV2Enabled) {
verify(authClient).authorize(any())
verify(authClient).createAccount(any())
verify(authClient).getTokens(any(), any(), any())
} else {
verify(authService).createAccount(any())
}

assertNotEquals(accountExternalId, authDataStore.externalId)
}

@Test
fun whenPurchaseFlowIfUserNotSignedInAndNotPurchaseStoredThenCreateAccount() = runTest {
givenUserIsNotSignedIn()
Expand Down Expand Up @@ -1381,7 +1402,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
authDataStore.refreshTokenV2ExpiresAt = null
}

private fun givenUserIsSignedIn(useAuthV2: Boolean = authApiV2Enabled) {
private fun givenUserIsSignedIn(useAuthV2: Boolean = authApiV2Enabled, accountExternalId: String = "1234") {
if (useAuthV2) {
authDataStore.accessTokenV2 = FAKE_ACCESS_TOKEN_V2
authDataStore.accessTokenV2ExpiresAt = timeProvider.currentTime + Duration.ofHours(4)
Expand All @@ -1391,7 +1412,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
authDataStore.accessToken = "accessToken"
authDataStore.authToken = "authToken"
}
authDataStore.externalId = "1234"
authDataStore.externalId = accountExternalId
}

private suspend fun givenCreateAccountFails() {
Expand Down

0 comments on commit d5315e0

Please sign in to comment.