|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "code-kanban/utils" |
| 13 | + |
| 14 | + "gopkg.in/yaml.v3" |
| 15 | +) |
| 16 | + |
| 17 | +type pageTitleSettingsResponse struct { |
| 18 | + Item pageTitleSettings `json:"item"` |
| 19 | +} |
| 20 | + |
| 21 | +func TestSystemPageTitleSettingsGetReturnsServerValue(t *testing.T) { |
| 22 | + cfg, _ := loadSystemDailyTipTestConfig(t, ` |
| 23 | +ui: |
| 24 | + pageTitle: Staging Board |
| 25 | +`) |
| 26 | + if got := cfg.UI.PageTitle; got != "Staging Board" { |
| 27 | + t.Fatalf("configured title = %q, want %q", got, "Staging Board") |
| 28 | + } |
| 29 | + app := newSystemDailyTipTestApp(t, cfg) |
| 30 | + |
| 31 | + resp := mustSystemDailyTipTestRequest(t, app, http.MethodGet, "/api/v1/system/page-title-settings", nil) |
| 32 | + if resp.StatusCode != http.StatusOK { |
| 33 | + t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusOK) |
| 34 | + } |
| 35 | + |
| 36 | + rawBody, err := io.ReadAll(resp.Body) |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("read response failed: %v", err) |
| 39 | + } |
| 40 | + var payload pageTitleSettingsResponse |
| 41 | + if err := json.Unmarshal(rawBody, &payload); err != nil { |
| 42 | + t.Fatalf("decode response failed: %v; body=%s", err, string(rawBody)) |
| 43 | + } |
| 44 | + if got := payload.Item.Title; got != "Staging Board" { |
| 45 | + t.Fatalf("title = %q, want %q; body=%s", got, "Staging Board", string(rawBody)) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestSystemPageTitleSettingsUpdateNormalizesAndPersistsConfig(t *testing.T) { |
| 50 | + cfg, configPath := loadSystemDailyTipTestConfig(t, ` |
| 51 | +ui: |
| 52 | + pageTitle: Code Kanban |
| 53 | +`) |
| 54 | + app := newSystemDailyTipTestApp(t, cfg) |
| 55 | + |
| 56 | + body, err := json.Marshal(pageTitleSettings{Title: " 工作实例 🚀 "}) |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("marshal request failed: %v", err) |
| 59 | + } |
| 60 | + resp := mustSystemDailyTipTestRequest( |
| 61 | + t, |
| 62 | + app, |
| 63 | + http.MethodPost, |
| 64 | + "/api/v1/system/page-title-settings/update", |
| 65 | + bytes.NewBuffer(body), |
| 66 | + ) |
| 67 | + if resp.StatusCode != http.StatusOK { |
| 68 | + rawBody, _ := io.ReadAll(resp.Body) |
| 69 | + t.Fatalf("status = %d, want %d; body=%s", resp.StatusCode, http.StatusOK, string(rawBody)) |
| 70 | + } |
| 71 | + if got := cfg.UI.PageTitle; got != "工作实例 🚀" { |
| 72 | + t.Fatalf("in-memory title = %q, want %q", got, "工作实例 🚀") |
| 73 | + } |
| 74 | + |
| 75 | + rewritten, err := os.ReadFile(configPath) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("read config failed: %v", err) |
| 78 | + } |
| 79 | + var persisted utils.AppConfig |
| 80 | + if err := yaml.Unmarshal(rewritten, &persisted); err != nil { |
| 81 | + t.Fatalf("parse persisted config failed: %v", err) |
| 82 | + } |
| 83 | + if got := persisted.UI.PageTitle; got != "工作实例 🚀" { |
| 84 | + t.Fatalf("persisted title = %q, want %q", got, "工作实例 🚀") |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestSystemPageTitleSettingsUpdateBlankRestoresDefault(t *testing.T) { |
| 89 | + cfg, _ := loadSystemDailyTipTestConfig(t, ` |
| 90 | +ui: |
| 91 | + pageTitle: Staging Board |
| 92 | +`) |
| 93 | + app := newSystemDailyTipTestApp(t, cfg) |
| 94 | + |
| 95 | + resp := mustSystemDailyTipTestRequest( |
| 96 | + t, |
| 97 | + app, |
| 98 | + http.MethodPost, |
| 99 | + "/api/v1/system/page-title-settings/update", |
| 100 | + bytes.NewBufferString(`{"title":" "}`), |
| 101 | + ) |
| 102 | + if resp.StatusCode != http.StatusOK { |
| 103 | + t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusOK) |
| 104 | + } |
| 105 | + if got := cfg.UI.PageTitle; got != utils.DefaultPageTitle { |
| 106 | + t.Fatalf("title = %q, want %q", got, utils.DefaultPageTitle) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestSystemPageTitleSettingsUpdateRejectsInvalidTitles(t *testing.T) { |
| 111 | + tests := []struct { |
| 112 | + name string |
| 113 | + title string |
| 114 | + }{ |
| 115 | + {name: "control character", title: "Bad\nTitle"}, |
| 116 | + {name: "too long", title: strings.Repeat("界", utils.MaxPageTitleRunes+1)}, |
| 117 | + } |
| 118 | + |
| 119 | + for _, tt := range tests { |
| 120 | + t.Run(tt.name, func(t *testing.T) { |
| 121 | + cfg, _ := loadSystemDailyTipTestConfig(t, ` |
| 122 | +ui: |
| 123 | + pageTitle: Original |
| 124 | +`) |
| 125 | + app := newSystemDailyTipTestApp(t, cfg) |
| 126 | + body, err := json.Marshal(pageTitleSettings{Title: tt.title}) |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("marshal request failed: %v", err) |
| 129 | + } |
| 130 | + |
| 131 | + resp := mustSystemDailyTipTestRequest( |
| 132 | + t, |
| 133 | + app, |
| 134 | + http.MethodPost, |
| 135 | + "/api/v1/system/page-title-settings/update", |
| 136 | + bytes.NewBuffer(body), |
| 137 | + ) |
| 138 | + if resp.StatusCode != http.StatusBadRequest { |
| 139 | + rawBody, _ := io.ReadAll(resp.Body) |
| 140 | + t.Fatalf("status = %d, want %d; body=%s", resp.StatusCode, http.StatusBadRequest, string(rawBody)) |
| 141 | + } |
| 142 | + if got := cfg.UI.PageTitle; got != "Original" { |
| 143 | + t.Fatalf("invalid update changed title to %q", got) |
| 144 | + } |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestPageTitleSettingsReadIsAnonymousButUpdateIsProtected(t *testing.T) { |
| 150 | + cfg := newAuthTestConfig() |
| 151 | + if !isAnonymousPath("/api/v1/system/page-title-settings", cfg) { |
| 152 | + t.Fatal("expected page title read endpoint to allow anonymous access") |
| 153 | + } |
| 154 | + if isAnonymousPath("/api/v1/system/page-title-settings/update", cfg) { |
| 155 | + t.Fatal("expected page title update endpoint to require protected access") |
| 156 | + } |
| 157 | +} |
0 commit comments