Skip to content

Commit

Permalink
Refactor pat to access token (#3648)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/488551667048375/1205697780079110/f

### Description
See task

### Steps to test this PR
See task
  • Loading branch information
marcosholgado authored Oct 11, 2023
1 parent f2eaf2b commit 1624c56
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import com.duckduckgo.networkprotection.subscription.NetpSubscriptionManager.Net
import com.duckduckgo.networkprotection.subscription.NetpSubscriptionManager.NetpAuthorizationStatus.Success
import com.duckduckgo.networkprotection.subscription.NetpSubscriptionManager.NetpAuthorizationStatus.UnableToAuthorize
import com.duckduckgo.networkprotection.subscription.NetpSubscriptionManager.NetpAuthorizationStatus.Unknown
import com.duckduckgo.subscriptions.api.PatResult
import com.duckduckgo.subscriptions.api.Subscriptions
import com.squareup.anvil.annotations.ContributesBinding
import javax.inject.Inject
Expand Down Expand Up @@ -64,9 +63,9 @@ class RealNetpSubscriptionManager @Inject constructor(
override suspend fun authorize() {
withContext(dispatcherProvider.io()) {
try {
val pat = subscriptions.getPAT()
if (pat is PatResult.Success) {
service.authorize(NetPAuthorizeRequest(pat.pat)).also {
val accessToken = subscriptions.getAccessToken()
if (accessToken != null) {
service.authorize(NetPAuthorizeRequest(accessToken)).also {
netPWaitlistRepository.setAuthenticationToken(it.token)
logcat { "Netp auth: Token received" }
netPWaitlistManager.upsertState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,15 @@ package com.duckduckgo.subscriptions.api
interface Subscriptions {

/**
* This method returns a sealed class with the long-lived PAT for the authenticated user or an error if it doesn't exist
* This method returns a [String] with the access token for the authenticated user or [null] if it doesn't exist
* or any errors arise.
* @return [PatResult]
* @return [String]
*/
suspend fun getPAT(): PatResult
suspend fun getAccessToken(): String?

/**
* This method returns a [true] if a given [product] can be found in the entitlements list or [false] otherwise
* @return [Boolean]
*/
suspend fun hasEntitlement(product: String): Boolean
}

/**
* Sealed class to be return when a PAT is requests. It could return either a [Success] with the PAT or [Failure] with
* an error message
*/
sealed class PatResult {
data class Success(val pat: String) : PatResult()
data class Failure(val message: String) : PatResult()
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.duckduckgo.subscriptions.impl

import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.subscriptions.api.PatResult
import com.duckduckgo.subscriptions.api.Subscriptions
import com.squareup.anvil.annotations.ContributesBinding
import javax.inject.Inject
Expand All @@ -26,10 +25,10 @@ import javax.inject.Inject
class RealSubscriptions @Inject constructor(
private val subscriptionsManager: SubscriptionsManager,
) : Subscriptions {
override suspend fun getPAT(): PatResult {
override suspend fun getAccessToken(): String? {
return when (val result = subscriptionsManager.getAccessToken()) {
is AccessToken.Success -> PatResult.Success(result.accessToken)
is AccessToken.Failure -> PatResult.Failure(result.message)
is AccessToken.Success -> result.accessToken
is AccessToken.Failure -> null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package com.duckduckgo.subscriptions.impl

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.duckduckgo.app.CoroutineTestRule
import com.duckduckgo.subscriptions.api.PatResult.Failure
import com.duckduckgo.subscriptions.api.PatResult.Success
import com.duckduckgo.subscriptions.impl.auth.Entitlement
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
Expand Down Expand Up @@ -47,19 +45,16 @@ class RealSubscriptionsTest {
}

@Test
fun whenSubscriptionDataSucceedsThenReturnSuccess() = runTest {
fun whenSubscriptionDataSucceedsThenReturnAccessToken() = runTest {
whenever(mockSubscriptionsManager.getAccessToken()).thenReturn(AccessToken.Success("accessToken"))
val result = subscriptions.getPAT()
assertTrue(result is Success)
assertEquals("accessToken", (result as Success).pat)
val result = subscriptions.getAccessToken()
assertEquals("accessToken", result)
}

@Test
fun whenSubscriptionDataFailsThenReturnFailure() = runTest {
fun whenSubscriptionDataFailsThenReturnNull() = runTest {
whenever(mockSubscriptionsManager.getAccessToken()).thenReturn(AccessToken.Failure("error"))
val result = subscriptions.getPAT()
assertTrue(result is Failure)
assertEquals("error", (result as Failure).message)
assertNull(subscriptions.getAccessToken())
}

@Test
Expand Down

0 comments on commit 1624c56

Please sign in to comment.