Improve youth mode action token flow

This commit is contained in:
richjohnrambo
2026-09-02 11:25:25 +08:00
parent 7f6995d8fe
commit 79947a2c6f
5 changed files with 387 additions and 13 deletions
+12 -2
View File
@@ -2132,6 +2132,13 @@ func TestServeIMIndependentPasswordSetAndVerify(t *testing.T) {
bcrypt.CompareHashAndPassword(security.independentPassword.PasswordHash, []byte("2794")) != nil {
t.Fatalf("password was not stored as a valid hash: %#v", security.independentPassword)
}
data := decodeIMHTTPResponse(t, rec).Data.(map[string]any)
if data["password_set"] != true || data["next_action"] != "enable_youth_mode" || data["enable_action_token"] == "" {
t.Fatalf("unexpected set response: %#v", data)
}
if security.actionToken == nil || security.actionToken.Action != "enable_youth_mode" || security.actionToken.Target != "youth_mode" {
t.Fatalf("unexpected enable token after set: %#v", security.actionToken)
}
expectIMBearer(t, ctrl, "token-verify", 7, 42)
req = httptest.NewRequest(http.MethodPost, "/v1/im/settings/independent-password/verify",
@@ -2144,7 +2151,7 @@ func TestServeIMIndependentPasswordSetAndVerify(t *testing.T) {
if rec.Code != http.StatusOK {
t.Fatalf("verify status: got %d, want %d; body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
data := decodeIMHTTPResponse(t, rec).Data.(map[string]any)
data = decodeIMHTTPResponse(t, rec).Data.(map[string]any)
if data["action"] != "close_youth_mode" || data["action_token"] == "" {
t.Fatalf("unexpected verify response: %#v", data)
}
@@ -3156,7 +3163,7 @@ func TestServeIMYouthModeEnableDisable(t *testing.T) {
defer ctrl.Finish()
oldSecurity := store.Security
security := &testSecurityStore{
actionToken: &types.ActionToken{Action: "set_independent_password", Target: "youth_mode"},
actionToken: &types.ActionToken{Action: "enable_youth_mode", Target: "youth_mode"},
independentPassword: &types.IndependentPassword{PasswordHash: []byte("hash")},
}
store.Security = security
@@ -3172,6 +3179,9 @@ func TestServeIMYouthModeEnableDisable(t *testing.T) {
if rec.Code != http.StatusOK {
t.Fatalf("enable status: got %d, want %d; body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
if security.consumedAction != "enable_youth_mode" || security.consumedTarget != "youth_mode" {
t.Fatalf("unexpected enable token consumption: action=%q target=%q", security.consumedAction, security.consumedTarget)
}
data := decodeIMHTTPResponse(t, rec).Data.(map[string]any)
if data["enabled"] != true || data["policy_version"].(float64) != 2 {
t.Fatalf("unexpected youth enable payload: %#v", data)