#7: Unit tests for oauth authorization
This commit is contained in:
parent
d7d2308d9e
commit
3a8b332796
|
@ -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() {
|
|||
|
||||
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
|
|||
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
|
|||
}
|
||||
|
||||
is Result.Failure -> {
|
||||
result.log("register()")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
|
|
@ -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 {
|
|||
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 {
|
|||
)
|
||||
}
|
||||
|
||||
@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…
Reference in New Issue