HTTP API: register, list and remove webhooks, kept across restarts (closes #6)
check / check (push) Successful in 1m19s
check / check (push) Successful in 1m19s
An external app can register webhooks on a chat (`POST /api/v1/chats/{id}/webhooks` with a URL), list them, and remove one. Registering the same URL again returns the existing registration. Registrations are kept in `$DATA_DIR/webhooks.json`, rewritten whole on each change through a file created 0600, synced and renamed, so a crash leaves the old file or the new one; a file present but unreadable stops startup. Delivery of incoming messages is the next unit.
Disclosures: the JSON body reading is now one helper shared with the send endpoint; gosec G304 is suppressed on reading the webhooks file; the 0600-from-creation claim rests on `os.CreateTemp`'s source, not a system-call trace.
Model: opus-5-5
This commit was merged in pull request #17.
This commit is contained in:
@@ -182,6 +182,25 @@ func TestStopDuringRequest(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestUnreadableWebhooks: a webhooks file that cannot be read stops Run
|
||||
// before it starts the chat client.
|
||||
func TestUnreadableWebhooks(t *testing.T) {
|
||||
// No chat client on PATH: starting one would fail with another error.
|
||||
t.Setenv("PATH", t.TempDir())
|
||||
|
||||
cfg := &config.Config{DataDir: t.TempDir(), Port: freePort(t)}
|
||||
|
||||
err := os.WriteFile(filepath.Join(cfg.DataDir, "webhooks.json"), []byte("{"), 0o600)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = bot.Run(t.Context(), slog.New(slog.DiscardHandler), cfg, freePort(t))
|
||||
if err == nil || !strings.Contains(err.Error(), "webhooks.json") {
|
||||
t.Errorf("Run = %v, want an error naming webhooks.json", err)
|
||||
}
|
||||
}
|
||||
|
||||
// freePort returns a TCP port that nothing listens on at the moment.
|
||||
func freePort(t *testing.T) int {
|
||||
t.Helper()
|
||||
|
||||
Reference in New Issue
Block a user