| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/cookiejar" |
| 8 | "net/http/httptest" |
| 9 | "net/url" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "gitlab.com/Timfa/bluegit/internal/access" |
| 16 | "gitlab.com/Timfa/bluegit/internal/auth" |
| 17 | "gitlab.com/Timfa/bluegit/internal/config" |
| 18 | "gitlab.com/Timfa/bluegit/internal/store" |
| 19 | ) |
| 20 | |
| 21 | func TestProfilePage(t *testing.T) { |
| 22 | storage := t.TempDir() |
| 23 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | t.Cleanup(func() { _ = st.Close() }) |
| 28 | cfg := config.Config{ |
| 29 | BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage, |
| 30 | Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test"}, |
| 31 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 32 | } |
| 33 | cfg.Auth.BaseURL = "https://auth.timfalken.com" |
| 34 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"} |
| 35 | ts := httptest.NewServer(srv.Handler()) |
| 36 | t.Cleanup(ts.Close) |
| 37 | srv.Cfg.BaseURL = ts.URL |
| 38 | |
| 39 | jar, _ := cookiejar.New(nil) |
| 40 | client := &http.Client{Jar: jar} |
| 41 | res, err := client.Get(ts.URL + "/login") |
| 42 | if err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | page := read(t, res) |
| 46 | ctx := context.Background() |
| 47 | tim, err := st.UserByUsername(ctx, "tim") |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | ada, err := st.UpsertUser(ctx, 2, "ada", "", false) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | plein, err := st.CreateGroup(ctx, tim.ID, "Plein", "plein", "open plein", access.RoleDeveloper) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | if err := st.UpdateGroup(ctx, plein.ID, "open plein", "public", access.RoleDeveloper); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | if err := st.SetLogoMIME(ctx, "group", plein.ID, "image/png"); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | kluis, err := st.CreateGroup(ctx, tim.ID, "Kluis", "kluis", "dichte kluis", access.RoleDeveloper) |
| 66 | if err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | zichtbaar, err := st.CreateRepo(ctx, "user", tim.ID, "zichtbaar", "Zichtbare repo", "master", "public") |
| 70 | if err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | if err := st.SetLogoMIME(ctx, "repo", zichtbaar.ID, "image/png"); err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | geheim, err := st.CreateRepo(ctx, "user", tim.ID, "geheim", "Geheim project", "master", "private") |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | if _, err := st.CreateRepo(ctx, "group", plein.ID, "gedeeld", "Groepsrepo", "master", "public"); err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | if err := st.AddContributions(ctx, []store.Contribution{ |
| 84 | {UserID: tim.ID, RepoID: zichtbaar.ID, Kind: "commit", Ref: "pub", At: time.Now()}, |
| 85 | {UserID: tim.ID, RepoID: geheim.ID, Kind: "commit", Ref: "priv", At: time.Now()}, |
| 86 | }); err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | if err := st.SetMember(ctx, "repo", repoID(t, st, "tim", "geheim"), "ada", access.RoleDeveloper); err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | if err := st.SetMember(ctx, "group", kluis.ID, "ada", access.RoleDeveloper); err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | |
| 96 | share := ts.URL + "/users/tim" |
| 97 | res, err = client.Get(ts.URL + "/") |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | dash := read(t, res) |
| 102 | for _, want := range []string{ |
| 103 | `class="usercard panel"`, |
| 104 | `<h2>tim</h2>`, |
| 105 | `@tim`, |
| 106 | `data-copy="` + share + `"`, |
| 107 | `href="/settings">Edit profile`, |
| 108 | `href="/users/tim"`, |
| 109 | `<strong>2</strong> <span>Repositories</span>`, |
| 110 | `<strong>2</strong> <span>Groups</span>`, |
| 111 | "plein/gedeeld", |
| 112 | "Activity", |
| 113 | } { |
| 114 | if !strings.Contains(dash, want) { |
| 115 | t.Fatalf("dashboard mist %q", want) |
| 116 | } |
| 117 | } |
| 118 | if strings.Index(dash, "Activity") > strings.Index(dash, "Repositories</span></h2>") { |
| 119 | t.Fatal("heatmap staat onder de repositorylijst") |
| 120 | } |
| 121 | |
| 122 | anon := &http.Client{} |
| 123 | res, err = anon.Get(share) |
| 124 | if err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
| 127 | pub := read(t, res) |
| 128 | for _, want := range []string{ |
| 129 | "<h1>tim</h1>", |
| 130 | `@tim`, |
| 131 | "Member since", |
| 132 | `data-copy="` + share + `"`, |
| 133 | `src="/tim/zichtbaar/-/logo"`, |
| 134 | `href="/tim/zichtbaar"`, |
| 135 | "Zichtbare repo", |
| 136 | `src="/groups/plein/logo"`, |
| 137 | `href="/groups/plein"`, |
| 138 | `<strong>1</strong> <span>Repositories</span>`, |
| 139 | `<strong>1</strong> <span>Groups</span>`, |
| 140 | `<strong>1</strong> <span>Contributions</span>`, |
| 141 | } { |
| 142 | if !strings.Contains(pub, want) { |
| 143 | t.Fatalf("openbaar profiel mist %q", want) |
| 144 | } |
| 145 | } |
| 146 | for _, ban := range []string{"geheim", "Geheim project", "kluis", "Kluis", "Edit profile", "plein/gedeeld"} { |
| 147 | if strings.Contains(pub, ban) { |
| 148 | t.Fatalf("openbaar profiel toont %q", ban) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | adaJar, _ := cookiejar.New(nil) |
| 153 | adaClient := &http.Client{Jar: adaJar} |
| 154 | sess, err := st.CreateSession(ctx, ada.ID, "dev", time.Now().Add(time.Hour)) |
| 155 | if err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | u, _ := url.Parse(ts.URL) |
| 159 | adaJar.SetCookies(u, []*http.Cookie{{Name: "bluegit_session", Value: sess.ID, Path: "/"}}) |
| 160 | res, err = adaClient.Get(share) |
| 161 | if err != nil { |
| 162 | t.Fatal(err) |
| 163 | } |
| 164 | adaPage := read(t, res) |
| 165 | for _, want := range []string{ |
| 166 | `href="/tim/geheim"`, |
| 167 | "Geheim project", |
| 168 | `href="/groups/kluis"`, |
| 169 | `<strong>2</strong> <span>Repositories</span>`, |
| 170 | `<strong>2</strong> <span>Groups</span>`, |
| 171 | `<strong>2</strong> <span>Contributions</span>`, |
| 172 | } { |
| 173 | if !strings.Contains(adaPage, want) { |
| 174 | t.Fatalf("ada ziet niet %q", want) |
| 175 | } |
| 176 | } |
| 177 | if strings.Contains(adaPage, "Edit profile") || strings.Contains(adaPage, "plein/gedeeld") { |
| 178 | t.Fatal("ada ziet Tims bewerkknop of een groepsrepository") |
| 179 | } |
| 180 | |
| 181 | res, err = client.Get(ts.URL + "/settings") |
| 182 | if err != nil { |
| 183 | t.Fatal(err) |
| 184 | } |
| 185 | settings := read(t, res) |
| 186 | if !strings.Contains(settings, "Display name") || !strings.Contains(settings, "Bio") { |
| 187 | t.Fatal("instellingen missen weergavenaam of bio") |
| 188 | } |
| 189 | res, err = client.PostForm(ts.URL+"/settings/profile", url.Values{ |
| 190 | "csrf": {csrfOf(t, settings)}, "name": {"Regel\neen"}, "bio": {"nee"}, |
| 191 | }) |
| 192 | if err != nil { |
| 193 | t.Fatal(err) |
| 194 | } |
| 195 | bad := read(t, res) |
| 196 | if !strings.Contains(bad, "line break") { |
| 197 | t.Fatalf("ongeldige naam:\n%s", bad) |
| 198 | } |
| 199 | res, err = client.PostForm(ts.URL+"/settings/profile", url.Values{ |
| 200 | "csrf": {csrfOf(t, bad)}, "name": {"Tim Falken"}, "bio": {"Bouwt BlueGit."}, |
| 201 | }) |
| 202 | if err != nil { |
| 203 | t.Fatal(err) |
| 204 | } |
| 205 | saved := read(t, res) |
| 206 | if !strings.Contains(saved, "Profile saved") || !strings.Contains(saved, `value="Tim Falken"`) { |
| 207 | t.Fatalf("profiel opslaan:\n%s", saved) |
| 208 | } |
| 209 | res, err = client.Get(share) |
| 210 | if err != nil { |
| 211 | t.Fatal(err) |
| 212 | } |
| 213 | named := read(t, res) |
| 214 | for _, want := range []string{"<h1>Tim Falken</h1>", "@tim", "Bouwt BlueGit.", "Edit profile"} { |
| 215 | if !strings.Contains(named, want) { |
| 216 | t.Fatalf("profiel na opslaan mist %q", want) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | timToken, err := st.CreateToken(ctx, tim.ID, "tim") |
| 221 | if err != nil { |
| 222 | t.Fatal(err) |
| 223 | } |
| 224 | adaToken, err := st.CreateToken(ctx, ada.ID, "ada") |
| 225 | if err != nil { |
| 226 | t.Fatal(err) |
| 227 | } |
| 228 | mine := apiGet(t, ts.URL+"/api/v1/users/tim", timToken) |
| 229 | if mine["name"] != "Tim Falken" || mine["bio"] != "Bouwt BlueGit." || mine["web_url"] != share { |
| 230 | t.Fatalf("api zelf: %#v", mine) |
| 231 | } |
| 232 | if mine["instance_admin"] != true || mine["language"] != "en" { |
| 233 | t.Fatalf("api zelf mist eigen velden: %#v", mine) |
| 234 | } |
| 235 | counts := mine["counts"].(map[string]any) |
| 236 | if counts["repos"] != 2.0 || counts["groups"] != 2.0 || counts["contributions"] != 2.0 { |
| 237 | t.Fatalf("api tellingen: %#v", counts) |
| 238 | } |
| 239 | if !apiPaths(mine["repos"]).has("tim/zichtbaar", "tim/geheim") || apiPaths(mine["repos"]).has("plein/gedeeld") { |
| 240 | t.Fatalf("api repos: %#v", mine["repos"]) |
| 241 | } |
| 242 | if !apiPaths(mine["groups"]).has("plein", "kluis") { |
| 243 | t.Fatalf("api groepen: %#v", mine["groups"]) |
| 244 | } |
| 245 | other := apiGet(t, ts.URL+"/api/v1/users/tim", adaToken) |
| 246 | if _, ok := other["instance_admin"]; ok || other["language"] != nil { |
| 247 | t.Fatalf("api van ada toont Tims privévelden: %#v", other) |
| 248 | } |
| 249 | if !apiPaths(other["repos"]).has("tim/geheim") || !apiPaths(other["groups"]).has("kluis") { |
| 250 | t.Fatalf("api van ada mist privé: %#v %#v", other["repos"], other["groups"]) |
| 251 | } |
| 252 | if other["counts"].(map[string]any)["contributions"] != 2.0 { |
| 253 | t.Fatalf("api van ada telt verborgen bijdragen niet mee: %#v", other["counts"]) |
| 254 | } |
| 255 | pubAPI := apiGet(t, ts.URL+"/api/v1/users/tim", "") |
| 256 | if _, ok := pubAPI["instance_admin"]; ok || pubAPI["language"] != nil { |
| 257 | t.Fatalf("anonieme api toont privévelden: %#v", pubAPI) |
| 258 | } |
| 259 | if apiPaths(pubAPI["repos"]).has("tim/geheim") || apiPaths(pubAPI["groups"]).has("kluis") || !apiPaths(pubAPI["repos"]).has("tim/zichtbaar") { |
| 260 | t.Fatalf("anonieme api filter: %#v %#v", pubAPI["repos"], pubAPI["groups"]) |
| 261 | } |
| 262 | if pubAPI["counts"].(map[string]any)["contributions"] != 1.0 { |
| 263 | t.Fatalf("anonieme api lekt privébijdragen: %#v", pubAPI["counts"]) |
| 264 | } |
| 265 | badTok, err := http.NewRequest(http.MethodGet, ts.URL+"/api/v1/users/tim", nil) |
| 266 | if err != nil { |
| 267 | t.Fatal(err) |
| 268 | } |
| 269 | badTok.Header.Set("Authorization", "Bearer bg_nietgeldig") |
| 270 | res, err = http.DefaultClient.Do(badTok) |
| 271 | if err != nil { |
| 272 | t.Fatal(err) |
| 273 | } |
| 274 | if res.StatusCode != http.StatusUnauthorized { |
| 275 | t.Fatalf("ongeldig token %d", res.StatusCode) |
| 276 | } |
| 277 | _ = res.Body.Close() |
| 278 | req, err := http.NewRequest(http.MethodPut, ts.URL+"/api/v1/user", strings.NewReader(`{"bio":"alleen bio"}`)) |
| 279 | if err != nil { |
| 280 | t.Fatal(err) |
| 281 | } |
| 282 | req.Header.Set("Authorization", "Bearer "+timToken) |
| 283 | req.Header.Set("Content-Type", "application/json") |
| 284 | res, err = http.DefaultClient.Do(req) |
| 285 | if err != nil { |
| 286 | t.Fatal(err) |
| 287 | } |
| 288 | put := decodeJSON(t, res) |
| 289 | if put["bio"] != "alleen bio" || put["name"] != "Tim Falken" || put["language"] != "en" { |
| 290 | t.Fatalf("put bio: %#v", put) |
| 291 | } |
| 292 | |
| 293 | noredir := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} |
| 294 | res, err = noredir.Get(ts.URL + "/tim") |
| 295 | if err != nil { |
| 296 | t.Fatal(err) |
| 297 | } |
| 298 | loc := res.Header.Get("Location") |
| 299 | if res.StatusCode != http.StatusFound || loc != "/users/tim" { |
| 300 | t.Fatalf("/tim is %d locatie %q", res.StatusCode, loc) |
| 301 | } |
| 302 | _ = res.Body.Close() |
| 303 | res, err = noredir.Get(ts.URL + "/plein") |
| 304 | if err != nil { |
| 305 | t.Fatal(err) |
| 306 | } |
| 307 | if res.StatusCode != http.StatusNotFound || res.Header.Get("Location") != "" { |
| 308 | t.Fatalf("groepsnaam /plein is %d locatie %q", res.StatusCode, res.Header.Get("Location")) |
| 309 | } |
| 310 | _ = res.Body.Close() |
| 311 | res, err = noredir.Get(ts.URL + "/tim/zichtbaar") |
| 312 | if err != nil { |
| 313 | t.Fatal(err) |
| 314 | } |
| 315 | if res.StatusCode == http.StatusFound && strings.HasPrefix(res.Header.Get("Location"), "/users/") { |
| 316 | t.Fatalf("repositorypad wijst naar een profiel: %s", res.Header.Get("Location")) |
| 317 | } |
| 318 | _ = res.Body.Close() |
| 319 | res, err = noredir.Get(ts.URL + "/login") |
| 320 | if err != nil { |
| 321 | t.Fatal(err) |
| 322 | } |
| 323 | if strings.Contains(res.Header.Get("Location"), "/users/") { |
| 324 | t.Fatalf("login wijst naar een profiel: %s", res.Header.Get("Location")) |
| 325 | } |
| 326 | _ = res.Body.Close() |
| 327 | res, err = anon.Get(ts.URL + "/users/nobody") |
| 328 | if err != nil { |
| 329 | t.Fatal(err) |
| 330 | } |
| 331 | if res.StatusCode != http.StatusNotFound { |
| 332 | t.Fatalf("onbekend profiel %d", res.StatusCode) |
| 333 | } |
| 334 | _ = res.Body.Close() |
| 335 | _ = page |
| 336 | } |
| 337 | |
| 338 | func repoID(t *testing.T, st *store.Store, owner, name string) int64 { |
| 339 | t.Helper() |
| 340 | repo, err := st.RepoByPath(context.Background(), owner, name) |
| 341 | if err != nil { |
| 342 | t.Fatal(err) |
| 343 | } |
| 344 | return repo.ID |
| 345 | } |
| 346 | |
| 347 | func apiGet(t *testing.T, rawURL, token string) map[string]any { |
| 348 | t.Helper() |
| 349 | req, err := http.NewRequest(http.MethodGet, rawURL, nil) |
| 350 | if err != nil { |
| 351 | t.Fatal(err) |
| 352 | } |
| 353 | if token != "" { |
| 354 | req.Header.Set("Authorization", "Bearer "+token) |
| 355 | } |
| 356 | res, err := http.DefaultClient.Do(req) |
| 357 | if err != nil { |
| 358 | t.Fatal(err) |
| 359 | } |
| 360 | body := decodeJSON(t, res) |
| 361 | if res.StatusCode != http.StatusOK { |
| 362 | t.Fatalf("api %s: %d %#v", rawURL, res.StatusCode, body) |
| 363 | } |
| 364 | return body |
| 365 | } |
| 366 | |
| 367 | func decodeJSON(t *testing.T, res *http.Response) map[string]any { |
| 368 | t.Helper() |
| 369 | defer res.Body.Close() |
| 370 | var body map[string]any |
| 371 | if err := json.NewDecoder(res.Body).Decode(&body); err != nil { |
| 372 | t.Fatal(err) |
| 373 | } |
| 374 | return body |
| 375 | } |
| 376 | |
| 377 | type pathSet map[string]bool |
| 378 | |
| 379 | func apiPaths(v any) pathSet { |
| 380 | out := pathSet{} |
| 381 | list, _ := v.([]any) |
| 382 | for _, item := range list { |
| 383 | row, _ := item.(map[string]any) |
| 384 | if p, ok := row["path"].(string); ok { |
| 385 | out[p] = true |
| 386 | } |
| 387 | } |
| 388 | return out |
| 389 | } |
| 390 | |
| 391 | func (p pathSet) has(want ...string) bool { |
| 392 | for _, w := range want { |
| 393 | if !p[w] { |
| 394 | return false |
| 395 | } |
| 396 | } |
| 397 | return true |
| 398 | } |