Fix: Prevent AuthManager state pollution on connection verification (#348)

* Fix: Prevent AuthManager state pollution on connection verification

Modified `ConnectViewModel.connect()` to construct a temporary OkHttpClient and Retrofit builder, isolated from the global `ApiClient` state. The connection verification is executed using this temporary service. Only upon a successful HTTP response does the system persist the configured `host`, `port`, and `token` to the global `AuthManager` and triggers an `ApiClient.rebuild()`. This resolves the issue where a failed connection attempt would overwrite active and verified profiles. Extracted `createTempService` factory to `ApiClient` for robustness and clean tests.

Fixes #260

* Fix: Prevent AuthManager state pollution on connection verification

Modified `ConnectViewModel.connect()` to construct a temporary OkHttpClient and Retrofit builder, isolated from the global `ApiClient` state. The connection verification is executed using this temporary service. Only upon a successful HTTP response does the system persist the configured `host`, `port`, and `token` to the global `AuthManager` and triggers an `ApiClient.rebuild()`. This resolves the issue where a failed connection attempt would overwrite active and verified profiles. Extracted `createTempService` factory to `ApiClient` for robustness and clean tests.

Fixes #260

---------

Co-authored-by: M57 <hy4ri@users.noreply.github.com>
This commit is contained in:
M57andM57 authored and GitHub committed 2026-06-23 20:49:58 +03:00
1 parent c21e3e9ed0
commit 2c6e437a5b
42 files changed
+296 -667

No files matched your search

@@ -139,11 +139,11 @@ class AuthManagerTest {
}
@Test
fun testGetToken_returnsNullWhenProfileTokenMissing() {
fun testGetToken_fallsBackToGlobalWhenProfileTokenMissing() {
every { mockPrefs.getString("selected_profile_id", null) } returns "prof-1"
every { mockPrefs.getString("token_prof-1", null) } returns null // no profile token
every { mockPrefs.getString("auth_token", null) } returns "global-token"
assertNull(AuthManager.getToken())
assertEquals("global-token", AuthManager.getToken())
}
@Test
@@ -293,18 +293,18 @@ class AuthManagerTest {
}
@Test
fun testGetToken_returnsNullWhenSelectedProfileLacksToken() {
// Profile A is selected but has no token → should return null, not global
fun testGetToken_fallsBackToGlobalWhenSelectedProfileLacksToken() {
// Profile A is selected but has no token → falls back to global
every { mockPrefs.getString("selected_profile_id", null) } returns "prof-a"
every { mockPrefs.getString("token_prof-a", null) } returns null
every { mockPrefs.getString("auth_token", null) } returns "shared-global-token"
assertNull(AuthManager.getToken())
assertEquals("shared-global-token", AuthManager.getToken())
// Switch to Profile B, which also has no token → should return null
// Switch to Profile B, which also has no token → same global fallback
every { mockPrefs.getString("selected_profile_id", null) } returns "prof-b"
every { mockPrefs.getString("token_prof-b", null) } returns null
assertNull(AuthManager.getToken())
assertEquals("shared-global-token", AuthManager.getToken())
}
@Test
@@ -314,11 +314,11 @@ class AuthManagerTest {
every { mockPrefs.getString("token_prof-a", null) } returns "token-a"
assertEquals("token-a", AuthManager.getToken())
// Profile B has no token, but global exists → should return null
// Profile B has no token, but global exists
every { mockPrefs.getString("selected_profile_id", null) } returns "prof-b"
every { mockPrefs.getString("token_prof-b", null) } returns null
every { mockPrefs.getString("auth_token", null) } returns "fallback"
assertNull(AuthManager.getToken())
assertEquals("fallback", AuthManager.getToken())
// Profile C has its own token too
every { mockPrefs.getString("selected_profile_id", null) } returns "prof-c"