Set a user's web UI password from the Users section
terdut-server v0.10.2 serves a web UI you sign in to with a password, and every user starts without one. Until now the only way to give somebody their first password was a curl call with an API key. p in Users sets the selected user's password. The form asks for the current password only in the one case the server checks it: you are changing your own password and already have one. The client has no other way to know who its key belongs to, so opening the form calls GET /api/me first and shows the fields once that answers. Setting someone else's password sends no current_password at all, rather than an empty one. Length (at least 10) and the repeated entry are checked before anything is sent, mirroring the server's rule so a typo costs no round trip. The server stays authoritative: a wrong current password comes back as its own 403 message on the dashboard. The status line says the user's other web sessions were signed out, because the server does that on every password change. API keys are not affected. Older servers have no /api/me. The client now returns a typed StatusError carrying the status code, so a 404 there reads as "needs terdut-server v0.10.2 or later" rather than a bare "server returned 404". Its Error() text is unchanged, so every existing message reads as before. Requires terdut-server v0.10.2 only for this form. Everything else works against the same servers as before.
This commit is contained in:
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -363,3 +364,52 @@ func TestIncidentStats_NullAveragesStayNil(t *testing.T) {
|
||||
t.Errorf("expected nil averages, got %v / %v", stats.MTTASeconds, stats.MTTRSeconds)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_Me(t *testing.T) {
|
||||
c, got := stub(t, http.StatusOK, `{"user":{"id":3,"username":"erik"},"has_password":true}`)
|
||||
me, err := c.Me()
|
||||
if err != nil {
|
||||
t.Fatalf("me: %v", err)
|
||||
}
|
||||
if got.method != http.MethodGet || got.path != "/api/me" {
|
||||
t.Errorf("expected GET /api/me, got %s %s", got.method, got.path)
|
||||
}
|
||||
if me.User.ID != 3 || !me.HasPassword {
|
||||
t.Errorf("unexpected decode %+v", me)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_SetPassword(t *testing.T) {
|
||||
c, got := stub(t, http.StatusNoContent, ``)
|
||||
if err := c.SetPassword(2, "a brand new secret", ""); err != nil {
|
||||
t.Fatalf("set password: %v", err)
|
||||
}
|
||||
if got.method != http.MethodPut || got.path != "/api/users/2/password" {
|
||||
t.Errorf("expected PUT /api/users/2/password, got %s %s", got.method, got.path)
|
||||
}
|
||||
// Setting someone else's password carries no current_password at all,
|
||||
// rather than an empty one.
|
||||
if got.body != `{"password":"a brand new secret"}` {
|
||||
t.Errorf("unexpected body %s", got.body)
|
||||
}
|
||||
|
||||
c, got = stub(t, http.StatusNoContent, ``)
|
||||
c.SetPassword(1, "a brand new secret", "the old one")
|
||||
if !strings.Contains(got.body, `"current_password":"the old one"`) {
|
||||
t.Errorf("current password missing from %s", got.body)
|
||||
}
|
||||
}
|
||||
|
||||
// Older servers have no /api/me; the caller tells that apart by the status
|
||||
// code, so the typed error has to carry it.
|
||||
func TestClient_StatusErrorKeepsCodeAndMessage(t *testing.T) {
|
||||
c, _ := stub(t, http.StatusNotFound, `404 page not found`)
|
||||
_, err := c.Me()
|
||||
var se *StatusError
|
||||
if !errors.As(err, &se) || se.Code != http.StatusNotFound {
|
||||
t.Fatalf("expected a 404 StatusError, got %v", err)
|
||||
}
|
||||
if err.Error() != "server returned 404" {
|
||||
t.Errorf("message changed: %q", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user