Handle error responses when querying principals for display names (closes bitfireAT/#237) (bitfireAT/#238)

* Add test with inaccessible principal

* Catch and log exceptions where server resource is inaccessible

* Catch and log every HttpException
This commit is contained in:
Sunik Kupfer 2023-03-31 12:52:05 +02:00 committed by Ricki Hirner
parent fc17d0ed69
commit 874ad7b1c6
No known key found for this signature in database
GPG key ID: 79A019FCAAEDD3AA
2 changed files with 48 additions and 6 deletions

View file

@ -68,6 +68,7 @@ class RefreshCollectionsWorkerTest {
private const val PATH_CARDDAV = "/carddav"
private const val SUBPATH_PRINCIPAL = "/principal"
private const val SUBPATH_PRINCIPAL_INACCESSIBLE = "/inaccessible-principal"
private const val SUBPATH_PRINCIPAL_WITHOUT_COLLECTIONS = "/principal2"
private const val SUBPATH_ADDRESSBOOK_HOMESET = "/addressbooks-homeset"
private const val SUBPATH_ADDRESSBOOK_HOMESET_EMPTY = "/addressbooks-homeset-empty"
@ -419,6 +420,42 @@ class RefreshCollectionsWorkerTest {
// refreshPrincipals
@Test
fun refreshPrincipals_inaccessiblePrincipal() {
val service = createTestService(Service.TYPE_CARDDAV)!!
// place principal without display name in db
val principalId = db.principalDao().insert(
Principal(
0,
service.id,
mockServer.url("$PATH_CARDDAV$SUBPATH_PRINCIPAL_INACCESSIBLE"), // no trailing slash
null // no display name for now
)
)
// add an associated collection - as the principal is rightfully removed otherwise
db.collectionDao().insertOrUpdateByUrl(
Collection(
0,
service.id,
null,
principalId, // create association with principal
Collection.TYPE_ADDRESSBOOK,
mockServer.url("$PATH_CARDDAV$SUBPATH_ADDRESSBOOK/"), // with trailing slash
)
)
// Refresh principals
RefreshCollectionsWorker.Refresher(db, service, settings, client.okHttpClient)
.refreshPrincipals()
// Check principal was not updated
val principals = db.principalDao().getByService(service.id)
assertEquals(1, principals.size)
assertEquals(mockServer.url("$PATH_CARDDAV$SUBPATH_PRINCIPAL_INACCESSIBLE"), principals[0].url)
assertEquals(null, principals[0].displayName)
}
@Test
fun refreshPrincipals_updatesPrincipal() {
val service = createTestService(Service.TYPE_CARDDAV)!!
@ -551,6 +588,7 @@ class RefreshCollectionsWorkerTest {
"</response>" +
"</multistatus>"
PATH_CARDDAV + SUBPATH_PRINCIPAL_INACCESSIBLE,
PATH_CARDDAV + SUBPATH_ADDRESSBOOK_INACCESSIBLE ->
responseCode = 404

View file

@ -432,13 +432,17 @@ class RefreshCollectionsWorker @AssistedInject constructor(
for (oldPrincipal in principals) {
val principalUrl = oldPrincipal.url
Logger.log.fine("Querying principal $principalUrl")
DavResource(httpClient, principalUrl).propfind(0, *DAV_PRINCIPAL_PROPERTIES) { response, _ ->
if (!response.isSuccess())
return@propfind
Principal.fromDavResponse(service.id, response)?.let { principal ->
Logger.log.fine("Got principal: $principal")
db.principalDao().insertOrUpdate(service.id, principal)
try {
DavResource(httpClient, principalUrl).propfind(0, *DAV_PRINCIPAL_PROPERTIES) { response, _ ->
if (!response.isSuccess())
return@propfind
Principal.fromDavResponse(service.id, response)?.let { principal ->
Logger.log.fine("Got principal: $principal")
db.principalDao().insertOrUpdate(service.id, principal)
}
}
} catch (e: HttpException) {
Logger.log.info("Principal update failed with response code ${e.code}. principalUrl=$principalUrl")
}
}