Add notify-creds subcommand to (re)email members git + mailbox creds

`agentbbs notify-creds` backfills credential emails to verified members
who signed up before the git/mailbox welcome emails existed.

- git (all verified): forgejo.EnsureUserReset resets each account to a
  fresh one-time password (must-change) and emails the web login link,
  username, and password. New method since the original one-time
  password is not recoverable for existing accounts.
- mailbox (all verified): ensures the forwardemail alias and emails the
  address + webmail link.
- Preview by default; --send executes. --git/--mail/--user filters.
  Refuses --send without SMTP; warns+skips when Forgejo/forwardemail
  are unconfigured.

Also folds in the welcome-email functions (gitWelcomeEmailBody,
mailWelcomeEmailBody, EnsureUser password return, provisionGit/
ensurePremium sends) that this builds on. README ops + forgejo tests.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Anthony Ettinger 2026-06-23 10:16:28 +00:00
parent 6dc94bd784
commit c967da9f50
5 changed files with 373 additions and 18 deletions

View file

@ -20,7 +20,7 @@ func TestConfiguredRequiresURLAndToken(t *testing.T) {
}
func TestEnsureUserNoOpWhenUnconfigured(t *testing.T) {
if _, err := (Config{}).EnsureUser("alice", "a@x.com"); err == nil {
if _, _, err := (Config{}).EnsureUser("alice", "a@x.com"); err == nil {
t.Fatal("expected error when unconfigured")
}
}
@ -52,13 +52,19 @@ func TestEnsureUserCreatesWhenMissing(t *testing.T) {
defer srv.Close()
c := Config{BaseURL: srv.URL, Token: "secret"}
created, err := c.EnsureUser("alice", "a@x.com")
created, password, err := c.EnsureUser("alice", "a@x.com")
if err != nil {
t.Fatalf("EnsureUser: %v", err)
}
if !created {
t.Fatal("expected created=true")
}
if password == "" {
t.Fatal("expected a generated temporary password for a new account")
}
if password != got.body["password"] {
t.Errorf("returned password %q does not match the one sent to forgejo %v", password, got.body["password"])
}
if !got.lookup || !got.create {
t.Fatalf("expected lookup+create, got %+v", got)
}
@ -70,6 +76,78 @@ func TestEnsureUserCreatesWhenMissing(t *testing.T) {
}
}
func TestEnsureUserResetCreatesWhenMissing(t *testing.T) {
var posted bool
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && r.URL.Path == "/api/v1/users/alice":
w.WriteHeader(http.StatusNotFound)
case r.Method == http.MethodPost && r.URL.Path == "/api/v1/admin/users":
posted = true
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte(`{"id":1}`))
default:
t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusTeapot)
}
}))
defer srv.Close()
c := Config{BaseURL: srv.URL, Token: "secret"}
created, password, err := c.EnsureUserReset("alice", "a@x.com")
if err != nil {
t.Fatalf("EnsureUserReset: %v", err)
}
if !created || password == "" || !posted {
t.Fatalf("expected created+password+POST, got created=%v pw=%q posted=%v", created, password, posted)
}
}
func TestEnsureUserResetPatchesWhenExists(t *testing.T) {
var patched bool
var body map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && r.URL.Path == "/api/v1/users/alice":
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"id":1}`))
case r.Method == http.MethodPatch && r.URL.Path == "/api/v1/admin/users/alice":
patched = true
_ = json.NewDecoder(r.Body).Decode(&body)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"id":1}`))
case r.Method == http.MethodPost:
t.Error("must not create when the account already exists")
w.WriteHeader(http.StatusTeapot)
default:
t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusTeapot)
}
}))
defer srv.Close()
c := Config{BaseURL: srv.URL, Token: "secret"}
created, password, err := c.EnsureUserReset("alice", "a@x.com")
if err != nil {
t.Fatalf("EnsureUserReset: %v", err)
}
if created {
t.Fatal("expected created=false for existing account")
}
if password == "" {
t.Fatal("expected a fresh password even for an existing account")
}
if !patched {
t.Fatal("expected a PATCH to reset the password")
}
if body["password"] != password {
t.Errorf("returned password %q does not match the one sent %v", password, body["password"])
}
if body["must_change_password"] != true {
t.Errorf("expected must_change_password=true, got %v", body["must_change_password"])
}
}
func TestEnsureUserNoOpWhenExists(t *testing.T) {
created := false
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -82,13 +160,16 @@ func TestEnsureUserNoOpWhenExists(t *testing.T) {
defer srv.Close()
c := Config{BaseURL: srv.URL, Token: "secret"}
got, err := c.EnsureUser("alice", "a@x.com")
got, password, err := c.EnsureUser("alice", "a@x.com")
if err != nil {
t.Fatalf("EnsureUser: %v", err)
}
if got {
t.Fatal("expected created=false for existing user")
}
if password != "" {
t.Fatal("expected empty password when account already exists")
}
if created {
t.Fatal("must not POST when the user already exists")
}