diff --git a/CHANGELOG.md b/CHANGELOG.md index 223b48c9..0a2f1167 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,17 @@ All notable changes to this project are documented here. ### Breaking changes - `ListCommunities.sort` is now a `CommunitySortType` instead of the post `SortType`. +- `connect` and `connectForVersion` fail with a `LemmyConnectException` (`NodeInfoUnavailable`, `NotLemmy`, `InvalidVersion`, `UnsupportedVersion`) instead of `NotSupportedException`. +- `NodeInfoClient.getLemmyVersion` fails with `LemmyConnectException.NotLemmy` for other software. +- On Lemmy 0.18, a request failure whose message would contain the token (error statuses without a Lemmy error, timeouts) is now a `TokenRedactedException`. + +### Added + +- `LemmyApiOptions.onAuthRejected`: called with the token of a request the server rejected as not logged in. ### Fixed +- `connect` no longer throws when the instance reports a version that isn't a version number; it fails with `LemmyConnectException.InvalidVersion`. - Lemmy 1.0: `Top*` sorts work; they are sent as `top` with a `time_range_seconds`. - Lemmy 1.0: a `top` default post sort no longer makes `getSite` and `getMyUser` fail. diff --git a/README.md b/README.md index 70726ae4..befd042d 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ val client = LemmyApiClient( maxRetries = 5, userAgent = "MyApp/1.0", versionPolicy = VersionPolicy.LatestKnownCompatible, + onAuthRejected = { rejected -> if (rejected.token == currentToken) signOut() }, ), ) val api = client.connect(LemmyInstance("voyager.lemmy.ml")).getOrThrow() @@ -105,16 +106,10 @@ It is a non-goal to provide a stable API. The API can change with every iteratio [badge-android]: http://img.shields.io/badge/-android-6EDB8D.svg?style=flat -[badge-android-native]: http://img.shields.io/badge/support-[AndroidNative]-6EDB8D.svg?style=flat -[badge-wearos]: http://img.shields.io/badge/-wearos-8ECDA0.svg?style=flat [badge-jvm]: http://img.shields.io/badge/-jvm-DB413D.svg?style=flat [badge-js]: http://img.shields.io/badge/-js-F8DB5D.svg?style=flat -[badge-js-ir]: https://img.shields.io/badge/support-[IR]-AAC4E0.svg?style=flat -[badge-nodejs]: https://img.shields.io/badge/-nodejs-68a063.svg?style=flat [badge-linux]: http://img.shields.io/badge/-linux-2D3F6C.svg?style=flat [badge-windows]: http://img.shields.io/badge/-windows-4D76CD.svg?style=flat -[badge-wasm]: https://img.shields.io/badge/-wasm-624FE8.svg?style=flat -[badge-apple-silicon]: http://img.shields.io/badge/support-[AppleSilicon]-43BBFF.svg?style=flat [badge-ios]: http://img.shields.io/badge/-ios-CDCDCD.svg?style=flat [badge-mac]: http://img.shields.io/badge/-macos-111111.svg?style=flat [badge-watchos]: http://img.shields.io/badge/-watchos-C0C0C0.svg?style=flat \ No newline at end of file diff --git a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyApiClient.kt b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyApiClient.kt index 257b0ea1..15bd096b 100644 --- a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyApiClient.kt +++ b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyApiClient.kt @@ -1,6 +1,7 @@ package it.vercruysse.lemmyapi import io.ktor.client.* +import it.vercruysse.lemmyapi.exception.LemmyConnectException import it.vercruysse.lemmyapi.nodeinfo.NodeInfoClient import it.vercruysse.lemmyapi.utils.runCatchingPreservingCancellation @@ -22,7 +23,11 @@ class LemmyApiClient( /** * Connects to an instance after discovering its Lemmy version. * - * Returns a failure if the instance isn't available, isn't a Lemmy host, or isn't supported. + * Fails with a [LemmyConnectException]: + * - [LemmyConnectException.NodeInfoUnavailable] when the NodeInfo document can't be loaded or read, + * - [LemmyConnectException.NotLemmy] when the instance runs other software, + * - [LemmyConnectException.InvalidVersion] when the reported version isn't a version number, + * - [LemmyConnectException.UnsupportedVersion] when the version isn't supported. * * Use the Feature Flags before using certain endpoints as they can be or not available depending * on the version of the Lemmy Server instance. @@ -30,16 +35,25 @@ class LemmyApiClient( suspend fun connect( instance: LemmyInstance, auth: LemmyAuth = LemmyAuth.Anonymous, - ): Result = - nodeInfoClient.getLemmyVersion(instance).fold( - onSuccess = { version -> connectForVersion(instance, auth, LemmyVersion(version)) }, - onFailure = { Result.failure(it) }, - ) + ): Result { + val nodeInfo = nodeInfoClient.getNodeInfo(instance) + .getOrElse { return Result.failure(LemmyConnectException.NodeInfoUnavailable(it)) } + if (!nodeInfoClient.isLemmyInstance(nodeInfo)) { + return Result.failure(LemmyConnectException.NotLemmy(nodeInfo)) + } + val rawVersion = nodeInfo.software.version + val version = try { + LemmyVersion(rawVersion) + } catch (exception: IllegalArgumentException) { + return Result.failure(LemmyConnectException.InvalidVersion(rawVersion, exception)) + } + return connectForVersion(instance, auth, version) + } /** * Connects to an instance with a known Lemmy version. * - * Returns a failure if the version is unsupported. + * Fails with [LemmyConnectException.UnsupportedVersion] if the version is unsupported. * * Be warned that this function assumes that the instance and the version are correct. * @@ -57,13 +71,14 @@ class LemmyApiClient( version, auth, options.versionPolicy, + options.onAuthRejected, ) } /** * Connects to an instance with a known Lemmy version. * - * Returns a failure if the version is unsupported. + * Fails with [LemmyConnectException.UnsupportedVersion] if the version is unsupported. * * Be warned that this function assumes that the instance and the version are correct. * diff --git a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyApiTypes.kt b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyApiTypes.kt index a08a18f0..57eed2a3 100644 --- a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyApiTypes.kt +++ b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyApiTypes.kt @@ -59,6 +59,15 @@ data class LemmyApiOptions( val maxRetries: Int = 3, val userAgent: String = "LemmyKotlinApi", val versionPolicy: VersionPolicy = VersionPolicy.LatestKnownCompatible, + /** + * Called with the token of an API request the server rejected as not logged in: a 401, or the Lemmy error + * `not_logged_in` or `incorrect_login`. Password checks (login, change password, delete account) are left out, + * as there `incorrect_login` means a wrong password. + * + * It gets the token the request was sent with, so a token replaced since can be told apart. It runs on the + * requesting coroutine before the result is returned; keep it quick. Exceptions it throws are ignored. + */ + val onAuthRejected: AuthRejectedListener? = null, ) { init { require(requestTimeout.isPositive() && requestTimeout.isFinite()) { "Request timeout must be positive and finite" } @@ -69,3 +78,6 @@ data class LemmyApiOptions( internal val LemmyAuth.token: String? get() = (this as? LemmyAuth.Bearer)?.token + +/** See [LemmyApiOptions.onAuthRejected]. */ +typealias AuthRejectedListener = (LemmyAuth.Bearer) -> Unit diff --git a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyApiWrapperFactory.kt b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyApiWrapperFactory.kt index 3e727a1a..4c5592bc 100644 --- a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyApiWrapperFactory.kt +++ b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyApiWrapperFactory.kt @@ -1,7 +1,7 @@ package it.vercruysse.lemmyapi import io.ktor.client.HttpClient -import it.vercruysse.lemmyapi.exception.NotSupportedException +import it.vercruysse.lemmyapi.exception.LemmyConnectException internal object LemmyApiWrapperFactory { @@ -14,6 +14,7 @@ internal object LemmyApiWrapperFactory { version: LemmyVersion, auth: LemmyAuth, versionPolicy: VersionPolicy, + onAuthRejected: AuthRejectedListener? = null, ): LemmyApiBaseController { val baseUrlInstance = instance.baseUrl val semanticVersion = version.semanticVersion @@ -23,41 +24,41 @@ internal object LemmyApiWrapperFactory { return when (semanticVersion.major) { 0 -> when (semanticVersion.minor) { 18 -> if (semanticVersion.patch <= 5 || versionPolicy == VersionPolicy.LatestKnownCompatible) { - it.vercruysse.lemmyapi.v0.x18.x5.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, baseUrlInstance, token) + it.vercruysse.lemmyapi.v0.x18.x5.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, baseUrlInstance, token, onAuthRejected) } else { - throw NotSupportedException("Unsupported Lemmy version: $version") + throw LemmyConnectException.UnsupportedVersion(version) } 19 -> when (semanticVersion.patch) { - 0, 1 -> it.vercruysse.lemmyapi.v0.x19.x0.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, baseUrlInstance, token) + 0, 1 -> it.vercruysse.lemmyapi.v0.x19.x0.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, baseUrlInstance, token, onAuthRejected) - 2, 3 -> it.vercruysse.lemmyapi.v0.x19.x3.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, baseUrlInstance, token) + 2, 3 -> it.vercruysse.lemmyapi.v0.x19.x3.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, baseUrlInstance, token, onAuthRejected) - 4, 5 -> it.vercruysse.lemmyapi.v0.x19.x4.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, baseUrlInstance, token) + 4, 5 -> it.vercruysse.lemmyapi.v0.x19.x4.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, baseUrlInstance, token, onAuthRejected) - 6, 7, 8, 9, 10 -> it.vercruysse.lemmyapi.v0.x19.x6.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, baseUrlInstance, token) + 6, 7, 8, 9, 10 -> it.vercruysse.lemmyapi.v0.x19.x6.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, baseUrlInstance, token, onAuthRejected) - 11 -> it.vercruysse.lemmyapi.v0.x19.x11.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, baseUrlInstance, token) + 11 -> it.vercruysse.lemmyapi.v0.x19.x11.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, baseUrlInstance, token, onAuthRejected) else -> if (versionPolicy == VersionPolicy.LatestKnownCompatible) { - it.vercruysse.lemmyapi.v0.x19.x11.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, baseUrlInstance, token) + it.vercruysse.lemmyapi.v0.x19.x11.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, baseUrlInstance, token, onAuthRejected) } else { - throw NotSupportedException("Unsupported Lemmy version: $version") + throw LemmyConnectException.UnsupportedVersion(version) } } - else -> throw NotSupportedException("Unsupported Lemmy minor version: $version") + else -> throw LemmyConnectException.UnsupportedVersion(version) } 1 -> if ( versionPolicy == VersionPolicy.LatestKnownCompatible || semanticVersion.minor == 0 ) { - it.vercruysse.lemmyapi.v1.x0.x0.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, token) + it.vercruysse.lemmyapi.v1.x0.x0.LemmyApiUniWrapper(client, apiBaseUrl, instance, version, token, onAuthRejected) } else { - throw NotSupportedException("Unsupported Lemmy version: $version") + throw LemmyConnectException.UnsupportedVersion(version) } - else -> throw NotSupportedException("Unsupported Lemmy major version: $version") + else -> throw LemmyConnectException.UnsupportedVersion(version) } } } diff --git a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyRequestClient.kt b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyRequestClient.kt index 3a0d5825..80bd6746 100644 --- a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyRequestClient.kt +++ b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/LemmyRequestClient.kt @@ -1,8 +1,12 @@ package it.vercruysse.lemmyapi import io.ktor.client.HttpClient +import io.ktor.client.plugins.ResponseException import io.ktor.client.request.HttpRequestBuilder import io.ktor.http.HttpHeaders +import io.ktor.http.HttpMethod +import it.vercruysse.lemmyapi.exception.LemmyBadRequestException +import it.vercruysse.lemmyapi.exception.TokenRedactedException import it.vercruysse.lemmyapi.utils.addQueryParams import it.vercruysse.lemmyapi.utils.deleteResult import it.vercruysse.lemmyapi.utils.getResult @@ -10,58 +14,142 @@ import it.vercruysse.lemmyapi.utils.postResult import it.vercruysse.lemmyapi.utils.postUploadResult import it.vercruysse.lemmyapi.utils.putResult import it.vercruysse.lemmyapi.utils.setJsonBody +import kotlinx.coroutines.CancellationException +import kotlinx.io.IOException internal class LemmyRequestClient( val client: HttpClient, val apiBaseUrl: String, val authProvider: AuthProvider?, + private val sendAuthHeader: Boolean = true, + private val onAuthRejected: AuthRejectedListener? = null, + private val redactToken: Boolean = false, ) { - suspend inline fun deleteResult(path: String, body: T): Result = - client.deleteResult(resolve(path)) { - authenticate() + suspend inline fun deleteResult(path: String, body: T): Result { + val token = currentToken() + return client.deleteResult(resolve(path)) { + authenticate(token) setJsonBody(body) - } + }.checked(HttpMethod.Delete, path, token) + } - suspend inline fun deleteResult(path: String): Result = - client.deleteResult(resolve(path)) { authenticate() } + suspend inline fun deleteResult(path: String): Result { + val token = currentToken() + return client.deleteResult(resolve(path)) { authenticate(token) }.checked(HttpMethod.Delete, path, token) + } - suspend inline fun getResult(path: String, form: T): Result = - client.getResult(resolve(path)) { - authenticate() + suspend inline fun getResult(path: String, form: T): Result { + val token = currentToken() + return client.getResult(resolve(path)) { + authenticate(token) addQueryParams(form) - } + }.checked(HttpMethod.Get, path, token) + } - suspend inline fun getResult(path: String): Result = - client.getResult(resolve(path)) { authenticate() } + suspend inline fun getResult(path: String): Result { + val token = currentToken() + return client.getResult(resolve(path)) { authenticate(token) }.checked(HttpMethod.Get, path, token) + } - suspend inline fun postResult(path: String, body: T): Result = - client.postResult(resolve(path)) { - authenticate() + suspend inline fun postResult(path: String, body: T): Result { + val token = currentToken() + return client.postResult(resolve(path)) { + authenticate(token) setJsonBody(body) - } + }.checked(HttpMethod.Post, path, token) + } - suspend inline fun postResult(path: String): Result = - client.postResult(resolve(path)) { authenticate() } + suspend inline fun postResult(path: String): Result { + val token = currentToken() + return client.postResult(resolve(path)) { authenticate(token) }.checked(HttpMethod.Post, path, token) + } suspend inline fun postUploadResult( path: String, image: ByteArray, noinline builder: HttpRequestBuilder.() -> Unit = {}, - ): Result = client.postUploadResult(resolve(path), image) { - authenticate() - builder() + ): Result { + val token = currentToken() + return client.postUploadResult(resolve(path), image) { + authenticate(token) + builder() + }.checked(HttpMethod.Post, path, token) } - suspend inline fun putResult(path: String, body: T): Result = - client.putResult(resolve(path)) { - authenticate() + suspend inline fun putResult(path: String, body: T): Result { + val token = currentToken() + return client.putResult(resolve(path)) { + authenticate(token) setJsonBody(body) - } + }.checked(HttpMethod.Put, path, token) + } + + private fun currentToken(): String? = authProvider?.invoke() private fun resolve(path: String): String = "$apiBaseUrl/$path" - private fun HttpRequestBuilder.authenticate() { + private fun HttpRequestBuilder.authenticate(token: String?) { headers.remove(HttpHeaders.Authorization) - authProvider?.invoke()?.let { token -> headers[HttpHeaders.Authorization] = "Bearer $token" } + if (sendAuthHeader && token != null) headers[HttpHeaders.Authorization] = "Bearer $token" + } + + private fun Result.checked(method: HttpMethod, path: String, token: String?): Result { + val failure = exceptionOrNull() ?: return this + if (token.isNullOrEmpty()) return this + if (failure.rejectsAuth() && !isCredentialCheck(method, path)) notifyAuthRejected(token) + return if (redactToken) Result.failure(failure.redacting(token)) else this + } + + private fun notifyAuthRejected(token: String) { + val listener = onAuthRejected ?: return + try { + listener(LemmyAuth.Bearer(token)) + } catch (exception: CancellationException) { + throw exception + } catch (_: Exception) { + // A failing listener must not turn the request's Result into a thrown exception + } } + + private companion object { + /** Lemmy error ids that mean the request's token is not (or no longer) valid. */ + val REJECTED_AUTH_ERRORS = setOf("not_logged_in", "incorrect_login") + + /** Requests that check a password, where `incorrect_login` means a wrong password rather than a bad token. */ + val CREDENTIAL_CHECKS = setOf( + HttpMethod.Post to "user/login", + HttpMethod.Post to "account/auth/login", + HttpMethod.Put to "user/change_password", + HttpMethod.Put to "account/auth/change_password", + HttpMethod.Post to "user/delete_account", + HttpMethod.Delete to "account", + ) + + fun isCredentialCheck(method: HttpMethod, path: String): Boolean = (method to path) in CREDENTIAL_CHECKS + + fun Throwable.rejectsAuth(): Boolean = when (this) { + is LemmyBadRequestException -> statusCode == 401 || id in REJECTED_AUTH_ERRORS + is ResponseException -> response.status.value == 401 + else -> false + } + } +} + +private const val REDACTED = "" +private const val MAX_CAUSE_DEPTH = 16 + +/** + * Replaces this failure by a [TokenRedactedException] when its message, or a cause's, contains [token]. + * Lemmy 0.18 sends the token in the query of GET requests, and Ktor puts the address in its exception messages. + */ +internal fun Throwable.redacting(token: String): Throwable { + // Bounded, as a cause chain can loop back on itself + val leaks = generateSequence(this) { it.cause }.take(MAX_CAUSE_DEPTH).any { it.message?.contains(token) == true } + if (!leaks) return this + val original = listOfNotNull(this::class.simpleName, message).joinToString(": ") + return TokenRedactedException( + message = original.replace(token, REDACTED), + statusCode = (this as? ResponseException)?.response?.status?.value, + isNetworkFailure = this is IOException, + ) } diff --git a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/exception/LemmyConnectException.kt b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/exception/LemmyConnectException.kt new file mode 100644 index 00000000..de11da05 --- /dev/null +++ b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/exception/LemmyConnectException.kt @@ -0,0 +1,23 @@ +package it.vercruysse.lemmyapi.exception + +import it.vercruysse.lemmyapi.LemmyVersion +import it.vercruysse.lemmyapi.nodeinfo.NodeInfo + +sealed class LemmyConnectException(message: String, cause: Throwable? = null) : Exception(message, cause) { + + /** The NodeInfo document could not be loaded or read; [cause] holds the transport or parsing failure. */ + class NodeInfoUnavailable(cause: Throwable) : + LemmyConnectException("Could not load NodeInfo: ${cause.message ?: cause::class.simpleName}", cause) + + /** The instance runs some other software than Lemmy, as named in [nodeInfo]. */ + class NotLemmy(val nodeInfo: NodeInfo) : + LemmyConnectException("Not a Lemmy instance: ${nodeInfo.software.name} ${nodeInfo.software.version}") + + /** The instance reports a version that is not a readable version number. */ + class InvalidVersion(val rawVersion: String, cause: Throwable? = null) : + LemmyConnectException("Unreadable Lemmy version: $rawVersion", cause) + + /** The instance runs a Lemmy version this library has no implementation for, under the configured version policy. */ + class UnsupportedVersion(val version: LemmyVersion) : + LemmyConnectException("Unsupported Lemmy version: $version") +} diff --git a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/exception/TokenRedactedException.kt b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/exception/TokenRedactedException.kt new file mode 100644 index 00000000..d3339b9a --- /dev/null +++ b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/exception/TokenRedactedException.kt @@ -0,0 +1,17 @@ +package it.vercruysse.lemmyapi.exception + +/** + * Replaces a request failure whose message contained the auth token. + * + * Lemmy 0.18 sends the token in the query of GET requests, and Ktor's exceptions (non-Lemmy error statuses, timeouts) + * name the full address. The original exception is dropped, not kept as the cause, so the token can't reach a log + * through it. [message] is the original type and message with the token replaced. + * + * @property statusCode the HTTP status when the server answered, or null when it didn't. + * @property isNetworkFailure whether the original failure was an I/O failure (timeout, unreachable host, ...). + */ +class TokenRedactedException( + message: String, + val statusCode: Int?, + val isNetworkFailure: Boolean, +) : Exception(message) diff --git a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/nodeinfo/NodeInfoClient.kt b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/nodeinfo/NodeInfoClient.kt index cc31515a..e4f3ad56 100644 --- a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/nodeinfo/NodeInfoClient.kt +++ b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/nodeinfo/NodeInfoClient.kt @@ -7,6 +7,7 @@ import io.ktor.client.request.* import io.ktor.http.HttpStatusCode import it.vercruysse.lemmyapi.LemmyApiOptions import it.vercruysse.lemmyapi.LemmyInstance +import it.vercruysse.lemmyapi.exception.LemmyConnectException import it.vercruysse.lemmyapi.exception.NotSupportedException import it.vercruysse.lemmyapi.installRequiredPlugins import it.vercruysse.lemmyapi.utils.runCatchingPreservingCancellation @@ -62,13 +63,13 @@ class NodeInfoClient( /** * Gets the Lemmy version from a NodeInfo document. * - * Returns a failure if the document does not describe a Lemmy instance. + * Returns a [LemmyConnectException.NotLemmy] failure if the document does not describe a Lemmy instance. */ fun getLemmyVersion(nodeInfo: NodeInfo): Result = if (isLemmyInstance(nodeInfo)) { Result.success(getVersion(nodeInfo)) } else { - Result.failure(NotSupportedException("Not a Lemmy instance")) + Result.failure(LemmyConnectException.NotLemmy(nodeInfo)) } /** diff --git a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x18/x5/LemmyApiUniWrapper.kt b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x18/x5/LemmyApiUniWrapper.kt index 6101e01f..4de91889 100644 --- a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x18/x5/LemmyApiUniWrapper.kt +++ b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x18/x5/LemmyApiUniWrapper.kt @@ -1,6 +1,7 @@ package it.vercruysse.lemmyapi.v0.x18.x5 import io.ktor.client.HttpClient +import it.vercruysse.lemmyapi.AuthRejectedListener import it.vercruysse.lemmyapi.LemmyApiBaseController import it.vercruysse.lemmyapi.LemmyInstance import it.vercruysse.lemmyapi.LemmyRequestClient @@ -30,9 +31,9 @@ import it.vercruysse.lemmyapi.v0.x18.x5.datatypes.GetUnreadRegistrationApplicati import it.vercruysse.lemmyapi.v0.x18.x5.datatypes.MarkPostAsRead import kotlin.map -internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instance: LemmyInstance, actualVersion: LemmyVersion, baseUrl: String, auth: String?) : +internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instance: LemmyInstance, actualVersion: LemmyVersion, baseUrl: String, auth: String?, onAuthRejected: AuthRejectedListener? = null) : LemmyApiBaseController(instance, actualVersion, auth) { - private val api = LemmyApiController(LemmyRequestClient(client, apiBaseUrl, null)) + private val api = LemmyApiController(LemmyRequestClient(client, apiBaseUrl, { this.auth }, sendAuthHeader = false, onAuthRejected = onAuthRejected, redactToken = true)) private val pictrsApi = PictrsService(client, baseUrl) { this.auth } private val transformer = Transformer(auth ?: "") @@ -141,7 +142,7 @@ internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instan val change = hide != null && isHidden(form.community_id) != hide if (change) checkCanChangeHidden() val response = api.editCommunity(transformer.fromUni(form)).getOrThrow() - transformer.toUni(if (change && hide != null) setHidden(response, hide) else response) + transformer.toUni(if (change) setHidden(response, hide) else response) } private suspend fun setHidden( diff --git a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x0/LemmyApiUniWrapper.kt b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x0/LemmyApiUniWrapper.kt index 4df6e3fa..92cac1e1 100644 --- a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x0/LemmyApiUniWrapper.kt +++ b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x0/LemmyApiUniWrapper.kt @@ -1,6 +1,7 @@ package it.vercruysse.lemmyapi.v0.x19.x0 import io.ktor.client.HttpClient +import it.vercruysse.lemmyapi.AuthRejectedListener import it.vercruysse.lemmyapi.LemmyApiBaseController import it.vercruysse.lemmyapi.LemmyInstance import it.vercruysse.lemmyapi.LemmyRequestClient @@ -24,9 +25,9 @@ import it.vercruysse.lemmyapi.utils.matchesLegacyFilters import it.vercruysse.lemmyapi.utils.runCatchingPreservingCancellation import it.vercruysse.lemmyapi.v0.x19.x0.datatypes.GetPersonDetails -internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instance: LemmyInstance, actualVersion: LemmyVersion, baseUrl: String, auth: String?) : +internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instance: LemmyInstance, actualVersion: LemmyVersion, baseUrl: String, auth: String?, onAuthRejected: AuthRejectedListener? = null) : LemmyApiBaseController(instance, actualVersion, auth) { - private val api = LemmyApiController(LemmyRequestClient(client, apiBaseUrl) { this.auth }) + private val api = LemmyApiController(LemmyRequestClient(client, apiBaseUrl, { this.auth }, onAuthRejected = onAuthRejected)) private val pictrsApi = PictrsService(client, baseUrl) { this.auth } private val transformer = Transformer() @@ -122,7 +123,7 @@ internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instan val change = hide != null && isHidden(form.community_id) != hide if (change) checkCanChangeHidden() val response = api.editCommunity(transformer.fromUni(form)).getOrThrow() - transformer.toUni(if (change && hide != null) setHidden(response, hide) else response) + transformer.toUni(if (change) setHidden(response, hide) else response) } private suspend fun setHidden( diff --git a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x11/LemmyApiUniWrapper.kt b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x11/LemmyApiUniWrapper.kt index 6b81342b..6ccd7b5d 100644 --- a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x11/LemmyApiUniWrapper.kt +++ b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x11/LemmyApiUniWrapper.kt @@ -1,6 +1,7 @@ package it.vercruysse.lemmyapi.v0.x19.x11 import io.ktor.client.HttpClient +import it.vercruysse.lemmyapi.AuthRejectedListener import it.vercruysse.lemmyapi.LemmyApiBaseController import it.vercruysse.lemmyapi.LemmyInstance import it.vercruysse.lemmyapi.LemmyRequestClient @@ -24,9 +25,9 @@ import it.vercruysse.lemmyapi.utils.matchesLegacyFilters import it.vercruysse.lemmyapi.utils.runCatchingPreservingCancellation import it.vercruysse.lemmyapi.v0.x19.x11.datatypes.GetPersonDetails -internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instance: LemmyInstance, actualVersion: LemmyVersion, baseUrl: String, auth: String?) : +internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instance: LemmyInstance, actualVersion: LemmyVersion, baseUrl: String, auth: String?, onAuthRejected: AuthRejectedListener? = null) : LemmyApiBaseController(instance, actualVersion, auth) { - private val api = LemmyApiController(LemmyRequestClient(client, apiBaseUrl) { this.auth }) + private val api = LemmyApiController(LemmyRequestClient(client, apiBaseUrl, { this.auth }, onAuthRejected = onAuthRejected)) private val pictrsApi = PictrsService(client, baseUrl) { this.auth } private val transformer = Transformer() @@ -122,7 +123,7 @@ internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instan val change = hide != null && isHidden(form.community_id) != hide if (change) checkCanChangeHidden() val response = api.editCommunity(transformer.fromUni(form)).getOrThrow() - transformer.toUni(if (change && hide != null) setHidden(response, hide) else response) + transformer.toUni(if (change) setHidden(response, hide) else response) } private suspend fun setHidden( diff --git a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x3/LemmyApiUniWrapper.kt b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x3/LemmyApiUniWrapper.kt index 1db5ed56..a213e95b 100644 --- a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x3/LemmyApiUniWrapper.kt +++ b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x3/LemmyApiUniWrapper.kt @@ -1,6 +1,7 @@ package it.vercruysse.lemmyapi.v0.x19.x3 import io.ktor.client.HttpClient +import it.vercruysse.lemmyapi.AuthRejectedListener import it.vercruysse.lemmyapi.LemmyApiBaseController import it.vercruysse.lemmyapi.LemmyInstance import it.vercruysse.lemmyapi.LemmyRequestClient @@ -25,9 +26,9 @@ import it.vercruysse.lemmyapi.utils.runCatchingPreservingCancellation import it.vercruysse.lemmyapi.v0.x19.x3.datatypes.GetPersonDetails // Based on tags/0.19.3-alpha.6 -internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instance: LemmyInstance, actualVersion: LemmyVersion, baseUrl: String, auth: String?) : +internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instance: LemmyInstance, actualVersion: LemmyVersion, baseUrl: String, auth: String?, onAuthRejected: AuthRejectedListener? = null) : LemmyApiBaseController(instance, actualVersion, auth) { - private val api = LemmyApiController(LemmyRequestClient(client, apiBaseUrl) { this.auth }) + private val api = LemmyApiController(LemmyRequestClient(client, apiBaseUrl, { this.auth }, onAuthRejected = onAuthRejected)) private val pictrsApi = PictrsService(client, baseUrl) { this.auth } private val transformer = Transformer() @@ -123,7 +124,7 @@ internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instan val change = hide != null && isHidden(form.community_id) != hide if (change) checkCanChangeHidden() val response = api.editCommunity(transformer.fromUni(form)).getOrThrow() - transformer.toUni(if (change && hide != null) setHidden(response, hide) else response) + transformer.toUni(if (change) setHidden(response, hide) else response) } private suspend fun setHidden( diff --git a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x4/LemmyApiUniWrapper.kt b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x4/LemmyApiUniWrapper.kt index 7b38052a..9f047347 100644 --- a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x4/LemmyApiUniWrapper.kt +++ b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x4/LemmyApiUniWrapper.kt @@ -1,6 +1,7 @@ package it.vercruysse.lemmyapi.v0.x19.x4 import io.ktor.client.HttpClient +import it.vercruysse.lemmyapi.AuthRejectedListener import it.vercruysse.lemmyapi.LemmyApiBaseController import it.vercruysse.lemmyapi.LemmyInstance import it.vercruysse.lemmyapi.LemmyRequestClient @@ -24,9 +25,9 @@ import it.vercruysse.lemmyapi.utils.matchesLegacyFilters import it.vercruysse.lemmyapi.utils.runCatchingPreservingCancellation import it.vercruysse.lemmyapi.v0.x19.x4.datatypes.GetPersonDetails -internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instance: LemmyInstance, actualVersion: LemmyVersion, baseUrl: String, auth: String?) : +internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instance: LemmyInstance, actualVersion: LemmyVersion, baseUrl: String, auth: String?, onAuthRejected: AuthRejectedListener? = null) : LemmyApiBaseController(instance, actualVersion, auth) { - private val api = LemmyApiController(LemmyRequestClient(client, apiBaseUrl) { this.auth }) + private val api = LemmyApiController(LemmyRequestClient(client, apiBaseUrl, { this.auth }, onAuthRejected = onAuthRejected)) private val pictrsApi = PictrsService(client, baseUrl) { this.auth } private val transformer = Transformer() @@ -122,7 +123,7 @@ internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instan val change = hide != null && isHidden(form.community_id) != hide if (change) checkCanChangeHidden() val response = api.editCommunity(transformer.fromUni(form)).getOrThrow() - transformer.toUni(if (change && hide != null) setHidden(response, hide) else response) + transformer.toUni(if (change) setHidden(response, hide) else response) } private suspend fun setHidden( diff --git a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x6/LemmyApiUniWrapper.kt b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x6/LemmyApiUniWrapper.kt index 7a684a1c..4c17dbb3 100644 --- a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x6/LemmyApiUniWrapper.kt +++ b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v0/x19/x6/LemmyApiUniWrapper.kt @@ -1,6 +1,7 @@ package it.vercruysse.lemmyapi.v0.x19.x6 import io.ktor.client.HttpClient +import it.vercruysse.lemmyapi.AuthRejectedListener import it.vercruysse.lemmyapi.LemmyApiBaseController import it.vercruysse.lemmyapi.LemmyInstance import it.vercruysse.lemmyapi.LemmyRequestClient @@ -25,9 +26,9 @@ import it.vercruysse.lemmyapi.utils.matchesLegacyFilters import it.vercruysse.lemmyapi.utils.runCatchingPreservingCancellation import it.vercruysse.lemmyapi.v0.x19.x6.datatypes.GetPersonDetails -internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instance: LemmyInstance, actualVersion: LemmyVersion, baseUrl: String, auth: String?) : +internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instance: LemmyInstance, actualVersion: LemmyVersion, baseUrl: String, auth: String?, onAuthRejected: AuthRejectedListener? = null) : LemmyApiBaseController(instance, actualVersion, auth) { - private val api = LemmyApiController(LemmyRequestClient(client, apiBaseUrl) { this.auth }) + private val api = LemmyApiController(LemmyRequestClient(client, apiBaseUrl, { this.auth }, onAuthRejected = onAuthRejected)) private val pictrsApi = PictrsService(client, baseUrl) { this.auth } private val transformer = Transformer() @@ -123,7 +124,7 @@ internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instan val change = hide != null && isHidden(form.community_id) != hide if (change) checkCanChangeHidden() val response = api.editCommunity(transformer.fromUni(form)).getOrThrow() - transformer.toUni(if (change && hide != null) setHidden(response, hide) else response) + transformer.toUni(if (change) setHidden(response, hide) else response) } private suspend fun setHidden( diff --git a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v1/x0/x0/LemmyApiUniWrapper.kt b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v1/x0/x0/LemmyApiUniWrapper.kt index e4097604..2622a876 100644 --- a/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v1/x0/x0/LemmyApiUniWrapper.kt +++ b/app/src/commonMain/kotlin/it/vercruysse/lemmyapi/v1/x0/x0/LemmyApiUniWrapper.kt @@ -1,6 +1,7 @@ package it.vercruysse.lemmyapi.v1.x0.x0 import io.ktor.client.HttpClient +import it.vercruysse.lemmyapi.AuthRejectedListener import it.vercruysse.lemmyapi.LemmyApiBaseController import it.vercruysse.lemmyapi.LemmyInstance import it.vercruysse.lemmyapi.LemmyRequestClient @@ -17,9 +18,9 @@ import it.vercruysse.lemmyapi.dto.ExportUserSettingsResponse import it.vercruysse.lemmyapi.dto.ImportUserSettings import it.vercruysse.lemmyapi.utils.runCatchingPreservingCancellation -internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instance: LemmyInstance, actualVersion: LemmyVersion, auth: String?) : +internal class LemmyApiUniWrapper(client: HttpClient, apiBaseUrl: String, instance: LemmyInstance, actualVersion: LemmyVersion, auth: String?, onAuthRejected: AuthRejectedListener? = null) : LemmyApiBaseController(instance, actualVersion, auth) { - private val api = LemmyApiController(LemmyRequestClient(client, apiBaseUrl) { this.auth }) + private val api = LemmyApiController(LemmyRequestClient(client, apiBaseUrl, { this.auth }, onAuthRejected = onAuthRejected)) private val transformer = Transformer() override suspend fun getSite(): Result = diff --git a/app/src/jvmTest/kotlin/ConnectFailuresAndAuthTest.kt b/app/src/jvmTest/kotlin/ConnectFailuresAndAuthTest.kt new file mode 100644 index 00000000..11b1a3b9 --- /dev/null +++ b/app/src/jvmTest/kotlin/ConnectFailuresAndAuthTest.kt @@ -0,0 +1,174 @@ +import io.ktor.client.HttpClient +import io.ktor.client.engine.mock.MockEngine +import io.ktor.client.engine.mock.MockRequestHandler +import io.ktor.client.engine.mock.respond +import io.ktor.http.HttpHeaders +import io.ktor.http.HttpStatusCode +import io.ktor.http.headersOf +import it.vercruysse.lemmyapi.LemmyApiClient +import it.vercruysse.lemmyapi.LemmyApiOptions +import it.vercruysse.lemmyapi.LemmyAuth +import it.vercruysse.lemmyapi.LemmyInstance +import it.vercruysse.lemmyapi.LemmyVersion +import it.vercruysse.lemmyapi.VersionPolicy +import it.vercruysse.lemmyapi.datatypes.Login +import it.vercruysse.lemmyapi.exception.LemmyBadRequestException +import it.vercruysse.lemmyapi.exception.LemmyConnectException +import it.vercruysse.lemmyapi.exception.TokenRedactedException +import it.vercruysse.lemmyapi.redacting +import kotlinx.coroutines.runBlocking +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertIs +import kotlin.test.assertNull +import kotlin.test.assertSame +import kotlin.test.assertTrue + +class ConnectFailuresAndAuthTest { + + private fun nodeInfo(software: String, version: String) = + """{"version":"2.0","software":{"name":"$software","version":"$version"},"protocols":["activitypub"],""" + + """"openRegistrations":true,"usage":{"users":{}}}""" + + private fun connectWith(nodeInfoBody: String, status: HttpStatusCode = HttpStatusCode.OK): Throwable? = runBlocking { + val http = HttpClient( + MockEngine { + respond(nodeInfoBody, status, headersOf(HttpHeaders.ContentType, "application/json")) + }, + ) + LemmyApiClient(http, LemmyApiOptions(maxRetries = 0, versionPolicy = VersionPolicy.Strict)).use { + it.connect(LemmyInstance("lemmy.world")).exceptionOrNull() + }.also { http.close() } + } + + private fun clientFor( + rejected: MutableList, + handler: MockRequestHandler, + ): Pair { + val http = HttpClient(MockEngine(handler)) + val options = LemmyApiOptions(maxRetries = 0, onAuthRejected = { rejected += it.token }) + return http to LemmyApiClient(http, options) + } + + private fun json(status: HttpStatusCode, body: String): MockRequestHandler = { + respond(body, status, headersOf(HttpHeaders.ContentType, "application/json")) + } + + @Test + fun `connect names software that is not Lemmy`() { + val failure = assertIs(connectWith(nodeInfo("mastodon", "4.2.0"))) + assertEquals("mastodon", failure.nodeInfo.software.name) + } + + @Test + fun `connect fails instead of throwing for an unreadable version`() { + val failure = assertIs(connectWith(nodeInfo("lemmy", "unknown"))) + assertEquals("unknown", failure.rawVersion) + } + + @Test + fun `connect names the unsupported version`() { + val failure = assertIs(connectWith(nodeInfo("lemmy", "0.17.4"))) + assertEquals(LemmyVersion("0.17.4"), failure.version) + } + + @Test + fun `connect wraps a NodeInfo failure`() { + val failure = assertIs( + connectWith("oops", HttpStatusCode.InternalServerError), + ) + assertTrue(failure.cause != null) + } + + @Test + fun `rejected token is reported with the token the request carried`() = runBlocking { + val rejected = mutableListOf() + val (http, client) = clientFor(rejected, json(HttpStatusCode.BadRequest, """{"error":"not_logged_in"}""")) + val api = client.connectForVersion(LemmyInstance("lemmy.world"), LemmyAuth.fromToken("old"), LemmyVersion("1.0.0")).getOrThrow() + + assertIs(api.getSite().exceptionOrNull()) + api.updateAuth(LemmyAuth.fromToken("new")) + api.getSite() + api.clearAuth() + api.getSite() + + assertEquals(listOf("old", "new"), rejected) + client.close() + http.close() + } + + @Test + fun `wrong password on login is not a rejected token`() = runBlocking { + val rejected = mutableListOf() + val (http, client) = clientFor(rejected, json(HttpStatusCode.BadRequest, """{"error":"incorrect_login"}""")) + val api = client.connectForVersion(LemmyInstance("lemmy.world"), LemmyAuth.fromToken("tok"), LemmyVersion("1.0.0")).getOrThrow() + + assertIs(api.login(Login("user", "wrong")).exceptionOrNull()) + + assertTrue(rejected.isEmpty()) + client.close() + http.close() + } + + @Test + fun `401 without a Lemmy error is a rejected token`() = runBlocking { + val rejected = mutableListOf() + val (http, client) = clientFor(rejected) { respond("", HttpStatusCode.Unauthorized) } + val api = client.connectForVersion(LemmyInstance("lemmy.world"), LemmyAuth.fromToken("tok"), LemmyVersion("0.19.11")).getOrThrow() + + api.getSite() + + assertEquals(listOf("tok"), rejected) + client.close() + http.close() + } + + @Test + fun `version zero eighteen reports a rejected token sent in the form`() = runBlocking { + val rejected = mutableListOf() + val (http, client) = clientFor(rejected, json(HttpStatusCode.BadRequest, """{"error":"not_logged_in"}""")) + val api = client.connectForVersion(LemmyInstance("lemmy.world"), LemmyAuth.fromToken("legacy"), LemmyVersion("0.18.5")).getOrThrow() + + api.getSite() + + assertEquals(listOf("legacy"), rejected) + client.close() + http.close() + } + + @Test + fun `version zero eighteen failures do not leak the token`() = runBlocking { + val (http, client) = clientFor(mutableListOf()) { respond("gateway down", HttpStatusCode.BadGateway) } + val api = client.connectForVersion(LemmyInstance("lemmy.world"), LemmyAuth.fromToken("secret-token"), LemmyVersion("0.18.5")).getOrThrow() + + val failure = assertIs(api.getSite().exceptionOrNull()) + + assertFalse(failure.message!!.contains("secret-token")) + assertTrue(failure.message!!.contains("")) + assertEquals(502, failure.statusCode) + assertFalse(failure.isNetworkFailure) + assertNull(failure.cause) + client.close() + http.close() + } + + @Test + fun `redaction stops on a cause chain that loops`() { + val first = Exception("outer") + val second = Exception("inner", first) + first.initCause(second) + + assertSame(first, first.redacting("secret-token")) + } + + @Test + fun `failures without the token keep their type`() = runBlocking { + val (http, client) = clientFor(mutableListOf()) { respond("gateway down", HttpStatusCode.BadGateway) } + val api = client.connectForVersion(LemmyInstance("lemmy.world"), LemmyAuth.fromToken("secret-token"), LemmyVersion("1.0.0")).getOrThrow() + + assertIs(api.getSite().exceptionOrNull()) + client.close() + http.close() + } +} diff --git a/app/src/jvmTest/kotlin/GetCorrectControllerTest.kt b/app/src/jvmTest/kotlin/GetCorrectControllerTest.kt index 16f0e8f7..7e5e513d 100644 --- a/app/src/jvmTest/kotlin/GetCorrectControllerTest.kt +++ b/app/src/jvmTest/kotlin/GetCorrectControllerTest.kt @@ -4,7 +4,7 @@ import it.vercruysse.lemmyapi.LemmyInstance import it.vercruysse.lemmyapi.LemmyVersion import it.vercruysse.lemmyapi.MINIMUM_API_VERSION import it.vercruysse.lemmyapi.enums.SortType -import it.vercruysse.lemmyapi.exception.NotSupportedException +import it.vercruysse.lemmyapi.exception.LemmyConnectException import org.junit.jupiter.api.Assertions.assertInstanceOf import org.junit.jupiter.api.Test import kotlin.test.assertEquals @@ -55,7 +55,7 @@ class GetCorrectControllerTest { LemmyApiClient().use { client -> val unsupportedVersion = client.connectForVersion(LemmyInstance("lemmy.world"), LemmyVersion("200.0.0")) - assertIs(unsupportedVersion.exceptionOrNull()) + assertIs(unsupportedVersion.exceptionOrNull()) } } diff --git a/app/src/jvmTest/kotlin/LemmyApiClientTest.kt b/app/src/jvmTest/kotlin/LemmyApiClientTest.kt index e2e56ee3..6843929a 100644 --- a/app/src/jvmTest/kotlin/LemmyApiClientTest.kt +++ b/app/src/jvmTest/kotlin/LemmyApiClientTest.kt @@ -13,7 +13,7 @@ import it.vercruysse.lemmyapi.LemmyInstance import it.vercruysse.lemmyapi.LemmyVersion import it.vercruysse.lemmyapi.VersionPolicy import it.vercruysse.lemmyapi.datatypes.GetPosts -import it.vercruysse.lemmyapi.exception.NotSupportedException +import it.vercruysse.lemmyapi.exception.LemmyConnectException import kotlinx.coroutines.runBlocking import kotlin.coroutines.cancellation.CancellationException import kotlin.test.Test @@ -258,9 +258,9 @@ class LemmyApiClientTest { val newLineResult = client.connectForVersion(instance, LemmyVersion("1.1.0")) assertTrue(result.isFailure) - assertIs(result.exceptionOrNull()) - assertIs(oldLineResult.exceptionOrNull()) - assertIs(newLineResult.exceptionOrNull()) + assertIs(result.exceptionOrNull()) + assertIs(oldLineResult.exceptionOrNull()) + assertIs(newLineResult.exceptionOrNull()) } }