Handle, Anzeigename, Beschreibung, Profilbilder, Profil- und Kontodaten, Passwortprüfung und das Löschen der kontoeigenen Tabellen lagen als rohes SQL in den Handlern, während account.Store nur Anlegen, Reset und Passwortwechsel kannte. Jetzt ist der Store die eine Stelle für die Tabelle account; die Handler lesen das Formular und antworten. Nebenbei: die Passwortprüfung gab es zweimal mit zwei Texten unter demselben Code auth.bad_password (ChangePassword und checkPassword). Jetzt VerifyPassword mit einem Sentinel, das Formularfeld hängt der Handler an. Profil und Konto gehen als Structs statt Maps über die Leitung, wie die übrigen Antworten. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
183 lines
6.0 KiB
Go
183 lines
6.0 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"image"
|
|
"image/png"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"kver/internal/account"
|
|
)
|
|
|
|
type profileJSON struct {
|
|
Header string `json:"header"`
|
|
Bio string `json:"bio"`
|
|
Entries int64 `json:"entries"`
|
|
}
|
|
|
|
func getProfile(t *testing.T, srv *testServer, username string) profileJSON {
|
|
t.Helper()
|
|
resp, err := http.Get(srv.URL + "/api/u/" + username + "/info")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
var p profileJSON
|
|
if err := json.NewDecoder(resp.Body).Decode(&p); err != nil {
|
|
t.Fatalf("decode profil: %v", err)
|
|
}
|
|
return p
|
|
}
|
|
|
|
// uploadImage schickt ein kleines PNG als Feld field an urlStr.
|
|
func uploadImage(t *testing.T, c *http.Client, urlStr, field string) int {
|
|
t.Helper()
|
|
var body bytes.Buffer
|
|
mw := multipart.NewWriter(&body)
|
|
fw, err := mw.CreateFormFile(field, "bild.png")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := png.Encode(fw, image.NewRGBA(image.Rect(0, 0, 30, 10))); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mw.Close()
|
|
req, err := http.NewRequest(http.MethodPost, urlStr, &body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("Content-Type", mw.FormDataContentType())
|
|
req.Header.Set("Accept", "application/json")
|
|
resp := do(t, c, req)
|
|
resp.Body.Close()
|
|
return resp.StatusCode
|
|
}
|
|
|
|
func TestProfileBio(t *testing.T) {
|
|
t.Parallel()
|
|
srv := newTestServer(t)
|
|
alice := registerAndLogin(t, srv, "alice")
|
|
createEntry(t, alice, srv, post("eins"))
|
|
createEntry(t, alice, srv, post("zwei"))
|
|
|
|
if got := postStatus(t, newClient(t), srv.URL+"/api/user/bio", url.Values{"bio": {"x"}}); got != http.StatusUnauthorized {
|
|
t.Fatalf("bio ohne login: erwartet 401, bekam %d", got)
|
|
}
|
|
|
|
// Zeilenumbrüche werden normalisiert, Überlänge runenweise gekürzt.
|
|
long := " Zeile 1\r\nZeile 2 " + strings.Repeat("ö", 400)
|
|
if got := postStatus(t, alice, srv.URL+"/api/user/bio", url.Values{"bio": {long}}); got != http.StatusOK {
|
|
t.Fatalf("bio: erwartet 200, bekam %d", got)
|
|
}
|
|
p := getProfile(t, srv, "alice")
|
|
if !strings.HasPrefix(p.Bio, "Zeile 1\nZeile 2 ö") || len([]rune(p.Bio)) != account.MaxBioRunes {
|
|
t.Fatalf("bio falsch gespeichert: %q (%d Runen)", p.Bio, len([]rune(p.Bio)))
|
|
}
|
|
if p.Entries != 2 {
|
|
t.Fatalf("beitragszahl: erwartet 2, bekam %d", p.Entries)
|
|
}
|
|
|
|
// Leer löscht die Beschreibung.
|
|
postStatus(t, alice, srv.URL+"/api/user/bio", url.Values{"bio": {" "}})
|
|
if p := getProfile(t, srv, "alice"); p.Bio != "" {
|
|
t.Fatalf("bio nicht gelöscht: %q", p.Bio)
|
|
}
|
|
}
|
|
|
|
func TestProfileHeader(t *testing.T) {
|
|
t.Parallel()
|
|
srv := newTestServer(t)
|
|
alice := registerAndLogin(t, srv, "alice")
|
|
exists := func(path string) bool {
|
|
_, err := os.Stat(filepath.Join(srv.Media.Root, filepath.FromSlash(path)))
|
|
return err == nil
|
|
}
|
|
|
|
if got := postStatus(t, alice, srv.URL+"/api/user/header", url.Values{}); got != http.StatusBadRequest {
|
|
t.Fatalf("header ohne bild: erwartet 400, bekam %d", got)
|
|
}
|
|
if got := uploadImage(t, alice, srv.URL+"/api/user/header", "header"); got != http.StatusOK {
|
|
t.Fatalf("header: erwartet 200, bekam %d", got)
|
|
}
|
|
first := getProfile(t, srv, "alice").Header
|
|
if first == "" || !exists(first) {
|
|
t.Fatalf("header nicht gespeichert: %q", first)
|
|
}
|
|
|
|
// Ein neues Bild ersetzt das alte, die alte Datei verschwindet.
|
|
uploadImage(t, alice, srv.URL+"/api/user/header", "header")
|
|
second := getProfile(t, srv, "alice").Header
|
|
if second == first || exists(first) || !exists(second) {
|
|
t.Fatalf("header nicht ersetzt: alt %q, neu %q", first, second)
|
|
}
|
|
|
|
if got := postStatus(t, alice, srv.URL+"/api/user/header/remove", url.Values{}); got != http.StatusOK {
|
|
t.Fatalf("header entfernen: erwartet 200, bekam %d", got)
|
|
}
|
|
if p := getProfile(t, srv, "alice"); p.Header != "" || exists(second) {
|
|
t.Fatalf("header nicht entfernt: %q", p.Header)
|
|
}
|
|
|
|
// Kontolöschung räumt das Header-Bild mit ab.
|
|
uploadImage(t, alice, srv.URL+"/api/user/header", "header")
|
|
third := getProfile(t, srv, "alice").Header
|
|
postStatus(t, alice, srv.URL+"/api/user/delete", url.Values{"pass1": {"supersecret1"}})
|
|
if exists(third) {
|
|
t.Fatal("header nach kontolöschung noch vorhanden")
|
|
}
|
|
}
|
|
|
|
// TestChangePassword: falsches bisheriges oder ungültiges neues Passwort wird
|
|
// abgewiesen; danach gilt nur das neue, die eigene Session bleibt, andere
|
|
// enden.
|
|
func TestChangePassword(t *testing.T) {
|
|
t.Parallel()
|
|
srv := newTestServer(t)
|
|
alice := registerAndLogin(t, srv, "alice")
|
|
other := newClient(t)
|
|
postForm(t, other, srv.URL+"/api/auth/login", url.Values{
|
|
"user": {"alice"}, "pass": {"supersecret1"}, "timeout": {"86400"},
|
|
}).Body.Close()
|
|
|
|
change := func(cur, pass1, pass2 string) int {
|
|
return postStatus(t, alice, srv.URL+"/api/user/password",
|
|
url.Values{"pass": {cur}, "pass1": {pass1}, "pass2": {pass2}})
|
|
}
|
|
if got := change("falsch", "neuesgeheim1", "neuesgeheim1"); got != http.StatusForbidden {
|
|
t.Fatalf("falsches bisheriges: erwartet 403, bekam %d", got)
|
|
}
|
|
if got := change("supersecret1", "kurz", "kurz"); got != http.StatusBadRequest {
|
|
t.Fatalf("zu kurz: erwartet 400, bekam %d", got)
|
|
}
|
|
if got := change("supersecret1", "neuesgeheim1", "anders12345"); got != http.StatusBadRequest {
|
|
t.Fatalf("ungleich: erwartet 400, bekam %d", got)
|
|
}
|
|
if got := change("supersecret1", "neuesgeheim1", "neuesgeheim1"); got != http.StatusOK {
|
|
t.Fatalf("ändern: erwartet 200, bekam %d", got)
|
|
}
|
|
|
|
if got := getStatus(t, alice, srv.URL+"/api/user/info"); got != http.StatusOK {
|
|
t.Fatalf("eigene session: erwartet 200, bekam %d", got)
|
|
}
|
|
if got := getStatus(t, other, srv.URL+"/api/user/info"); got != http.StatusUnauthorized {
|
|
t.Fatalf("andere session: erwartet 401, bekam %d", got)
|
|
}
|
|
login := func(pass string) int {
|
|
return postStatus(t, newClient(t), srv.URL+"/api/auth/login",
|
|
url.Values{"user": {"alice"}, "pass": {pass}, "timeout": {"86400"}})
|
|
}
|
|
if got := login("supersecret1"); got != http.StatusUnauthorized {
|
|
t.Fatalf("altes passwort: erwartet 401, bekam %d", got)
|
|
}
|
|
if got := login("neuesgeheim1"); got != http.StatusOK {
|
|
t.Fatalf("neues passwort: erwartet 200, bekam %d", got)
|
|
}
|
|
}
|