新功能(#5): 新增 acceptance 测试脚手架 (#8)

This commit is contained in:
2026-06-03 20:07:06 -07:00
committed by GitHub
parent 53fb541846
commit 5e4cc33ff6
10 changed files with 717 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
package acceptance
import (
"context"
"encoding/json"
"testing"
)
type loginResponse struct {
Token string `json:"token"`
}
func loginAdmin(t *testing.T, ctx context.Context, client *Client, c Config) string {
t.Helper()
return login(t, ctx, client, "/v1/auth/admin/login", c.AdminEmail, c.AdminPassword)
}
func loginUser(t *testing.T, ctx context.Context, client *Client, c Config) string {
t.Helper()
return login(t, ctx, client, "/v1/auth/login", c.UserEmail, c.UserPassword)
}
func login(t *testing.T, ctx context.Context, client *Client, path string, email string, password string) string {
t.Helper()
requireSecret(t, "login email", email)
requireSecret(t, "login password", password)
envelope, raw, status, err := client.PostJSON(ctx, path, map[string]string{
"email": email,
"password": password,
})
if err != nil {
t.Fatalf("login request failed: %v", err)
}
assertOK(t, envelope, raw, status)
var data loginResponse
if err := json.Unmarshal(envelope.Data, &data); err != nil {
t.Fatalf("decode login response: %v", err)
}
if data.Token == "" {
t.Fatal("login response token is empty")
}
return data.Token
}