fix(dl): only log in when qBittorrent asks
ci / web (push) Successful in 32s
e2e / e2e (push) Failing after 1m56s
ci / rust (push) Successful in 2m0s
ci / image (push) Successful in 2m27s

Logging in before the first call turned a stale or wrong password into a
hard failure on calls that would have succeeded: an instance that bypasses
authentication for arr's address answers everything, and never needed the
credentials at all. Authentication is now established on the first 403.

The health probe was reading a 403 from `app/version` as healthy, on the
reasoning that a live daemon is all the lamp claims. That is exactly the
state arr cannot use, and it left the lamp green while every grab and every
reaper tick failed on rejected credentials. It now lists torrents through
the configured client, so the lamp fails when arr's own calls do.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Miguel Palhas
2026-08-29 18:42:43 +01:00
co-authored by Claude Opus 5
parent ab4f95b166
commit 9ecbfde656
3 changed files with 70 additions and 27 deletions
+45 -7
View File
@@ -479,10 +479,11 @@ impl QbitClient {
path: &str,
build: impl Fn() -> reqwest::RequestBuilder,
) -> Result<String, Error> {
if self.credentials.is_some() && self.session.read().await.is_none() {
self.login().await?;
}
// Deliberately no login before the first call. An instance that
// bypasses authentication for arr's address answers everything, and
// logging in first would turn a stale or wrong password into a hard
// failure on a call that would have succeeded. Authentication is
// established only when qBittorrent actually asks for it, below.
for attempt in 0..2 {
let session = self.session.read().await.clone();
let mut request = build().header(REFERER, &self.origin);
@@ -496,7 +497,7 @@ impl QbitClient {
if self.credentials.is_none() {
return Err(Error::Unauthenticated);
}
// The session expired or the client restarted under us.
// No session yet, or the one held expired.
self.login().await?;
continue;
}
@@ -668,7 +669,7 @@ mod tests {
use std::path::PathBuf;
use serde_json::json;
use wiremock::matchers::{body_string_contains, method, path, query_param};
use wiremock::matchers::{body_string_contains, header, method, path, query_param};
use wiremock::{Mock, MockServer, ResponseTemplate};
use super::{AddTorrent, Error, QbitClient, TorrentSource, TorrentState};
@@ -688,7 +689,7 @@ mod tests {
}
#[tokio::test]
async fn logs_in_once_and_reuses_the_session() {
async fn logs_in_once_on_the_first_403_and_reuses_the_session() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/api/v2/auth/login"))
@@ -702,8 +703,17 @@ mod tests {
.await;
Mock::given(method("GET"))
.and(path("/api/v2/torrents/info"))
.and(header("cookie", "SID=abc123"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!([])))
.expect(2)
.with_priority(1)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path("/api/v2/torrents/info"))
.respond_with(ResponseTemplate::new(403).set_body_string("Forbidden"))
.expect(1)
.with_priority(2)
.mount(&server)
.await;
@@ -713,9 +723,37 @@ mod tests {
assert!(client.list_torrents().await.unwrap().is_empty());
}
/// An instance that bypasses authentication for arr's address answers
/// every call, so a stale password configured alongside it must not turn
/// a working call into a failure. Nothing here ever reaches `auth/login`.
#[tokio::test]
async fn a_bypassed_instance_is_never_logged_into() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/api/v2/auth/login"))
.respond_with(ResponseTemplate::new(200).set_body_string("Fails."))
.expect(0)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path("/api/v2/torrents/info"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!([])))
.mount(&server)
.await;
let client =
QbitClient::with_credentials(&server.uri(), "user".into(), "stale".into()).unwrap();
assert!(client.list_torrents().await.unwrap().is_empty());
}
#[tokio::test]
async fn a_wrong_password_is_a_login_error_not_a_retry_loop() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/api/v2/torrents/info"))
.respond_with(ResponseTemplate::new(403).set_body_string("Forbidden"))
.mount(&server)
.await;
Mock::given(method("POST"))
.and(path("/api/v2/auth/login"))
.respond_with(ResponseTemplate::new(200).set_body_string("Fails."))