HTTP API: server, credential and the list of chats (closes #4)
check / check (push) Successful in 1m1s
check / check (push) Successful in 1m1s
The bot now serves an HTTP API on PORT (default 8080) beside the chat client. Every request needs the credential read at startup from the file named by API_TOKEN_FILE, sent as a bearer token; without one, every request is refused. Responses carry the security headers, bodies are capped at 64 KiB and each request's work at 10 seconds. GET /api/v1/chats lists the bot's contacts from the chat client's /_contacts command, ordered by id, and marks the contacts who deleted their chat with the bot, which the chat client keeps listing. bot.Run starts the API after set-up and stops it within 5 seconds; a listener failure ends the bot as a chat client failure does. Model: opus-5-5
This commit is contained in:
@@ -2,6 +2,9 @@ package config_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
@@ -26,8 +29,11 @@ func TestAbsentValuesTakeDefaults(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for name, kv := range map[string]map[string]string{
|
||||
"unset": nil,
|
||||
"whitespace only": {config.EnvDataDir: " ", config.EnvDebug: " "},
|
||||
"unset": nil,
|
||||
"whitespace only": {
|
||||
config.EnvDataDir: " ", config.EnvDebug: " ",
|
||||
config.EnvPort: " ", config.EnvAPITokenFile: "\t",
|
||||
},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
@@ -44,6 +50,14 @@ func TestAbsentValuesTakeDefaults(t *testing.T) {
|
||||
if c.Debug {
|
||||
t.Error("Debug must default off")
|
||||
}
|
||||
|
||||
if c.Port != config.DefaultPort {
|
||||
t.Errorf("Port = %d, want %d", c.Port, config.DefaultPort)
|
||||
}
|
||||
|
||||
if c.APIToken != "" {
|
||||
t.Error("APIToken must default to none")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -94,3 +108,79 @@ func TestValidValuesAreUsed(t *testing.T) {
|
||||
t.Error("Debug = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPort: PORT is a whole number from 1 to 65535, and anything else
|
||||
// aborts.
|
||||
func TestPort(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for raw, want := range map[string]int{"1": 1, "8081": 8081, "65535": 65535} {
|
||||
c, err := config.Load(env(map[string]string{config.EnvPort: raw}))
|
||||
if err != nil {
|
||||
t.Errorf("PORT=%q was rejected: %v", raw, err)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if c.Port != want {
|
||||
t.Errorf("PORT=%q: Port = %d, want %d", raw, c.Port, want)
|
||||
}
|
||||
}
|
||||
|
||||
for _, raw := range []string{"0", "65536", "-1", "80.5", "8080x", "http"} {
|
||||
_, err := config.Load(env(map[string]string{config.EnvPort: raw}))
|
||||
if !errors.Is(err, config.ErrInvalidConfig) {
|
||||
t.Errorf("PORT=%q: error = %v, want ErrInvalidConfig", raw, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestAPITokenFile: the credential is the file's content without the
|
||||
// whitespace around it. A file that cannot be read, or holds too short
|
||||
// a credential, aborts, and the error never shows what the file holds.
|
||||
func TestAPITokenFile(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
token := strings.Repeat("k", config.MinAPITokenLength)
|
||||
short := strings.Repeat("s", config.MinAPITokenLength-1)
|
||||
|
||||
for name, content := range map[string]string{
|
||||
"good": " " + token + "\n",
|
||||
"short": "\n" + short + " \n",
|
||||
} {
|
||||
err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o600)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
load := func(name string) (*config.Config, error) {
|
||||
return config.Load(env(map[string]string{
|
||||
config.EnvAPITokenFile: filepath.Join(dir, name),
|
||||
}))
|
||||
}
|
||||
|
||||
c, err := load("good")
|
||||
if err != nil {
|
||||
t.Fatalf("a good file was rejected: %v", err)
|
||||
}
|
||||
|
||||
if c.APIToken != token {
|
||||
t.Errorf("APIToken = %q, want %q", c.APIToken, token)
|
||||
}
|
||||
|
||||
_, err = load("missing")
|
||||
if !errors.Is(err, config.ErrInvalidConfig) {
|
||||
t.Errorf("a missing file: error = %v, want ErrInvalidConfig", err)
|
||||
}
|
||||
|
||||
_, err = load("short")
|
||||
if !errors.Is(err, config.ErrInvalidConfig) {
|
||||
t.Fatalf("a short credential: error = %v, want ErrInvalidConfig", err)
|
||||
}
|
||||
|
||||
if strings.Contains(err.Error(), short) {
|
||||
t.Errorf("the error shows the file's content: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user