Browse Source

Merge branch 'feature/7-add-oauth-tests' into 'develop'

#7: Unit tests for oauth authorization

See merge request funkwhale/funkwhale-android!50
enhancement/speed-up-pipelines
Ryan Harg 3 years ago
parent
commit
77a177be6a
  1. 22
      app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt
  2. 30
      app/src/test/java/audio/funkwhale/ffa/utils/DefaultOAuthTest.kt

22
app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt

@ -5,6 +5,7 @@ import android.content.Context @@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.coroutines.awaitObjectResponseResult
import com.github.kittinunf.fuel.gson.gsonDeserializerOf
import com.github.kittinunf.fuel.gson.jsonBody
@ -31,13 +32,13 @@ fun AuthState.save() { @@ -31,13 +32,13 @@ fun AuthState.save() {
interface OAuth {
fun exchange(context: Activity, authorization: Intent, success: () -> Unit, error: () -> Unit)
fun exchange(context: Context, authorization: Intent, success: () -> Unit, error: () -> Unit)
fun init(hostname: String): AuthState
fun register(authState: AuthState? = null, callback: () -> Unit)
fun authorize(context: Activity)
fun authorize(activity: Activity)
fun isAuthorized(context: Context): Boolean
@ -153,7 +154,7 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService @@ -153,7 +154,7 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService
override fun register(authState: AuthState?, callback: () -> Unit) {
(authState ?: state()).authorizationServiceConfiguration?.let { config ->
runBlocking {
val (_, _, result) = Fuel.post(config.registrationEndpoint.toString())
val (_, _, result: Result<App, FuelError>) = Fuel.post(config.registrationEndpoint.toString())
.header("Content-Type", "application/json")
.jsonBody(registrationBody())
.awaitObjectResponseResult(gsonDeserializerOf(App::class.java))
@ -178,6 +179,7 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService @@ -178,6 +179,7 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService
}
is Result.Failure -> {
result.log("register()")
}
}
}
@ -192,18 +194,16 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService @@ -192,18 +194,16 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService
)
}
override fun authorize(context: Activity) {
val intent = service(context).run {
authorizationRequest()?.let {
getAuthorizationRequestIntent(it)
}
override fun authorize(activity: Activity) {
val authService = service(activity)
authorizationRequest()?.let { it ->
val intent = authService.getAuthorizationRequestIntent(it)
activity.startActivityForResult(intent, 0)
}
context.startActivityForResult(intent, 0)
}
override fun exchange(
context: Activity,
context: Context,
authorization: Intent,
success: () -> Unit,
error: () -> Unit

30
app/src/test/java/audio/funkwhale/ffa/utils/DefaultOAuthTest.kt

@ -1,6 +1,8 @@ @@ -1,6 +1,8 @@
package audio.funkwhale.ffa.utils
import android.app.Activity
import android.content.Context
import android.content.Intent
import com.github.kittinunf.fuel.core.Client
import com.github.kittinunf.fuel.core.FuelManager
import com.github.kittinunf.fuel.core.Request
@ -240,7 +242,7 @@ class DefaultOAuthTest { @@ -240,7 +242,7 @@ class DefaultOAuthTest {
val state = oAuth.init("https://example.com")
oAuth.register(state) { }
val requestSlot = slot<com.github.kittinunf.fuel.core.Request>()
val requestSlot = slot<Request>()
coVerify { mockkClient.awaitRequest(capture(requestSlot)) }
@ -257,6 +259,32 @@ class DefaultOAuthTest { @@ -257,6 +259,32 @@ class DefaultOAuthTest {
)
}
@Test
fun `authorize() should start activity for result`() {
mockkStatic(PowerPreference::class)
every { PowerPreference.getFileByName(any()) } returns mockPreference
every { mockPreference.getString(any()) } returns "{}"
every { mockPreference.setString(any(), any()) } returns true
mockkStatic(AuthState::class)
val authState = mockk<AuthState>()
every { AuthState.jsonDeserialize(any<String>()) } returns authState
val mockConfig = mockk<AuthorizationServiceConfiguration>()
every { authState.authorizationServiceConfiguration } returns mockConfig
every { authState.lastRegistrationResponse } returns mockk()
every { authServiceFactory.create(any()) } returns authService
val mockkIntent = mockk<Intent>()
every { authService.getAuthorizationRequestIntent(any()) } returns mockkIntent
val activity = mockk<Activity>(relaxed = true)
oAuth.authorize(activity)
verify { activity.startActivityForResult(mockkIntent, 0) }
}
private fun <T> deserializeJson(
capturedRequest: Request
): T {

Loading…
Cancel
Save