@@ -0,0 +1,152 @@ |
| 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 | +const ( |
| 14 | + //nolint:gosec // G101 false positive: test fixture, not a real credential. |
| 15 | + pwOriginal = "correct horse battery staple" |
| 16 | + //nolint:gosec // G101 false positive: test fixture, not a real credential. |
| 17 | + pwNew = "tr0ub4dor & 3 horseshoes" |
| 18 | +) |
| 19 | + |
| 20 | +func loginPasswordUser(t *testing.T, name string) *client { |
| 21 | + t.Helper() |
| 22 | + httpsrv, captor := newTestServer(t, false) |
| 23 | + cli := newClient(t, httpsrv) |
| 24 | + mustSignup(t, cli, name, name+"@example.com", pwOriginal) |
| 25 | + tok := extractTokenFromMessage(t, captor.all()[0], "/verify-email") |
| 26 | + _ = cli.get(t, "/verify-email/"+tok).Body.Close() |
| 27 | + |
| 28 | + csrf := cli.extractCSRF(t, "/login") |
| 29 | + resp := cli.post(t, "/login", url.Values{ |
| 30 | + "csrf_token": {csrf}, |
| 31 | + "username": {name}, |
| 32 | + "password": {pwOriginal}, |
| 33 | + }) |
| 34 | + if resp.StatusCode != http.StatusSeeOther { |
| 35 | + t.Fatalf("login: %d", resp.StatusCode) |
| 36 | + } |
| 37 | + _ = resp.Body.Close() |
| 38 | + return cli |
| 39 | +} |
| 40 | + |
| 41 | +func TestPasswordChange_Roundtrip(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + cli := loginPasswordUser(t, "pwa") |
| 44 | + csrf := cli.extractCSRF(t, "/settings/password") |
| 45 | + resp := cli.post(t, "/settings/password", url.Values{ |
| 46 | + "csrf_token": {csrf}, |
| 47 | + "current_password": {pwOriginal}, |
| 48 | + "new_password": {pwNew}, |
| 49 | + "confirm_password": {pwNew}, |
| 50 | + }) |
| 51 | + defer func() { _ = resp.Body.Close() }() |
| 52 | + body, _ := io.ReadAll(resp.Body) |
| 53 | + if !strings.Contains(string(body), "Password updated") { |
| 54 | + t.Fatalf("expected success, got: %s", body) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestPasswordChange_RejectsWrongCurrent(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + cli := loginPasswordUser(t, "pwb") |
| 61 | + csrf := cli.extractCSRF(t, "/settings/password") |
| 62 | + resp := cli.post(t, "/settings/password", url.Values{ |
| 63 | + "csrf_token": {csrf}, |
| 64 | + "current_password": {"not-the-right-password"}, |
| 65 | + "new_password": {pwNew}, |
| 66 | + "confirm_password": {pwNew}, |
| 67 | + }) |
| 68 | + defer func() { _ = resp.Body.Close() }() |
| 69 | + body, _ := io.ReadAll(resp.Body) |
| 70 | + if !strings.Contains(string(body), "Current password is incorrect") { |
| 71 | + t.Fatalf("expected incorrect-current error, got: %s", body) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestPasswordChange_RejectsMismatch(t *testing.T) { |
| 76 | + t.Parallel() |
| 77 | + cli := loginPasswordUser(t, "pwc") |
| 78 | + csrf := cli.extractCSRF(t, "/settings/password") |
| 79 | + resp := cli.post(t, "/settings/password", url.Values{ |
| 80 | + "csrf_token": {csrf}, |
| 81 | + "current_password": {pwOriginal}, |
| 82 | + "new_password": {pwNew}, |
| 83 | + "confirm_password": {pwNew + "X"}, |
| 84 | + }) |
| 85 | + defer func() { _ = resp.Body.Close() }() |
| 86 | + body, _ := io.ReadAll(resp.Body) |
| 87 | + // HTML-escaping turns "don't" into "don't", so we check for the |
| 88 | + // stable substring "and confirmation". |
| 89 | + if !strings.Contains(string(body), "and confirmation") { |
| 90 | + t.Fatalf("expected confirmation mismatch error, got: %s", body) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestPasswordChange_RejectsTooShort(t *testing.T) { |
| 95 | + t.Parallel() |
| 96 | + cli := loginPasswordUser(t, "pwd") |
| 97 | + csrf := cli.extractCSRF(t, "/settings/password") |
| 98 | + resp := cli.post(t, "/settings/password", url.Values{ |
| 99 | + "csrf_token": {csrf}, |
| 100 | + "current_password": {pwOriginal}, |
| 101 | + "new_password": {"short"}, |
| 102 | + "confirm_password": {"short"}, |
| 103 | + }) |
| 104 | + defer func() { _ = resp.Body.Close() }() |
| 105 | + body, _ := io.ReadAll(resp.Body) |
| 106 | + if !strings.Contains(string(body), "at least 10") { |
| 107 | + t.Fatalf("expected too-short error, got: %s", body) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestPasswordChange_RejectsCommon(t *testing.T) { |
| 112 | + t.Parallel() |
| 113 | + cli := loginPasswordUser(t, "pwe") |
| 114 | + csrf := cli.extractCSRF(t, "/settings/password") |
| 115 | + resp := cli.post(t, "/settings/password", url.Values{ |
| 116 | + "csrf_token": {csrf}, |
| 117 | + "current_password": {pwOriginal}, |
| 118 | + "new_password": {"qwertyuiop"}, // 10-char common-list entry |
| 119 | + "confirm_password": {"qwertyuiop"}, |
| 120 | + }) |
| 121 | + defer func() { _ = resp.Body.Close() }() |
| 122 | + body, _ := io.ReadAll(resp.Body) |
| 123 | + if !strings.Contains(string(body), "too common") { |
| 124 | + t.Fatalf("expected too-common error, got: %s", body) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestPasswordChange_OriginalNoLongerWorks(t *testing.T) { |
| 129 | + t.Parallel() |
| 130 | + cli := loginPasswordUser(t, "pwf") |
| 131 | + csrf := cli.extractCSRF(t, "/settings/password") |
| 132 | + resp := cli.post(t, "/settings/password", url.Values{ |
| 133 | + "csrf_token": {csrf}, |
| 134 | + "current_password": {pwOriginal}, |
| 135 | + "new_password": {pwNew}, |
| 136 | + "confirm_password": {pwNew}, |
| 137 | + }) |
| 138 | + _ = resp.Body.Close() |
| 139 | + |
| 140 | + // New session, attempt login with the OLD password — must fail. |
| 141 | + cli2 := newClient(t, cli.srv) |
| 142 | + csrf = cli2.extractCSRF(t, "/login") |
| 143 | + resp = cli2.post(t, "/login", url.Values{ |
| 144 | + "csrf_token": {csrf}, |
| 145 | + "username": {"pwf"}, |
| 146 | + "password": {pwOriginal}, |
| 147 | + }) |
| 148 | + defer func() { _ = resp.Body.Close() }() |
| 149 | + if resp.StatusCode == http.StatusSeeOther { |
| 150 | + t.Fatalf("expected old password to fail; login succeeded") |
| 151 | + } |
| 152 | +} |