Files
uptimesebi/internal/api/support_test.go
T
Sebastian UnterschützandClaude Opus 5.5 f3d402705e
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 7m0s
ci / test (pull_request) Successful in 12m1s
ci / build (pull_request) Successful in 18m14s
ci / image (pull_request) Successful in 5m9s
feat: support tickets between accounts and platform admins
Accounts open tickets under Support and admins answer them under Admin →
Support tickets.

- A ticket is open while it waits for the admins and answered while it
  waits for the account. Either side can close or reopen it, and a reply
  reopens it.
- The admins are mailed about new tickets and replies, and the account
  about answers.
- A ticket can name the active organization and point at a monitor,
  notification channel, alert group, status page or incident in it.
  Each must be something the account can read.
- Next to the conversation, admins see the account at a glance: state,
  sign-in, organizations with their limits and usage, and open tickets.
- Admins switch support off and limit how many tickets an account has
  open at once.
- Suspended accounts and those yet to accept the terms reach support too;
  a suspended account opens one ticket.
- Tickets are part of the account export and are deleted with the
  account.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-24 18:01:50 +01:00

191 lines
9.7 KiB
Go

package api_test
import (
"context"
"net/http"
"strings"
"testing"
"time"
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
"git.zb-server.de/ZB-Server/uptimesebi/internal/notify/notifytest"
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
)
func TestSupportTickets(t *testing.T) {
mail := notifytest.NewSMTP(t)
e := newEnvWith(t, func(c *config.API) { c.SMTPURL, c.SignupVerify = mail.URL(), "off" })
ctx := context.Background()
admin := e.browser(t)
admin.signup("a@"+uniq("sup")+".test", uniq("sup-"))
if err := tenant.New(e.dbPool).Tx(ctx, tenant.Global(), func(q *store.Queries) error {
return q.MakeAdmin(ctx, admin.userIDSelf())
}); err != nil {
t.Fatal(err)
}
userEmail := "u@" + uniq("sup") + ".test"
user := e.browser(t)
user.signup(userEmail, uniq("sup-"))
userID := user.userIDSelf()
mailed := func(to, link string) {
t.Helper()
waitFor(t, 5*time.Second, "a mail with "+link, func() bool {
for _, m := range mail.Messages() {
if strings.Contains(strings.Join(m.To, ","), to) && strings.Contains(m.Data, link) {
return true
}
}
return false
})
}
st, body := user.do("POST", "/api/v1/support/tickets", map[string]any{"subject": " ", "message": "x"})
expect(t, "ticket without subject", st, body, http.StatusUnprocessableEntity, "invalid_input")
st, created := user.do("POST", "/api/v1/support/tickets", map[string]any{"subject": "Checks are late", "message": "Since **Monday**."})
if st != http.StatusCreated || created["status"] != "open" {
t.Fatalf("open ticket = %d %v", st, created)
}
id := created["id"].(string)
mailed(adminEmailOf(t, admin), "/admin/support/"+id)
// Only the account and the admins see it.
other := e.browser(t)
other.signup("o@"+uniq("sup")+".test", uniq("sup-"))
if st, _ := other.do("GET", "/api/v1/support/tickets/"+id, nil); st != http.StatusNotFound {
t.Errorf("another account reads the ticket = %d", st)
}
if st, _ := other.do("POST", "/api/v1/support/tickets/"+id+"/messages", map[string]any{"message": "me too"}); st != http.StatusNotFound {
t.Errorf("another account replies = %d", st)
}
if st, _ := user.do("GET", "/api/v1/admin/support/tickets", nil); st != http.StatusForbidden && st != http.StatusNotFound {
t.Errorf("an account lists every ticket = %d", st)
}
_, list := admin.do("GET", "/api/v1/admin/support/tickets", nil)
if !strings.Contains(mustJSON(list), id) || !strings.Contains(mustJSON(list), userEmail) {
t.Errorf("open tickets for the admins lack it: %v", list)
}
// Answering moves it to the account and mails it; its reply moves it back.
if st, body := admin.do("POST", "/api/v1/admin/support/tickets/"+id+"/messages", map[string]any{"message": "Fixed, sorry!"}); st != http.StatusCreated || body["status"] != "answered" {
t.Fatalf("answer = %d %v", st, body)
}
mailed(userEmail, "/support/"+id)
_, ticket := user.do("GET", "/api/v1/support/tickets/"+id, nil)
msgs, _ := ticket["messages"].([]any)
if ticket["status"] != "answered" || len(msgs) != 2 || msgs[1].(map[string]any)["from_staff"] != true || msgs[1].(map[string]any)["author"] != nil {
t.Errorf("ticket for the account = %v", ticket)
}
_, _, html := user.page("/support/" + id)
for _, want := range []string{"Checks are late", "<strong>Monday</strong>", "Fixed, sorry!", "Close ticket"} {
if !strings.Contains(html, want) {
t.Errorf("ticket page lacks %q", want)
}
}
if _, _, html := admin.page("/admin/support/" + id); !strings.Contains(html, userEmail) {
t.Error("the admins' ticket page does not name the account")
}
user.do("POST", "/api/v1/support/tickets/"+id+"/messages", map[string]any{"message": "Thanks!"})
if _, tk := user.do("GET", "/api/v1/support/tickets/"+id, nil); tk["status"] != "open" {
t.Errorf("after the account's reply status = %v", tk["status"])
}
// Accounts close and reopen theirs, nothing else.
st, body = user.do("PUT", "/api/v1/support/tickets/"+id+"/status", map[string]any{"status": "answered"})
expect(t, "account marks answered", st, body, http.StatusUnprocessableEntity, "invalid_input")
if st, _ := user.do("PUT", "/api/v1/support/tickets/"+id+"/status", map[string]any{"status": "closed"}); st != http.StatusNoContent {
t.Errorf("close = %d", st)
}
_, closed := admin.do("GET", "/api/v1/admin/support/tickets?status=closed", nil)
if !strings.Contains(mustJSON(closed), id) {
t.Error("the closed filter lacks the ticket")
}
if _, _, html := user.page("/support"); !strings.Contains(html, "Checks are late") || !strings.Contains(html, "closed") {
t.Error("the support page lacks the closed ticket")
}
// Tickets point at things of the active organization the account can
// read, never at another's; the admins see the account at a glance.
_, mon := user.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Checkout API", "type": "heartbeat", "interval_seconds": 3600})
_, foreign := other.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Theirs", "type": "heartbeat", "interval_seconds": 3600})
st, body = user.do("POST", "/api/v1/support/tickets", map[string]any{"subject": "Peek", "message": "x", "monitor_id": foreign["id"]})
expect(t, "ticket about another organization's monitor", st, body, http.StatusUnprocessableEntity, "invalid_input")
if _, _, page := user.page("/support"); !strings.Contains(page, `name="monitor_id"`) || !strings.Contains(page, "Checkout API") {
t.Error("the new ticket form does not offer the monitors")
}
st, withRef := user.do("POST", "/api/v1/support/tickets", map[string]any{"subject": "Heartbeat missing", "message": "x", "monitor_id": mon["id"], "channel_id": ""})
if st != http.StatusCreated {
t.Fatalf("ticket about a monitor = %d %v", st, withRef)
}
refID := withRef["id"].(string)
_, got := user.do("GET", "/api/v1/support/tickets/"+refID, nil)
if refs := mustJSON(got["references"]); !strings.Contains(refs, "Checkout API") || !strings.Contains(refs, `"org"`) {
t.Errorf("references = %s", refs)
}
if _, _, page := admin.page("/admin/support/" + refID); !strings.Contains(page, "Checkout API") || !strings.Contains(page, "Organizations owned") || !strings.Contains(page, "1 / ") {
t.Error("the admins' ticket page lacks the references or the account's details")
}
// The admins limit open tickets per account and can switch support off.
t.Cleanup(func() {
admin.do("PUT", "/api/v1/admin/support/settings", map[string]any{"enabled": true, "max_open_tickets": 3})
})
if st, body := admin.do("PUT", "/api/v1/admin/support/settings", map[string]any{"enabled": true, "max_open_tickets": 2}); st != http.StatusNoContent {
t.Fatalf("set support settings = %d %v", st, body)
}
if st, body := user.do("POST", "/api/v1/support/tickets", map[string]any{"subject": "Second", "message": "x"}); st != http.StatusCreated {
t.Fatalf("second open ticket = %d %v", st, body)
}
st, body = user.do("POST", "/api/v1/support/tickets", map[string]any{"subject": "Third", "message": "x"})
expect(t, "third open ticket", st, body, http.StatusConflict, "too_many_tickets")
st, body = user.do("PUT", "/api/v1/support/tickets/"+id+"/status", map[string]any{"status": "open"})
expect(t, "reopen beyond the limit", st, body, http.StatusConflict, "too_many_tickets")
st, body = user.do("POST", "/api/v1/support/tickets/"+id+"/messages", map[string]any{"message": "again"})
expect(t, "reply to a closed ticket beyond the limit", st, body, http.StatusConflict, "too_many_tickets")
admin.do("PUT", "/api/v1/admin/support/settings", map[string]any{"enabled": false, "max_open_tickets": 0})
st, body = user.do("POST", "/api/v1/support/tickets", map[string]any{"subject": "Off", "message": "x"})
expect(t, "ticket while support is off", st, body, http.StatusForbidden, "support_disabled")
if _, _, page := user.page("/support"); !strings.Contains(page, "switched off") || strings.Contains(page, `href="/support"`) {
t.Error("the support page or the menu still offers support while it is off")
}
if st, _, _ := admin.page("/admin/support/" + refID); st != http.StatusOK {
t.Errorf("admins read tickets while support is off = %d", st)
}
admin.do("PUT", "/api/v1/admin/support/settings", map[string]any{"enabled": true, "max_open_tickets": 0})
// A suspended account can still ask why, and its export holds the
// conversation.
if st, body := admin.do("PUT", "/api/v1/admin/users/"+userID.String()+"/suspension", map[string]any{"suspended": true, "reason": "Abuse"}); st != http.StatusNoContent {
t.Fatalf("suspend = %d %v", st, body)
}
user = e.browser(t)
if st, body := user.do("POST", "/api/v1/auth/login", map[string]any{"email": userEmail, "password": password}); st != http.StatusOK {
t.Fatalf("suspended account signs in = %d %v", st, body)
}
if _, _, page := user.page("/account/suspended"); !strings.Contains(page, `href="/support"`) {
t.Error("the suspension page does not lead to support")
}
if st, body := user.do("POST", "/api/v1/support/tickets", map[string]any{"subject": "Why?", "message": "I did nothing."}); st != http.StatusCreated {
t.Errorf("suspended account opens a ticket = %d %v", st, body)
}
st, body = user.do("POST", "/api/v1/support/tickets", map[string]any{"subject": "Why, again?", "message": "Hello?"})
expect(t, "second ticket of a suspended account", st, body, http.StatusConflict, "suspended_ticket_limit")
if st, _, _ := user.page("/support"); st != http.StatusOK {
t.Errorf("support page of a suspended account = %d", st)
}
_, _, export := user.download("/api/v1/me/export")
if !strings.Contains(string(export), "Checks are late") || !strings.Contains(string(export), "Fixed, sorry!") {
t.Error("the account export lacks the tickets")
}
}
func adminEmailOf(t *testing.T, b *browser) string {
t.Helper()
_, me := b.do("GET", "/api/v1/me", nil)
email, _ := me["email"].(string)
if email == "" {
t.Fatalf("no email in %v", me)
}
return email
}