* Logout the use when revokeExternalAuth is called

* callback even if onGetExternalAuth failed

* Add http log

* Open on boarding activity when logout

* fix revoke token field name
This commit is contained in:
Cedrick Flocon 2019-11-22 17:30:29 +01:00 committed by Paulus Schoutsen
parent 2ed8fab74a
commit 433ead291d
9 changed files with 70 additions and 10 deletions

View file

@ -5,6 +5,8 @@ interface WebView {
fun loadUrl(url: String)
fun setExternalAuth(callback: String, externalAuth: String)
fun setExternalAuth(script: String)
fun openOnBoarding()
}

View file

@ -13,6 +13,7 @@ import io.homeassistant.companion.android.DaggerPresenterComponent
import io.homeassistant.companion.android.PresenterModule
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.common.dagger.GraphComponentAccessor
import io.homeassistant.companion.android.onboarding.OnboardingActivity
import io.homeassistant.companion.android.settings.SettingsActivity
import org.json.JSONObject
import javax.inject.Inject
@ -58,6 +59,11 @@ class WebViewActivity : AppCompatActivity(), io.homeassistant.companion.android.
presenter.onGetExternalAuth(JSONObject(callback).get("callback") as String)
}
@JavascriptInterface
fun revokeExternalAuth(callback: String) {
presenter.onRevokeExternalAuth(JSONObject(callback).get("callback") as String)
}
@JavascriptInterface
fun externalBus(message: String) {
Log.d(TAG, "External bus $message")
@ -89,6 +95,11 @@ class WebViewActivity : AppCompatActivity(), io.homeassistant.companion.android.
presenter.onViewReady()
}
override fun openOnBoarding() {
finish()
startActivity(Intent(this, OnboardingActivity::class.java))
}
override fun onBackPressed() {
if (webView.canGoBack()) {
webView.goBack()
@ -101,9 +112,9 @@ class WebViewActivity : AppCompatActivity(), io.homeassistant.companion.android.
webView.loadUrl(url)
}
override fun setExternalAuth(callback: String, externalAuth: String) {
override fun setExternalAuth(script: String) {
webView.post {
webView.evaluateJavascript("$callback(true, $externalAuth);", null)
webView.evaluateJavascript(script, null)
}
}

View file

@ -7,6 +7,8 @@ interface WebViewPresenter {
fun onGetExternalAuth(callback: String)
fun onRevokeExternalAuth(callback: String)
fun onFinish()
}

View file

@ -35,9 +35,23 @@ class WebViewPresenterImpl @Inject constructor(
override fun onGetExternalAuth(callback: String) {
mainScope.launch {
try {
view.setExternalAuth(callback, authenticationUseCase.retrieveExternalAuthentication())
view.setExternalAuth("$callback(true, ${authenticationUseCase.retrieveExternalAuthentication()})")
} catch (e: Exception) {
Log.e(TAG, "Unable to retrieve external auth", e)
view.setExternalAuth("$callback(false)")
}
}
}
override fun onRevokeExternalAuth(callback: String) {
mainScope.launch {
try {
authenticationUseCase.revokeSession()
view.setExternalAuth("$callback(true)")
view.openOnBoarding()
} catch (e: Exception) {
Log.e(TAG, "Unable to revoke session", e)
view.setExternalAuth("$callback(false)")
}
}
}

View file

@ -49,7 +49,7 @@ object WebViewPresenterImplSpec : Spek({
}
it("should set external auth") {
verify { view.setExternalAuth("externalAuthSetToken", "{\"access_token\":\"ABCDEFGH\",\"expires_in\":1800}") }
verify { view.setExternalAuth("externalAuthSetToken(true, {\"access_token\":\"ABCDEFGH\",\"expires_in\":1800})") }
}
}
@ -60,9 +60,30 @@ object WebViewPresenterImplSpec : Spek({
}
it("should not crash") {
coVerify {
view wasNot Called
}
verify { view.setExternalAuth("externalAuthSetToken(false)") }
}
}
describe("on revoke external auth on success") {
beforeEachTest {
coEvery { authenticationUseCase.revokeSession() } just runs
presenter.onRevokeExternalAuth("externalAuthRevokeToken")
}
it("should set external auth") {
verify { view.setExternalAuth("externalAuthRevokeToken(true)") }
verify { view.openOnBoarding() }
}
}
describe("on revoke external auth on error") {
beforeEachTest {
coEvery { authenticationUseCase.revokeSession() } throws Exception()
presenter.onRevokeExternalAuth("externalAuthRevokeToken")
}
it("should set external auth") {
verify { view.setExternalAuth("externalAuthRevokeToken(false)") }
}
}
}

View file

@ -21,6 +21,7 @@ dependencies {
implementation "com.squareup.retrofit2:retrofit:$retrofit2Version"
implementation "com.squareup.retrofit2:converter-jackson:$retrofit2Version"
implementation "com.squareup.okhttp3:okhttp:$okhttp3Version"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttp3Version"
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion"
implementation "org.threeten:threetenbp:$threeTenBpVersion"

View file

@ -3,6 +3,8 @@ package io.homeassistant.companion.android.data
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.PropertyNamingStrategy
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory
import javax.inject.Inject
@ -18,6 +20,13 @@ class HomeAssistantRetrofit @Inject constructor(url: String) {
.registerKotlinModule()
)
)
.client(
OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
})
.build()
)
.baseUrl(url)
.build()
}

View file

@ -33,7 +33,7 @@ interface AuthenticationService {
@FormUrlEncoded
@POST("auth/token")
suspend fun revokeToken(
@Field("refresh_token") refreshToken: String,
@Field("token") refreshToken: String,
@Field("action") action: String
)

View file

@ -83,7 +83,7 @@ object AuthenticationServiceSpec : Spek({
assertThat(request.method).isEqualTo("POST")
assertThat(request.path).isEqualTo("/auth/token")
assertThat(request.body.readUtf8())
.contains("refresh_token=IJKLMNOPQRST")
.contains("token=IJKLMNOPQRST")
.contains("action=revoke")
}
}