diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cfc5f9e..0af824f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,6 +25,8 @@ on: - 'gradle/**' - 'gradlew' - 'gradle.properties' + # Run the suite when the workflow itself changes, so CI edits are validated. + - '.github/workflows/ci.yml' pull_request: paths: - 'app/**' @@ -35,6 +37,8 @@ on: - 'gradle/**' - 'gradlew' - 'gradle.properties' + # Run the suite when the workflow itself changes, so CI edits are validated. + - '.github/workflows/ci.yml' workflow_dispatch: permissions: diff --git a/BACKLOG.md b/BACKLOG.md index da74e1c..4764f8e 100644 --- a/BACKLOG.md +++ b/BACKLOG.md @@ -45,6 +45,23 @@ fast enough in real use, don't build it. --- ## Done +- ✅ `2026-07-04` — **Android CI runs when its own workflow file changes** (wave-3 X7). `ci.yml`'s `paths:` + filters caught `**/*.kts` but not `.github/workflows/ci.yml` itself, so a PR editing only the CI workflow + skipped the build/test job (the Laravel repo already had this via W20). Added the workflow path to both the + `push` and `pull_request` `paths:` lists. +- ✅ `2026-07-04` — **`AuthInterceptor` 401 session-expiry path is unit-tested** (wave-3 X8). The reactive + logout (a 401 on any call clears the token and flips `sessionActive` → the app returns to login) is named a + critical path in `CLAUDE.md` but had no unit coverage. Added `AuthInterceptorTest`: 401 clears the token + + flips auth state; 200 leaves it; the `Authorization: Bearer` header is attached only when a token exists. + MockWebServer is `androidTestImplementation`-only, so it uses a fake `Interceptor.Chain` + fake + `TokenStore` — pure JVM, no emulator. +- ✅ `2026-07-04` — **Google sign-in 401 shows Google-specific copy (regression test)** (wave-3 X12). A + rejected Google ID token comes back as a 401 — the same status the email/password flow maps to "Incorrect + email or password." The audit suspected the Google button would show that wrong copy, but on re-check + `AuthViewModel.toGoogleAuthErrorMessage()` already maps a Google 401 → "Google sign-in failed. Please try + again." No behaviour change was needed; added a regression test pinning that copy so a future refactor + can't route the Google path through the shared password mapper (`FakeAuthRepository`'s failure `Throwable` + is now injectable). - ✅ `2026-07-04` — **Household create/join/leave now refreshes the shared hierarchy** (wave-3 X4). `HouseholdsViewModel.create()/leave()` and `JoinHouseholdViewModel.join()` updated only the `HouseholdRepository` cache + their own UI state, so the drawer/home/dashboard (driven by the `@Singleton HierarchyStore`) stayed stale until a manual diff --git a/app/src/test/java/dev/scuttle/inventory/AuthInterceptorTest.kt b/app/src/test/java/dev/scuttle/inventory/AuthInterceptorTest.kt new file mode 100644 index 0000000..263c69f --- /dev/null +++ b/app/src/test/java/dev/scuttle/inventory/AuthInterceptorTest.kt @@ -0,0 +1,92 @@ +package dev.scuttle.inventory + +import dev.scuttle.inventory.data.api.AuthInterceptor +import dev.scuttle.inventory.data.storage.TokenStore +import kotlinx.coroutines.flow.MutableStateFlow +import okhttp3.Call +import okhttp3.Connection +import okhttp3.Interceptor +import okhttp3.Protocol +import okhttp3.Request +import okhttp3.Response +import okhttp3.ResponseBody.Companion.toResponseBody +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test +import java.util.concurrent.TimeUnit + +/** + * Pure-JVM coverage of the reactive-logout mechanism (auth/token is a critical + * path per CLAUDE.md): a 401 must clear the stored token (which flips authState → + * the app returns to login); a non-401 must not; and the Bearer header must be + * attached only when a token exists. Uses a fake [Interceptor.Chain] so no + * emulator / MockWebServer is needed. + */ +class AuthInterceptorTest { + + private class FakeTokenStore(initial: String?) : TokenStore { + private var token = initial + private val _authState = MutableStateFlow(initial != null) + override fun get(): String? = token + override fun set(token: String) { this.token = token; _authState.value = true } + override fun clear() { token = null; _authState.value = false } + override val authState = _authState + } + + private class FakeChain( + private val request: Request, + private val responseCode: Int, + val onProceed: (Request) -> Unit = {}, + ) : Interceptor.Chain { + override fun request(): Request = request + override fun proceed(request: Request): Response { + onProceed(request) + return Response.Builder() + .request(request) + .protocol(Protocol.HTTP_1_1) + .code(responseCode) + .message("") + .body("".toResponseBody(null)) + .build() + } + override fun connection(): Connection? = null + override fun call(): Call = throw NotImplementedError() + override fun connectTimeoutMillis(): Int = 0 + override fun withConnectTimeout(timeout: Int, unit: TimeUnit): Interceptor.Chain = this + override fun readTimeoutMillis(): Int = 0 + override fun withReadTimeout(timeout: Int, unit: TimeUnit): Interceptor.Chain = this + override fun writeTimeoutMillis(): Int = 0 + override fun withWriteTimeout(timeout: Int, unit: TimeUnit): Interceptor.Chain = this + } + + private val request = Request.Builder().url("http://localhost/api/v1/households").build() + + @Test + fun clears_the_token_on_a_401() { + val store = FakeTokenStore("valid-token") + AuthInterceptor(store).intercept(FakeChain(request, responseCode = 401)) + assertNull(store.get()) + assertEquals(false, store.authState.value) + } + + @Test + fun keeps_the_token_on_a_200() { + val store = FakeTokenStore("valid-token") + AuthInterceptor(store).intercept(FakeChain(request, responseCode = 200)) + assertEquals("valid-token", store.get()) + assertEquals(true, store.authState.value) + } + + @Test + fun attaches_bearer_header_only_when_a_token_exists() { + var withToken: Request? = null + AuthInterceptor(FakeTokenStore("abc123")) + .intercept(FakeChain(request, 200) { withToken = it }) + assertEquals("Bearer abc123", withToken?.header("Authorization")) + + var withoutToken: Request? = null + AuthInterceptor(FakeTokenStore(null)) + .intercept(FakeChain(request, 200) { withoutToken = it }) + assertNull(withoutToken?.header("Authorization")) + } +} diff --git a/app/src/test/java/dev/scuttle/inventory/AuthViewModelTest.kt b/app/src/test/java/dev/scuttle/inventory/AuthViewModelTest.kt index 254a12b..aa89bad 100644 --- a/app/src/test/java/dev/scuttle/inventory/AuthViewModelTest.kt +++ b/app/src/test/java/dev/scuttle/inventory/AuthViewModelTest.kt @@ -5,19 +5,25 @@ import dev.scuttle.inventory.data.error.ErrorLogger import dev.scuttle.inventory.ui.auth.AuthViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.test.runTest +import okhttp3.ResponseBody.Companion.toResponseBody import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test +import retrofit2.HttpException +import retrofit2.Response class AuthViewModelTest { @get:Rule val mainDispatcherRule = MainDispatcherRule() - private class FakeAuthRepository(private val succeed: Boolean) : AuthRepository { + private class FakeAuthRepository( + private val succeed: Boolean, + private val error: Throwable = RuntimeException("Invalid credentials."), + ) : AuthRepository { private var authed = false val session = MutableStateFlow(false) @@ -42,7 +48,7 @@ class AuthViewModelTest { } private fun maybeFail() { - if (!succeed) throw RuntimeException("Invalid credentials.") + if (!succeed) throw error authed = true session.value = true } @@ -92,6 +98,24 @@ class AuthViewModelTest { assertFalse(viewModel.state.value.authenticated) } + @Test + fun a_google_401_shows_the_google_specific_copy_not_the_password_copy() = runTest { + // X12: a rejected Google ID token comes back as a 401, the same status the + // email/password path uses for "Incorrect email or password." That wrong copy + // would be nonsensical on the Google button — the Google 401 must map to its + // own message. Guards toGoogleAuthErrorMessage() against a future regression to + // the shared password mapper. + val http401 = HttpException(Response.error(401, "".toResponseBody(null))) + val viewModel = AuthViewModel(FakeAuthRepository(succeed = false, error = http401), FakeErrorLogger()) + + viewModel.loginWithGoogle("bad-id-token") + + val state = viewModel.state.value + assertFalse(state.authenticated) + assertFalse(state.googleLoading) + assertEquals("Google sign-in failed. Please try again.", state.error) + } + @Test fun a_mid_session_token_loss_flips_authenticated_to_false() = runTest { val repo = FakeAuthRepository(succeed = true)