Skip to content

Commit

Permalink
tweak FacebookAccountProvider to support 401 responses
Browse files Browse the repository at this point in the history
  • Loading branch information
frett committed Oct 6, 2023
1 parent 97d9019 commit 800fb51
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,24 @@ internal class FacebookAccountProvider @Inject constructor(
// endregion Login/Logout

override suspend fun authenticateWithMobileContentApi(): AuthToken? {
var accessToken = accessTokenManager.currentAccessToken
var resp = authenticateWithMobileContentApi(accessToken)
var accessToken = accessTokenManager.currentAccessToken ?: return null
var resp = accessToken.authenticateWithMobileContentApi()

// try refreshing the access token if the API rejected it
if (resp?.code() == 400) {
if (!resp.isSuccessful) {
accessToken = try {
accessTokenManager.refreshCurrentAccessToken()
} catch (e: FacebookException) {
null
}
resp = authenticateWithMobileContentApi(accessToken)
} ?: return null
resp = accessToken.authenticateWithMobileContentApi()
}

val token = resp?.takeIf { it.isSuccessful }?.body()?.takeUnless { it.hasErrors }?.dataSingle
if (accessToken != null && token != null) prefs.edit { putString(PREF_USER_ID(accessToken), token.userId) }
return token
return resp.takeIf { it.isSuccessful }
?.body()?.takeUnless { it.hasErrors }?.dataSingle
?.also { prefs.edit { putString(PREF_USER_ID(accessToken), it.userId) } }
}

private suspend fun authenticateWithMobileContentApi(accessToken: AccessToken?) =
accessToken?.token?.let { authApi.authenticate(AuthToken.Request(fbAccessToken = it)) }
private suspend fun AccessToken.authenticateWithMobileContentApi() =
authApi.authenticate(AuthToken.Request(fbAccessToken = token))
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ class FacebookAccountProviderTest {
}

@Test
fun `authenticateWithMobileContentApi() - Error - HTTP 400 - Refresh successful`() = runTest {
fun `authenticateWithMobileContentApi() - Error - Refresh successful`() = runTest {
val accessToken = accessToken()
val accessToken2 = accessToken()
val token = AuthToken(userId = UUID.randomUUID().toString())
currentAccessTokenFlow.value = accessToken
coEvery { api.authenticate(AuthToken.Request(fbAccessToken = accessToken.token)) }
.returns(Response.error(400, "".toResponseBody()))
.returns(Response.error(401, "".toResponseBody()))
coEvery { accessTokenManager.refreshCurrentAccessToken() } returns accessToken2
coEvery { api.authenticate(AuthToken.Request(fbAccessToken = accessToken2.token)) }
.returns(Response.success(JsonApiObject.of(token)))
Expand All @@ -161,10 +161,10 @@ class FacebookAccountProviderTest {
}

@Test
fun `authenticateWithMobileContentApi() - Error - HTTP 400 - Refresh doesn't return access_token`() = runTest {
fun `authenticateWithMobileContentApi() - Error - Refresh doesn't return access_token`() = runTest {
val accessToken = accessToken()
currentAccessTokenFlow.value = accessToken
coEvery { api.authenticate(any()) } returns Response.error(400, "".toResponseBody())
coEvery { api.authenticate(any()) } returns Response.error(401, "".toResponseBody())
coEvery { accessTokenManager.refreshCurrentAccessToken() } returns null

assertNull(provider.authenticateWithMobileContentApi())
Expand All @@ -175,10 +175,10 @@ class FacebookAccountProviderTest {
}

@Test
fun `authenticateWithMobileContentApi() - Error - HTTP 400 - Refresh throws exception`() = runTest {
fun `authenticateWithMobileContentApi() - Error - Refresh throws exception`() = runTest {
val accessToken = accessToken()
currentAccessTokenFlow.value = accessToken
coEvery { api.authenticate(any()) } returns Response.error(400, "".toResponseBody())
coEvery { api.authenticate(any()) } returns Response.error(401, "".toResponseBody())
coEvery { accessTokenManager.refreshCurrentAccessToken() } throws FacebookException()

assertNull(provider.authenticateWithMobileContentApi())
Expand Down

0 comments on commit 800fb51

Please sign in to comment.