Skip to content

Commit

Permalink
update Facebook isAuthenticated logic to return true if a userId is s…
Browse files Browse the repository at this point in the history
…et for the user
  • Loading branch information
frett committed Nov 6, 2023
1 parent 259407c commit d3050f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch
import org.ccci.gto.android.common.androidx.activity.result.contract.transformInput
import org.ccci.gto.android.common.facebook.login.currentAccessTokenFlow
import org.ccci.gto.android.common.facebook.login.isAuthenticatedFlow
import org.ccci.gto.android.common.facebook.login.isExpiredFlow
import org.ccci.gto.android.common.facebook.login.refreshCurrentAccessToken
import org.ccci.gto.android.common.kotlin.coroutines.getStringFlow
import org.cru.godtools.account.AccountType
Expand Down Expand Up @@ -53,11 +54,23 @@ internal class FacebookAccountProvider @Inject constructor(
@VisibleForTesting
internal val prefs by lazy { context.getSharedPreferences(PREFS_FACEBOOK_ACCOUNT_PROVIDER, Context.MODE_PRIVATE) }

override val userId get() = accessTokenManager.currentAccessToken?.let { prefs.getString(it.PREF_USER_ID, null) }
override val isAuthenticated get() = accessTokenManager.currentAccessToken?.isExpired == false
override val userId get() = accessTokenManager.currentAccessToken?.apiUserId
override val isAuthenticated
get() = accessTokenManager.currentAccessToken?.let { !it.isExpired && it.apiUserId != null } == true
override fun userIdFlow() = accessTokenManager.currentAccessTokenFlow()
.flatMapLatest { it?.let { prefs.getStringFlow(it.PREF_USER_ID, null) } ?: flowOf(null) }
override fun isAuthenticatedFlow() = accessTokenManager.isAuthenticatedFlow()
.flatMapLatest { it?.apiUserIdFlow() ?: flowOf(null) }
override fun isAuthenticatedFlow() = accessTokenManager.currentAccessTokenFlow()
.flatMapLatest {
when {
it != null -> combine(it.isExpiredFlow(), it.apiUserIdFlow()) { isExpired, userId ->
!isExpired && userId != null
}
else -> flowOf(false)
}
}

private val AccessToken.apiUserId get() = prefs.getString(PREF_USER_ID, null)
private fun AccessToken.apiUserIdFlow() = prefs.getStringFlow(PREF_USER_ID, null)

// region Login/Logout
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import okhttp3.ResponseBody.Companion.toResponseBody
import org.ccci.gto.android.common.facebook.login.currentAccessTokenFlow
Expand All @@ -41,7 +39,6 @@ import retrofit2.Response
private const val CLASS_ACCESS_TOKEN_MANAGER_KTX = "org.ccci.gto.android.common.facebook.login.AccessTokenManagerKt"

@RunWith(AndroidJUnit4::class)
@OptIn(ExperimentalCoroutinesApi::class)
class FacebookAccountProviderTest {
private val currentAccessTokenFlow = MutableStateFlow<AccessToken?>(null)

Expand Down Expand Up @@ -70,6 +67,7 @@ class FacebookAccountProviderTest {
unmockkStatic(CLASS_ACCESS_TOKEN_MANAGER_KTX)
}

// region userId()
@Test
fun `userId()`() = runTest {
assertNull(provider.userId)
Expand All @@ -80,6 +78,7 @@ class FacebookAccountProviderTest {
provider.prefs.edit { putString(token.PREF_USER_ID, user) }
assertEquals(user, provider.userId)
}
// endregion userId()

// region userIdFlow()
@Test
Expand All @@ -89,31 +88,27 @@ class FacebookAccountProviderTest {
provider.prefs.edit { putString(token.PREF_USER_ID, user) }

provider.userIdFlow().test {
assertNull(expectMostRecentItem())
assertNull(awaitItem())

currentAccessTokenFlow.value = token
runCurrent()
assertEquals(user, expectMostRecentItem())
assertEquals(user, awaitItem())

currentAccessTokenFlow.value = null
runCurrent()
assertNull(expectMostRecentItem())
assertNull(awaitItem())
}
}

@Test
fun `userIdFlow() - Emit new userId when it changes`() = runTest {
val user = UUID.randomUUID().toString()
val token = accessToken()
currentAccessTokenFlow.value = token

provider.userIdFlow().test {
currentAccessTokenFlow.value = token
runCurrent()
assertNull(expectMostRecentItem())
assertNull(awaitItem())

provider.prefs.edit { putString(token.PREF_USER_ID, user) }
runCurrent()
assertEquals(user, expectMostRecentItem())
assertEquals(user, awaitItem())
}
}
// endregion userIdFlow()
Expand Down

0 comments on commit d3050f0

Please sign in to comment.