Files
Niklas Ye a023274280 Let team owners create service accounts in the web UI
Making a credential for CI meant signing in with curl, minting a personal
API key, finding a user id and then calling the service-accounts API by
hand. Nobody should need that to let a pipeline record a release event.

Team -> Automation lists the team's accounts and lets an owner create one,
add a key (rotation), revoke a key or delete the account. A new key is shown
once, next to the team id, because the next place it goes is a CI secret.
It sits behind the owner check, as the rest of a team's configuration does.

Two routes back it: GET /api/teams/{id}/service-accounts returns the team's
accounts with each key's name, age and last use and never a key value, and
DELETE /api/teams/{id}/service-accounts/{id} removes one, scoped to the
team so another team's account answers 404. Creating an account and minting
or revoking a key reuse the existing routes. Deleting an account keeps its
history: timelines, events and notes keep their rows, shown without a name.

Instance-scoped accounts are still an administrator's API call and are not
offered in the UI, since they reach every team. Nothing in the TUI or the
operator's client uses these routes, so neither needs a change.
2026-11-04 21:44:31 +01:00

87 lines
3.2 KiB
Go

package web
import (
"strings"
"testing"
)
// The events pages have no build step to catch a missing piece: a section the
// router names but index.html lacks, or a call the view makes that the API
// client does not define, is a blank page. This checks the pieces line up.
func TestEventsPagesAreRoutedAndCallTheEventsAPI(t *testing.T) {
if !strings.Contains(read(t, "index.html"), `id="view-events"`) {
t.Error("index.html has no #view-events section")
}
app := read(t, "js/app.js")
for _, want := range []string{"events: {", `/^\/events\/(\d+)\/?$/`, "name === 'events'"} {
if !strings.Contains(app, want) {
t.Errorf("app.js does not route events: no %s", want)
}
}
view := read(t, "js/events.js")
client := read(t, "js/api.js")
for call, path := range map[string]string{
"api.events(": "'/events'",
"api.event(": "`/events/${id}`",
"api.eventTimeline(": "`/events/${id}/timeline`",
"api.createEvent(": "'/events', { body }",
"api.updateEvent(": "PATCH",
"api.deleteEvent(": "DELETE', `/events/${id}`",
"api.attachIncidents(": "`/events/${id}/incidents`",
"api.detachIncident(": "`/events/${id}/incidents/${incidentID}`",
"api.setEventTrigger(": "`/events/${id}/trigger`",
"api.addEventNote(": "`/events/${id}/notes`",
} {
if !strings.Contains(view, call) {
t.Errorf("events.js never calls %s", call)
}
if !strings.Contains(client, path) {
t.Errorf("api.js lacks %s", path)
}
}
// The incident page shows its events and the server's suggestions, and an
// older server (no event_count) must not get an empty card.
inc := read(t, "js/incident.js")
for _, want := range []string{"api.relatedEvents(", "inc.event_count !== undefined", "attachOne"} {
if !strings.Contains(inc, want) {
t.Errorf("incident.js lacks %s", want)
}
}
if !strings.Contains(client, "/incidents/${incidentID}/related-events") {
t.Error("api.js has no call to related-events")
}
}
// An event's link is rendered as an anchor, so it must never be one a browser
// would run: the server accepts http(s) only, and the page checks again.
func TestEventLinkIsOnlyEverHTTP(t *testing.T) {
if !strings.Contains(read(t, "js/events.js"), `/^https?:\/\//i.test(ev.external_url)`) {
t.Error("events.js renders external_url without checking its scheme")
}
}
// The Automation tab is the only way to make a service account without curl.
// Same failure mode as above: a tab the strip lists but the client cannot call
// is a page that errors on first use.
func TestAutomationTabCallsTheServiceAccountAPI(t *testing.T) {
view := read(t, "js/team.js")
client := read(t, "js/api.js")
for _, call := range []string{
"api.teamServiceAccounts(", "api.createServiceAccount(", "api.deleteServiceAccount(",
"api.createServiceAccountKey(", "api.deleteServiceAccountKey(",
} {
if !strings.Contains(view, call) {
t.Errorf("team.js never calls %s", call)
}
name := strings.TrimSuffix(strings.TrimPrefix(call, "api."), "(")
if !strings.Contains(client, "export const "+name+" =") {
t.Errorf("api.js does not define %s", name)
}
}
if !strings.Contains(view, "path: '/team/accounts'") {
t.Error("team.js has no /team/accounts tab")
}
}