mirror of
https://github.com/bitfireAT/davx5-ose
synced 2024-07-23 19:50:18 +00:00
DavResourceFinder: make input arguments explicit (bitfireAT/davx5#298)
This commit is contained in:
parent
764c382af3
commit
50cb223bb6
|
@ -10,20 +10,27 @@ import androidx.test.platform.app.InstrumentationRegistry
|
|||
import at.bitfire.dav4jvm.DavResource
|
||||
import at.bitfire.dav4jvm.property.AddressbookHomeSet
|
||||
import at.bitfire.dav4jvm.property.ResourceType
|
||||
import at.bitfire.davdroid.network.HttpClient
|
||||
import at.bitfire.davdroid.db.Credentials
|
||||
import at.bitfire.davdroid.log.Logger
|
||||
import at.bitfire.davdroid.network.HttpClient
|
||||
import at.bitfire.davdroid.servicedetection.DavResourceFinder.Configuration.ServiceInfo
|
||||
import at.bitfire.davdroid.settings.SettingsManager
|
||||
import at.bitfire.davdroid.ui.setup.LoginModel
|
||||
import dagger.hilt.android.testing.HiltAndroidRule
|
||||
import dagger.hilt.android.testing.HiltAndroidTest
|
||||
import okhttp3.mockwebserver.Dispatcher
|
||||
import okhttp3.mockwebserver.MockResponse
|
||||
import okhttp3.mockwebserver.MockWebServer
|
||||
import okhttp3.mockwebserver.RecordedRequest
|
||||
import org.junit.*
|
||||
import org.junit.Assert.*
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Assume
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import java.net.URI
|
||||
import javax.inject.Inject
|
||||
|
||||
|
@ -56,20 +63,18 @@ class DavResourceFinderTest {
|
|||
|
||||
lateinit var finder: DavResourceFinder
|
||||
lateinit var client: HttpClient
|
||||
lateinit var loginModel: LoginModel
|
||||
|
||||
@Before
|
||||
fun initServerAndClient() {
|
||||
server.dispatcher = TestDispatcher()
|
||||
server.start()
|
||||
|
||||
loginModel = LoginModel()
|
||||
loginModel.baseURI = URI.create("/")
|
||||
loginModel.credentials = Credentials("mock", "12345")
|
||||
val baseURI = URI.create("/")
|
||||
val credentials = Credentials("mock", "12345")
|
||||
|
||||
finder = DavResourceFinder(InstrumentationRegistry.getInstrumentation().targetContext, loginModel)
|
||||
finder = DavResourceFinder(InstrumentationRegistry.getInstrumentation().targetContext, baseURI, credentials)
|
||||
client = HttpClient.Builder(InstrumentationRegistry.getInstrumentation().targetContext)
|
||||
.addAuthentication(null, loginModel.credentials!!)
|
||||
.addAuthentication(null, credentials)
|
||||
.build()
|
||||
|
||||
Assume.assumeTrue(NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted)
|
||||
|
|
|
@ -16,7 +16,6 @@ import at.bitfire.davdroid.db.Collection
|
|||
import at.bitfire.davdroid.db.Credentials
|
||||
import at.bitfire.davdroid.log.StringHandler
|
||||
import at.bitfire.davdroid.network.HttpClient
|
||||
import at.bitfire.davdroid.ui.setup.LoginModel
|
||||
import at.bitfire.davdroid.util.DavUtils
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||
|
@ -38,10 +37,15 @@ import java.util.logging.Logger
|
|||
* - services (CalDAV and/or CardDAV),
|
||||
* - principal,
|
||||
* - homeset/collections (multistatus responses are handled through dav4jvm).
|
||||
*
|
||||
* @param context to build the HTTP client
|
||||
* @param baseURI user-given base URI (either mailto: URI or http(s):// URL)
|
||||
* @param credentials optional login credentials (username/password, client certificate, OAuth state)
|
||||
*/
|
||||
class DavResourceFinder(
|
||||
val context: Context,
|
||||
private val loginModel: LoginModel
|
||||
private val baseURI: URI,
|
||||
credentials: Credentials? = null
|
||||
): AutoCloseable {
|
||||
|
||||
enum class Service(val wellKnownName: String) {
|
||||
|
@ -61,7 +65,7 @@ class DavResourceFinder(
|
|||
var encountered401 = false
|
||||
|
||||
private val httpClient: HttpClient = HttpClient.Builder(context, logger = log).let {
|
||||
loginModel.credentials?.let { credentials ->
|
||||
credentials?.let { credentials ->
|
||||
it.addAuthentication(null, credentials)
|
||||
}
|
||||
it.setForeground(true)
|
||||
|
@ -109,9 +113,6 @@ class DavResourceFinder(
|
|||
}
|
||||
|
||||
private fun findInitialConfiguration(service: Service): Configuration.ServiceInfo? {
|
||||
// user-given base URI (either mailto: URI or http(s):// URL)
|
||||
val baseURI = loginModel.baseURI!!
|
||||
|
||||
// domain for service discovery
|
||||
var discoveryFQDN: String? = null
|
||||
|
||||
|
|
|
@ -18,11 +18,13 @@ import androidx.lifecycle.AndroidViewModel
|
|||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import at.bitfire.davdroid.R
|
||||
import at.bitfire.davdroid.db.Credentials
|
||||
import at.bitfire.davdroid.log.Logger
|
||||
import at.bitfire.davdroid.servicedetection.DavResourceFinder
|
||||
import at.bitfire.davdroid.ui.DebugInfoActivity
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import java.lang.ref.WeakReference
|
||||
import java.net.URI
|
||||
import java.util.logging.Level
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
|
@ -31,10 +33,14 @@ class DetectConfigurationFragment: Fragment() {
|
|||
private val loginModel by activityViewModels<LoginModel>()
|
||||
private val model by viewModels<DetectConfigurationModel>()
|
||||
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
model.detectConfiguration(loginModel).observe(this) { result ->
|
||||
val baseURI = loginModel.baseURI ?: return
|
||||
val credentials = loginModel.credentials ?: return
|
||||
|
||||
model.detectConfiguration(baseURI, credentials).observe(this) { result ->
|
||||
// save result for next step
|
||||
loginModel.configuration = result
|
||||
|
||||
|
@ -57,14 +63,12 @@ class DetectConfigurationFragment: Fragment() {
|
|||
inflater.inflate(R.layout.detect_configuration, container, false)!!
|
||||
|
||||
|
||||
class DetectConfigurationModel(
|
||||
application: Application
|
||||
): AndroidViewModel(application) {
|
||||
class DetectConfigurationModel(application: Application): AndroidViewModel(application) {
|
||||
|
||||
private var detectionThread: WeakReference<Thread>? = null
|
||||
private var result = MutableLiveData<DavResourceFinder.Configuration>()
|
||||
|
||||
fun detectConfiguration(loginModel: LoginModel): LiveData<DavResourceFinder.Configuration> {
|
||||
fun detectConfiguration(baseURI: URI, credentials: Credentials): LiveData<DavResourceFinder.Configuration> {
|
||||
synchronized(result) {
|
||||
if (detectionThread != null)
|
||||
// detection already running
|
||||
|
@ -77,7 +81,7 @@ class DetectConfigurationFragment: Fragment() {
|
|||
}
|
||||
|
||||
try {
|
||||
DavResourceFinder(getApplication(), loginModel).use { finder ->
|
||||
DavResourceFinder(getApplication(), baseURI, credentials).use { finder ->
|
||||
result.postValue(finder.findInitialConfiguration())
|
||||
}
|
||||
} catch(e: Exception) {
|
||||
|
|
Loading…
Reference in a new issue