Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tweak FacebookAccountProvider to support 401 responses #3145

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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