@@ -0,0 +1,102 @@ |
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package auth_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func loginAppearanceUser(t *testing.T, name string) *client { |
| 14 | + t.Helper() |
| 15 | + httpsrv, captor := newTestServer(t, false) |
| 16 | + cli := newClient(t, httpsrv) |
| 17 | + mustSignup(t, cli, name, name+"@example.com", "correct horse battery staple") |
| 18 | + tok := extractTokenFromMessage(t, captor.all()[0], "/verify-email") |
| 19 | + _ = cli.get(t, "/verify-email/"+tok).Body.Close() |
| 20 | + |
| 21 | + csrf := cli.extractCSRF(t, "/login") |
| 22 | + resp := cli.post(t, "/login", url.Values{ |
| 23 | + "csrf_token": {csrf}, |
| 24 | + "username": {name}, |
| 25 | + "password": {"correct horse battery staple"}, |
| 26 | + }) |
| 27 | + if resp.StatusCode != http.StatusSeeOther { |
| 28 | + t.Fatalf("login: %d", resp.StatusCode) |
| 29 | + } |
| 30 | + _ = resp.Body.Close() |
| 31 | + return cli |
| 32 | +} |
| 33 | + |
| 34 | +func TestAppearance_PersistsTheme(t *testing.T) { |
| 35 | + t.Parallel() |
| 36 | + cli := loginAppearanceUser(t, "appra") |
| 37 | + csrf := cli.extractCSRF(t, "/settings/appearance") |
| 38 | + resp := cli.post(t, "/settings/appearance", url.Values{ |
| 39 | + "csrf_token": {csrf}, |
| 40 | + "theme": {"dark"}, |
| 41 | + }) |
| 42 | + defer func() { _ = resp.Body.Close() }() |
| 43 | + body, _ := io.ReadAll(resp.Body) |
| 44 | + if !strings.Contains(string(body), "Appearance updated") { |
| 45 | + t.Fatalf("expected success, got: %s", body) |
| 46 | + } |
| 47 | + if !strings.Contains(string(body), "THEME=dark") { |
| 48 | + t.Fatalf("expected THEME=dark, got: %s", body) |
| 49 | + } |
| 50 | + |
| 51 | + // Cookie should also be set. |
| 52 | + var found bool |
| 53 | + for _, c := range resp.Cookies() { |
| 54 | + if c.Name == "theme" && c.Value == "dark" { |
| 55 | + found = true |
| 56 | + break |
| 57 | + } |
| 58 | + } |
| 59 | + if !found { |
| 60 | + t.Fatalf("expected theme=dark cookie in response; got %+v", resp.Cookies()) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestAppearance_RejectsUnknownTheme(t *testing.T) { |
| 65 | + t.Parallel() |
| 66 | + cli := loginAppearanceUser(t, "apprb") |
| 67 | + csrf := cli.extractCSRF(t, "/settings/appearance") |
| 68 | + resp := cli.post(t, "/settings/appearance", url.Values{ |
| 69 | + "csrf_token": {csrf}, |
| 70 | + "theme": {"neon"}, |
| 71 | + }) |
| 72 | + defer func() { _ = resp.Body.Close() }() |
| 73 | + body, _ := io.ReadAll(resp.Body) |
| 74 | + if !strings.Contains(string(body), "Unknown theme") { |
| 75 | + t.Fatalf("expected unknown-theme error, got: %s", body) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestAppearance_EmptyClearsCookie(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + cli := loginAppearanceUser(t, "apprc") |
| 82 | + |
| 83 | + // Set a theme first. |
| 84 | + csrf := cli.extractCSRF(t, "/settings/appearance") |
| 85 | + _ = cli.post(t, "/settings/appearance", url.Values{ |
| 86 | + "csrf_token": {csrf}, |
| 87 | + "theme": {"dark"}, |
| 88 | + }).Body.Close() |
| 89 | + |
| 90 | + // Now clear it. |
| 91 | + csrf = cli.extractCSRF(t, "/settings/appearance") |
| 92 | + resp := cli.post(t, "/settings/appearance", url.Values{ |
| 93 | + "csrf_token": {csrf}, |
| 94 | + "theme": {""}, |
| 95 | + }) |
| 96 | + defer func() { _ = resp.Body.Close() }() |
| 97 | + for _, c := range resp.Cookies() { |
| 98 | + if c.Name == "theme" && c.MaxAge >= 0 { |
| 99 | + t.Fatalf("expected theme cookie cleared (MaxAge<0); got %+v", c) |
| 100 | + } |
| 101 | + } |
| 102 | +} |